nemo_automodel.components.distributed.megatron_fsdp
nemo_automodel.components.distributed.megatron_fsdp
Module Contents
Classes
Functions
Data
API
Manager for parallelizing models using MegatronFSDP with TP, DP, CP sharding.
This manager applies parallelization to the model using a prescribed TP sharding plan. It supports mixed precision and various FSDP options.
The device mesh must be created externally and passed in.
Parameters:
Configuration for MegatronFSDP distributed training.
Device mesh for distributed operations.
Parallelizes the given model using MegatronFSDP and TP sharding strategies.
Parameters:
The model to be parallelized.
The optimizer for the model. If None, user needs to call model.finish_grad_sync() before optimizer.step(), model.install_optimized_model_weights() and model.zero_grad_buffer() after optimizer.zero_grad().
Returns:
(parallelized_model, optimizer)
Register an already-built optimizer with a MegatronFSDP-wrapped model.
Megatron-FSDP 0.5.0’s fully_shard_optimizer recovers the owning
MegatronFSDP from a _megatron_fsdp_model attribute that
MegatronFSDP.__init__ stamps onto each distributed Parameter. That
attribute is a plain Python attribute and does not survive operations that
rebuild Parameter objects (e.g. the dtype/device cast the from_pretrained
load path performs after wrapping). The combined fully_shard(model, optimizer)
entry point never hits this because it registers the optimizer in the same call,
before any such op runs; the recipe’s separate build-model-then-build-optimizer
order does, leaving fully_shard_optimizer unable to find the reference and
aborting before the first optimizer step. Re-stamp the reference (mirroring the
wheel’s own __init__ logic) on the current distributed params right before
deferred sharding so the separate sequence matches the combined entry point.
Shard the optimizer with Megatron-FSDP when the strategy requires it.
Returns the optimizer unchanged unless distributed_config is a
:class:MegatronFSDPConfig running in a distributed (world size > 1) job.
Parameters:
The (already sharded) model part the optimizer belongs to.
The optimizer to (optionally) shard.
Distributed strategy config; only triggers sharding
when it is a :class:MegatronFSDPConfig.
Guard for optimizers incompatible with Megatron-FSDP sharding (e.g. Dion); asserts when sharding would otherwise apply.
Re-apply Megatron-FSDP per-parameter attributes dropped by a post-wrap rebuild.
Companion to :func:snapshot_distributed_param_attrs. For each current parameter
(matched by name, since the rebuild replaced the objects) it restores any snapshot
attribute the rebuilt parameter is missing, following the fix suggested by the
Megatron-FSDP maintainer on NVIDIA/Megatron-LM#5790: only attributes absent on the
new parameter are copied, so genuinely re-derived state is never clobbered.
Parameters:
The Megatron-FSDP-wrapped model whose parameters were rebuilt.
The mapping returned by :func:snapshot_distributed_param_attrs, or
None (no-op).
Snapshot the per-parameter attributes Megatron-FSDP stamps on distributed params.
MegatronFSDP.__init__ decorates each distributed Parameter with plain
Python attributes that later training steps depend on: _megatron_fsdp_model
(owning-model back-ref used by :func:fully_shard_optimizer), _is_shared
(set on tied parameters so _grad_acc routes their gradients through the root
hook instead of double-accumulating), orig_param/megatron_fsdp_dist_index/
megatron_fsdp_slice and the reset_attribute closure. These attributes live
in the Parameter.__dict__ and are silently dropped by any post-wrap operation
that rebuilds Parameter objects — e.g. the from_pretrained checkpoint
reload and the lm_head re-tie the recipe performs after wrapping. Capture them
here, keyed by parameter name (object identity does not survive the rebuild), so
:func:restore_distributed_param_attrs can re-apply them afterwards.
remove_duplicate=False is required so tied parameters (e.g. lm_head.weight
aliasing model.embed_tokens.weight) are captured under every name they appear
under, including the _is_shared marker Megatron-FSDP places on the tied alias.
Parameters:
The (possibly Megatron-FSDP-wrapped) model to snapshot.
Returns: dict[str, dict] | None
A mapping from parameter name to a copy of its __dict__, or None when