Repository navigation
[graph_trainer] Enable DeepSeek V4 in GraphTrainer - #5833
Draft
SherlockNoMad wants to merge 3 commits into
Draft
SherlockNoMad wants to merge 3 commits into
SherlockNoMad wants to merge 3 commits into
Conversation
Add GraphTrainerKimiK3Model and its flavor builder so the Kimi K3 decoder (KDA + MLA attention, attention residuals, LatentMoE) trains through GraphTrainer's whole-step make_fx trace with SimpleFSDP, TP, and EP. - Kimi K3 gains a `debugmodel_text` flavor: the debug decoder without its vision encoder. GraphTrainer traces one static-shape step, while the vision encoder reads per-batch image grids on the host, so the GraphTrainer config rejects a vision encoder. CP and PP raise ValueError for now. - KDA passes LinearAttentionMetadata as a model kwarg, so GraphTrainer registers it as a pytree node. maybe_register_blockmask_pytree_node is renamed to maybe_register_attention_metadata_pytree_nodes (mechanical caller updates). - The GraphTrainer test recipe trains packed C4 text with AdamW and FP32 parameters. With BF16 parameters, SimpleFSDP rounds each loss chunk's lm_head gradient to BF16 before accumulating it, while FSDP2 accumulates chunks in the FP32 reduce dtype. Testing: - test_bitwise_deterministic.py::TestKimiK3BitwiseDeterministic: eager vs aot_fx_trace, trace vs precompile, and numerics_changing_optim run-to-run are bitwise identical in loss, model hash, and gradient hash (GB300). - test_numerics.py::test_moe_kimi_k3_aot_fx_trace_vs_eager: eager FSDP2 vs GraphTrainer with FSDP2+TP2+EP2 on 4 GPUs, loss and grad_norm bitwise identical for 20 steps.
Add GraphTrainerGptOssModel and its flavor builder so GPT-OSS (attention
sinks, sliding-window attention, biased grouped experts) trains through
GraphTrainer with SimpleFSDP, TP, and EP. CP raises ValueError until its
attention backends are validated under GraphTrainer.
The multi-GPU numerics comparison uses FlexAttention: on Blackwell, FA4's
Varlen backward is not deterministic for full causal attention (dQ differs
run to run even with deterministic=True; sliding-window layers are
deterministic), so an eager Varlen run does not reproduce itself.
Testing:
- test_bitwise_deterministic.py::TestGptOss{,FlexAttn}BitwiseDeterministic:
eager vs aot_fx_trace, trace vs precompile (Varlen), and
numerics_changing_optim run-to-run are bitwise identical in loss, model
hash, and gradient hash (GB300).
- test_numerics.py::test_moe_gpt_oss_aot_fx_trace_vs_eager: eager FSDP2 vs
GraphTrainer with FSDP2+TP2+EP2 on 4 GPUs, loss and grad_norm bitwise
identical for 20 steps.
- The Varlen GraphTrainer recipe trains 10 steps with FSDP2+TP2+EP2.
Add GraphTrainerDeepSeekV4Model and its flavor builder so DeepSeek V4 (hyper-connection branches, sliding-window/compressed/sparse attention, hash-routed MoE) trains through GraphTrainer with SimpleFSDP, TP, and EP. MTP raises ValueError: the traced loss receives the stacked labels tensor rather than the per-depth tuple MTPCrossEntropyLoss needs. DeepSeek V4's compressor cached its Hadamard matrix with a global functools.cache. The first trace cached a FakeTensor, which then leaked into every later trace (and would leak into eager calls), so a second trace in the same process failed with a fake-mode mismatch. The matrix is now built, not cached, while tracing; eager calls keep the cache. Values are unchanged. The bitwise harness accepts attn_backend=None for models whose flavors fix their own attention. Testing: - test_bitwise_deterministic.py::TestDSv4BitwiseDeterministic: eager vs aot_fx_trace, trace vs precompile, and numerics_changing_optim run-to-run are bitwise identical in loss, model hash, and gradient hash (GB300). - test_numerics.py::test_moe_dsv4_aot_fx_trace_vs_eager: eager FSDP2 vs GraphTrainer with FSDP2+TP2+EP2 on 4 GPUs, loss and grad_norm bitwise identical for 20 steps.
SherlockNoMad
force-pushed
the
SherlockNoMad/gt_gpt_oss
branch
from
November 4, 2026 23:30
259b540 to
dbe4475
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #5832.
Summary (human-written)
Why
DeepSeek V4 (hyper-connection branches, sliding-window/compressed/sparse
attention, hash-routed MoE) has no GraphTrainer model class. Tracing it also
exposes a bug on
main: the compressor caches its Hadamard matrix with a globalfunctools.cache. The first trace caches a FakeTensor, which leaks into everylater trace and eager call in the process.
Example.
After this PR, a trace builds the matrix in the graph and eager calls keep the
cache. The values are unchanged.
What changed
graph_trainer/deepseek_v4/:GraphTrainerDeepSeekV4Modeland itsbuild_model_config. MTP raisesValueError: the traced loss receives thestacked labels tensor rather than the per-depth labels tuple
MTPCrossEntropyLossneeds.models/deepseek_v4/compressor.py:_hadamardbuilds the matrix whiletracing (
torch.compiler.is_compiling()or an active proxy mode, sincemake_fx does not set
is_compiling) and uses the cache otherwise.attn_backend=Nonefor models whose flavorschoose their own attention.
torchtitan_recipes/tests/graph_trainer/deepseek_v4.py; 4-GPUnumerics configs; README row.
Numerics
Eager FSDP2 vs GraphTrainer, seed 42, deterministic,
scripts/loss_compare.py --assert-equal. Measured on base0f3481537.Rebasing onto
d0ab95e46changed only recipe field names in this diff(#5776, #5779); re-verification on the new base is in progress.
debugmodel, FSDP2 + TP2 + EP2, 20 stepsThe compressor change does not alter eager numerics: eager calls still use the
cached matrix, and the traced construction computes the same values.
Testing
test_bitwise_deterministic.py::TestDSv4BitwiseDeterministic(1 GPU):eager vs aot_fx_trace, trace vs precompile, and
numerics_changing_optimrun-to-run produce bitwise identical loss, model hash, and gradient hash.
test_numerics.py::test_moe_dsv4_aot_fx_trace_vs_eager(4 GPUs): the tableabove.
Known issues / follow-ups
ValueError(TODO ingraph_trainer/deepseek_v4/model.py).