Repository navigation
[CI Fix][GraphTrainer] Stop the Flex bitwise tests from compiling flex per layer - #5547
Merged
Merged
Conversation
…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.
wwwjn
requested review from
IvanKobzarev,
SherlockNoMad,
aditvenk,
sanketpurandare and
tianyu-l
as code owners
October 19, 2026 20:39
tianyu-l
approved these changes
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.
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.
TLDR: Two CI fixes for the GraphTrainer and 8 GPU Integration jobs.
ValueError: too many values to unpack (expected 3).1. Flex bitwise tests compiled flex per layer (#5366)
Problem
_run_stepscalledannotate_graph_trainer_modelfor bothTrainerandGraphTrainer. It wraps every submodule's forward withannotate_fn({"module_fqn": fqn}). Only GraphTrainer's graph passes read these tags. The core and RL trainers never set them.FX_ANNOTATIONguard. Each layer'sinner_attentionhas a differentmodule_fqn, so the eager compiled flex compiles once per layer: 8 graphs for Qwen3 debugmodel_moe, all identical.flex_attention's code object, which outlives thetorch.compilewrapper rebuilt insetUp. Llama3 Flex leaves 6. Qwen3 MoE Flex then passes the limit of 8 at its third layer.hit config.recompile_limit (8), theneager vs aot_fx_trace: loss mismatch.Solution
fullgraph=Trueandrecompile_limit=16on 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.torch._dynamo.reset()insetUp. Tests no longer share flex graphs.2. PP callers still unpacked 3 values after #3980
Problem
TrainingEngine._preprocess_microbatch_groupreturn(arg_mbs, kwarg_mbs, target_mbs, loss_kwarg_mbs)under PP.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 parentefdde5edaand fails at [rl] Support pipeline parallelism in trainer #3980a4a62b349.test_dense_llama3_hsdp_graph_pp_vs_eagerfails. The GraphTrainer job usesset -eux, so it stops there and never reachestest_bitwise_deterministic.py.prepare_for_capturestill fed the old 3-tuple, which hid the mismatch.Solution
prepare_for_captureunpacks 4 values and passes the first microbatch's loss arguments plusglobal_loss_token_countsto schedule initialization. Initialization calls the loss on that microbatch, as the real step does.(arg_mbs, kwarg_mbs, target_mbs), as before [rl] Support pipeline parallelism in trainer #3980.ValueError.Results
You may ask: does the core trainer recompile flex per layer too? No. Llama3 1B (16 layers) with flex on the core
Trainercompiles 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_eagerneeds 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.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.