rotary
rotary
¶
RotaryPositionalEncoding
¶
RotaryPositionalEncoding(embedding_dimension, number_of_heads, base_frequency=10000.0, learnable_frequencies=False)
Bases: Module
Base class for rotary positional encoding, from https://arxiv.org/pdf/2104.09864.
Initialize rotary positional encoding frequencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Full model embedding dimension. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
base_frequency
|
float
|
Base frequency for geometric spacing. |
10000.0
|
learnable_frequencies
|
bool
|
Whether frequency bands are trainable. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If attention dimensions or frequencies are invalid. |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
apply_rotation
staticmethod
¶
Apply rotary transformation using interleaved even/odd convention.
Pairs even and odd indices: [-odd, even] interleaved back.
This is the original RoFormer/VersatIL convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input (B, number_of_heads, L, head_dim). |
required |
sine
|
Tensor
|
Sine components matching sequence + head_dim shape. |
required |
cosine
|
Tensor
|
Cosine components matching sequence + head_dim shape. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Rotated tensor of same shape. |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
apply_rotation_half
staticmethod
¶
Apply rotary transformation using split-half convention.
Splits the last dimension in half: [-second_half, first_half].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input (B, number_of_heads, L, head_dim). |
required |
sine
|
Tensor
|
Sine components matching sequence + head_dim shape. |
required |
cosine
|
Tensor
|
Cosine components matching sequence + head_dim shape. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Rotated tensor of same shape. |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
RotaryPositionalEncoding1D
¶
RotaryPositionalEncoding1D(embedding_dimension, number_of_heads, base_frequency=10000.0, learnable_frequencies=False)
Bases: RotaryPositionalEncoding
Rotary positional encoding for 1D sequences.
Source code in src/versatil/models/layers/positional_encoding/rotary.py
compute_rotation_components
¶
Computes sine and cosine components for 1D sequence positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_len
|
int
|
Sequence length. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (sine, cosine) tensors of shape (seq_len, head_dim). |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
RasterRotaryPositionalEncoding2D
¶
RasterRotaryPositionalEncoding2D(embedding_dimension, number_of_heads, base_frequency=10000.0, learnable_frequencies=False)
Bases: RotaryPositionalEncoding
Rotary encoding over flattened raster positions of a 2D grid.
Matches the DFormerv2 reference convention: every token is rotated by its
flattened index row * width + column with a single frequency band
spanning the full head dimension, spaced as
1 / base_frequency ** linspace(0, 1, head_dim // 2) with the endpoint
included. Pretrained DFormerv2 checkpoints require exactly this scheme.
Initialize raster rotary encoding with endpoint-spaced frequencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Full model embedding dimension. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
base_frequency
|
float
|
Base frequency for geometric spacing. |
10000.0
|
learnable_frequencies
|
bool
|
Whether frequency bands are trainable. |
False
|
Source code in src/versatil/models/layers/positional_encoding/rotary.py
compute_rotation_components
¶
Computes sine and cosine components for raster grid positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
int
|
Grid height. |
required |
width
|
int
|
Grid width. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (sine, cosine) tensors of shape (H, W, head_dim). |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
RotaryPositionalEncoding2D
¶
RotaryPositionalEncoding2D(embedding_dimension, number_of_heads, base_frequency=10000.0, learnable_frequencies=False)
Bases: RotaryPositionalEncoding
Rotary positional encoding for 2D spatial grids.
Initialize rotary positional encoding for 2D grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Full model embedding dimension. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
base_frequency
|
float
|
Base frequency for geometric spacing. |
10000.0
|
learnable_frequencies
|
bool
|
Whether frequency bands are trainable. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If per-axis head dimensions are invalid. |
Source code in src/versatil/models/layers/positional_encoding/rotary.py
compute_rotation_components
¶
Computes sine and cosine components for 2D grid positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
int
|
Grid height. |
required |
width
|
int
|
Grid width. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (sine, cosine) tensors of shape (H, W, head_dim). |