Skip to content

[graph_trainer] Enable Qwen3.5 in GraphTrainer - #5834

Draft
SherlockNoMad wants to merge 1 commit into
SherlockNoMad/gt_deepseek_v4from
SherlockNoMad/gt_qwen3_5
Draft

SherlockNoMad wants to merge 1 commit into
SherlockNoMad/gt_deepseek_v4from
SherlockNoMad/gt_qwen3_5

Conversation

@SherlockNoMad

Copy link
Copy Markdown
Contributor

Stacked on #5833.

Summary (human-written)

Why

Qwen3.5 (Gated DeltaNet and gated full-attention layers, MRoPE, MoE with a
shared expert) has no GraphTrainer model class. Enabling it also exposed a
silent failure for any linear-attention model in GraphTrainer. Without
dataloader.max_num_documents, the varlen cu_seqlens length depends on each
batch's document count, and the trace bakes the first batch's GDN chunk
capacity into the packed kernels. A later batch with more documents overruns
that capacity and reads uninitialized memory.

Example.

config.dataloader.max_num_documents = None
# Before: trains, then grads go NaN from step 2 in about a third of FSDP runs
#         (every run with memory_policy="none").
# After:  ValueError: GraphTrainer bakes the traced step's document count into
#         linear attention's chunk metadata; set dataloader.max_num_documents ...

What changed

  • graph_trainer/qwen3_5/: GraphTrainerQwen35Model and its
    build_model_config. Qwen3.6 and Qwen3.8 reuse Qwen35Model, so their
    flavors convert to the same wrapper. A vision encoder or CP raises
    ValueError: the vision encoder builds per-batch shapes from host-side image
    grids, and the GDN CP metadata (ContextParallelLinearAttentionMetadata and
    its Attention Gym routing) is not a traceable pytree yet.
  • graph_trainer/trainer.py: GraphTrainingEngine requires
    dataloader.max_num_documents when the model has linear attention.
  • models/qwen3_5/flavors.py: new text-only debugmodel_moe_text flavor.
    qwen3_6 and qwen3_8 bind with_vision=True for their existing
    debugmodel_moe (mechanical).
  • Bitwise harness: a param_dtype for single-GPU runs, which skip FSDP's
    mixed-precision cast (fused chunk GDN takes BF16 Q/K/V only).
    tests/utils.py hashes BF16 tensors through an int16 view because NumPy has
    no bfloat16.
  • Test recipe torchtitan_recipes/tests/graph_trainer/qwen3_5.py (fixed
    max_num_documents); 4-GPU numerics configs; README row and note.

User-facing changes

Before After
GraphTrainer with a linear-attention model and max_num_documents=None trains and can silently produce NaN grads ValueError at engine construction

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_moe_text, FSDP2 + TP2 + EP2, 20 steps 4x GB300 loss and grad_norm bitwise identical
debugmodel_moe_text, PP2 Interleaved1F1B, 10 steps 2x GB300 loss and grad_norm bitwise identical

Testing

  • test_bitwise_deterministic.py::TestQwen35MoEBitwiseDeterministic (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_qwen35_aot_fx_trace_vs_eager (4 GPUs): the
    FSDP2 + TP2 + EP2 row above.
  • The PP row was run locally; it is not a CI test.

Known issues / follow-ups

  • CP raises ValueError (TODO in graph_trainer/qwen3_5/model.py).
  • Next in the stack: Kimi K2.7.

Add GraphTrainerQwen35Model and its flavor builder so Qwen3.5 (Gated
DeltaNet and gated full-attention layers, MRoPE, MoE with a shared expert)
trains through GraphTrainer with SimpleFSDP, TP, EP, and PP. Qwen3.6 and
Qwen3.8 reuse Qwen35Model, so to_graph_trainer_config converts their flavors
to the same wrapper. A vision encoder or CP raises ValueError: the vision
encoder builds per-batch shapes from host-side image grids, and the GDN CP
metadata (ContextParallelLinearAttentionMetadata and its Attention Gym
routing) is not a traceable pytree yet.

Add a text-only debugmodel_moe_text flavor (the debug MoE decoder without
its vision encoder); qwen3_6 and qwen3_8 bind with_vision=True for their
existing debugmodel_moe.

GraphTrainer now requires dataloader.max_num_documents for linear-attention
models. Without it, the varlen cu_seqlens length depends on each batch's
document count, and the trace bakes the first batch's GDN chunk capacity
into the packed kernels. A later batch with more documents overruns that
capacity and reads uninitialized memory: on FSDP with deterministic
algorithms (the fused GDN path), grads became NaN from step 2 in about a
third of runs, and in every run with memory_policy="none".

Fused chunk GDN takes BF16 Q/K/V only, so the bitwise harness gains a
param_dtype for single-GPU runs that skip FSDP's mixed-precision cast, and
hash_model hashes BF16 tensors through an int16 view because NumPy has no
bfloat16.

Testing:
- test_bitwise_deterministic.py::TestQwen35MoEBitwiseDeterministic: 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). The
  full file passes (30 passed, 10 skipped).
- test_numerics.py::test_moe_qwen35_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.

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

ciflow/rl 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