Skip to content

[graph_trainer] Enable Kimi K2.7 in GraphTrainer - #5835

Draft
SherlockNoMad wants to merge 1 commit into
SherlockNoMad/gt_qwen3_5from
SherlockNoMad/gt_kimi_k2_7
Draft

SherlockNoMad wants to merge 1 commit into
SherlockNoMad/gt_qwen3_5from
SherlockNoMad/gt_kimi_k2_7

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

Stacked on #5834.

Summary (human-written)

Why

Kimi K2.7 (MLA flex attention with YaRN RoPE, MoE with shared experts) has no
GraphTrainer model class. Separately, every single-GPU Kimi K2.7 run with QK
clipping fails at trainer construction on main, because
register_qk_clip_hook reads the "loss" mesh with get_mesh, which raises
when neither data nor context parallelism is enabled.

Example.

# main, 1 GPU, any Kimi K2.7 recipe with QK clipping
register_qk_clip_hook(...)
# -> ValueError: Mesh 'loss' is not available. Ensure the corresponding
#    parallelism axes are active.

# After: the mesh is optional; without one, the local maxima are already global.
reduction_mesh = parallelism_context.get_optional_mesh("loss")

What changed

  • graph_trainer/kimi_k2_7/: GraphTrainerKimiK25Model, its
    build_model_config, and without_qk_clip. A vision encoder, QK clipping,
    or CP raises ValueError. QK clipping appends each step's attention logits
    to a Python list that the optimizer hook reads, and a traced step does not
    replay the append, so the GraphTrainer builder uses plain flex attention.
    GraphTrainerModel.parallelize takes precedence over KimiK25Model's, so
    the wrapper repeats its CP check.
  • models/kimi_k2_7/flavors.py: new text-only debugmodel_text flavor.
  • models/kimi_k2_7/qk_clip.py: get_optional_mesh("loss"); qk_clip skips
    the max all-reduce without a mesh. test_qk_clip.py mocks the new call.
  • Test recipe torchtitan_recipes/tests/graph_trainer/kimi_k2_7.py. It
    returns a plain Trainer.Config because it trains with AdamW: the Kimi
    trainer config's DistMuon-only validation rejects TP whenever the config is
    re-validated.
  • numerics.py: the Kimi K3 numerics helper is shared as _kimi_numerics;
    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_text, FSDP2 + TP2 + EP2, 20 steps 4x GB300 loss and grad_norm bitwise identical
debugmodel_text, PP2 Interleaved1F1B, 10 steps 2x GB300 loss and grad_norm bitwise identical

Testing

  • test_bitwise_deterministic.py::TestKimiK27BitwiseDeterministic (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. It
    runs on 1 GPU, so it also covers the "loss" mesh fix.
  • test_numerics.py::test_moe_kimi_k2_7_aot_fx_trace_vs_eager (4 GPUs): the
    FSDP2 + TP2 + EP2 row above.
  • tests/unit_tests/gpu/models/kimi_k2_7/test_qk_clip.py passes.
  • The PP row was run locally; it is not a CI test.

Known issues / follow-ups

  • QK clipping and CP raise ValueError (TODOs in
    graph_trainer/kimi_k2_7/).
  • Next in the stack: Muse Glimmer numerics coverage.

Add GraphTrainerKimiK25Model and its flavor builder so Kimi K2.7 (MLA flex
attention with YaRN RoPE, MoE with shared experts) trains through
GraphTrainer with SimpleFSDP, TP, EP, and PP. A vision encoder, QK clipping,
or CP raises ValueError. QK clipping appends each step's attention logits to
a Python list that the optimizer hook reads, and a traced step does not
replay the append, so the GraphTrainer builder (without_qk_clip) uses plain
flex attention. GraphTrainerModel.parallelize takes precedence over
KimiK25Model's, so the wrapper repeats its CP check.

Add a text-only debugmodel_text flavor (the debug decoder without its
vision encoder).

register_qk_clip_hook read the "loss" mesh with get_mesh, which raises
without data or context parallelism, so every single-GPU Kimi K2.7 run
failed at trainer construction. It now uses get_optional_mesh, and qk_clip
skips the max all-reduce when there is no mesh.

The test recipe returns a plain Trainer.Config because it trains with AdamW:
the Kimi trainer config's DistMuon-only validation rejects TP whenever the
config is re-validated. The Kimi K3 numerics helper is shared as
_kimi_numerics.

Testing:
- test_bitwise_deterministic.py::TestKimiK27BitwiseDeterministic: 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_k2_7_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.
- Local: Interleaved1F1B PP2 eager vs GraphTrainer on 2 GPUs, loss and
  grad_norm bitwise identical for 10 steps.
- tests/unit_tests/gpu/models/kimi_k2_7/test_qk_clip.py passes.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 4, 2026
SherlockNoMad added a commit that referenced this pull request Nov 9, 2026
Stacked on #5835.

## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->

## Why
GraphTrainer already runs Muse Glimmer in its integration tests, but
nothing
checks its numerics against eager, so a GraphTrainer change could
silently
alter Muse Glimmer's loss. The vision flavor (`debugmodel_mm`) also
fails deep
in tracing with a data-dependent-shape error instead of a clear message.

**Example.**
```python
to_graph_trainer_config(muse_glimmer_debugmodel_mm(), GraphTrainerMuseGlimmerModel.Config)
# Before: fails during make_fx with a data-dependent-shape error
# After:  ValueError at config time: GraphTrainer MuseGlimmer supports
#         text-only flavors; got a vision encoder.
```

## What changed
- `graph_trainer/muse_glimmer/model.py`: the config rejects a vision
encoder
with `ValueError`, like Kimi K3, Kimi K2.7, and Qwen3.5 earlier in this
stack.
- Bitwise harness classes for Varlen and FlexAttention; a FlexAttention
test
  recipe; 4-GPU numerics configs (FSDP2 + TP2); 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`, FlexAttention, FSDP2 + TP2, 20 steps | 4x GB300 | loss
and grad_norm bitwise identical |
| `debugmodel`, PP2 Interleaved1F1B, 10 steps | 2x GB300 | loss and
grad_norm bitwise identical |

The 4-GPU comparison uses FlexAttention. FA4's Varlen backward is not
deterministic for full causal attention on Blackwell, so two Varlen
eager runs
with FSDP2 + TP2 already differ in grad_norm at step 1.

## Testing
- `test_bitwise_deterministic.py::TestMuseGlimmerBitwiseDeterministic`
and
  `TestMuseGlimmerFlexAttnBitwiseDeterministic` (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_dense_muse_glimmer_aot_fx_trace_vs_eager` (4
GPUs):
  the FSDP2 + TP2 row above.
- The PP row was run locally; it is not a CI test.

## Known issues / follow-ups
- CP is not covered.
- Next in the stack: Muse Spark (in progress).

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