Skip to navigation

nemo_automodel.components.models.ergo.magi_cp

View as MarkdownOpen in Claude

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

NameDescription
ErgoMagiBatchPlansPlans of the pipeline microbatches of one step; the pipeline’s index tensor selects each.
ErgoMagiPlanAttention keys of one packed microbatch; every mask keeps the same dispatched token order.
_ErgoMagiModelErgo model state needed to build model-owned Magi CP inputs.
_MagiAttentionKeyMagi key fields consumed by the Ergo integration.

Functions

NameDescription
_build_magi_planBuild the full-causal and the local-window key of one packed microbatch.
prepare_magi_inputsPrepare model-owned packed CP, with one independent plan per pipeline microbatch.

Data

ErgoMagiBatch

ErgoMagiBatchValue

_TOKEN_FIELD_PADDING

logger

API

class nemo_automodel.components.models.ergo.magi_cp.ErgoMagiBatchPlans(
)
Dataclass

Plans of the pipeline microbatches of one step; the pipeline’s index tensor selects each.

plans
tuple[ErgoMagiPlan, ...]

One plan per microbatch, in batch order.

nemo_automodel.components.models.ergo.magi_cp.ErgoMagiBatchPlans.select(
microbatch_index: torch.Tensor

Return the plan of one microbatch.

Parameters:

microbatch_index
torch.Tensor

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.

class nemo_automodel.components.models.ergo.magi_cp.ErgoMagiPlan(
)
Dataclass

Attention keys of one packed microbatch; every mask keeps the same dispatched token order.

keys
dict[int, _MagiAttentionKey]

Magi key by window length, with zero for full causal attention.

nemo_automodel.components.models.ergo.magi_cp.ErgoMagiPlan.attention(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
window: int,
scale: float
) -> torch.Tensor

Compute local THD output with Magi’s own key/value communication.

Parameters:

query
torch.Tensor

CP-local queries of shape [tokens, heads, head_dim].

key
torch.Tensor

CP-local keys of shape [tokens, kv_heads, head_dim].

value
torch.Tensor

CP-local values of shape [tokens, kv_heads, head_dim].

window
int

Sliding-window length in tokens, the query included, or zero for full causal attention.

scale
float

Attention score scale.

Returns: torch.Tensor

Attention output of shape [tokens, heads, head_dim] in the local token order.

class nemo_automodel.components.models.ergo.magi_cp._ErgoMagiModel()
Protocol

Ergo model state needed to build model-owned Magi CP inputs.

config
ErgoConfig
cp_mesh
DeviceMesh | None
class nemo_automodel.components.models.ergo.magi_cp._MagiAttentionKey()
Protocol

Magi key fields consumed by the Ergo integration.

total_seqlen_q
int
nemo_automodel.components.models.ergo.magi_cp._build_magi_plan(
cu_seqlens: torch.Tensor,
group: torch.distributed.ProcessGroup

Build the full-causal and the local-window key of one packed microbatch.

Parameters:

cu_seqlens
torch.Tensor

CPU int32 padded document boundaries of shape [documents + 1].

config
ErgoConfig

Ergo attention configuration.

group
torch.distributed.ProcessGroup

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:

model
_ErgoMagiModel

Ergo model or pipeline stage with a bound context-parallel mesh.

batch
ErgoMagiBatch

Global token IDs, labels and positions of shape [batch, sequence] and packed document lengths of shape [batch, documents].

num_chunks
int

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,

nemo_automodel.components.models.ergo.magi_cp.ErgoMagiBatch = dict[str, ErgoMagiBatchValue]
nemo_automodel.components.models.ergo.magi_cp.ErgoMagiBatchValue = torch.Tensor | str | int | float | bool | ErgoMagiPlan | ErgoMagiBatchPlans | No...
nemo_automodel.components.models.ergo.magi_cp._TOKEN_FIELD_PADDING: dict[str, int | bool] = {'position_ids': 0, 'labels': -100, 'loss_mask': 0, 'padding_mask': True}
nemo_automodel.components.models.ergo.magi_cp.logger = logging.getLogger(__name__)