Attention
Attention
Bases: Module
Attention class that creates an attention mechanism with optional context.
Source code in models/tts/styledtts2/diffusion/attention.py
__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
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
AttentionBase
Bases: Module
AttentionBase class that creates a base attention mechanism.
Source code in models/tts/styledtts2/diffusion/attention.py
__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
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
RelativePositionBias
Bases: Module
RelativePositionBias class that creates a relative position bias for attention mechanisms.
Source code in models/tts/styledtts2/diffusion/attention.py
__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
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
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. |