nemo_automodel.components.models.glm_moe_dsa.model
nemo_automodel.components.models.glm_moe_dsa.model
Module Contents
Classes
Functions
Data
API
Bases: Module
Run the block and return (hidden_states, topk_indices).
topk_indices is this layer’s DSA selection — freshly computed on “full” layers,
or prev_topk_indices passed through on “shared” layers — so the caller can thread
it to subsequent shared layers (GLM IndexShare).
Replay this block’s DSA selection, and under context parallelism its gathered latent, in the recompute.
The parallelizer composes this hook into the block’s checkpoint context. A full indexer
layer records its top-k indices in the checkpoint forward and returns them in the recompute
(GlmMoeDsaMLA._select_topk); an IndexShare “shared” layer takes its indices from the
previous full layer and needs no selection replay. Under context parallelism every layer
records its all-gathered latent K/V so the recompute does not communicate again.
Parameters:
Existing forward/recompute context factory, or None.
Returns: Callable[[], tuple[AbstractContextManager, AbstractContextManager]] | None
The factory wrapped with the replays this layer uses.
Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin
True when this module is a trimmed pipeline-parallel stage (not the whole model).
Return the last pipeline stage’s output: logits, or final hidden states for linear CE.
Parameters:
Final normalized hidden states in the backend layout (THD: [T, H];
bshd: [B, S, H]).
Zero to project all positions, a positive integer to project the last
N positions, or an integer tensor of shape [selected_positions] indexing the
token/sequence axis. Ignored when returning hidden states; that branch keeps all
positions.
Whether the stage ran in THD layout; a 2D result regains its leading batch dim.
Returns: torch.Tensor
Logits of shape [batch, selected_sequence, vocab], where selected_sequence
Forward pass.
Single process (no pipeline parallelism): returns
CausalLMOutputWithPast, threading the IndexShare
top-k internally (seeded None).
Pipeline parallelism: input_ids is the upstream hidden state on non-first stages.
IndexShare models additionally use *carry for the previous stage’s running top-k
selection. Non-last stages return the declared pipeline outputs and the last stage returns
the logits tensor, or the final hidden states when a linear CE loss has set
_pp_return_hidden_states.
Parameters:
Token-ID tensor of shape [batch, sequence] (BSHD) or [1, tokens]
(packed THD) on the first stage. On later stages, the upstream hidden-state
tensor has shape [batch, sequence, hidden] or [tokens, hidden].
Optional top-k tensor carried from the previous pipeline stage. Optimized
THD backends use float32 [tokens, 1, index_topk]; dense BSHD uses float32
[batch, sequence, min(index_topk, sequence)].
Optional position-ID tensor of shape [batch, sequence] or
[1, tokens] for packed THD.
Optional key-mask tensor of shape [batch, sequence] or additive
mask of shape [batch, 1, sequence, sequence].
Optional padding-mask tensor of shape [batch, sequence] or
[1, tokens] for packed THD.
If 0, project all positions; else only the last logits_to_keep.
A tensor value contains the one-dimensional token indices to project.
When set (single-process), carry final hidden states on the output.
Additional attention metadata forwarded to the base model. Packed THD
uses an int32 cu_seqlens tensor of shape [sequences + 1] and
qkv_format="thd".
Returns: CausalLMOutputWithPast | tuple[torch.Tensor, ...] | torch.Tensor
Single-process execution returns a causal-LM output whose logits tensor has shape
Declare PP inter-stage I/O metas, adding a top-k carry only for IndexShare models.
IndexShare models additionally receive and emit the previous “full” layer’s top-k selection so a stage that begins with a “shared” layer has the indices it needs. Models with all-full indexers need only the hidden-state channel.
Parameters:
Whether this module is the first pipeline stage.
Number of sequences in the pipeline microbatch.
Sequence or packed-token length represented by the metadata.
Hidden-state dtype; also the logits dtype when the head’s logits_dtype is unset.
Returns: tuple[torch.Tensor, ...]
Pair of input and output metadata tuples. Optimized THD stages use hidden-state
Attach GLM DSA’s packed THD context-parallel batch sharder.
BackendConfig.cp_query_layout picks the query shard each rank owns: contiguous
(rank r keeps tokens [r * T/cp, (r + 1) * T/cp)) or head/tail (rank r keeps chunks r and
2 * cp - 1 - r of 2 * cp, which balances the indexer’s causal work across ranks).
Parameters:
The batch dict.
Number of chunks for load-balanced CP sharding.
Return whether validation must use the optimized packed THD layout.
Delegate the noaux router correction-bias update to the inner model.
Bases: Module
Run the decoder stack, returning (hidden_states, topk_indices).
Parameters:
Token IDs with shape [batch, sequence] (or packed
[tokens]), or previous-stage hidden states with the matching token
axes plus [hidden] when this pipeline stage has no embedding.
Optional integer positions with the same token axes as
input_ids. Packed THD callers supply shape [tokens].
Optional token mask with shape [batch, sequence] or
additive attention mask broadcastable to [batch, heads, query, key].
Optional boolean mask with the input token axes; True
marks padding. Packed THD callers supply shape [tokens].
Optional previous pipeline stage’s IndexShare selection,
shaped [batch, sequence, K] or packed [tokens, 1, K]. Packed
values are global THD padded-storage K/V coordinates.
Attention layout metadata. Packed THD uses cu_seqlens
and optional cu_seqlens_padded of shape [sequences + 1], plus
optional glm_dsa_cp_query_indices of shape [tokens] containing
global padded-storage query coordinates.
Returns: torch.Tensor
Hidden states with the input token axes plus [hidden], and the latest
Update the noaux router correction bias of each local MoE layer; dense layers and disabled gates are skipped.
Return whether the model has layers that reuse another layer’s DSA indices.