Skip to content

[graph_trainer] Enable DeepSeek V4 in GraphTrainer - #5833

Draft
SherlockNoMad wants to merge 3 commits into
SherlockNoMad/gt_gpt_ossfrom
SherlockNoMad/gt_deepseek_v4
Draft

SherlockNoMad wants to merge 3 commits into
SherlockNoMad/gt_gpt_ossfrom
SherlockNoMad/gt_deepseek_v4

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

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 global
functools.cache. The first trace caches a FakeTensor, which leaks into every
later trace and eager call in the process.

Example.

# models/deepseek_v4/compressor.py on main
@cache
def _hadamard(dim, dtype, device): ...

make_fx(step)(...)  # caches a FakeTensor for (dim, dtype, device)
make_fx(step)(...)  # fails: fake-mode mismatch with the cached FakeTensor

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/: GraphTrainerDeepSeekV4Model and its
    build_model_config. MTP raises ValueError: the traced loss receives the
    stacked labels tensor rather than the per-depth labels tuple
    MTPCrossEntropyLoss needs.
  • models/deepseek_v4/compressor.py: _hadamard builds the matrix while
    tracing (torch.compiler.is_compiling() or an active proxy mode, since
    make_fx does not set is_compiling) and uses the cache otherwise.
  • The bitwise harness accepts attn_backend=None for models whose flavors
    choose their own attention.
  • Test recipe torchtitan_recipes/tests/graph_trainer/deepseek_v4.py; 4-GPU
    numerics configs; README row.

Numerics

Eager FSDP2 vs GraphTrainer, seed 42, deterministic,
scripts/loss_compare.py --assert-equal. Measured on base 0f3481537.
Rebasing onto d0ab95e46 changed only recipe field names in this diff
(#5776, #5779); re-verification on the new base is in progress.

Config GPUs Result
debugmodel, FSDP2 + TP2 + EP2, 20 steps 4x GB300 loss and grad_norm bitwise identical

The 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_optim
    run-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 table
    above.

Known issues / follow-ups

  • MTP raises ValueError (TODO in graph_trainer/deepseek_v4/model.py).
  • PP is not covered.
  • Next in the stack: Qwen3.5.

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 4, 2026
@SherlockNoMad
SherlockNoMad force-pushed the SherlockNoMad/gt_gpt_oss branch from 259b540 to dbe4475 Compare November 4, 2026 23:30

This branch has not been deployed

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

Labels

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.

1 participant