nemo_automodel.components.distributed.parallelizer
nemo_automodel.components.distributed.parallelizer
Module Contents
Classes
Functions
Data
MegatronFSDPMixedPrecisionPolicy
_MEGATRON_FSDP_050_REQUIRED_MSG
_megatron_fsdp_nan_check_noop_warned
API
Single model-owned parallelization sidecar contract.
Apply the shared dense FSDP2 implementation.
Wrap the model’s submodules into FSDP2 units.
Model-owned sidecar strategies deriving from this class override this
hook to change how parameters are grouped into FSDP units without
reimplementing the surrounding TP/AC/mixed-precision flow. Override
_fully_shard_module when a model needs a different primitive.
Apply the FSDP2 primitive used by this model sidecar.
Give large trainable untied embedding tables independent FSDP buffers.
The generic dense path otherwise leaves both tables in the root FSDP unit.
With fp32 gradient reduction, that unit allocates one contiguous
reduce-scatter input containing both gradients. Keeping the two trainable
leaf modules in separate FSDP units bounds that allocation by the larger
table instead of their sum. This pass skips tied tables to preserve aliasing,
and frozen tables because they have no gradient communication buffer
to split. Tables already wrapped as FSDP units retain their existing
reshard policy. With an explicit reshard_after_forward=True, a
container-hosted head reshards while a newly split top-level head stays
gathered. FusedLinearCrossEntropy handles the sharded weight with correct
gradients, at the cost of an extra all-gather.
Parameters:
Model whose input and output embedding modules may be sharded.
Device mesh that owns the FSDP shards.
Mixed-precision policy used by the surrounding FSDP units.
Optional offload policy used by the surrounding FSDP units.
Whether the input embedding unit reshards its parameters after forward.
Return whether this model safely opts into whole-layer checkpointing.
Validate the model’s attention topology against its TP mesh.
Apply every requested parallelism and return the parallelized model.
Compile each decoder layer in-place after FSDP2 sharding.
Compiles at decoder-layer granularity (not sub-module) so that AOT autograd traces the joint fwd+bwd graph under the training loop’s enable_grad context. Sub-module compile (e.g. on mlp alone) would be traced during activation checkpointing’s first forward pass which runs under no_grad, producing a forward-only graph that drops LoRA and other trainable-parameter gradients.
Prerequisite: NO_REENTRANT checkpoint_wrapper must already be applied to self_attn
and mlp before FSDP2 sharding (done by ModelParallelizer). This
function only handles the compile step.
Whole-block selective-AC wrappers (tagged with SELECTIVE_AC_WRAPPER_FLAG)
are compiled OUTER — the wrapper itself is compiled so the selective policy
is traced and the partitioner honors its recompute tags. Other layer-level
CheckpointWrappers (e.g. the PP path) are unwrapped and the decoder layer is
compiled directly.
nn.Module.compile() is used instead of torch.compile() to compile in-place without introducing an _orig_mod wrapper, which would add a key prefix and break checkpoint loading.
_patch_dtensor_spec_hash_for_symint() is called to allow torch.compile with dynamic shapes to coexist with DTensor’s lru_cache-based sharding propagation.
Resolve the language-model config structurally for TP validation.
Return True when the TP plan column-wise shards any QKV attention projection.
When Q/K/V projections use ColwiseParallel with sharded output (the
default), each TP rank holds num_heads / tp_size heads and the model
config / layer attributes must be updated accordingly.
Plans that keep attention replicated (e.g. Phi-3 with RowwiseParallel
on fused QKV and Replicate output) should not trigger a head-count
update.
Derive the MegatronFSDP wrap classes from a model’s _no_split_modules.
Used when a config does not specify megatron_fsdp_unit_modules. HF
PreTrainedModel and the NeMo custom models both define _no_split_modules
as a list of block class names (for example ["LlamaDecoderLayer"]).
Walking model.modules() and matching type(module).__name__ against those
names resolves the actual instantiated classes, so the result is correct for
both the HF backend and the NeMo-custom backend (which use distinct classes that
share the same name). For VLM/MoE models whose top-level _no_split_modules
lists several block classes (for example vision and language towers), every
matching class found anywhere in the module tree is collected.
Parameters:
The (already TP-parallelized) model to be wrapped by MegatronFSDP.
Returns: list[type[nn.Module]]
The de-duplicated list of submodule classes to wrap as MegatronFSDP units,
Raises:
ValueError: If the model does not expose a non-empty_no_split_modules, or if none of those names match an instantiated submodule. Raised with an actionable message instead of letting MegatronFSDP later fail withZeroDivisionError(total_fsdp_module=0) when zero modules are wrapped.
Discover language, vision, and audio layer containers structurally.
Extract transformer layers grouped by model role.
Extract layers from different model architectures for parallelization.
This compatibility wrapper flattens grouped language/vision/audio layers.
New activation-checkpointing code should use _extract_model_layer_groups
so scope decisions can be explicit.
Select trainable activation-checkpointed layers from grouped model layers.
Resolve the optional Hugging Face embedding getters.
Select the tensor-parallel plan for the given model.
Priority order:
- If
tp_shard_planis provided as a dict or import path, use it. - If the model type exists in
PARALLELIZE_FUNCTIONS, use its optimised plan; on failure, fall back to HF plan. - Otherwise, prefer the model’s HF-native
_tp_plan(viaget_hf_tp_shard_plan). - Otherwise, fall back to the default base plan.
When tp_size > 1 and the model falls through to path 4 and the
model class was loaded from a custom-code source (HF’s
trust_remote_code=True path, where the dynamic class lives under
transformers_modules.*), this raises ValueError instead of
returning the default base plan. On recent PyTorch the default plan’s
placements do not populate shard_order and trip an internal assert in
torch.distributed.tensor._redistribute on the first weight
redistribute, which surfaces to the user as an opaque PyTorch internal
error. Custom-code architectures are the only known-broken case (see
https://github.com/NVIDIA-NeMo/Automodel/issues/2243); known HF
architectures that happen to fall through (e.g. Mixtral) are left on the
default plan with a warning, since they have been working in practice.
When the model did define a _tp_plan but get_hf_tp_shard_plan
raised while translating it (e.g. styles nemo does not recognize), the
translator’s error message is folded into the ValueError as a
diagnostic so the user can tell whether to add a _tp_plan from
scratch or fix the styles in the one they already have.
Return whether whole-block activation checkpointing is safe for a KV-shared model.
checkpoint_wrapper replays a whole decoder block during backward with the
arguments the forward saw. Unlike HF’s GradientCheckpointingLayer.__call__
it cannot drop past_key_values from that replay, so every layer that
writes to a cache writes to it a second time. A KV-shared model then needs
both halves to hold:
- the layers that populate the cache — the non-shared ones, which are what
call
Cache.update()— must not accumulate on the replay, or the recomputed K/V stops matching the forward; - the shared layers must still read the K/V their source layer produced.
Neither holds for a model backed by an accumulating Cache: the second
Cache.update() grows the entry and backward dies with a
CheckpointError about changed tensor metadata (observed on native HF
Gemma3nForCausalLM with use_cache=True). KV-shared models therefore
stay on apply_submodule_checkpointing, which leaves attention unwrapped,
by default.
A model that satisfies both halves opts in by setting the class attribute
kv_sharing_survives_checkpoint_replay = True. Gemma4 E2B/E4B qualify: a
pass-through holder stands in for the cache, and the shared layers read a
separate store that the replay does not disturb (see
gemma4_moe/model.py).
Parameters:
The model about to be checkpointed.
Returns: bool
Whether the model declares its KV sharing safe under whole-block replay.
Translate the config precision controls to the Megatron-FSDP 0.5.0 API.
megatron-fsdp==0.5.0, the only supported release, expresses precision
through a MixedPrecisionPolicy plus a more expensive per-parameter NaN
reporter. The reporter stays a separate opt-in rather than being silently
enabled from the legacy buffer-check setting; because 0.5.0 has no
buffer-level NaN check at all, a truthy check_for_nan_in_grad is
dropped with a one-time warning that points at report_nan_in_param_grad
as the opt-in replacement. Any other fully_shard signature — older or
newer releases alike — fails loudly instead of guessing a translation.
Fix a crash when torch.compile + DTensor are used together.
Problem: torch.compile traces with symbolic shapes (SymInt). DTensorSpec hashes its shape to cache sharding decisions, but SymInt is not hashable -> crash.
Fix: if hashing the shape fails, fall back to hashing only (mesh, placements). Cache hits are slightly reduced but correctness is unaffected.
Return whether HF-native gradient checkpointing can preserve AutoModel’s AC scope.
Return the most specific nested Hugging Face TP-plan provider.
After TP sharding, the Q/K/V outputs are split across ranks (each rank has num_heads/tp_size heads). Update the config and each attention layer’s num_heads / num_key_value_heads so the forward uses the local head count instead of the global one (avoids shape mismatches in .view()).
Return whether model contains Automodel’s grouped custom-MoE layer.
Recursively apply FSDP2 sharding to modules, with optimizations for ModuleList.
This utility function traverses a model hierarchy and applies FSDP2 sharding to each module. For ModuleList instances (commonly used for transformer layers), it applies an optimization where the last layer doesn’t reshard after forward since FSDP2 will prefetch it immediately.
Handles both single-level and nested ModuleList/ModuleDict structures. If a ModuleList contains other ModuleLists, it will recurse into them instead of trying to wrap them (since ModuleList doesn’t have a forward method).
Note: This function modifies the module in-place by replacing modules with their FSDP2-subclassed versions.
Parameters:
The module to apply FSDP sharding to.
The device mesh for FSDP sharding.
Mixed precision policy for FSDP.
CPU offload policy for FSDP. Defaults to None.
Enable explicit forward/backward prefetch chains.
Backward prefetch depth.
Forward prefetch depth.
Optional override for each layer’s
fully_shard reshard behavior.
Optional model sidecar that owns the FSDP primitive.
Whether fully frozen multimodal modules are owned by the root FSDP unit, sharded per layer, or replicated.
Accumulator for replicated frozen multimodal parameters that must be ignored by ancestor FSDP roots.
Apply selective activation checkpointing to model end to end.
Standalone entry point (detects KV-sharing, disables use_cache, and
wraps transformer blocks) for paths where the FSDP2 parallelize flow is
skipped — notably single-GPU training.
Parameters:
The model to checkpoint.
Whether per-layer torch.compile will be applied.
Which extracted layer groups to wrap.
Apply parallelisms and activation checkpointing to the model.
Compatibility entry point for the model-owned parallelization contract:
- Resolves an optional sidecar from the model class
- Uses the generic infrastructure implementation when no sidecar is supplied
- Custom parallel plan support (dict or string path)
- Sequence parallel support
- Activation checkpointing for linear layers
- Model validation (attention heads divisible by TP size)
- Better fallback logic
NOTE: The passed-in model preferably should be on meta device. Otherwise, the model must fit on GPU or CPU memory.
Parameters:
The model to be parallelized.
The device mesh for distributed training.
Mixed precision policy for model parallelism.
The offload policy for FSDP.
Whether to use sequence parallelism. Defaults to False.
Whether to use activation checkpointing. Defaults to False.
Custom tensor parallel plan for the model. Can be:
- A dictionary mapping module names to parallel styles
- A string path to a dictionary or function that returns a dictionary If provided, this takes precedence over automatic plan generation.
Key name for the data parallel replicate mesh in device_mesh. Used when data parallel replicate is enabled. Defaults to “dp_replicate”.
Key name for the data parallel shard + context parallel mesh in device_mesh. Used when data parallel shard is enabled. Defaults to “dp_shard_cp”.
Key name for the tensor parallel mesh in device_mesh. Defaults to “tp”.
Whether fully frozen multimodal modules are
owned by the root FSDP unit ("root"), sharded normally
("per_layer"), or excluded from FSDP and copied on every rank
("replicate").
Optional callback that re-resolves parameter trainability after strategy-specific model surgery and immediately before FSDP construction.
Returns: nn.Module
The parallelized model.
Translate a model-owned Hugging Face TP plan without importing model classes.
Return transformer layers grouped by model role (e.g. language, vision).
Public accessor over the per-model layer-group mapping used for FSDP wrapping and activation checkpointing, so other components (e.g. the MoE parallelizer) can consume the grouping without importing private helpers.
Parameters:
Root model to extract grouped layers from.
Returns: Dict[str, List[nn.Module]]
Mapping from group name to the list of transformer blocks in that group.
Import a class from a string path (e.g. ‘torch.optim.AdamW’).
Parameters:
Full path to class including module path and class name
Returns: Any
The imported class object
Helper function to import classes from string paths.
Parameters:
The list of string paths to the classes.
Returns:
List of imported classes.
Apply tensor/data parallelism (MegatronFSDP) and optional activation-checkpointing to the model.
NOTE: The passed-in model should preferably reside on the meta device. Otherwise, ensure the model fits into available GPU or CPU memory.
NOTE: The user must ensure that the provided tp_shard_plan is compatible with the model architecture.
Parameters:
The model to be parallelized.
The device mesh describing the physical devices used for distributed training.
Class paths of the sub-modules that
should become individual MegatronFSDP units. When None or empty, the wrap classes
are auto-derived from the model’s _no_split_modules (see
_derive_megatron_fsdp_unit_modules).
A tensor-parallel sharding plan. Keys are module names; values specify the parallel style to apply (e.g., RowwiseParallel, ColwiseParallel, SequenceParallel).
The zero-DP strategy to use.
If True, construct the model on a meta device first and materialize weights lazily to reduce memory fragmentation.
Reduce gradients in FP32 irrespective of the parameter precision to improve numerical stability.
Keep a master FP32 copy of weights when training in reduced precision (e.g., FP16/BF16).
If True, overlap gradient reduction with backward computation.
If True, overlap parameter gathering with forward computation.
Legacy buffer-level gradient NaN check.
BREAKING CHANGE on megatron-fsdp 0.5.0: this flag is a no-op,
preserved only for config compatibility. 0.5.0 removed the
buffer-level NaN check entirely, so gradient NaN checking is now OFF
regardless of this value; a truthy value is dropped with a one-time
warning per process. Enable report_nan_in_param_grad to restore
gradient NaN checking.
Whether Megatron-FSDP should perform
its precise per-parameter gradient NaN check. This is the 0.5.0
replacement for check_for_nan_in_grad and is disabled by default
because it can significantly reduce training throughput.
Perform gradient averaging inside the collective operation instead of dividing afterward.
Disable gradient bucketing; gradients are reduced immediately as they are produced.
Compute loss normalized by the number of tokens instead of the number of sequences.
Retain the FP8 transpose cache when using a custom MegatronFSDP wrapper.
Enable NCCL user-buffer API (experimental) for reduced latency on some networks.
Enable double buffering of parameters to overlap communication and computation in MegatronFSDP.
Key name for the data parallel mesh in device_mesh. Defaults to “dp”.
Key name for the tensor parallel mesh in device_mesh. Defaults to “tp”.
Optional callback that re-resolves parameter trainability after tensor-parallel surgery and immediately before Megatron-FSDP construction.
Translates string descriptions to parallelism plans.
In model configurations, we use a neutral type (string) to specify parallel styles, here we translate them into torch.distributed tensor-parallel types.
Explicitly unshard and then reshard the FSDP2 modules. Useful for logprob inference.
Validate that every sharded language attention shape divides the TP size.