nemo_automodel.components.models.ergo.magi_cp
nemo_automodel.components.models.ergo.magi_cp
Model-owned packed context parallelism with MagiAttention.
MagiAttention partitions the tokens of a packed row across the context-parallel ranks and exchanges only
the keys and values each rank’s queries can attend. A local layer attends at most
local_attention_window keys of the query’s document, so its exchange is a narrow band; a global layer
exchanges the causal keys of each document. The two masks share one token partition, so the residual
stream never moves between layers.
The plan follows muse_spark/magi_cp.py: one dispatch per pack in 512-token chunks, a full-causal key and window
keys that share its token partition, and plans selected per pipeline microbatch. It stays separate because the
input contracts differ: Ergo concatenates the packs of one pipeline microbatch into one token stream and has a
single window, local_attention_window; Muse dispatches one pack per microbatch and builds one key per entry of
layer_sliding_windows.
Module Contents
Classes
Functions
Data
API
Plans of the pipeline microbatches of one step; the pipeline’s index tensor selects each.
One plan per microbatch, in batch order.
Return the plan of one microbatch.
Parameters:
Int64 tensor of shape [1]: the microbatch’s position in the step’s batch,
split from an arange by the pipeline schedule.
Returns: ErgoMagiPlan
The immutable plan of that microbatch.
Attention keys of one packed microbatch; every mask keeps the same dispatched token order.
Magi key by window length, with zero for full causal attention.
Compute local THD output with Magi’s own key/value communication.
Parameters:
CP-local queries of shape [tokens, heads, head_dim].
CP-local keys of shape [tokens, kv_heads, head_dim].
CP-local values of shape [tokens, kv_heads, head_dim].
Sliding-window length in tokens, the query included, or zero for full causal attention.
Attention score scale.
Returns: torch.Tensor
Attention output of shape [tokens, heads, head_dim] in the local token order.
Ergo model state needed to build model-owned Magi CP inputs.
Magi key fields consumed by the Ergo integration.
Build the full-causal and the local-window key of one packed microbatch.
Parameters:
CPU int32 padded document boundaries of shape [documents + 1].
Ergo attention configuration.
This pipeline stage’s context-parallel process group.
Returns: ErgoMagiPlan
Keys for full causal attention and for the local window, with identical local token ownership.
Prepare model-owned packed CP, with one independent plan per pipeline microbatch.
Parameters:
Ergo model or pipeline stage with a bound context-parallel mesh.
Global token IDs, labels and positions of shape [batch, sequence] and packed document lengths of shape [batch, documents].
Number of pipeline microbatches, which evenly divides the batch size. The packed rows of one microbatch form one token stream.
Returns: dict[str, ContextParallelSharder]
A model-owned CP sharder. Token IDs stay global with shape [chunks, tokens]; labels, positions,