Skip to content

[graph_trainer] Add a registry for extra graph passes selected by compile.extra_passes - #5827

Merged
SherlockNoMad merged 2 commits into
mainfrom
bahuang/graph_trainer_extra_passes
Nov 4, 2026
Merged

SherlockNoMad merged 2 commits into
mainfrom
bahuang/graph_trainer_extra_passes

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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.passes already existed, but nothing read it except the precompile fingerprint.

Example.

# my_pkg/fusions.py -- any module, in or outside torchtitan
from torchtitan.experiments.graph_trainer.registry import register_graph_pass

@register_graph_pass
def my_kernel_fusion_pass(gm, example_inputs):
    ...  # replace matched subgraphs with calls to a custom op
    return gm

# recipe
from my_pkg.fusions import my_kernel_fusion_pass
config.compile.extra_passes = [my_kernel_fusion_pass.__name__]

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):

   9. joint_transformer_block_bucketing_reordering_pass
  10. my_kernel_fusion_pass          <- compile.extra_passes
  11. annotate_flex_attention_for_regional_inductor_pass
  12. regional_inductor_pass

What changed

  • registry.py: @register_graph_pass and GRAPH_PASS_REGISTRY, keyed by function name, which is also the name extra_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_passes raises ValueError for unknown or repeated names. compile_time_passes appends 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.
  • Mechanical rename 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.
  • Docs: "Registered Graph Passes" in the GraphTrainer README and a third pass tier in AGENTS.md (the diff shows .claude/CLAUDE.md, which AGENTS.md links to).

Alternatives considered

  • Trainer subclass hook (_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.
  • Callables in the config instead of names: not serializable, and can't be hashed into the precompile fingerprint.

User-facing changes

Before After
compile.passes (read by nothing) compile.extra_passes: registered pass names, applied in order
Adding a pass means editing passes.py @register_graph_pass in any module, enabled per recipe

Precompile fingerprints change (key renamed), so existing precompile artifacts need regenerating.

Numerics

No change for the default empty extra_passes: the pass list is identical on main and 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 on main either: 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.

Comparison Step 1 Steps 2-20
main vs branch identical max abs diff 3.8e-5 (step 14)
branch vs branch (same commit, two runs) identical max abs diff 1.9e-5 (step 4)

test_bitwise_deterministic.py passes.

Testing

  • New 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.
  • 8x H100, torch 2.16.0.dev20261102+cu130: test_passes.py + test_precompile.py + test_registry.py 229 passed, 1 skipped; test_bitwise_deterministic.py 16 passed, 9 skipped.
  • End to end on the 1-GPU Llama 3 debug model, with a pass registered from a module outside the repo: on SPMD it ran right before 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 run passes on the changed files.

Known issues / follow-ups

  • No in-repo pass registers yet; the first consumer (a kernel-fusion pass) is a follow-up.
  • PASS_PIPELINE_REGISTRY still has no registrants. If a custom pipeline were selected, extra_passes would not apply. Candidate for removal in a separate PR.
  • Reproduce on 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 under loss_compare.py.

…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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 4, 2026
@SherlockNoMad
SherlockNoMad marked this pull request as ready for review November 4, 2026 20:48
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 4, 2026
@SherlockNoMad
SherlockNoMad requested a review from ydwu4 November 4, 2026 20:57
…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.
@SherlockNoMad
SherlockNoMad merged commit d0ab95e into main Nov 4, 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.

3 participants