nemo_automodel.components.moe.megatron.fused_a2a
nemo_automodel.components.moe.megatron.fused_a2a
Module Contents
Classes
Functions
Data
_hybrid_ep_initialized_capacity
_hybridep_dispatch_replay_state
API
Per-checkpoint-frame log of DeepEP dispatch results, replayed on recompute.
Next recorded dispatch, or None when the replay outruns the log.
Bases: Function
Fused combine operation for MoE output combining computation and communication.
Backward pass of fused combine.
Forward pass of fused combine.
Bases: Function
Fused dispatch operation for MoE routing combining computation and communication.
Backward pass of fused dispatch.
Forward pass of fused dispatch.
Bases: Function
Fused combine operation for permute + combine a2a + permute using the HybridEP backend.
Redispatch combined-output gradients through the saved layout.
Parameters:
Autograd context populated by forward.
Combined-output gradients with shape [tokens, hidden].
Returns: torch.Tensor
Gradient for x with shape [dispatched_tokens, hidden], followed by None
Combine dispatched expert outputs through HybridEP.
Parameters:
Autograd context that retains the dispatch handle for backward.
Expert outputs with shape [dispatched_tokens, hidden].
Opaque handle returned by dispatch.
Optional static dispatched-token capacity.
Optional token padding multiple.
Whether to fuse output unpermutation into combine.
Returns: torch.Tensor
Combined hidden states with shape [tokens, hidden].
Bases: Function
Fused dispatch operation for permute + dispatch a2a + permute using the HybridEP backend.
Combine gradients while retaining the dense probability-gradient layout.
Parameters:
Autograd context populated by forward.
Hidden-state gradients with shape [dispatched_tokens, hidden].
Probability gradients with shape [dispatched_tokens], aligned with grad_x.
Optional gradient for dispatch scaling metadata.
Ignored gradient for token counts with shape [local_experts].
Ignored gradient for the opaque dispatch handle.
Returns: torch.Tensor | None
Gradients for every forward argument. The hidden-state gradient has shape
Dispatch hidden states using compact indices or a dense routing map.
The probability tensor remains dense so its gradient layout is unchanged. When both
routing representations are provided, HybridEP gives routing_map precedence.
Parameters:
Autograd context that retains the dispatch handle for backward.
Hidden states with shape [tokens, hidden].
Optional Boolean routing map with shape [tokens, experts].
Dense routing probabilities with shape [tokens, experts].
Expert-parallel process group.
Number of experts owned by each rank.
Number of SMs used by the dispatch API.
Number of SMs used by the combine API.
Optional static dispatched-token capacity.
Optional token padding multiple.
Optional global expert indices with shape [tokens, top_k], with -1 for unused slots.
Global expert count required with compact indices.
Whether to fuse token permutation into dispatch.
Optional SM count for routing-metadata preprocessing.
Optional dispatch permutation block count.
Optional combine unpermutation block count.
Returns: torch.Tensor
Dispatched hidden states with shape [dispatched_tokens, hidden], grouped by local
Record HybridEP layouts from checkpoint forward for deterministic replay.
HybridEP’s sync-free replay API expects the dispatched-token extent as a host
integer. Reading it with .item() right after every checkpointed block
stalls the host until the GPU has drained that block, once per layer per
microbatch; on a pipeline-parallel step those stalls surface as idle GPU time.
The recorder therefore splits the read in two: finalize (called when
the checkpoint-forward op context exits) runs the reduction and starts an
asynchronous device-to-host copy, and resolve (called when the
recompute context opens, long after the copy completed) turns the pinned
scalars into integers. Neither step runs inside a checkpoint op context, so
the forward and recompute traces stay identical.
Start the device-to-host copy of each layout extent (no host synchronization).
Runs after the checkpoint-forward op context exits, so the reduction does not enter the forward trace; CPU tensors are converted immediately.
Turn the copied extents into host integers, waiting only for copies still in flight.
Called when the recompute context opens — outside its op context, so the conversion never enters the recompute trace.
Return the next forward dispatch record, or None on divergence.
Bases: Function
Fused combine using UCCL-EP instead of DeepEP.
Bases: Function
Fused dispatch using UCCL-EP instead of DeepEP.
Bases: local
Thread-local replay binding. threading.local runs __init__ once per
thread, so both attributes always exist on every thread that touches it.
Bases: local
Installed DeepEP version (dist-info first: the package itself carries no __version__).
Return (shared cache dir for this toolchain and GPU, this process’s HybridEP JIT dir).
The per-process path mirrors get_jit_dir() in DeepEP’s csrc/hybrid_ep/jit/compiler.cu.
Check if DeepEP was compiled with NVSHMEM support.
Uses is_sm90_compiled() as proxy — DeepEP’s build enforces that NVSHMEM is disabled when SM90 features are disabled.
Copy the shared cache’s kernels into this process’s JIT dir. Returns whether the cache is active.
Bind recorder for the enclosed region. mode is ‘record’ or ‘replay’.
Destroy the global DeepEP Buffer and release its NVSHMEM/cpp runtime.
DeepEP keeps a process-global communication buffer backed by NVSHMEM symmetric memory.
It is normally never torn down (destroy_process_group hangs on DeepEP’s NCCL
sub-groups, so cleanup is skipped), but that leftover GPU state survives process exit for
the whole Slurm allocation and corrupts subsequent forwards. Destroying the buffer first
frees the runtime and lets a clean destroy_process_group follow without hanging.
Perform fused combine operation if deep_ep is available.
Parameters:
Input tensor
Process group
Communication handle
Previous CUDA event
Returns:
Result of FusedCombine
Perform fused dispatch operation if deep_ep is available.
Parameters:
Input tensor [num_tokens, hidden_size]
Token routing indices [num_tokens, topk]
Token routing probabilities [num_tokens, topk]
Number of experts
Process group
Previous CUDA event
Returns:
Result of FusedDispatch
Get or create a buffer for all-to-all communication.
Parameters:
Process group for communication
Number of hidden bytes needed
Returns:
Communication buffer
Calculate the number of hidden bytes for a tensor.
Parameters:
Input tensor
Returns: int
Number of hidden bytes
Return process-global initialized capacity after validating the resource signature.
Parameters:
Full configuration and device signature for the HybridEP buffer.
Returns: int
The greatest successfully initialized token capacity, or zero before initialization.
Raises:
RuntimeError: If the active process-global buffer has an incompatible signature.
Get or create a UCCL-EP buffer for all-to-all communication.
Combine expert outputs through HybridEP.
Parameters:
Permuted expert outputs with shape [dispatched_tokens, hidden].
Opaque handle returned by hybrid_ep_dispatch.
Static token capacity used by non-blocking dispatch.
Optional token padding multiple.
Fuse output unpermutation into the combine kernel.
Returns: torch.Tensor
Combined hidden states in the original token order with shape [tokens, hidden].
Dispatch tokens through HybridEP while retaining dense probability layout.
Parameters:
Hidden states with shape [tokens, hidden].
Optional dense routing map with shape [tokens, experts].
Dense routing probabilities with shape [tokens, experts].
Expert-parallel process group.
Number of experts owned by each rank.
SMs reserved for HybridEP dispatch.
SMs reserved for HybridEP combine.
Static output capacity for non-blocking dispatch.
Optional token padding multiple.
Optional global expert indices with shape [tokens, top_k], with -1 for unused slots.
Global expert count required with compact indices.
Fuse token permutation into the dispatch kernel.
Optional SM count for routing-metadata preprocessing.
Optional dispatch permutation block count.
Optional combine unpermutation block count.
Returns: torch.Tensor
Dispatched hidden states with shape [dispatched_tokens, hidden], grouped by local
Bind a HybridEP dispatch recorder for checkpoint forward or recompute.
Initialize the HybridEP buffer, including buffer allocation and metadata initialization.
If a runtime dispatch/combine requires a larger buffer than the one initialized, the buffer will be reallocated at runtime, incuring extra run-time overhead.
Parameters:
Process group for HybridEP all-to-all communication.
Hidden dimension of the input tensor.
Maximum sequence length of the input tensor.
Number of local experts.
Number of SMs used by the dispatch API.
Number of SMs used by the combine API.
Whether to use FP8 communication during the dispatch phase.
Optional SM count for routing-metadata preprocessing.
Optional dispatch permutation block count.
Optional combine unpermutation block count.
Publish a successfully initialized process-global HybridEP capacity.
Parameters:
Full configuration and device signature for the HybridEP buffer.
Successfully initialized token capacity.
Raises:
RuntimeError: If the active process-global buffer has an incompatible signature.
Reset the HybridEP buffer and its published pipeline runtime state.
Sets the number of SMs to use for DeepEP.
Sets the number of SMs to use for UCCL-EP.
Store the kernels HybridEP compiled in this process into the shared cache (local rank 0 only).
Files are written to a temporary name and atomically renamed, so concurrent writers from several nodes leave a complete file behind; existing entries are kept.
Perform fused combine using UCCL-EP.
Perform fused dispatch using UCCL-EP.