Repository navigation
[bug] dsv3 double a double rms-norm with MTP>1 - #6408
Conversation
|
@claude review |
There was a problem hiding this comment.
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+cpintests/integration_tests/models.py:663-675) build ondeepseek_v3_debugmodel_mtp, which hardcodesnum_mtp_layers = 1. Neither has a golden file. Changing this to2makes the TP+CP+SP test (spmd typechecking on) cover chaining between depths, including theS(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, updateMTP.md:13("It setsnum_mtp_layers=1").
Questions
- Numerics says "TODO: loss comparison". Can you attach a short
num_mtp_layers=2loss/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.forwardareDeepSeekV3Model.forwardand the fake_AddMTPBlockintests/unit_tests/cpu/components/test_loss.py, which is updated.torchtitan/experiments/graph_trainer/deepseek_v3/model.pyreusesDeepSeekV3Modelwithout overriding the MTP forward. No other model usesDeepSeekV3MTPBlock. - 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'shnormin 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
forwardloop. Expect a small rebase conflict; the semantics don't overlap. test_lora_head_only_matches_cross_entropyfailing 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.
Summary (human-written)
DeepSeek V3 applies double RMS normalization between MTP layers:
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 thenormed output of the previous depth (
mtp_norm, i.e.shared_head.norm),and immediately normalizes it again with its own
hnorm. Two RMSNorms run backto back on the hidden path. The reference (DeepSeek-V3 paper; vLLM
deepseek_mtp.py, which appliesshared_headonly when computing logits)chains the pre-norm hidden state. The main decoder here already does this
(
prev_depth_hidden = his taken before the final norm), and so doesDeepSeekV4MTPBlock.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, eventhough
DeepSeekV3MTPBlock.forwardsays it arrivesShard(0).Example. (depth 2 of a 2-layer MTP)
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.forwardreturns(h, gathered mtp_norm(h))instead ofonly the normed output, matching
DeepSeekV4MTPBlock.DeepSeekV3Model.forwardchains the first value to the next depth and sendsthe second to
lm_head.Numerics
num_mtp_layers=1(all released DSV3 checkpoints): unchanged. The depth-1computation path is not touched.
num_mtp_layers>=2: depth 2 and later change on purpose, because the doublenorm is removed. TODO: loss comparison.
Testing
hnorminput isbit-identical to depth 1's pre-
mtp_normhidden state.tests/unit_tests/gpu/models/deepseek_v3/test_mtp_remat.py(2 MTP layers,full and default remat): passes.
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_entropyfails 3 subtests both on this branch and on its parent commit, so the failure
is unrelated.