Repository navigation
[graph_trainer] Add a registry for extra graph passes selected by compile.extra_passes - #5827
Merged
Merged
Conversation
…pile.extra_passes Add an open extension point for model- or shape-specialized graph rewrites, such as replacing a subgraph with a handwritten kernel. - registry.py: `@register_graph_pass` registers a pass under its function name in `GRAPH_PASS_REGISTRY`. The registering module can live anywhere, including outside torchtitan; the recipe imports it. - configs.py: rename the unused `compile.passes` to `compile.extra_passes`, the ordered list of registered pass names to apply. - passes.py: `compile_time_passes` runs the selected passes after graph normalization and before memory policy tagging, which covers SPMD, SPMD gradient accumulation, GraphPP, and precompile. - trainer.py: unknown or repeated names raise at engine init. - precompile.py and test helpers: follow the rename.
SherlockNoMad
marked this pull request as ready for review
November 4, 2026 20:48
SherlockNoMad
requested review from
acisseJZhong,
aditvenk,
fegin,
felipemello1,
sanketpurandare,
shuhuayu,
tianyu-l and
wwwjn
as code owners
November 4, 2026 20:48
ydwu4
approved these changes
Nov 4, 2026
…h passes Move the registered passes from before memory policy tagging to the end of compile_time_passes: after memory policy, remat, collective bucketing/reordering, and async TP, right before the Inductor passes. GraphPP and SPMD gradient accumulation still run them once on the joint graph, before splitting it into per-action graphs, so a fusion can match a forward subgraph together with its backward.
aditvenk
approved these changes
Nov 4, 2026
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.
Summary (human-written)
Why
GraphTrainer has no way to add a graph pass without editing
passes.py. Model- or shape-specialized rewrites, such as replacing a traced subgraph with a handwritten kernel, are wanted only by some recipes and may live outside torchtitan, so they don't belong in the default pipeline or behind a global flag.compile.passesalready existed, but nothing read it except the precompile fingerprint.Example.
The selected passes run in list order on the joint graph after the other GraphTrainer passes, right before Inductor (pass log from the Llama 3 debug model):
What changed
registry.py:@register_graph_passandGRAPH_PASS_REGISTRY, keyed by function name, which is also the nameextra_passes,disable_passes, and the pass log use. Registering a different function under a taken name raises; re-importing the defining module does not.passes.py:resolve_registered_graph_passesraisesValueErrorfor unknown or repeated names.compile_time_passesappends the passes after memory policy, remat, collective bucketing/reordering, and async TP, right before the Inductor passes. SPMD, SPMD gradient accumulation, GraphPP, and precompile all build their pass lists there; GraphPP and SPMD gradient accumulation run them once on the joint graph before splitting it into per-action graphs, so a fusion can match a forward subgraph together with its backward.trainer.py:GraphTrainingEngine.__init__resolves the names, so a typo fails before the model is built instead of at the first trace.compile.passes->compile.extra_passes:configs.py, the precompile fingerprint key, and test helpers (_trainer_test_utils.py,test_bitwise_deterministic.py,test_precompile.py). Skimmable.AGENTS.md(the diff shows.claude/CLAUDE.md, whichAGENTS.mdlinks to).Alternatives considered
_get_extra_graph_passes, added in [GraphTrainer] Add memory policy registry and extra graph passes hook #3215 and later removed): needs a trainer subclass per recipe and can't be selected from config.compile.pass_pipeline/PASS_PIPELINE_REGISTRY: replaces the whole pipeline, so each extension would copy the default pass list.User-facing changes
compile.passes(read by nothing)compile.extra_passes: registered pass names, applied in orderpasses.py@register_graph_passin any module, enabled per recipePrecompile fingerprints change (key renamed), so existing precompile artifacts need regenerating.
Numerics
No change for the default empty
extra_passes: the pass list is identical onmainand this branch (12 passes, same order,graph_trainer_llama3_debugmodel, 8x H100).scripts/loss_compare.py main <branch> --assert-equal --no-seed-checkpoint(seed 42, deterministic, 20 steps) cannot show bitwise equality here, because this config is not bitwise reproducible run to run onmaineither: running the same commit twice matches at step 1, then drifts from step 2 on (~2e-6 at step 2, growing to ~1e-5), the same magnitude as main vs branch.test_bitwise_deterministic.pypasses.Testing
tests/test_registry.py(CPU): catches unknown or repeated names being silently skipped, selected passes running out of order, and two different passes registering under one name.2.16.0.dev20261102+cu130:test_passes.py+test_precompile.py+test_registry.py229 passed, 1 skipped;test_bitwise_deterministic.py16 passed, 9 skipped.annotate_flex_attention_for_regional_inductor_pass; on SPMD gradient accumulation it ran once, last in the joint-graph passes, and not in the per-action Inductor passes. Both trained normally.pre-commit runpasses on the changed files.Known issues / follow-ups
PASS_PIPELINE_REGISTRYstill has no registrants. If a custom pipeline were selected,extra_passeswould not apply. Candidate for removal in a separate PR.main:loss_compare.py's seed checkpoint fails for GraphTrainer ('GraphTrainingEngine' object has no attribute 'pp_schedule'), and the GraphTrainer Llama 3 debug config is not bitwise reproducible run to run underloss_compare.py.