nemo_automodel.components.models.glm5_next.model
nemo_automodel.components.models.glm5_next.model
Native AutoModel implementation of GLM-5.3-Flash.
Module Contents
Classes
Functions
Data
API
Bases: CausalLMOutputWithPast
GLM-5.3 output extended with the MTP head’s per-depth hidden states.
Weight of the summed per-depth MTP loss.
One [batch, local_sequence, hidden] tensor per MTP
depth, already normalized for the shared lm_head; None
outside training or when MTP is disabled.
Bases: HFCheckpointingMixin, Module, MoEFSDPSyncMixin
Trainable GLM-5.3 VLM with PP, EP and contiguous packed CP support.
Expose the text module through the multimodal discovery protocol.
Embed the full sequence and replace image placeholder positions.
Shift the full-sequence embeddings once per MTP depth and keep this rank’s slice.
Parameters:
Spliced embeddings [batch, padded_sequence, hidden]
covering the whole (CP-padded) sequence.
Document map whose doc_ids are [batch, padded_sequence].
Returns: torch.Tensor
One [batch, local_sequence, hidden] tensor per depth; position t
Decode ids [B,S] or a PP activation [B,Slocal,hc_mult,H].
Vision splice and CP slicing run only on the embedding stage. Packed
metadata arrives as a chunkable tuple under multi-microbatch PP, then
gets a fresh per-microbatch boundary cache. streams below is
config.text_config.hc_mult; padded_global_sequence is the
global length padded to a multiple of the CP size, and
local_sequence is that length divided by the CP size.
Parameters:
Integer tensor of shape [microbatch, global_sequence] (or [global_sequence]) on the embedding stage; it is not CP sharded. Later PP stages receive residual streams of shape [microbatch, local_sequence, streams, hidden] in this position, already local to this CP rank.
Optional integer tensor of shape [microbatch,
global_sequence], binary or holding one-based document ids.
Read only when glm5_next_packed_context is None.
Ignored; positions follow the document map.
Optional tensor of shape [microbatch, global_sequence, hidden] on the embedding stage, padded and sliced here to this rank’s interval; or already-local [microbatch, local_sequence, streams, hidden] on later stages.
Optional Boolean tensor of shape [microbatch, local_sequence], with True marking padding. Defaults to nonpositive local document ids.
Optional flattened image patches of shape
[vision_patches, channels * temporal_patch * patch * patch].
Under PP microbatching the embedding stage reads its chunk
from _vlm_pixel_values_chunks instead.
Integer tensor of shape [images, 3] holding each
image’s (t, h, w) patch grid; required with pixel_values.
Unsupported; must be None. Empty PP chunks are treated as None.
0 to project every local position, or the
positions to keep as in slice_logits_to_keep.
Whether to attach final hidden states.
Per-microbatch context or
Glm5NextPackedMetadata (doc_ids, seq_start, cp_size,
original_seq_len). doc_ids is an
integer tensor of shape [microbatch, padded_global_sequence],
replicated across CP ranks; nonpositive values mark padding.
seq_start is the global offset of this rank’s contiguous
interval, and original_seq_len is the length before CP
padding. The global map is used even on later PP stages,
whose activations are already local. When None, a non-CP
context is built from input_ids, attention_mask,
padding_mask and cu_seqlens/_packed_seq_ids.
cu_seqlens and _packed_seq_ids feed the
fallback context. All kwargs reach the decoder layers.
Returns: Glm5NextCausalLMOutputWithPast | torch.Tensor
With _pp_return_hidden_states, the text-model tensor: [microbatch,
Raises:
ValueError: If the MTP head is enabled while the model is split into pipeline stages.
Construct from an already resolved native config.
Resolve the local config and generation settings; checkpoint loading is owned by AutoModel.
Return raw and merged features for flattened image patches.
Return the token embedding table.
Return the untied language-model head.
Describe token ids [B,S] and local mHC activations [B,S/CP,M,H].
CP pads the global length to a multiple of its size. Only the last
stage emits collapsed hidden states [B,S/CP,H] or logits
[B,S/CP,V]; the first stage still receives unsharded token ids.
Parameters:
Whether the stage owns the token embedding.
PP microbatch size.
Global sequence length before CP padding.
Activation dtype.
Returns: tuple[torch.Tensor, ...]
One-element input and output tuples of meta tensors. The input is
Initialize all tensors for checkpoint-free construction.
Install GLM’s contiguous packed sharder while leaving media and ids global.
Shift MTP targets globally with the same document map the GLM CP sharder uses.
Parameters:
Unsharded batch with input_ids and labels of shape
[batch, sequence] (or [sequence] from THD collaters) and
the packing metadata read by the GLM CP sharder.
Target fill value past each document end.
Returns: MTPContextParallelInputs
Global per-depth token ids, position ids, targets and validity masks of
Replace the token embedding table.
Replace the language-model head.
Update no-aux-loss router correction biases after an optimizer step.
Bases: Module
Checkpoint-layout container for visual and language_model.
Encode image patches and split-free concatenated features.
Bases: Module
Embedding, mHC decoder stack, mean stream collapse and final RMSNorm.
Decode local tokens, preserving residual streams between PP stages.
Only the embedding stage (embed_tokens present) expands the
streams; the final-norm stage (norm present) averages them and
applies RMSNorm. streams below is config.hc_mult.
Parameters:
Optional integer tensor of shape [batch,
local_sequence], embedded on the embedding stage. Exactly one
of input_ids and inputs_embeds must be given.
Optional tensor of shape [batch, local_sequence, hidden] on the embedding stage, or residual streams of shape [batch, local_sequence, streams, hidden] on later PP stages.
Global document layout. Its doc_ids
tensor has shape [batch, padded_global_sequence] and is
replicated across CP ranks; seq_start locates this rank’s
local interval. Each row’s document-range cache is filled in
place before the decoder layers run.
Optional Boolean tensor of shape [batch, local_sequence], with True marking padding.
Forwarded unchanged to every decoder layer.
Returns: torch.Tensor
On the final-norm stage, a tensor of shape [batch, local_sequence,
Initialize a checkpoint-free text model on buffer_device.
Update every sparse layer’s no-aux-loss routing correction bias.
Build one global document map for a non-CP forward.
Sources are tried in order: packed_seq_ids, cu_seqlens, a 2-D
attention_mask, then padding_mask; otherwise every token belongs
to document 1.
Parameters:
Tensor of shape [batch, sequence]. Only its shape and device are read.
Optional integer tensor of shape [batch, sequence], either binary (one document) or one-based packed document ids with 0 at padding.
Optional Boolean tensor of shape [batch, sequence], with True marking padding.
Optional integer tensor of cumulative document boundaries, flattened to [documents + 1]; negative entries are ignored. Describes a single packed row.
Optional integer tensor of shape [batch, sequence] or [sequence] holding one-based document ids, with 0 at padding.
Returns: Glm5NextPackedContext
Context whose doc_ids is an int32 tensor of shape [batch,
Translate the GLM router/expert contract to AutoModel’s grouped MoE.