-
Notifications
You must be signed in to change notification settings - Fork 239
feat(mimo): phase 2 - model provider, DDP wrapping, process groups #2004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yaoyu-33
merged 6 commits into
NVIDIA-NeMo:main
from
aroshanghias-nvd:mimo/phase2-model
Mar 12, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59b7d32
feat(mimo): phase 2 - DDP wrapping, embedding groups, process groups
aroshanghias-nvd 67816bd
refactor(mimo): address phase 2 review comments
aroshanghias-nvd c0f9483
refactor(mimo): rename populate_embedding_and_position_groups to crea…
aroshanghias-nvd 55dcedd
fix(mimo): fix is_pp_first/last_stage test failures after refactor
aroshanghias-nvd 82c3426
Merge branch 'main' into mimo/phase2-model
aroshanghias-nvd fd4a3af
Merge branch 'main' into mimo/phase2-model
aroshanghias-nvd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| """DDP wrapping utilities for MIMO models. | ||
|
|
||
| Called from the training layer after MimoModelProvider.provide(). | ||
|
|
||
| Note: This module only supports DDP wrapping. FSDP is not yet implemented. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Dict, Optional | ||
|
|
||
| from megatron.bridge.models.mimo.mimo_builder import is_current_rank_in_grid | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from megatron.core.distributed import DistributedDataParallelConfig | ||
| from megatron.core.hyper_comm_grid import HyperCommGrid | ||
| from megatron.core.models.mimo import MimoModel | ||
| from megatron.core.process_groups_config import ProcessGroupCollection | ||
|
|
||
| from megatron.bridge.models.mimo.mimo_config import MimoParallelismConfig | ||
|
|
||
|
|
||
| def wrap_mimo_model_distributed( | ||
aroshanghias-nvd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mimo_model: "MimoModel", | ||
| ddp_config: "DistributedDataParallelConfig", | ||
| mimo_parallelism_config: "MimoParallelismConfig", | ||
| grids: Dict[str, "HyperCommGrid"], | ||
| pg_collections: Dict[str, Optional["ProcessGroupCollection"]], | ||
| ) -> "MimoModel": | ||
| """Wrap MIMO model's submodules with DDP. | ||
|
|
||
| Modifies mimo_model in-place and returns it. | ||
|
|
||
| Args: | ||
| mimo_model: The MimoModel to wrap. | ||
| ddp_config: DDP configuration from Bridge. | ||
| mimo_parallelism_config: MIMO parallelism configuration. | ||
| grids: Module name to HyperCommGrid mapping. | ||
| pg_collections: Module name to ProcessGroupCollection mapping. | ||
|
|
||
| Returns: | ||
| The same mimo_model with wrapped submodules. | ||
| """ | ||
| from megatron.core.distributed import DistributedDataParallel | ||
|
|
||
| # Wrap language model if present and rank participates | ||
| if mimo_model.language_model is not None: | ||
| llm_grid = grids["llm"] | ||
| if is_current_rank_in_grid(llm_grid): | ||
| llm_pg = pg_collections.get("llm") | ||
| if llm_pg is not None: | ||
| mimo_model.language_model = DistributedDataParallel( | ||
| config=mimo_model.language_model.config, | ||
| ddp_config=ddp_config, | ||
| module=mimo_model.language_model, | ||
| pg_collection=llm_pg, | ||
| ) | ||
|
|
||
| # Wrap modality submodules | ||
| if hasattr(mimo_model, "modality_submodules"): | ||
| for module_name, submodule in mimo_model.modality_submodules.items(): | ||
| if submodule is None: | ||
| continue | ||
| module_grid = grids[module_name] | ||
| if not is_current_rank_in_grid(module_grid): | ||
| continue | ||
|
|
||
| module_pg = pg_collections.get(module_name) | ||
| if module_pg is None: | ||
| continue | ||
|
|
||
| # Get config from first encoder in the submodule. | ||
| # Note: We use the first encoder's config for DDP bucket sizing. | ||
| # This assumes all encoders in a modality submodule share similar | ||
| # parallelism settings, which is typical for MIMO models. | ||
| if hasattr(submodule, "encoders") and submodule.encoders: | ||
| encoder_key = next(iter(submodule.encoders.keys())) | ||
| first_encoder = submodule.encoders[encoder_key] | ||
|
|
||
| if not hasattr(first_encoder, "config"): | ||
| raise AttributeError( | ||
| f"Encoder '{encoder_key}' in modality '{module_name}' does not have " | ||
| f"a 'config' attribute. Encoders must be MegatronModule subclasses." | ||
| ) | ||
|
|
||
| wrapped = DistributedDataParallel( | ||
| config=first_encoder.config, | ||
| ddp_config=ddp_config, | ||
| module=submodule, | ||
| pg_collection=module_pg, | ||
| ) | ||
| mimo_model.modality_submodules[module_name] = wrapped | ||
|
|
||
| return mimo_model | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.