nemo_automodel.components.models.glm5_next.cp
nemo_automodel.components.models.glm5_next.cp
Contiguous packed context parallelism for GLM-5.3-Flash.
Kimi Delta Attention carries recurrent and short-convolution state from left to right, so the generic load-balanced CP permutation is not valid. This module keeps one contiguous token interval per CP rank and one global document-id map that both KDA and KPool-DSA consume.
Module Contents
Classes
Functions
Data
API
Global packed-document layout for one model step.
Return whether the sequence is split across more than one rank.
Number of context-parallel ranks.
Integer document ids [batch, global_sequence]. Zero marks
padding and positive values identify independent packed documents.
Return document ids [batch, local_sequence] for this rank.
Return the padded local sequence length.
Sequence length before CP divisibility padding.
Global offset of this rank’s contiguous local token interval.
Return device/CPU segment boundaries for one packed batch row.
Return cached non-padding document ranges in global row coordinates.
Document ids are immutable for the lifetime of this per-step context. Materialize the cache before decoder FSDP hooks rebuild the dataclass.
Bases: NamedTuple
Packed-document layout in the form the PP kwargs splitter can chunk.
The splitter splits doc_ids along the batch axis and replicates the integers, and returns
the same NamedTuple type per microbatch; it treats a dataclass as one replicated value.
Number of context-parallel ranks.
Integer document ids [batch, global_sequence].
Sequence length before CP divisibility padding.
Global offset of this rank’s contiguous local token interval.
Bases: Function
Return zero while retaining a backward edge to a gathered tensor.
Send a zero gradient to the gathered tensor.
Parameters:
Autograd context saved by forward.
Tensor of shape [batch, 1, 1].
Returns: torch.Tensor
Zero tensor with the forward input’s shape [batch,
Return a broadcastable zero tied to gathered.
Parameters:
Autograd context; stores the input shape for backward.
Tensor of shape [batch, global_sequence, hidden], the all-gathered sequence.
Returns: torch.Tensor
Zero tensor of shape [batch, 1, 1] in gathered’s dtype and
Bases: Function
Autograd-aware all-gather of equal contiguous sequence shards.
Reduce a private copy of the full-sequence gradient.
Parameters:
Autograd context saved by forward.
Tensor with the forward output’s shape, global sequence on ctx.dim. Its dtype matches the forward input, and its storage may still be owned by the caller or another autograd branch.
Returns:
Local sequence-gradient tensor, followed by two None entries for
Concatenate every rank’s sequence shard in rank order.
Parameters:
Autograd context; stores the group, dimension, rank and local shard length for backward.
Tensor of arbitrary rank whose dim axis is this
rank’s local_sequence shard. Every rank must pass the same
shape and dtype.
Context-parallel process group of size cp_size.
Sequence axis; negative values count from the end.
Returns: torch.Tensor
Contiguous tensor with local_tensor’s shape except that axis
Resolve the global document map before removing packing metadata.
Restore the placeholder batch axis used by THD VLM collaters.
Create a zero-valued dependency that keeps gather backward collective-safe.
Gather a sequence-sharded tensor while preserving K/V gradient flow.
Build FLA’s KDA context for one batch row.
Parameters:
Global document layout whose doc_ids tensor has
shape [batch, padded_global_sequence]. Its cached device and CPU
boundaries, each an integer tensor of shape [segments + 1] where
segments are consecutive equal-id runs including padding, are
read; FLA receives a fresh clone of the device boundaries.
Batch row being executed.
Context-parallel process group.
Short-convolution width used for the left halo.
Returns:
FLA FLACPContext carrying segment and process-group metadata.
Convert cumulative document boundaries to ids [1, sequence].
Convert per-document lengths [batch, documents] to ids [batch, sequence].
Return boundaries for consecutive document-id runs covering the full row.
Contiguously shard packed GLM-5.3 token streams.
The top-level VLM uses shard_primary=False: image features must be
spliced into the full embedding sequence inside forward before that
differentiable primary stream is sliced. Labels and other no-grad token
streams are still sharded here.
Parameters:
One-dimensional CP mesh or None.
Unused tensor-parallel mesh, accepted by the sharder protocol.
Batch mutated in place. Token tensors such as input_ids,
labels, position_ids and padding_mask have shape
[batch, sequence] (a 1-D THD row gains a batch axis). Packing
metadata (_packed_seq_ids or attention_mask of shape
[batch, sequence], seq_lens/seq_lens_padded of shape
[batch, documents], or cu_seqlens of shape [documents + 1])
is consumed and removed.
Optional tensor of shape [batch, sequence].
Fill value for padded token ids.
Whether to shard input_ids in this function.
PP microbatches. With multiple chunks, expose the document map as a tensor in a tuple so Torch splits its batch axis too.
Returns: Callable[[], AbstractContextManager[None]]
Context factory, mutated local batch, and the global shard layout.