nemo_automodel.components.models.deepseek_v41.layers
nemo_automodel.components.models.deepseek_v41.layers
Single-pass mHC and modality-aware routing for DeepSeek V4.1.
The coefficient handoff follows section 2.4.1 of the official technical report: each sublayer consumes the pre-mix produced by the preceding sublayer.
Module Contents
Classes
Functions
Data
API
Bases: Module
Predict one sublayer’s residual mixing coefficients in FP32.
Collapse streams using the preceding sublayer’s coefficients.
Parameters:
Tensor of shape [batch, sequence, streams, hidden].
FP32 tensor of shape [batch, sequence, streams].
Returns: torch.Tensor
Tensor of shape [batch, sequence, hidden], with the input dtype.
Mix the sublayer output and residual in the source’s coefficient orientation.
Parameters:
Tensor of shape [batch, sequence, hidden].
Tensor of shape [batch, sequence, streams, hidden].
FP32 pre/post tensors of shape [batch, sequence, streams] and comb of shape [batch, sequence, input_streams, output_streams].
Returns: torch.Tensor
Tensor of shape [batch, sequence, streams, hidden], with output’s dtype.
Predict coefficients, preserving projection-before-RMS arithmetic.
Parameters:
Tensor of shape [batch, sequence, streams, hidden].
Returns: DeepseekV41Mix
Coefficients with pre/post tensors of shape [batch, sequence, streams]
Initialize all coefficients before checkpoint-free execution.
Bases: NamedTuple
FP32 coefficients: pre/post [batch, sequence, streams], comb [batch, sequence, streams, streams].
Bases: Module
Normalize in FP32 and multiply the scale before casting the result.
Apply the released reference’s RMS normalization.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
Tensor of shape […, hidden], with the input dtype.
Coefficient projection up to the Sinkhorn input: pre, post and the [streams, streams] combination logits.
Wrap the mHC projection, collapse and expand cores with torch.compile (BackendConfig.compile_hc).
Runs once per process, static shapes (dynamic=False: the fusion of expand needs concrete shapes; a new
sequence length compiles the three cores again). No fullgraph: past dynamo’s recompile limit (many distinct
sequence lengths) a core runs eager instead of raising FailOnRecompileLimitHit; the unit tests assert the
cores compile without graph breaks. The Sinkhorn normalisation keeps its own backend (TileKernels or torch):
compiling the 20-iteration torch loop is slower than the fused TileKernels kernel.
Wrap the fp32 RMSNorm core with torch.compile (BackendConfig.compile_norm); runs once per process.
Static shapes: one specialization per norm site (block, attention, compressor and indexer norms) and grad mode.
The dynamic-shape variant (Kimi K3’s setting) was measured 15 % slower per call and moved the first-step loss of
the 12-layer mini benchmark by 9e-4, while the static kernels reproduced the eager loss to four decimals; no
fullgraph, so a shape past dynamo’s recompile limit falls back to eager instead of raising.