Positional Embeddings
About
The d9d.module.block.positional package implements rotary position embedding (RoPE) from RoFormer.
Rotary Position Embedding
RoPE uses two classes:
RotaryEmbeddingProviderusually belongs to the model. It caches the(cos, sin)embedding tensors and returns them for given position IDs.RotaryEmbeddingApplicatorusually belongs to the attention layer. It rotates the query and key states with these tensors.
Layout Styles
RotaryEmbeddingStyle selects how features are paired for rotation:
HALFpairs the first half of the head dimension with the second half.INTERLEAVEDpairs adjacent features.
The provider and the applicator must use the same style.
Scaling
RotaryEmbeddingProvider takes an optional RopeScaling strategy to extend the context length:
NoRopeScalingis the default and applies no scaling.LinearRopeScalingdivides all frequencies by a constant factor.NtkRopeScalingimplements NTK-aware scaling.YarnRopeScalingimplements YaRN.
Usage
API Reference
d9d.module.block.positional
Position embeddings, such as rotary position embeddings (RoPE).
LinearRopeScaling
Bases: RopeScaling
Linear scaling strategy for rotary position embeddings.
attention_mscale
property
The attention scale multiplier (mscale) applied to the cosine and sine embeddings.
__init__(factor)
Constructs the LinearRopeScaling object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
float
|
Linear scaling factor. The inverse frequencies are divided by it. |
required |
NoRopeScaling
Bases: RopeScaling
Strategy that applies no scaling to rotary position embeddings.
attention_mscale
property
The attention scale multiplier (mscale) applied to the cosine and sine embeddings.
NtkRopeScaling
Bases: RopeScaling
NTK-Aware (Neural Tangent Kernel) scaling strategy for position embeddings.
References
attention_mscale
property
The attention scale multiplier (mscale) applied to the cosine and sine embeddings.
__init__(factor)
Constructs the NtkRopeScaling object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
float
|
Sequence length expansion factor. |
required |
RopeScaling
Bases: ABC
Abstract base class for rotary position embedding (RoPE) scaling strategies.
attention_mscale
property
The attention scale multiplier (mscale) applied to the cosine and sine embeddings.
inverse_frequencies(rope_base, head_dim)
abstractmethod
Computes the RoPE inverse frequencies for this scaling strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rope_base
|
int
|
Base of the geometric progression of RoPE frequencies. |
required |
head_dim
|
int
|
Dimensionality of the attention head. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The inverse frequencies. Shape: |
RotaryEmbeddingApplicator
Bases: Module
Layer that applies rotary position embeddings (RoPE) to Q and K projections.
__init__(style)
Constructs the RotaryEmbeddingApplicator object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
RotaryEmbeddingStyle
|
RoPE layout style. |
required |
forward(query_states, key_states, position_embedding_cos, position_embedding_sin)
Rotates query and key states using provided cosine and sine embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_states
|
Tensor
|
Query tensor. Shape: |
required |
key_states
|
Tensor
|
Key tensor. Shape: |
required |
position_embedding_cos
|
Tensor
|
Cosine values for the positions. Shape: |
required |
position_embedding_sin
|
Tensor
|
Sine values for the positions. Shape: |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
A tuple of the rotated query and key tensors, with the input shapes. |
RotaryEmbeddingProvider
Bases: Module, ModuleLateInit
Module that caches rotary position embeddings and returns them for given positions.
__init__(rope_base, head_dim, max_position_ids, style, rope_scaling=None)
Constructs the RotaryEmbeddingProvider object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rope_base
|
int
|
Base of the geometric progression of RoPE frequencies. |
required |
head_dim
|
int
|
Dimensionality of the attention head. |
required |
max_position_ids
|
int
|
Number of cached positions. Position indices must be smaller than this value. |
required |
style
|
RotaryEmbeddingStyle
|
RoPE layout style. |
required |
rope_scaling
|
RopeScaling | None
|
Optional scaling strategy for extended context lengths. If |
None
|
forward(position_ids)
Returns the cached cosine and sine embeddings for the given positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position_ids
|
Tensor
|
Position indices, usually with shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
A tuple of |
reset_parameters()
Recomputes the cached cosine and sine buffers.
RotaryEmbeddingStyle
Bases: StrEnum
Supported rotary position embedding (RoPE) layout styles.
Attributes:
| Name | Type | Description |
|---|---|---|
HALF |
Rotates pairs formed by the first and second halves of the feature dimension. |
|
INTERLEAVED |
Rotates pairs of adjacent feature elements. |
YarnRopeScaling
Bases: RopeScaling
YaRN (Yet another RoPE extensioN) scaling strategy for position embeddings.
__init__(factor, beta_fast, beta_slow, original_max_position_embeddings)
Constructs the YarnRopeScaling object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
float
|
Context extension factor. |
required |
beta_fast
|
float
|
Fast boundary (upper bound) of the frequency ramp, in rotations. |
required |
beta_slow
|
float
|
Slow boundary (lower bound) of the frequency ramp, in rotations. |
required |
original_max_position_embeddings
|
int
|
Original context length of the base model. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |