Attention
Attention
Bases: Module
Attention class that creates an attention mechanism with optional context.
Source code in models/tts/styledtts2/diffusion/attention.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
__init__(features, *, head_features, num_heads, out_features=None, context_features=None, use_rel_pos, rel_pos_num_buckets=None, rel_pos_max_distance=None)
Initialize the Attention with features, head features, number of heads, and relative position parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
int
|
The number of input features. |
required |
head_features |
int
|
The number of features in each head. |
required |
num_heads |
int
|
The number of heads. |
required |
out_features |
Optional[int]
|
The number of output features. If None, it will be set to the number of input features. |
None
|
context_features |
Optional[int]
|
The number of context features. If None, it will be set to the number of input features. |
None
|
use_rel_pos |
bool
|
Whether to use relative position bias. |
required |
rel_pos_num_buckets |
Optional[int]
|
The number of buckets for relative position bias. Required if use_rel_pos is True. |
None
|
rel_pos_max_distance |
Optional[int]
|
The maximum distance for relative position bias. Required if use_rel_pos is True. |
None
|
Source code in models/tts/styledtts2/diffusion/attention.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
forward(x, *, context=None)
Forward pass of the Attention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor. |
required |
context |
Optional[Tensor]
|
The context tensor. If None, the input tensor will be used as the context. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The output tensor. |
Source code in models/tts/styledtts2/diffusion/attention.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
|
AttentionBase
Bases: Module
AttentionBase class that creates a base attention mechanism.
Source code in models/tts/styledtts2/diffusion/attention.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
__init__(features, *, head_features, num_heads, use_rel_pos, out_features=None, rel_pos_num_buckets=None, rel_pos_max_distance=None)
Initialize the AttentionBase with features, head features, number of heads, and relative position parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
int
|
The number of input features. |
required |
head_features |
int
|
The number of features in each head. |
required |
num_heads |
int
|
The number of heads. |
required |
use_rel_pos |
bool
|
Whether to use relative position bias. |
required |
out_features |
Optional[int]
|
The number of output features. If None, it will be set to the number of input features. |
None
|
rel_pos_num_buckets |
Optional[int]
|
The number of buckets for relative position bias. Required if use_rel_pos is True. |
None
|
rel_pos_max_distance |
Optional[int]
|
The maximum distance for relative position bias. Required if use_rel_pos is True. |
None
|
Source code in models/tts/styledtts2/diffusion/attention.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
forward(q, k, v)
Forward pass of the AttentionBase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
q |
Tensor
|
The query tensor. |
required |
k |
Tensor
|
The key tensor. |
required |
v |
Tensor
|
The value tensor. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The output tensor. |
Source code in models/tts/styledtts2/diffusion/attention.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
RelativePositionBias
Bases: Module
RelativePositionBias class that creates a relative position bias for attention mechanisms.
Source code in models/tts/styledtts2/diffusion/attention.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
__init__(num_buckets, max_distance, num_heads)
Initialize the RelativePositionBias with a number of buckets, maximum distance, and number of heads.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_buckets |
int
|
The number of buckets for the relative position bias. |
required |
max_distance |
int
|
The maximum distance for the relative position bias. |
required |
num_heads |
int
|
The number of heads for the relative position bias. |
required |
Source code in models/tts/styledtts2/diffusion/attention.py
16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
forward(num_queries, num_keys)
Forward pass of the RelativePositionBias.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_queries |
int
|
The number of queries. |
required |
num_keys |
int
|
The number of keys. |
required |
Returns:
Name | Type | Description |
---|---|---|
Tensor |
Tensor
|
The output tensor. |
Source code in models/tts/styledtts2/diffusion/attention.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
FeedForward(features, multiplier)
Creates a feed-forward neural network with GELU activation in the middle layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
int
|
The number of input and output features. |
required |
multiplier |
int
|
The factor to multiply the number of features to get the number of features in the middle layer. |
required |
Returns:
Type | Description |
---|---|
Module
|
nn.Module: A feed-forward neural network module. |
Source code in models/tts/styledtts2/diffusion/attention.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|