Skip to content

feat(moe): Support placing MTP layers into standalone stages - #2136

Merged
ericharper merged 22 commits into
NVIDIA:mainfrom
BestJuly:lit/mtp_layer_standalone_main
Jan 14, 2026
Merged

feat(moe): Support placing MTP layers into standalone stages#2136
ericharper merged 22 commits into
NVIDIA:mainfrom
BestJuly:lit/mtp_layer_standalone_main

Conversation

@BestJuly

@BestJuly BestJuly commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

What does this PR do ?

Support placing MTP to a standalone stage, e.g., MTP in the 2nd last VPP stage for better VPP balance.
This is the PR for main branch, and the corresponding PR1916 for dev branch.

Highlights

This document outlines the design of a new MTP (Multi-Token Prediction) standalone feature. This feature allows MTP layers to be placed into a standalone VPP (Virtual Pipeline Parallelism) stage, rather than being confined to the last VPP stage.

Key Benefits:

  • Enhanced Flexibility in MTP VPP Placement
    • MTP layers can now be positioned in either the last or second-to-last VPP stage, offering greater control over pipeline configuration.
  • Improved Load Balancing
    • The MTP standalone feature isolates transformer block-related computations, which have a similar computational cost to a normal transformer block. This split, with MTP loss calculation remaining in the second-to-last VPP stage, contributes to a more balanced workload.
  • Minimal Impact on Existing Code Paths
    • The design aims for near-zero impact on current code. A minor change involves the final layernorm layer, which will now be placed at the end of the decoder stage. In scenarios where the last stage contains only MTP layers and loss, this layernorm will reside in the second-to-last layer alongside a decoder layer. While this differs from previous implementations (where the final layernorm was always in the last stage), bitwise correctness has been validated under various conditions, including forceful layernorm placement and disabled gradient clipping.

Considerations:

  • Changes in P2P Communication Shapes
    • The need to pass original and MTP hidden states to subsequent VPP stages will alter p2p communication shapes. To mitigate the impact on core communication, the shape is communicated upfront when MTP standalone settings are detected in the pipeline layout.

Design idea

We split MTP computation into two parts

  • Output head computation.
  • Others.
image

The advantage is that we put the MTP loss computation as main, so for the untie embedding part, the MTP loss can use exactly the same code path of the lm loss as they are in the same vpp stage, which is simpler than putting MTP loss calculation in the same vpp stage as MR#2996 so that we can avoid bugs related to this. With the new design, the DSv3 TFLOPs will also be compatible with the #2996 one which included in the 0.13 EA branch.We divide MTP computation into two parts: output head computation and others. This approach offers the advantage of treating MTP loss computation as primary. Consequently, for the untied embedding, the MTP loss can utilize the exact same code path as the LM loss, as they are within the same VPP stage. This simplifies the process compared to placing MTP loss calculation in the same VPP stage as MR#2996, thereby mitigating potential bugs. Furthermore, this new design ensures DSv3 TFLOPs compatibility with the #2996 version included in the 0.13 EA branch.

Perf and results

Perf

  • The throughput in DSv3 is on par or even higher than 0.13 EA (where we use previous design for this standalone feature).

Convergence

On full DSv3
(Real training mode) Almost overlapped with the main branch settings for the metrics which is related to convergence.

Deterministic correctness check
We validate the correctness in mainly two parts (when using different pp layout, can be bitwise aligned when manually disabling grad clipping)

  • Deterministic alignment between main branch and this MR.
    • The same pipeline layout settings;
    • Different pipeline layout settings.
  • Deterministic alignment within this MR to validate the compatibility of 1F1B overlap.

Pre-checks

  • I want this PR in a versioned release and have added the appropriate Milestone (e.g., Core 0.8)
  • 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

The following process is enforced via the CODEOWNERS file for changes into megatron/core. For changes outside of megatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.

For MRs into `main` branch

(Step 1): Add PR label Expert Review

(Step 2): Collect the expert reviewers reviews

  1. Attach the Expert Review label when your PR is ready for review.
  2. GitHub auto-assigns expert reviewers based on your changes. They will get notified and pick up your PR soon.

⚠️ Only proceed to the next step once all reviewers have approved, merge-conflict are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

(Step 3): Final Review

  1. Add Final Review label
  2. GitHub auto-assigns final reviewers based on your changes. They will get notified and pick up your PR soon.

(Optional Step 4): Cherry-pick into release branch

If this PR also needs to be merged into core_r* release branches, after this PR has been merged, select Cherry-pick to open a new PR into the release branch.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

Merging your PR

Any member of core-adlr and core-nemo will be able to merge your PR.

shifangx and others added 14 commits November 4, 2025 18:17
move output layer outside mtp block

support mtp layer for --pipeline-model-parallel-layout

run test with mtp on the second last vpp stage, with the following error
message.

can run, but need to check lm loss and mtp loss with baseline.

fix some issue by refering to MR2996

Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
…apes later

Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
Signed-off-by: Li Tao <lit@nvidia.com>
@BestJuly
BestJuly requested review from a team as code owners November 5, 2025 02:22
@ko3n1g ko3n1g added this to the Core 0.16 milestone Nov 5, 2025
@BestJuly BestJuly added module: moe Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. labels Nov 5, 2025
@Phlip79

Phlip79 commented Dec 30, 2025

Copy link
Copy Markdown
Member

@BestJuly can you please fix conflicts?

@asolergi-nv asolergi-nv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take another look once conflicts are fixed & tests passing

Edit: Let's also update docs/user-guide/features/multi_token_prediction.md

), "All of the MTP layers must be in the same one virtual pipeline stage"
for vpp_rank in range(self.virtual_pipeline_model_parallel_size - 1):
assert LayerType.mtp not in self.layout[0][vpp_rank], (
f"Corrently we restrict that the MTP should not be in the first pp rank."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks for your review and I have modified the code according to your review comments. The UT should pass and I have checked the failed one which also passed with the CI container. I believe that would be a environment issue.

============================================================================= short test summary info =============================================================================
PASSED tests/unit_tests/data/test_builder.py::test_builder

May I have your further review then? Thank you.

Comment on lines +87 to +90

# print("========================")
# print("[DEBUG] mtp_block_spec is ", mtp_block_spec)
# exit()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# print("========================")
# print("[DEBUG] mtp_block_spec is ", mtp_block_spec)
# exit()

Comment on lines +397 to +399
# TODO(shifangx): Currently, we only support put all of MTP layers
# on the last pipeline stage, so the offset is always 0.
# We will support more flexible MTP placement in the future.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿?

"""Build pretraining datasets."""
if train_valid_test_num_samples is None:
train_valid_test_num_samples = get_train_valid_test_num_samples()
print_rank_0(' > datasets target sizes (minimum size):')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we drop this message?

Comment thread megatron/training/training.py Outdated
Comment on lines +2831 to +2834
if vp_stage is not None:
return build_train_valid_test_datasets_provider(train_valid_test_num_samples, vp_stage=vp_stage)
else:
return build_train_valid_test_datasets_provider(train_valid_test_num_samples)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this?

@Phlip79

Phlip79 commented Jan 6, 2026

Copy link
Copy Markdown
Member

/ok to test 50d34a3

@asolergi-nv

Copy link
Copy Markdown
Contributor

/ok to test d03c64c

@asolergi-nv asolergi-nv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Labels

complexity: high dev2main: mbridge dev to main: this PR is needed in main for mbridge Expert Review [deprecated] Apply this label to indicate that your PR is ready for expert review. Final Review PR is in the "final review" stage module: moe

Projects

None yet

Development

Successfully merging this pull request may close these issues.