bridge.models.megatron_mimo.megatron_mimo_builder#

Module Contents#

Functions#

_lazy_nccl_pg_options

Return new NCCL options that create a MegatronMIMO group’s communicator on first use.

build_hypercomm_grids

Create HyperCommGrid objects per module from MegatronMIMO parallelism config.

populate_embedding_and_position_groups

Create embedding-related process groups from globally enumerated PP ranks.

is_pp_first_stage

Check if current rank is first stage in pipeline.

is_pp_last_stage

Check if current rank is last stage in pipeline.

Data#

API#

bridge.models.megatron_mimo.megatron_mimo_builder.EXPERT_VIEW_NAME#

‘expert’

bridge.models.megatron_mimo.megatron_mimo_builder._lazy_nccl_pg_options() → torch.distributed.ProcessGroupNCCL.Options | None#

Return new NCCL options that create a MegatronMIMO group’s communicator on first use.

PyTorch builds whose nccl backend is ProcessGroupNCCL2 bind the default process group to a device, so every later new_group splits its NCCL communicator eagerly, even if the group never communicates. MegatronMIMO creates every parallelism group of every module on every rank, so eager splitting holds NCCL buffers for groups the rank never uses. lazy_init instead creates a group’s communicator at the group’s first operation. That creation is collective over all members, so every member must take part in the first operation: MIMO groups are used by collectives over the whole group, and every pipeline stage issues its point-to-point operations on its pipeline group during the warm-up phase.

Returns:

Options with lazy_init=True when the default process group is a ProcessGroupNCCL2 group bound to a device and PyTorch has the option, else None (default options). lazy_init is specific to ProcessGroupNCCL2; on the legacy backend it would only replace the split with an eager per-group initialization. Each call returns a new object because PyTorch writes per-group settings into the options it is given.

bridge.models.megatron_mimo.megatron_mimo_builder.build_hypercomm_grids(
megatron_mimo_parallelism_config: megatron.bridge.models.megatron_mimo.megatron_mimo_config.MegatronMIMOParallelismConfig,
) → Dict[str, megatron.core.hyper_comm_grid.HyperCommGrid]#

Create HyperCommGrid objects per module from MegatronMIMO parallelism config.

Creates grids on ALL ranks (required for consistent collective calls), but only ranks in each grid’s range will participate in its operations.

Each grid is built with a dense base view plus a registered expert view over the same rank span. Dense process groups (tp/cp/dp/pp) come from the base view; expert-parallel groups (expt_tp/ep/expt_dp) come from the expert view, matching the contract mcore’s get_mimo_optimizer expects.

The groups are created with lazy NCCL initialization when the backend would otherwise split every group eagerly (see _lazy_nccl_pg_options), so a group allocates NCCL resources only once it communicates.

Parameters:

megatron_mimo_parallelism_config – MegatronMIMOParallelismConfig specifying parallelism per module.

Returns:

Dict mapping module names to their HyperCommGrids.

bridge.models.megatron_mimo.megatron_mimo_builder.populate_embedding_and_position_groups(
pp_rank_groups: list[list[int]] | None,
) → Tuple[Optional[torch.distributed.ProcessGroup], Optional[torch.distributed.ProcessGroup]]#

Create embedding-related process groups from globally enumerated PP ranks.

Following MCore semantics:

  • pos_embd_pg: Only rank 0 of PP (first stage) - for position embeddings

  • embd_pg: Ranks 0 and -1 of PP (first and last stages) - for tied word embeddings

IMPORTANT: This calls dist.new_group which is a collective operation. Must be called on all ranks that could participate.

Parameters:

pp_rank_groups – Every pipeline-parallel rank group in global creation order.

Returns:

Tuple of process groups for the current rank. Returns (None, None) when no pipeline-parallel rank groups are provided.

bridge.models.megatron_mimo.megatron_mimo_builder.is_pp_first_stage(
pp_group: Optional[torch.distributed.ProcessGroup],
) → bool#

Check if current rank is first stage in pipeline.

bridge.models.megatron_mimo.megatron_mimo_builder.is_pp_last_stage(
pp_group: Optional[torch.distributed.ProcessGroup],
) → bool#

Check if current rank is last stage in pipeline.