spatial_decay
spatial_decay
¶
SpatialDecayMask
¶
Bases: Module
Generates attention decay based on spatial distance between positions.
Farther positions receive exponentially decaying attention weights, with per-head decay rates allowing different receptive fields.
Initializes spatial decay mask generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_heads
|
int
|
Number of attention heads. |
required |
initial_decay
|
float
|
Initial decay rate. |
5.0
|
decay_range
|
float
|
Range of decay rates across heads. |
3.0
|
Source code in src/versatil/models/layers/geometric_attention/spatial_decay.py
compute_2d_distance_matrix
¶
Computes pairwise Manhattan distances for 2D grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
int
|
Grid height. |
required |
width
|
int
|
Grid width. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Distance matrix of shape (HW, HW). |
Source code in src/versatil/models/layers/geometric_attention/spatial_decay.py
compute_1d_distance_matrix
¶
Computes pairwise distances for 1D sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Sequence length. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Distance matrix of shape (length, length). |
Source code in src/versatil/models/layers/geometric_attention/spatial_decay.py
forward
¶
Generates spatial decay mask(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
int
|
Grid height. |
required |
width
|
int
|
Grid width. |
required |
decomposition_mode
|
str
|
Whether to generate full or separable masks. |
value
|
Returns:
| Type | Description |
|---|---|
Tensor
|
If FULL: Single mask of shape (number_of_heads, HW, HW). |
...
|
If SEPARABLE: Tuple of (height_mask, width_mask) each (number_of_heads, H, H) and (number_of_heads, W, W). |