nemo_automodel.components.distributed.context_parallel.utils
nemo_automodel.components.distributed.context_parallel.utils
Module Contents
Functions
API
Read the configured attention backend from a live model.
The named submesh, or None when the mesh or dimension is absent.
Whether a live model owns a vision or audio tower.
Recreate the per-forward Magi handle from the model and device mesh.
Resolve a ContextParallelSharder and shard the batch; a no-op when no CP prep applies.
Every CP backend is a :class:ContextParallelSharder. A model that owns its CP
attention returns one from its prepare_model_inputs_for_cp hook
(threaded here as cp_sharder — an explicit parameter, never a batch
key, so the batch stays pure tensors); the framework constructs one for
magi, TE/THD, and the default load-balanced torch context_parallel
path. Resolution order: model-owned > magi > TE > generic. magi and TE
also run at cp_size <= 1 (packing conversion / mask-spec activation);
model is passed through opaquely to magi for its per-step key/spec
stamping.
Parameters:
The device mesh; its cp/tp submeshes are read.
The input batch containing (string, torch.Tensor)
Returns: (contextmanager, dict[str, torch.Tensor], ContextParallelSharder)
The forward
Size of a named submesh, 0 when the mesh or dimension is absent.
Resolve and configure a CP sharder for its public constructor.
The model hook may return a ContextParallelSharder; otherwise this
function resolves a framework-owned sharder from the live model’s attention
backend and the batch’s token layout. When CP is active and the model exposes
prepare_model_inputs_for_cp, that sharder-only hook is invoked directly as
a plain method (it constructs a sharder and touches no weights; embed / vision
splice / sequence shard run in the model’s own forward per microbatch).
Returns:
The resolved and mesh-configured :class:ContextParallelSharder (the
identity sharder when no CP prep applies).
Parameters:
The (first) model part, or None (e.g. no-model contexts).
The full device mesh (cp/tp submeshes are read).
The full-sequence batch. Model hook updates are merged in place;
:meth:ContextParallelSharder.shard performs the actual sharding.
Pad sentinel for input_ids.
THD chunk count, forwarded to the hook and TE sharding.
Optional per-token mask forwarded to the batch sharding.
Invoke the model hook when CP is active or the model owns native THD preparation. Recipes pass False for PP stages without embeddings and for KD paths that never wired model-owned CP.
Additional batch keys mapped to their sequence axis
(e.g. {"teacher_logits": 1}), padded and sharded alongside the
batch on the generic torch path (rejected on the TE THD path;
ignored by backends that own their transport).
Resolve the ContextParallelSharder for this forward: model-owned > magi > TE > generic > none.
Always returns a sharder: when no CP prep applies, an identity sharder,
so callers hold working token verbs at every cp_size and
need no branches. The generic torch context_parallel path only shards
at cp_size > 1; model-owned, magi, and TE sharders may also run at
cp_size <= 1 for native THD packing conversion / mask-spec activation. The
magi and THD token layouts depend on batch content (cu_seqlens
partitioning / dispatch solver), not just
(cp_mesh, seq_len), so their sharders construct without
local_token_global_indices and install the index map computed during
shard_batch (token verbs raise before the first shard).
Shard a gradient-bearing buffer with CP’s head-tail load-balancing order.
Whether a live model uses native or injected TE attention.
Attach forward pre-hooks to self_attn modules to fix attention masks for context parallelism.
Context parallelism shards Q/K/V on the sequence dimension as DTensors,
so explicit 4D attention masks would have mismatched shapes. This function
registers a hook on every self_attn sub-module that strips the
attention_mask kwarg and sets is_causal=True instead, letting
SDPA handle causal masking internally.
Based on accelerate.big_modeling._attach_context_parallel_hooks.
Inject CP-aware SDPA into self_attn modules for compile + CP>1 correctness.
Problem: when per-layer torch.compile is active, Dynamo traces through the decoder layer including Q/K/V projections. At the F.scaled_dot_product_attention call site, Q/K/V are already local tensors (DTensor metadata was never propagated through the compiled graph). The DTensor SDPA dispatch — which triggers the CP allgather — never fires, so each rank silently attends only to its local sequence shard.
Fix: swap F.scaled_dot_product_attention with a @torch._dynamo.disable wrapper for the duration of each self_attn forward. Dynamo sees the disabled function and creates a graph break there, so:
- Everything before (Q/K/V proj + RoPE) is compiled and fused.
- The disabled wrapper runs eagerly: re-wraps local Q/K/V as DTensors with Shard(2) on the CP mesh so the DTensor SDPA dispatch fires the allgather.
- Everything after (O proj + residual + MLP) is compiled and fused.
Seq dim at the SDPA call is 2: tensors are [B, nH, S/cp_size, D] after HF reshape.
Configure Transformer Engine attention modules for context and tensor parallelism.
Parameters:
Model or pipeline stage containing self_attn modules.
Optional one-dimensional context-parallel device mesh. When its size is greater than one, every attention module communicates over this mesh; no tensor is mutated.
Optional one-dimensional tensor-parallel device mesh. When its size is greater than one, Q/K/V use per-rank head shards and every attention module is configured with the corresponding process group.
Returns: int
Number of Transformer Engine attention modules configured.
Temporarily suspend torch’s context_parallel ring-SDPA monkeypatch.
A model that embeds and sequence-shards its own primary stream in forward
(Megatron-style per-microbatch CP) runs any auxiliary non-CP attention — a
VLM vision tower over image patches — inside the ring-SDPA
context_parallel context while CP is active. That attention is
bidirectional and unsharded, so torch’s load-balanced ring SDPA all-gathers
its Q/K/V and rejects it with “Load balancing requires is_causal=True”.
The legacy buffer-API context_parallel installs the ring by monkeypatching
F.scaled_dot_product_attention; this restores the original SDPA for the
wrapped vision forward, then re-installs the ring for the sharded text decoder.
Parameters:
The context-parallel submesh. A no-op when None or size <= 1, so callers pass the same mesh they shard with; re-enabling needs it.
Create a context parallel context.
Parameters:
The device mesh for context parallel.
The buffers for context parallel.
The sequence dimensions for context parallel.
The no restore buffers for context parallel.
The rotation method for context parallel, such as “allgather” or “addtoall”.
Create a train context.
Parameters:
Whether to enable loss parallelism.
Whether to enable compiled autograd.
Build a CP batch for Transformer Engine using THD format.
This function converts BSHD format batches to THD format and shards them across context parallel ranks for use with Transformer Engine. It processes the batch in chunks if num_chunks > 1, allowing for better memory efficiency with large sequences.
The function performs three main steps:
- Converts BSHD format to THD format using split_batch_into_thd_chunks
- Optionally splits the batch into multiple chunks for memory efficiency
- Shards each chunk across CP ranks using Transformer Engine’s partitioning
Parameters:
The device mesh for context parallel. If None or size <= 1, returns the batch in THD format without sharding.
The input batch in BSHD format containing:
- input_ids: Input token IDs [batch_size, seq_len] or [batch_size, seq_len, hidden_dim]
- labels: Label token IDs [batch_size, seq_len]
- position_ids (optional): Position IDs [batch_size, seq_len]
- seq_lens: Actual sequence lengths [batch_size, num_packs]
- seq_lens_padded: Padded sequence lengths [batch_size, num_packs]
Format for QKV tensors. Currently only “thd” is supported.
Token ID used for padding in input_ids (default: 0)
Number of chunks to split the batch into. If > 1, the batch dimension is split and each chunk is processed separately (default: 1)
Sentinel value used to indicate padding in seq_lens/seq_lens_padded tensors (default: -1000)
Also return this rank’s local-token global
index map (the thd_get_partitioned_indices partition; an
identity arange when CP is inactive; None in chunked mode, where
each chunk is its own token space). Used by the THD ContextParallelSharder’s
token verbs.
Returns:
Processed batch in THD format (or (dict, LongTensor | None)
Raises:
ValueError: If qkv_format is not “thd”KeyError: If required fields (seq_lens, seq_lens_padded) are missing from batch
Restore a tensor from PyTorch’s load-balanced context-parallel layout.
Parameters:
One-dimensional context-parallel mesh of size C.
Tensor of shape [..., local_sequence, ...] whose sequence
axis is selected by seq_dim and uses PyTorch’s load-balanced CP
layout.
Axis containing the local sequence extent.
Returns: torch.Tensor
Replicated tensor of shape [..., sequence, ...] with the same axis