Skip to content

Add load-time GPT-to-Hybrid checkpoint translation (#5675) - #5792

Merged
balasaajay merged 15 commits into
NVIDIA:mainfrom
guihong-nv:guihongl/gpt-hybrid-ckpt-interop-main
Jul 28, 2026
Merged

Add load-time GPT-to-Hybrid checkpoint translation (#5675)#5792
balasaajay merged 15 commits into
NVIDIA:mainfrom
guihong-nv:guihongl/gpt-hybrid-ckpt-interop-main

Conversation

@guihong-nv

@guihong-nv guihong-nv commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Allow HybridModel runs to load GPT (pure transformer) distributed checkpoints directly, without an offline conversion step. This supports both torch_dist and Megatron-FSDP (fsdp_dtensor) checkpoints, including model weights and distributed optimizer state. Instead of rewriting the checkpoint on disk, load_checkpoint retargets the hybrid model's own sharded state dict at the GPT checkpoint: attention and MLP entries are rewritten to the checkpoint's homogeneous layer format (layer index dropped from the storage key and prepended as a sharding axis), the final norm key is renamed, and layers without a GPT counterpart (for example Mamba) keep their fresh initialization via LocalNonpersistentObject. The regular distributed-checkpointing machinery then reads the GPT checkpoint directly and reshards across parallel-layout changes.

--hybrid-layer-pattern is required for GPT-to-Hybrid translation. --finetune is optional and keeps its normal meaning: a direct --load without it resumes iteration, optimizer, scheduler, RNG, and rerun state under normal compatibility rules; using --finetune resets training progress while still allowing translated optimizer state to load. --no-load-optim remains available for a weights-only load. The layer pattern must pair one-to-one with the checkpoint's layers (no MTP, no GDN/DSA positions, uniform dense or MoE MLP kind).

Also add --use-legacy-model so the hybrid entrypoint can keep training a GPT checkpoint as GPTModel without any translation.

Tests cover the pure layer-map/retarget logic and distributed model-plus-optimizer round trips across DP, Megatron FSDP, TP, PP, CP, EP, and ETP reconfigurations, including gated-MLP (SwiGLU factory) and grouped-GEMM MoE variants. Dedicated cases verify resume without --finetune for both torch_dist and fsdp_dtensor checkpoints.

This PR is required by #5675.

  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@guihong-nv
guihong-nv requested review from a team as code owners July 14, 2026 02:24
@copy-pr-bot

copy-pr-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft July 14, 2026 02:25
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 6cd111c

Comment thread megatron/training/arguments.py Outdated
@guihong-nv
guihong-nv force-pushed the guihongl/gpt-hybrid-ckpt-interop-main branch from 6cd111c to bda9121 Compare July 14, 2026 16:12
Allow HybridModel runs to load GPT (pure transformer) torch_dist
checkpoints directly, without an offline conversion step. Instead of
rewriting the checkpoint on disk, load_checkpoint retargets the hybrid
model's own sharded state dict at the GPT checkpoint: attention and MLP
entries are rewritten to the checkpoint's homogeneous layer format (layer
index dropped from the storage key and prepended as a sharding axis), the
final norm key is renamed, and layers without a GPT counterpart (e.g.
Mamba) keep their fresh initialization via LocalNonpersistentObject. The
regular dist-checkpointing machinery then reads the GPT checkpoint
directly and reshards across any TP/PP/EP/ETP layout change.

Requires --finetune and --no-load-optim, and validates that the hybrid
layer pattern pairs one-to-one with the checkpoint's layers (no MTP, no
GDN/DSA positions, uniform dense or MoE MLP kind).

Tests cover the pure layer-map/retarget logic and distributed round-trips
across TP/PP/EP/ETP reconfigurations, including gated-MLP (swiglu
factory) and grouped-GEMM MoE variants.

Signed-off-by: guihong-nv <guihongl@nvidia.com>
@guihong-nv
guihong-nv force-pushed the guihongl/gpt-hybrid-ckpt-interop-main branch from bda9121 to 562b4f4 Compare July 14, 2026 16:14
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 20ca49f

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30155062432

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 25, 2026
@guihong-nv
guihong-nv enabled auto-merge July 27, 2026 16:17
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 0f73838

Signed-off-by: guihong-nv <guihongl@nvidia.com>
Signed-off-by: guihong-nv <guihongl@nvidia.com>
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 716875a

Signed-off-by: guihong-nv <guihongl@nvidia.com>
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 49a44bd

1 similar comment
@guihong-nv

Copy link
Copy Markdown
Contributor Author

/ok to test 49a44bd

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30313936889

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30315004225

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30317255643

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30332765580

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30337950766

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants