For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Python class
RotaryEmbedding
RotaryEmbedding
class max.experimental.nn.common_layers.rotary_embedding.RotaryEmbedding(dim, n_heads, theta, max_seq_len, device, head_dim=None, interleaved=True)
Bases: Module
RotaryEmbedding layer to calculate and apply the frequency tensor for complex exponentials.
-
Parameters:
compute_scale()
compute_scale(user_scale=None)
Computes the attention scale factor from head_dim or the given user scale.
dim
dim: int
forward()
forward(x, start_pos=None, seq_len=None)
Applies rotary positional embeddings (RoPE) to x.
-
Parameters:
-
Returns:
-
Input activation tensor with rotary positional embeddings applied and the same shape as x.
-
Return type:
freqs_cis
property freqs_cis: Tensor
Returns the flattened frequency tensor for rotary embeddings.
freqs_cis_base()
freqs_cis_base()
Computes the frequency tensor for complex exponentials (cis).
Tensor is scaled with theta parameter. Required to apply Rotary Position Embedding (RoPE) to tensor. See ‘Roformer: Enhanced Transformer with Rotary Embedding’ (arxiv.org/pdf/2104.09864).
-
Returns:
-
The frequency tensor for complex exponentials with shape (max_seq_len * 2, head_dim / 2, 2)
-
Return type:
head_dim
head_dim: int
head_dim = dim // n_heads if not specified in the config.
interleaved
interleaved: bool = True
max_seq_len
max_seq_len: int
The maximum sequence length for model’s input.
n_heads
n_heads: int
theta
theta: float
Hyperparameter used to control the frequency scaling of the sinusoidal components of the embeddings.