nemo_automodel.components.models.deepseek_v4.kernels.qnorm_rope
nemo_automodel.components.models.deepseek_v4.kernels.qnorm_rope
Training Q RMSNorm plus partial interleaved RoPE in token-major storage.
The two BF16 rounding boundaries are intentional: normalize before rotating, and rotate the upstream gradient before applying RMSNorm backward.
Module Contents
Classes
Functions
API
Bases: Function
Autograd implementation with FP32 saved inverse RMS.
Apply conjugate rotation then the RMSNorm input gradient.
Parameters:
Saved input, frequencies, and inverse RMS.
Upstream gradient of shape [batch, heads, sequence, channels], any strides.
Returns: torch.Tensor
Input gradient of shape [batch, heads, sequence, channels] in x’s dtype
Normalize and rotate Q while saving its FP32 inverse RMS.
Parameters:
Autograd context.
Query tensor of shape [batch, heads, sequence, channels], any strides.
Fixed FP32 cosines of shape [batch, sequence, rotary_channels]; the first half holds the unique pair frequencies.
Fixed FP32 sines with cos’s shape and frequency order.
Number of trailing channels to rotate.
Normalization epsilon.
Returns: torch.Tensor
Tensor of shape [batch, heads, sequence, channels] in x’s dtype, backed
Normalize and rotate M query rows per program into token-major storage.
Parameters:
Input with logical shape [batch, heads, sequence, channels] and strides (XB, XH, XS, XD); D is channels and R rotary_channels.
Cosines of shape [batch, sequence, rotary_channels] with strides (CB, CS, CD); the first half holds the unique pair frequencies.
Sines with cos’s layout and strides (SB, SS, SD).
Contiguous output storage of shape [batch, sequence, heads, channels].
FP32 inverse RMS of shape [batch * sequence * heads], one per row in the output’s token-major order.
batch * sequence * heads.
Rotate the upstream gradient back, then apply the RMSNorm input gradient.
Parameters:
Saved forward input with logical shape [batch, heads, sequence, channels] and strides (XB, XH, XS, XD).
Upstream gradient with x’s logical shape and strides (GB, GH, GS, GD).
Cosines of shape [batch, sequence, rotary_channels] with strides (CB, CS, CD).
Sines with cos’s layout and strides (SB, SS, SD).
FP32 inverse RMS of shape [batch * sequence * heads] from the forward.
Contiguous input-gradient storage of shape [batch, sequence, heads, channels].
batch * sequence * heads.
Normalize Q and rotate its trailing interleaved pairs without a transpose copy.
Parameters:
CUDA tensor with logical shape [batch, heads, sequence, channels].
Fixed cosine table of shape [batch, sequence, rotary_channels].
Fixed sine table with cos’s shape.
Positive even number of trailing channels to rotate.
Positive RMSNorm epsilon.
Returns: torch.Tensor
Normalized and rotated tensor with x’s logical shape and token-major storage.