fix(checkpoint): load base weights under DDP and MegatronFSDP - #3597
Open
ralovets wants to merge 2 commits into
Open
fix(checkpoint): load base weights under DDP and MegatronFSDP#3597ralovets wants to merge 2 commits into
ralovets wants to merge 2 commits into
Conversation
AutoModel's own model implementations (anything in MODEL_ARCH_MAPPING) trained from random weights under `strategy: ddp` and `strategy: megatron_fsdp`, with no error or warning. `apply_model_infrastructure` gated the base-checkpoint read on `is_meta_device`, whose `else` branch assumed "not on meta" implies "weights already loaded". That holds for the HuggingFace fallback path, where `from_pretrained` populates the weights during init. It does not hold for custom implementations, which `_init_model` builds with `model_cls(hf_config)` -- architecture only. `auto_model.py` excludes DDPManager and MegatronFSDPManager from meta-device init, so every custom model under those two wrappers took the `else` branch and never read its checkpoint. Setting `checkpoint_already_loaded = True` then suppressed the post-shard load that would otherwise have caught it. Branch on `weights_already_loaded` instead, which is the flag that answers whether the model still needs its checkpoint. It was already computed in `auto_model.py` and already consulted at the post-shard load site. fsdp2 is unaffected above world size 1: its mesh has a real `dp_shard` axis, so `dp_shard_size > 1` routes it to the post-shard load. megatron_fsdp is affected at every world size, its mesh axes being (dp, cp, tp) with no `dp_shard`. Verified on 2 GPUs with a tiny Llama checkpoint of all-0.5 weights and with Qwen3-1.7B, comparing the language-modeling loss on a known sentence: strategy GPUs tensors holding 0.5 Qwen3-1.7B loss ddp 1 0/21 -> 21/21 11.5824 -> 2.9304 ddp 2 0/21 -> 21/21 12.6028 -> 2.9304 megatron_fsdp 1 0/21 -> 21/21 12.4241 -> 2.9304 megatron_fsdp 2 0/12 local -> 12/12 12.2534 -> 2.9304 fsdp2 1 21/21 -> 21/21 2.9304 -> 2.9304 fsdp2 2 21/21 -> 21/21 2.9375 -> 2.9375 ln(151936) = 11.93, so every broken row sat at or above a uniform distribution over the vocabulary, and the values were not reproducible run to run. After the fix all five unsharded configurations agree exactly. The fsdp2 2-GPU value is unchanged and differs only because that is the one configuration applying FSDP2's bf16 MixedPrecisionPolicy. Adds four CPU tests. Three cover the load decision: two pinning it for either value of `weights_already_loaded`, and one end-to-end check that a model built without weights ends up holding the checkpoint's tensors. Two of those three fail without this change. The fourth pins the flag computation that feeds the decision, so a regression in the manager exclusion or in `weights_already_loaded` cannot silently reintroduce random-weight training. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Roman Ralovets <roman@ralovets.com>
Contributor
|
/ok to test ee5f50a |
yuhezhang-ai
approved these changes
Aug 20, 2026
yuhezhang-ai
left a comment
Contributor
There was a problem hiding this comment.
Thanks for fixing it! LGTM
akoumpa
enabled auto-merge (squash)
August 20, 2026 20:02
Contributor
|
/ok to test 6078fbc |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do ?
Loads the pretrained checkpoint for
MODEL_ARCH_MAPPINGmodels understrategy: ddpandstrategy: megatron_fsdp. These models previously trained from random weights, with noerror or warning.
The pre-shard load in
apply_model_infrastructurewas gated onis_meta_device. Itselsebranch assumes the weights already came fromfrom_pretrained, which is true onlyon the HuggingFace fallback path. AutoModel's own models are built architecture-only, and
auto_model.py:517excludesDDPManagerandMegatronFSDPManagerfrom meta init, so thecheckpoint read was skipped.
checkpoint_already_loaded = Truethen suppressed thepost-shard load. The fix branches on
weights_already_loaded, which is already computedin
auto_model.pyand already used at the post-shard load site.Changelog
Fix
nemo_automodel/_transformers/infrastructure.pyload_before_shardpath, branch onweights_already_loadedinstead ofis_meta_devicewhen deciding whether to read the base checkpoint.initialize_model_weightsstays gated onis_meta_device. Only meta models needtheir parameter shells materialized.
Tests
tests/unit_tests/_transformers/test_infrastructure.py(3 CPU tests, load decision)test_load_before_shard_loads_checkpoint_when_init_left_weights_unloaded: reads thecheckpoint when init left the weights unloaded. Fails without the fix.
test_load_before_shard_skips_checkpoint_when_init_already_loaded_weights: skips theread when
from_pretrainedalready populated the weights.test_load_before_shard_populates_unloaded_model_from_checkpoint: end to end againsta real safetensors checkpoint, asserts every parameter holds its value. Fails without
the fix.
tests/unit_tests/_transformers/test_auto_model.py(1 CPU test, flag computation)test_custom_model_under_ddp_still_needs_its_checkpoint: assertsis_meta_deviceandweights_already_loadedare both False for aMODEL_ARCH_MAPPINGmodel underDDPManager.Before your PR is "Ready for review"
Pre checks:
config key, or documented behavior changes.
Additional Information
Reproducing