Skip to content

feat(megatron): support MTP in split training - #3881

Open
yfw wants to merge 2 commits into
mainfrom
yifu/v2_mtp_training
Open

feat(megatron): support MTP in split training#3881
yfw wants to merge 2 commits into
mainfrom
yifu/v2_mtp_training

Conversation

@yfw

@yfw yfw commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

Ports the Megatron multi-token prediction (MTP) training lifecycle from #2801 into the SingleController split training path.

  • Derives the MTP loss mask from the existing token and sample masks before microbatch packing.
  • Leaves mtp_grad_scale_func unset so MCore inherits the optimizer's loss scale, including fp16 dynamic scaling.
  • Normalizes detached MTP auxiliary gradients by the global valid-token count independently of the main loss denominator.
  • Rejects nonzero attached-head MTP with a sequence-level main loss because that mixed backbone gradient cannot be corrected after split-step accumulation.
  • Collects the independently clipped MTP gradient norm plus per-layer loss and acceptance metrics after the optimizer step.
  • Propagates worker-reduced MTP metrics through TQPolicy to the training logger.

No TQ schema or new user configuration fields are required.

Issues

Follow-up to #2801.

Usage

Existing MTP-enabled Megatron recipes now use the same MTP training behavior when driven through the SingleController split API.

Before your PR is "Ready for review"

Pre checks:

  • Read and followed the contributor guidelines.
  • Added focused lifecycle and TQ propagation tests.
  • Ran the available focused unit tests and static checks locally.
  • No documentation update is needed because this restores parity for existing configuration.

Additional Information

Validation:

  • Focused unit tests: 10 passed, 1 skipped.
  • Ruff lint and formatting checks passed.
  • Repository pre-commit hooks, including Pyrefly, passed.
  • Python compilation and git diff checks passed.

The MCore-specific state-machine tests were skipped locally because Transformer Engine is unavailable on this host. Installing the full MCore extras is also blocked here by the missing infiniband/mlx5dv.h system header.

Add MTP loss masking and gradient scaling to the split Megatron train-step lifecycle. Collect MTP losses, acceptance rates, and gradient norms at finish, then propagate them through TQPolicy.

Signed-off-by: Yi-Fu Wu <yifu.wu@gmail.com>
@yfw
yfw requested review from a team as code owners August 27, 2026 19:52
@copy-pr-bot

copy-pr-bot Bot commented Aug 27, 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.

@yfw yfw left a comment

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.

The MTP lifecycle port looks sound. scale_gradients does reach the MTP parameters (it iterates buffers + expert_parallel_buffers and scales each whole buffer, with no MTP or expert exclusion), so the raw-accumulate-per-chunk then normalize-once-at-finish contract holds. The loss mask is load-bearing too — without it process_mtp_loss gets loss_mask=None and trains MTP over prompt and padding tokens.

Three inline comments: a latent fp16 issue in the gradient-scale hook, a test assertion that cannot currently fail, and one intent question about auxiliary-branch normalization that is pre-existing on this path.

Generated by Claude Code

Comment thread nemo_rl/models/policy/workers/megatron_policy_worker.py Outdated
Comment thread tests/unit/models/policy/test_megatron_split_state.py Outdated
Comment thread nemo_rl/models/policy/workers/megatron_policy_worker.py
Let MCore inherit the optimizer loss scale for MTP and normalize detached MTP gradients by valid tokens when the main loss uses a sequence denominator. Reject the nonzero attached-head combination that cannot be corrected after split-step accumulation.

Signed-off-by: Yi-Fu Wu <yifu.wu@gmail.com>
@yfw yfw added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Aug 28, 2026
@yfw

yfw commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 60c55c8

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

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant