nemo_automodel.components.distributed.blockdiag_cp.runtime
nemo_automodel.components.distributed.blockdiag_cp.runtime
Block-diagonal SDPA routing and collective-safe fallback policy.
Module Contents
Functions
Data
API
True if the CP group spans more than one node.
The needed-only exchanges (halo neighbor-p2p, a2a all-to-all-v) are verified when the CP group is node-local (cp_size <= GPUs/node, the common mesh layout where cp is the fast dim). Cross-node needed-only exchange is still fabric-sensitive, so the production default falls back to NCCL all-gather when the CP group spans nodes. Heuristic: cp_size > the local node’s world size (LOCAL_WORLD_SIZE / NPROC_PER_NODE / visible GPUs).
Set NEMO_CP_ALLOW_XNODE=1 to force the needed-only path cross-node for targeted
validation on a specific cluster.
Make a rank-local varlen result safe to act on collectively.
In particular, an all-padding rank returns a zero tensor without invoking
FlashAttention/TE. It must nevertheless learn when a peer’s real-token
kernel returned None before either rank advances to another collective.
Collectively decide whether every CP rank can run the varlen kernel.
Return (attn_backend, kv_exchange) for this step.
Prefers the per-step snapshot threaded into state by the batch/ctx builder (so
:func:cp_blockdiag_sdpa is a function of (qkv, state) on the production path),
and falls back to the runtime configuration owned by
:mod:~nemo_automodel.components.distributed.blockdiag_cp.state for callers that
set the step state manually (e.g. parity tests).
Decide this step’s KV-exchange path and WHY.
Every downgrade to all-gather names its cause (mode, kernel, missing varlen meta, or cross-node topology) so a silent fall-through can’t hide a misconfiguration.
Parameters:
The per-step CP state dict (memoizes the plan and preflight).
The CP process group.
Replicated per-position document ids [B, S_full] (0 == pad).
Per-rank local sequence length L.
Device for plan tensors and the preflight all-reduce.
Global position of this rank’s first local query row.
Query dtype for the kernel preflight (skip when None).
Attention dropout probability.
Returns:
(path, plan, reason) where path is "halo"/"a2a"/"allgather",
Block-diagonal context-parallel SDPA.
Drop-in replacement for torch.nn.functional.scaled_dot_product_attention
while the context returned by
:func:~nemo_automodel.components.distributed.blockdiag_cp.batch.make_cp_blockdiag_batch_and_ctx
is active; a plain pass-through to stock SDPA otherwise. K/V are exchanged
across the CP group (all-gather, or a needed-only halo/a2a exchange) and one
local attention runs the local queries against the delivered keys with a
per-document causal mask. The passed attn_mask / is_causal are ignored
on the CP path — masking is rebuilt from the document ids so packed sequences
never attend across document boundaries.
Parameters:
This rank’s LOCAL query shard [B, Hq, L, D] (B = batch,
Hq = query heads, L = local sequence length, D = head dim).
This rank’s LOCAL key shard [B, Hkv, L, D].
This rank’s LOCAL value shard [B, Hkv, L, D].
Ignored on the CP path (forwarded to stock SDPA otherwise).
Dropout probability.
Ignored on the CP path (forwarded to stock SDPA otherwise).
Softmax scale (None -> D**-0.5).
Grouped-query attention flag as passed by HF’s sdpa path.
Ignored; accepted for SDPA signature compatibility.
Returns: torch.Tensor
Attention output [B, Hq, L, D] for this rank’s local rows.