nemo_automodel.components.models.glm5_next.layers
nemo_automodel.components.models.glm5_next.layers
Native GLM-5.3 text layers.
The released model alternates Kimi Delta Attention (KDA) and GLM-style KPool-compressed Dynamic Sparse Attention (DSA), with manifold-constrained Hyper-Connections (mHC) around both sublayers. FLA owns the production KDA kernel; small pure-Torch fallbacks keep CPU construction and unit tests useful.
Module Contents
Classes
Functions
Data
API
Bases: Module
One mHC decoder block with KDA/DSA and dense/MoE feed-forward.
Apply attention and feed-forward sublayers through two mHC mixers.
Parameters:
Tensor of shape [batch, local_sequence, streams,
hidden], where streams is config.hc_mult.
Global document layout passed to attention;
doc_ids has shape [batch, padded_global_sequence].
Optional Boolean tensor of shape [batch, local_sequence], with True marking padding.
Forwarded to the attention module.
Returns: torch.Tensor
Tensor with hidden_streams’s shape and dtype.
Initialize all decoder children.
Update the correction bias for this layer’s learned MoE router.
Bases: Module
Manifold-constrained mixer for hc_mult residual streams.
hidden_streams is [batch, sequence, hc_mult, hidden]. The returned
tensors are post [batch, sequence, hc_mult], comb [batch, sequence, hc_mult, hc_mult] and collapsed [batch, sequence, hidden].
Expose the checkpoint’s flat mHC base parameter.
Expose the checkpoint’s flat mHC scale parameter.
Build Sinkhorn mixing weights and collapse streams for one sublayer.
Parameters:
Tensor of shape [batch, sequence, hc_mult, hidden], where hc_mult is the number of residual streams.
Returns: torch.Tensor
FP32 post weights [batch, sequence, hc_mult], FP32 combination
Initialize mHC parameters on buffer_device.
Bases: Module
Own mHC parameters that must remain fp32 under FSDP mixed precision.
Build FP32 mHC weights while this holder’s FSDP unit is unsharded.
streams below is hc, the number of residual streams
(config.hc_mult).
Parameters:
FP32 tensor of shape [batch, sequence, streams], the raw collapse coefficients.
FP32 tensor of shape [batch, sequence, streams], the raw expansion coefficients.
FP32 tensor of shape [batch, sequence, streams * streams], row-major raw mixing logits indexed [source_stream, destination_stream]. It may be a last-axis slice of a larger projection.
Number of residual streams.
Additive stabilizer for pre and Sinkhorn denominators.
Number of Sinkhorn normalization rounds.
Returns: torch.Tensor
pre of shape [batch, sequence, streams] (sigmoid plus eps),
Bases: Module
Own recurrent-decay parameters that must remain fp32 under FSDP.
Return log-decay gates [batch, sequence, heads, head_dim].
Bases: Module
KPool-compressed DSA indexer for training without a KV cache.
Prepare one document and select indices for a query chunk.
full_hidden is [1, keys, hidden]; query tensors are [1, queries, ...] and positions are document-local [queries].
Returns [1, queries, index_topk + kpool - 1] int32 indices.
Initialize indexer projections and KPool parameters.
Compress one document’s keys into KPool candidates.
Parameters:
Hidden states with shape [1, keys, hidden] for one
unpadded document.
Returns: torch.Tensor
Pool keys with shape [complete_pools, index_head_dim] and raw
Select raw key indices for one query chunk.
Parameters:
Tensor of shape [1, queries, hidden].
Tensor of shape [1, queries, q_lora_rank], the normalized low-rank query states.
Integer tensor of shape [queries] holding document-local positions. The cuDNN backend requires them to be contiguous and ascending.
Tensor of shape [complete_pools, index_head_dim] from
prepare_pools.
Integer tensor of shape [complete_pools, index_kpool] holding document-local token indices.
Number of tokens in the unpadded document.
Original projection tile size, preserved when the cuDNN score kernel processes several tiles together.
Returns: torch.Tensor
Int32 document-local indices of shape [1, queries, index_topk +
Bases: Module
Kimi Delta Attention with released GLM-5.3 checkpoint parameter names.
Expose the checkpoint’s flat A_log parameter name.
Expose the checkpoint’s flat dt_bias parameter name.
Project and execute KDA for one packed row or a regular batch.
Parameters:
Tensor of shape [rows, sequence, hidden]. Packed and CP calls pass one row, [1, local_sequence, hidden].
Optional integer tensor of shape [segments + 1] on the input device with ascending row-local boundaries from 0 to sequence. It is cloned before use and never mutated.
Optional CPU tensor of shape [segments + 1] with
the same values as cu_seqlens, passed to FLA to avoid a
device synchronization.
Optional FLA CP context for this row, carrying its own
boundaries; callers pass it instead of cu_seqlens.
Returns: torch.Tensor
Tensor of shape [rows, sequence, hidden] in the projection dtype.
Run KDA over local tokens without crossing documents.
Parameters:
Tensor of shape [batch, local_sequence, hidden], this rank’s contiguous CP interval.
Optional global document layout whose
doc_ids has shape [batch, padded_global_sequence]. With CP
each row runs through FLA’s CP context; without CP its
per-row boundaries reset the recurrence. None runs each row
as one sequence.
Optional Boolean tensor of shape [batch, local_sequence], with True marking padding.
Returns: torch.Tensor
Tensor of shape [batch, local_sequence, hidden], zero at padding.
Initialize KDA while preserving fp32 recurrent parameters.
Attach the one-dimensional contiguous CP mesh.
Bases: Module
RMSNorm with fp32 variance accumulation.
Input and output have shape [batch, sequence, hidden]; the leading axes
may be replaced by any token layout as long as hidden is last.
Normalize the last axis and preserve the input dtype.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
Tensor with hidden_states’s shape. Normalization runs in FP32
Bases: Module
NoPE MLA whose visibility is selected by the GLM KPool indexer.
Execute sparse attention for a local query interval of one full document.
Parameters:
Unpadded document states with shape [1, key_tokens, hidden].
Inclusive document-local index of the first local query.
Exclusive document-local index of the final local query.
Returns: torch.Tensor
Projected attention output with shape
Batch document-isolated attention after gathering global hidden states.
Preserve the original per-document K/V projection, indexer and query projection order and shapes. Concatenate only the attention inputs.
Parameters:
CUDA tensor of shape [batch, padded_global_sequence, hidden], all-gathered across CP ranks (the local tensor when CP is disabled).
Global document layout. doc_ids has shape
[batch, padded_global_sequence]; seq_start is this rank’s
global query offset.
Number of tokens owned by this CP rank.
Returns: torch.Tensor
Tensor of shape [batch, local_sequence, hidden], zero at tokens
Select document-local keys using the original indexer projection tiles.
Parameters:
Tensor of shape [1, queries, hidden], where queries is query_end - query_start.
Tensor of shape [1, queries, q_lora_rank], the normalized low-rank queries.
Tensor of shape [complete_pools, index_head_dim].
Integer tensor of shape [complete_pools, index_kpool] holding document-local token indices.
First local query’s position within the document.
Exclusive document-local query end.
Full document length.
Returns: torch.Tensor
Contiguous int32 tensor of shape [queries, 1, selection_width]
Run document-isolated sparse attention for this rank’s queries.
Keys come from the full document, so under CP the local states are all-gathered along the sequence with a gradient-summing backward.
Parameters:
Tensor of shape [batch, local_sequence, hidden], this rank’s contiguous interval of the CP-padded sequence.
Global document layout. doc_ids is an integer
tensor of shape [batch, padded_global_sequence], replicated
across CP ranks, with nonpositive values at padding;
seq_start is this rank’s global query offset.
Optional Boolean tensor of shape [batch, local_sequence], with True marking padding.
Returns: torch.Tensor
Tensor of shape [batch, local_sequence, hidden], zero at padding
Initialize MLA and indexer parameters.
Attach the CP mesh used for differentiable full-sequence gathering.
Bases: Module
Parameter-free fp32 RMS normalization used by mHC.
Normalize [..., hc_streams * hidden] without a learned weight.
Bases: Glm5NextRMSNorm
Gated per-head RMSNorm with optional model-owned compilation.
Normalize each head then sigmoid-gate it.
Parameters:
Tensor of shape [batch, sequence, heads, head_dim].
Tensor with hidden_states’s shape.
Returns: torch.Tensor
Tensor with hidden_states’s shape and dtype; see
Bases: Module
Depthwise causal Conv1d matching FLA ShortConvolution state keys.
Apply causal depthwise convolution and SiLU, resetting at packed boundaries.
Parameters:
Tensor of shape [batch, sequence, channels]. Packed callers pass one row, [1, sequence, channels].
Optional integer tensor of shape [segments + 1] on
x’s device, holding ascending row-local boundaries from 0
to sequence; segments are consecutive equal-document-id runs,
padding included. The CPU fallback applies the same
boundaries to every batch row.
Optional CPU tensor of shape [segments + 1] with
the same values as cu_seqlens. Only the FLA CUDA kernel
reads it, to avoid a device synchronization.
FLA-only cache (initial state),
output_final_state and cp_context; the CPU fallback
ignores them.
Returns: torch.Tensor
Output of shape [batch, sequence, channels] in x’s dtype, and
Enumerate every visible key when all complete causal pools fit the budget.
Parameters:
Integer tensor of shape [queries] holding document-local query positions.
Maximum number of tokens selected from complete pools.
Number of tokens per complete pool.
Whether to include the incomplete causal pool.
Returns: torch.Tensor
Int32 tensor of shape [1, queries, index_topk + tail_width], where
Differentiable reference KDA for CPU/small tensors.
Parameters:
Query tensor of shape [batch, sequence, heads, head_dim].
Key tensor with q’s layout.
Value tensor with q’s layout.
Log-decay tensor with q’s layout.
Update weights of shape [batch, sequence, heads].
Optional integer tensor [documents + 1] containing packed boundaries. Each boundary resets the FP32 recurrent state.
Returns: torch.Tensor
Tensor of shape [batch, sequence, heads, head_dim] in v’s dtype.