Skip to content

[bug] dsv3 double a double rms-norm with MTP>1 - #6408

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
francesco-bertolotti:f14-fix-double-mtp-norm
Nov 26, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
francesco-bertolotti:f14-fix-double-mtp-norm

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

Summary (human-written)

DeepSeek V3 applies double RMS normalization between MTP layers:

  • The first MTP layer returns the RMS-normalized hidden state, which is fed to the MTP head.
  • The second MTP layer receives only the previously RMS-normalized hidden state and applies another RMS normalization to it.

DeepSeek V4 handles this correctly. This PR mirrors the V4 implementation.

Why

With num_mtp_layers >= 2, each DeepSeek-V3 MTP depth after the first gets the
normed output of the previous depth (mtp_norm, i.e. shared_head.norm),
and immediately normalizes it again with its own hnorm. Two RMSNorms run back
to back on the hidden path. The reference (DeepSeek-V3 paper; vLLM
deepseek_mtp.py, which applies shared_head only when computing logits)
chains the pre-norm hidden state. The main decoder here already does this
(prev_depth_hidden = h is taken before the final norm), and so does
DeepSeekV4MTPBlock.

The chained tensor has also already been TP-gathered by maybe_gather_tp_input.
Under SP, the next depth therefore receives a replicated prev_embed, even
though DeepSeekV3MTPBlock.forward says it arrives Shard(0).

Example. (depth 2 of a 2-layer MTP)

main:   h1 -> mtp_norm -> gather -> hnorm -> eh_proj -> ...   (double norm)
                  \-> lm_head
after:  h1 -> hnorm -> eh_proj -> ...
         \-> mtp_norm -> gather -> lm_head

The bug has been there since MTP was added (#3392). Depth 1 is unaffected, and
so are released checkpoints, which have a single MTP layer.

What changed

  • DeepSeekV3MTPBlock.forward returns (h, gathered mtp_norm(h)) instead of
    only the normed output, matching DeepSeekV4MTPBlock.
  • DeepSeekV3Model.forward chains the first value to the next depth and sends
    the second to lm_head.

Numerics

  • num_mtp_layers=1 (all released DSV3 checkpoints): unchanged. The depth-1
    computation path is not touched.
  • num_mtp_layers>=2: depth 2 and later change on purpose, because the double
    norm is removed. TODO: loss comparison.

Testing

  • GPU check, debugmodel with 2 MTP layers: depth 2's hnorm input is
    bit-identical to depth 1's pre-mtp_norm hidden state.
  • tests/unit_tests/gpu/models/deepseek_v3/test_mtp_remat.py (2 MTP layers,
    full and default remat): passes.
  • CPU: tests/unit_tests/cpu/models/deepseek_v3/,
    tests/unit_tests/cpu/models/common/test_transformer_block.py: pass.

Known issues / follow-ups

  • tests/unit_tests/cpu/components/test_loss.py::TestChunkedLossWrapper::test_lora_head_only_matches_cross_entropy
    fails 3 subtests both on this branch and on its parent commit, so the failure
    is unrelated.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 26, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 26, 2026
@francesco-bertolotti francesco-bertolotti changed the title [dsv3] MTP: chain the pre-norm hidden state to the next MTP depth [bug] dsv3 double a double rms-norm with MTP>1 Nov 26, 2026
@tianyu-l

Copy link
Copy Markdown
Contributor

@claude review

@claude

claude Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 2m 20s —— View job


Approve -- review posted.

@claude claude Bot 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.

Verdict: Approve -- a minimal, correct fix (+9/-6) that chains the pre-norm hidden state between MTP depths. It matches the main decoder (prev_depth_hidden = h before _pre_lm_head_norm_and_gather) and DeepSeekV4MTPBlock (torchtitan/models/deepseek_v4/mtp.py:86 returns (next_hc_hidden, prediction_hidden)).

What this PR does. DeepSeekV3MTPBlock.forward now returns (h, gathered mtp_norm(h)) instead of only the second value. DeepSeekV3Model.forward passes the first value to the next depth and sends the second to lm_head. With one MTP layer, which covers every released checkpoint and every in-repo MTP recipe, nothing changes: the extra returned h has no consumer.

Example. Depth 2 of num_mtp_layers=2, TP=2 with SP:

main:  depth1 h (S(0)) -> mtp_norm -> tp_gather (S(0)->R) -> depth2.hnorm -> eh_proj ...
       two RMSNorms in a row, and depth 2 gets R where line 70 says it gets S(0)
PR:    depth1 h (S(0)) -> depth2.hnorm -> eh_proj ...
       depth1 h        -> mtp_norm -> tp_gather -> lm_head

TP without SP has the same kind of mismatch: maybe_gather_tp_input is I -> R (torchtitan/models/common/linear/linear.py:285), and its backward all-reduces. So on main, depth 2's gradient into depth 1 went through an extra all-reduce. The PR removes that path as well. The description could say so, since TP gradients at depth >= 2 change too, not only the forward.

Should consider

  • torchtitan_recipes/tests/models/deepseek_v3.py:109 -- CI never runs the changed path. Both MTP integration tests (deepseek_v3_mtp_fsdp+ep, deepseek_v3_mtp_tp+cp in tests/integration_tests/models.py:663-675) build on deepseek_v3_debugmodel_mtp, which hardcodes num_mtp_layers = 1. Neither has a golden file. Changing this to 2 makes the TP+CP+SP test (spmd typechecking on) cover chaining between depths, including the S(0) layout this PR fixes. The cost is one more debug-size block. It is the cheapest guard against this bug coming back. If you change it, update MTP.md:13 ("It sets num_mtp_layers=1").

Questions

  • Numerics says "TODO: loss comparison". Can you attach a short num_mtp_layers=2 loss/grad_norm comparison (main vs PR, same seed, deterministic) before merge? It should show depth-1 loss bit-identical and the depth-2 MTP loss moving. It doesn't block, given the mechanism argument above.
Details
  • Callsites: the only producer and consumer of DeepSeekV3MTPBlock.forward are DeepSeekV3Model.forward and the fake _AddMTPBlock in tests/unit_tests/cpu/components/test_loss.py, which is updated. torchtitan/experiments/graph_trainer/deepseek_v3/model.py reuses DeepSeekV3Model without overriding the MTP forward. No other model uses DeepSeekV3MTPBlock.
  • Remat: tuple returns from MTP blocks already work for DSV4, and the GPU remat test with 2 MTP layers passes (per the description).
  • Memory: at depth >= 2, the pre-norm h (sequence-sharded under SP) is now kept for the next depth's hnorm in place of the gathered normed tensor. The difference is negligible, and there is no change at depth 1.
  • Overlapping PRs (searched "mtp", "deepseek_v3 mtp.py"): #5329 ("MTP1 eager pipeline parallelism") and #4488 ("Support MTP+PP") edit the same forward loop. Expect a small rebase conflict; the semantics don't overlap.
  • test_lora_head_only_matches_cross_entropy failing on the parent commit too is unrelated to this diff.

Not verified: GPU numerics (depth-2 hnorm input parity, TP gradient change) -- based on reading the code only.

A maintainer still needs to review.

@tianyu-l
tianyu-l merged commit f432f8c into pytorch:main Nov 26, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants