nemo_automodel.components.models.glm_moe_dsa.cp
nemo_automodel.components.models.glm_moe_dsa.cp
Context-parallel helpers for GLM MoE DSA TileLang attention.
Module Contents
Classes
Functions
Data
API
Bases: Function
Permute a rank-order gather into global token order; the backward permutes the gradient back.
Map the global-order gradient back onto the rank-order gather.
Reorder gathered so that index i along dim holds global token i.
Parameters:
Autograd context.
Rank-order all-gather result.
Global token held by each gathered row (see gather_order).
Gathered row holding each global token.
Gather dimension.
Returns:
The gathered tensor in global token order.
Bases: Function
Return a recorded all-gather result while keeping the gradient path of the gather.
Reduce-scatter the gathered gradient back to each rank’s shard, as the gather’s backward does.
Hand back gathered (the forward’s all-gather of local) without communicating.
Parameters:
Autograd context.
This rank’s shard, only used for its gradient shape and dtype.
The recorded all-gather of local across the CP group along dim, in global
token order when from_global is given and in rank order otherwise.
Gather dimension.
CP process group.
Global token held by each rank-order gathered row when the recorded tensor
was reordered into global order (see gather_order); None for rank order.
Returns: torch.Tensor
gathered as a fresh view.
Complete an asynchronous functional collective result.
Row maps between the rank-order all-gather of layout shards and global token order.
all_gather_tensor_autograd concatenates the ranks’ shards in rank order, so gathered row
r * T/cp + j holds the j-th local token of rank r. Every consumer of the gathered K/V
(the cuDNN packed metadata’s key sources and causal windows, the top-k coordinates FlashMLA and
cuDNN read kv_latent with, the TileLang causal windows) addresses rows by global token
position, which coincides with rank order only for the contiguous layout.
Parameters:
Query layout the batch was sliced with.
Context-parallel world size.
Global padded token count (local tokens times cp_size).
Device of the returned index tensors.
Returns: GatherOrder | None
None when rank order already is global order (contiguous layout, or cp_size <= 1);
All-gather an activation across CP ranks along dim while preserving autograd.
One functional collective produces the concatenated result directly (no per-rank list and
concat copy), and its backward reduce-scatters the gradient to the owning ranks. With
replay bound in record mode the gathered tensor is recorded; in replay mode (the
activation-checkpoint recompute) the recorded tensor is returned without communication,
with the same gradient path.
Parameters:
This rank’s shard; [local_tokens, ...] for dim=0.
Concatenation dimension.
CP process group; a size-one or missing group returns tensor unchanged.
Optional record/replay channel consulted inside a checkpointed block.
Optional gather_order maps; when given, the result is permuted from rank
order into global token order (and the backward permutes the gradient back). Required
for every layout whose rank order is not global order, such as head/tail.
Returns: torch.Tensor
Tensor whose dim holds every rank’s shard in rank order, or in global token order when
Return whether a real GLM DSA CP process group is active.
Global query positions rank rank of cp_size owns under layout, in its local order.
Mesh-free form of contiguous_local_indices / round_robin_local_indices, which the
batch slicer uses for the calling rank; this one serves every rank so gather_order can
describe the whole gathered tensor.
Parameters:
Query layout, see QueryLayout.
Context-parallel rank in [0, cp_size).
Context-parallel world size.
Global padded token count; a multiple of cp_size (contiguous) or
2 * cp_size (head/tail).
Device of the returned index tensor.
Returns: torch.Tensor
Int64 tensor of shape [total_tokens / cp_size] with the rank’s global token positions in
Return this rank’s global query positions for layout (see QueryLayout).
Convert packed GLM DSA batches to THD and keep one query shard per CP rank.
GLM DSA sparse attention gathers K/V activations inside the model. The batch
side only slices local query tokens (query_layout decides which, see
QueryLayout) and carries the full packed-sequence cu_seqlens plus
per-query global token indices for the kernels’ causal top-k windows.
ContextParallelSharder.shard_batch wrapper for GLM DSA packed CP.
generated_rows is set by the model’s own sharder: the query rows were just generated for
query_layout, so the batch carries them with that layout (_glm_dsa_shard_rows and
_glm_dsa_shard_layout) and the model compares layouts instead of reading the rows back from
the device. Callers that supply glm_dsa_cp_query_indices themselves leave it False and get
the value check in validate_query_rows.
Check that the batch’s query rows are the rows layout assigns to this rank.
The K/V gather and every kernel after it assume the configured layout, so rows sliced with another layout would make the kernels read wrong keys. This holds for the contiguous layout too: its gather keeps rank order, which is global order only for contiguous shards.
Parameters:
The model’s configured query layout (BackendConfig.cp_query_layout).
This rank’s index in the context-parallel group.
Context-parallel world size.
Number of local query rows.
The batch’s glm_dsa_cp_query_indices (global positions of the local rows).
Raises:
ValueError: If the batch was sliced with a different layout than the configured one.