-
Notifications
You must be signed in to change notification settings - Fork 4.2k
avoid circular dependency usage between mcore and modelopt #5644
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
base: main
Are you sure you want to change the base?
Changes from 18 commits
445e107
106a0be
422590f
818fd81
4fc8774
b6d4e6d
f98b41d
be77ec4
eb7551f
1479557
6e1bf96
14faa28
75b1e68
ff53f31
c2e5206
f52c7bb
f9a7a8e
de51811
d720d2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
|
|
||
| """Dist checkpointing modules needed for ModelOpt.""" | ||
|
|
||
| import copy | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from megatron.core import mpu | ||
| from megatron.core.safe_globals import safe_load_from_bytes | ||
|
|
||
| from ..serialization import load, load_common_state_dict, save | ||
| from ..validation import StrictHandling | ||
| from .torch import TorchDistLoadShardedStrategy | ||
|
|
||
| try: | ||
| import modelopt | ||
| import modelopt.torch.opt as mto | ||
| import modelopt.torch.utils.distributed as dist | ||
|
|
||
| has_nvidia_modelopt = True | ||
| except (ModuleNotFoundError, ImportError): | ||
|
dimapihtar marked this conversation as resolved.
Outdated
|
||
| has_nvidia_modelopt = False | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def remove_per_module_state(modelopt_state: dict[str, Any]) -> None: | ||
| """Remove metadata from the modelopt_state. | ||
|
|
||
| The metadata of the modelopt_state contains keys which may change with different pipeline | ||
| and expert parallelism. As a result, the metadata must be stored as several ShardedObject with | ||
| global and local layer offset mapping. | ||
|
|
||
| Args: | ||
| modelopt_state: the state_dict that contains all algorithms that have been applied | ||
| to the given model. | ||
| """ | ||
| if "modelopt_state_dict" not in modelopt_state: | ||
| return | ||
|
|
||
| for mode, config in modelopt_state["modelopt_state_dict"]: | ||
| metadata = config.get("metadata", None) | ||
| if metadata is not None: | ||
| _ = metadata.pop("quantizer_state", None) | ||
| _ = metadata.pop("subnet_config", None) | ||
| _ = metadata.pop("real_quantizer_state", None) | ||
| _ = metadata.pop("q_tensor_state", None) | ||
| else: | ||
| config["metadata"] = {} | ||
|
|
||
|
|
||
| def save_modelopt_state(model: list[torch.nn.Module], state_dict: dict[str, Any]) -> None: | ||
| """Save modelopt_state as a part of the per rank state_dict. | ||
|
|
||
| NOTE: Only used for Megatron-LM. | ||
|
|
||
| Args: | ||
| model: the modelopt optimized model | ||
| state_dict: the current modelopt optimized model state_dict to store | ||
| """ | ||
| if not mto.ModeloptStateManager.is_converted(model[0]): | ||
| return | ||
| if len(model) == 1: | ||
| state_dict["modelopt_state"] = mto.modelopt_state(model[0]) | ||
| else: | ||
| for i in range(len(model)): | ||
| mpu.set_virtual_pipeline_model_parallel_rank(i) | ||
| state_dict[f"modelopt_state_{i}"] = mto.modelopt_state(model[i]) | ||
|
|
||
|
|
||
| def save_sharded_modelopt_state( | ||
| model: list[torch.nn.Module], | ||
| checkpoint_name: str | Path, | ||
| sharded_strategy: tuple[str, int] | None = None, | ||
| prefix: str = "", | ||
| ) -> None: | ||
| """Save modelopt_state in the sharded state_dict format. | ||
|
|
||
| Args: | ||
| model: the model to restore the modelopt optimization | ||
| checkpoint_name: the checkpoint folder path | ||
| sharded_strategy: configures sharded tensors saving behavior and backend | ||
| prefix: the prefix to add to the modelopt_state keys ("model." for NeMo) | ||
| """ | ||
| if not mto.ModeloptStateManager.is_converted(model[0]): | ||
| return | ||
| if len(model) > 1: | ||
| raise ValueError("sharded_modelopt_state does not support virtual pipeline parallel!") | ||
| modelopt_checkpoint_name = f"{checkpoint_name}/modelopt_state" | ||
| if dist.is_master(): | ||
| os.makedirs(modelopt_checkpoint_name, exist_ok=True) | ||
| modelopt_state = copy.deepcopy(mto.modelopt_state(model[0])) | ||
| remove_per_module_state(modelopt_state) | ||
| save(modelopt_state, modelopt_checkpoint_name, sharded_strategy) | ||
|
|
||
|
|
||
| def _load_extra_state_from_sharded_checkpoint( | ||
| model: torch.nn.Module, | ||
| checkpoint_name: str | Path, | ||
| prefix: str, | ||
| metadata: dict[str, Any] | None = None, | ||
| ) -> None: | ||
| """Load extra state from sharded checkpoint. | ||
|
|
||
| Note: since extra_state is a subset of full the sharded_state_dict, we use | ||
| strict=StrictHandling.LOG_UNEXPECTED instead of LOG_ALL. | ||
|
|
||
| Args: | ||
| model: the model to load extra state into | ||
| checkpoint_name: the checkpoint folder path | ||
| prefix: the prefix to add to the modelopt_state keys | ||
| metadata: the metadata for distributed checkpointing | ||
|
|
||
| Note: | ||
| The metadata includes several breaking changes. For example, `singleton_local_shards` | ||
| is set to `True` (was not set before) in megatron-core-0.15.0. This flag affects the | ||
| sharded state_dict format and must be consistent between saving and loading. | ||
| """ | ||
| sharded_state_dict = model.sharded_state_dict(prefix=prefix) | ||
| extra_sharded_state_dict = {k: v for k, v in sharded_state_dict.items() if "_extra_state" in k} | ||
| extra_state_dict = load( | ||
| extra_sharded_state_dict, | ||
| checkpoint_name, | ||
| TorchDistLoadShardedStrategy(), | ||
| strict=StrictHandling.LOG_UNEXPECTED, | ||
| ) | ||
| extra_state_dict_no_prefix = {} | ||
|
|
||
| for k, v in extra_state_dict.items(): | ||
| if k.startswith(prefix): | ||
| extra_state_dict_no_prefix[k[len(prefix) :]] = v | ||
| model.load_state_dict(extra_state_dict_no_prefix, strict=False) | ||
|
|
||
|
|
||
| def restore_sharded_modelopt_state( | ||
| model: list[torch.nn.Module], | ||
| checkpoint_name: str | Path, | ||
| prefix: str = "", | ||
| metadata: dict[str, Any] | None = None, | ||
| ) -> None: | ||
| """Restore modelopt_state from the sharded state_dict format. | ||
|
|
||
| Args: | ||
| model: the model to restore the modelopt optimization | ||
| checkpoint_name: the checkpoint folder path | ||
| prefix: the prefix to add to the modelopt_state keys ("model." for NeMo) | ||
| metadata: the metadata for distributed checkpointing | ||
|
|
||
| Note: | ||
| The metadata includes several breaking changes. For example, `singleton_local_shards` | ||
| is set to `True` (was not set before) in megatron-core-0.15.0. This flag affects the | ||
| sharded state_dict format and must be consistent between saving and loading. | ||
| """ | ||
| if len(model) > 1: | ||
| raise ValueError("sharded_modelopt_state does not support virtual pipeline parallel!") | ||
|
|
||
| modelopt_checkpoint_name = f"{checkpoint_name}/modelopt_state" | ||
|
|
||
| # Early return if the model already has a modelopt_state or the checkpoint does not exist. | ||
| if not os.path.exists(modelopt_checkpoint_name) or mto.ModeloptStateManager.is_converted( | ||
| model[0] | ||
| ): | ||
| return | ||
|
|
||
| # Loading the common modelopt_state (replicated on all ranks). | ||
| # Detect format: legacy checkpoints store common state in a standalone common.pt file; | ||
| # newer sharded checkpoints store it as a ShardedObject inside the torch_dist checkpoint. | ||
| legacy_common_path = os.path.join(modelopt_checkpoint_name, "common.py") | ||
| if os.path.exists(legacy_common_path): | ||
| common_modelopt_state = safe_load_from_bytes(legacy_common_path) | ||
| else: | ||
| common_modelopt_state = load_common_state_dict(modelopt_checkpoint_name) | ||
|
|
||
| modelopt_load_version = common_modelopt_state["modelopt_version"] | ||
|
|
||
| logger.info( | ||
| f"nvidia-modelopt ckpt/inst version: {modelopt_load_version}/{modelopt.__version__}" | ||
| ) | ||
|
|
||
| model[0] = mto.restore_from_modelopt_state(model[0], common_modelopt_state) | ||
|
|
||
| _load_extra_state_from_sharded_checkpoint(model[0], checkpoint_name, prefix, metadata=metadata) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't these go into the other file too?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are only two places where we use these modules: Do you think it'd be better to have separate file for these modelopt modules?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need to use these functions in megatron bridge as well so having in megatron.core makes more sense |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is has_nvidia_modelopt used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a default style of guarding in mcore