nemo_automodel.components.models.deepseek_v3.rope_utils
nemo_automodel.components.models.deepseek_v3.rope_utils
Module Contents
Functions
API
Rotate adjacent channel pairs using precomputed complex frequencies.
Parameters:
Tensor of shape [batch, sequence, heads, channels] for bshd or
[tokens, heads, channels] for thd. Shared keys may omit the head
dimension when unsqueeze_dim inserts it.
Complex frequency table with channels / 2 pairs per token, shaped [batch, sequence, channels / 2] or [tokens, channels / 2]. Cached CUDA complex64 frequencies use the fused kernel; trainable frequencies retain autograd.
thd adds a temporary batch dimension for packed inputs.
Optional head dimension to insert after normalizing packed inputs to include a batch dimension.
Returns: torch.Tensor
Tensor with the input shape and dtype. Rotation uses FP32 complex
Apply half-split (rotate_half) rotary embeddings using the complex freqs_cis table.
The DeepSeek-V3.2 DSA indexer rotates its rope slice with the non-interleaved layout
(pair dim j with j + d/2), unlike the main MLA attention which pairs adjacent
dims (apply_rotary_emb). freqs_cis is the same complex table used by the MLA
(exp(i * theta_j * pos) for j in [0, d/2)); its real/imag parts are the cos/sin
for frequency j, so the rotation angles match the interleaved path exactly.
Compute the rotation in FP32, as in the official DeepSeek implementation and
vLLM’s CUDA indexer RoPE, then cast the result back to the input dtype.
Parameters:
Rope slice, [B, S, H, d] / [B, S, d] (bshd) or [T, H, d] / [T, d] (thd).
Complex RoPE table with trailing dim d/2 ([B, S, d/2] or [T, d/2]).
"bshd" or "thd".
Returns: torch.Tensor
Rotated tensor with the same shape and dtype as x.
Precomputes frequency-based complex exponential values for rotary positional embeddings.
Parameters:
Dimensionality of the rotary positional embeddings.
Maximum sequence length.
Original sequence length.
Fast beta value for the exponential computation.
Slow beta value for the exponential computation.
Base value for the exponential computation.
Factor value for the exponential computation.
Returns: torch.Tensor
torch.Tensor: Precomputed complex exponential values for positional embeddings.