Skip to content

[CI Fix][GraphTrainer] Stop the Flex bitwise tests from compiling flex per layer - #5547

Merged
wwwjn merged 2 commits into
pytorch:mainfrom
wwwjn:jianiw/issue5366-flex-recompile
Oct 19, 2026
Merged

wwwjn merged 2 commits into
pytorch:mainfrom
wwwjn:jianiw/issue5366-flex-recompile

Conversation

@wwwjn

@wwwjn wwwjn commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

TLDR: Two CI fixes for the GraphTrainer and 8 GPU Integration jobs.

  1. Fixes [CI] A10G Qwen3 MoE FlexAttention fails eager vs graph bitwise equivalence #5366. The Flex eager-vs-trace bitwise tests compiled flex once per layer in the eager reference, and after 8 graphs Dynamo silently ran unfused eager flex. The traced side was correct; the eager reference changed. With this PR the eager reference compiles flex once, like the core and RL trainers, and hitting the recompile limit raises instead of falling back.
  2. Fixes PP after [rl] Support pipeline parallelism in trainer #3980. [rl] Support pipeline parallelism in trainer #3980 made PP preprocessing return 4 values, but two callers still unpacked 3, so every PP + CUDA graph integration test and the GraphTrainer PP numerics test fail on main with ValueError: too many values to unpack (expected 3).

1. Flex bitwise tests compiled flex per layer (#5366)

Problem

  1. The eager reference was annotated. _run_steps called annotate_graph_trainer_model for both Trainer and GraphTrainer. It wraps every submodule's forward with annotate_fn({"module_fqn": fqn}). Only GraphTrainer's graph passes read these tags. The core and RL trainers never set them.
  2. Dynamo now guards on the annotation. [dynamo] Preserve fx.traceback.annotate across graph breaks (#198959) pytorch#198959 (October 10 nightly, the first failing one) adds an FX_ANNOTATION guard. Each layer's inner_attention has a different module_fqn, so the eager compiled flex compiles once per layer: 8 graphs for Qwen3 debugmodel_moe, all identical.
  3. The graphs carry over between tests. Dynamo caches them on flex_attention's code object, which outlives the torch.compile wrapper rebuilt in setUp. Llama3 Flex leaves 6. Qwen3 MoE Flex then passes the limit of 8 at its third layer.
  4. Silent fallback. Past the limit, Dynamo runs flex eagerly, which is the unfused implementation, so the eager loss changes: hit config.recompile_limit (8), then eager vs aot_fx_trace: loss mismatch.

Solution

  1. Annotate only GraphTrainer runs. The eager reference stays unannotated, like the core and RL trainers.
  2. fullgraph=True and recompile_limit=16 on the test's flex compile, the same settings as [attention] Flex attention: recompile limit 16, and fail instead of running eager #5442. Hitting the limit raises instead of falling back.
  3. torch._dynamo.reset() in setUp. Tests no longer share flex graphs.
  4. Unchanged: the GraphTrainer runs, their annotations, and every assertion.

2. PP callers still unpacked 3 values after #3980

Problem

  1. [rl] Support pipeline parallelism in trainer #3980 made TrainingEngine._preprocess_microbatch_group return (arg_mbs, kwarg_mbs, target_mbs, loss_kwarg_mbs) under PP.
  2. prepare_for_capture (PP + CUDA graphs, added by Share CUDA graph memory between training and validation #5241 before [rl] Support pipeline parallelism in trainer #3980 landed) still unpacked 3 values. Every PP test in the 8 GPU features job fails there: 3d, pp_1f1b, pp_dp_tp, pp_looped_zero_bubble, lora, and others. On main, that job passes at [rl] Support pipeline parallelism in trainer #3980's parent efdde5eda and fails at [rl] Support pipeline parallelism in trainer #3980 a4a62b349.
  3. GraphTrainer PP reuses the core preprocessing, and its forward-backward loop also unpacked 3 values, so test_dense_llama3_hsdp_graph_pp_vs_eager fails. The GraphTrainer job uses set -eux, so it stops there and never reaches test_bitwise_deterministic.py.
  4. The CPU test for prepare_for_capture still fed the old 3-tuple, which hid the mismatch.

Solution

  1. prepare_for_capture unpacks 4 values and passes the first microbatch's loss arguments plus global_loss_token_counts to schedule initialization. Initialization calls the loss on that microbatch, as the real step does.
  2. GraphTrainer PP drops the fourth element. GraphTrainer rejects per-microbatch loss arguments earlier, so it is always empty, and both paths now return (arg_mbs, kwarg_mbs, target_mbs), as before [rl] Support pipeline parallelism in trainer #3980.
  3. The CPU test feeds the PP layout with a per-microbatch loss argument and checks the loss arguments passed to initialization. It fails without the fix with the same ValueError.

Results

torch 2.16.0.dev20261019+cu132, GB200 (2 GPUs), flash-attn-4

1. Llama3 Flex -> Qwen3 MoE Flex test_aot_fx_trace_vs_eager, in one process
main:     "hit config.recompile_limit (8)", then
          eager vs aot_fx_trace: loss mismatch: 6.995096683502197 vs 6.995151519775391
this PR:  pass, 1 flex graph per test, eager == traced == 6.995151519775391

flex graphs after the Qwen3 MoE Flex eager run (test run alone)
annotated eager (main):  8 (one per layer)
unannotated eager:       1, same loss

Full test_bitwise_deterministic.py
main:     recompile limit hit once; on GB200 it breaks
          TestDSv3FlexAttnBitwiseDeterministic::test_aot_fx_trace_vs_eager and
          ::test_ep_chunk_matches_eager_chunking_bitwise
this PR:  16 passed (Varlen and Flex), 9 skipped, no recompile limit hit

2. PP
pp_1f1b integration test (2 GPUs, PP + CUDA graphs)
main:     ValueError in prepare_for_capture
this PR:  10 steps pass

GraphTrainer PP (the HSDP+PP numerics config reduced to PP=2, 2 GPUs)
main:     ValueError in GraphTrainer _forward_backward_body
this PR:  trains

You may ask: does the core trainer recompile flex per layer too? No. Llama3 1B (16 layers) with flex on the core Trainer compiles flex once and never recompiles. Only an annotated eager model recompiles per layer.

NOTE: GraphTrainer's validator runs the annotated model eagerly, so Flex validation on a model with more than 8 layers should hit the same per-layer recompiles. Not covered here.
NOTE: test_dense_llama3_hsdp_graph_pp_vs_eager needs 8 GPUs, so I could not run it locally. On the PP=2 reduction, eager PP vs GraphPP misses its grad_norm tolerance (max 1.7e-4) both with this PR and at #3980's parent, so that gap comes from the reduced config, not this PR. The full 8 GPU test passed on main before #3980.

Test plan

  • pytest torchtitan/experiments/graph_trainer/tests/test_bitwise_deterministic.py: 16 passed, 9 skipped.
  • The Llama3 Flex -> Qwen3 MoE Flex pair from [CI] A10G Qwen3 MoE FlexAttention fails eager vs graph bitwise equivalence #5366 passes in one process.
  • python -m tests.integration_tests.run_tests <out> --test_suite features --test_name pp_1f1b --ngpu 2: passes; fails on main.
  • pytest tests/unit_tests/cpu/test_trainer.py tests/unit_tests/cpu/distributed/test_cuda_graph.py tests/unit_tests/cpu/components/test_validator.py tests/unit_tests/cpu/distributed/test_pipeline_parallel.py: 135 passed.
  • CI: GraphTrainer 8 GPU and 8 GPU Integration (features) jobs.

…x per layer

Fixes pytorch#5366.

The eager Trainer reference was annotated with GraphTrainer's per-module
module_fqn tags. Since pytorch/pytorch#198959, Dynamo guards on the active
annotation, so eager compiled flex compiled once per layer, and the graphs
carried over between tests. Past recompile_limit (8), Dynamo ran unfused
eager flex and the eager loss changed.

- Annotate only GraphTrainer runs. The eager reference stays unannotated,
  like the core and RL trainers.
- Compile flex with fullgraph=True and recompile_limit=16 (same as pytorch#5442),
  so hitting the limit raises instead of silently falling back.
- Call torch._dynamo.reset() in setUp so tests don't share flex graphs.
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 19, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 19, 2026
pytorch#3980 made PP preprocessing return (arg_mbs, kwarg_mbs, target_mbs,
loss_kwarg_mbs), but two callers still unpacked three values:

- TrainingEngine's prepare_for_capture (PP + CUDA graphs, from pytorch#5241)
  raised "too many values to unpack" in every PP integration test. It now
  passes the first microbatch's loss arguments to schedule initialization,
  which calls the loss on that microbatch, as the real step does.
- GraphTrainer PP reuses the core preprocessing, so its forward-backward
  loop failed the same way. Per-microbatch loss arguments are rejected
  there, so the PP branch drops the empty fourth element.

The CPU test for prepare_for_capture still fed the old 3-tuple, which hid
the mismatch; it now uses the PP layout and checks the loss arguments.
@wwwjn
wwwjn requested review from fegin and wconstab as code owners October 19, 2026 22:25
@wwwjn
wwwjn merged commit 323cf2b into pytorch:main Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

[CI] A10G Qwen3 MoE FlexAttention fails eager vs graph bitwise equivalence

2 participants