Skip to content

Decide decomposition reruns from the graph, with yes/no pass declarations. - #5066

Merged
5had3z merged 7 commits into
one-shot-decomp-in-tracefrom
one-shot-decomp-pass-declarations
Nov 25, 2026
Merged

5had3z merged 7 commits into
one-shot-decomp-in-tracefrom
one-shot-decomp-pass-declarations

Conversation

@5had3z

@5had3z 5had3z commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Proposed change to #4596, which applies the Torch-TRT decomposition table during dynamo.trace so that compile can skip run_decompositions. This PR fixes two problems with that design and replaces its settings fingerprint with a check of the graph itself.

Pre-export passes silently did nothing

The passes in pre_lowering_pass_list run after trace, so with one-shot decomposition they saw an already decomposed graph:

  • replace_interleave_stack_with_cat found no aten.stack to rewrite.
  • preserve_attention_output_layout found no scaled_dot_product_attention under decompose_attention=True.

Both silently became no-ops.

Each pre-export pass now declares whether, under the given settings, it needs the graph before Torch-TRT decompositions:

@runs_on_decomposed_graph
def remove_detach(gm, settings): ...

@needs_undecomposed_graph(when=lambda settings: settings.replace_interleave_stack_with_cat)
def replace_interleave_stack_with_cat(gm, settings): ...

If any pass needs it, trace falls back to a stock torch.export, and compile decomposes after the passes have run, exactly as before #4596. This replaces autocast's special cases in trace, MutableTorchTensorRTModule and maybe_run_decompositions. The three flags that need the undecomposed graph are replace_interleave_stack_with_cat, decompose_attention and enable_autocast. All default to False, so the default path keeps one-shot.

An undecorated pass counts as needing the undecomposed graph and logs a warning. Forgetting the decorator therefore costs a second trace rather than a silent no-op. A test also fails CI for any pass in ATEN_PRE_LOWERING_PASSES without a declaration.

compile decides from the graph, not a settings fingerprint

maybe_run_decompositions skips run_decompositions only when both of these hold:

  1. The program is post-dispatch ATen IR. Stock torch.export returns training IR, which can hold in-place ops outside the table, such as mul_. Those still need run_decompositions to functionalize them.
  2. No op in the graph is one that compile's table, filtered for this graph, would rewrite, other than ops a table has already tried.

The one-shot export, and each rerun, records which ops of the table it applied. An op that a table tried and left in place does not count as pending. For example, _fused_rms_norm on CUDA has a real kernel, so its composite-implicit decomposition never applies. Without the record, every CUDA model with RMSNorm would retrace on every compile.

This removes the stamp, the fingerprint and its version. It also covers cases the fingerprint missed: a graph edited after trace, and changes to the table's contents. A mismatch between trace and compile settings that adds table entries is still caught, because the newly covered ops are still in the graph. The check costs about 0.2 ms on a 4k-node graph.

Dynamic scatter_add

scatter_add's decomposition unrolls the index extent, so applying it during export failed with a constraint violation when that extent was symbolic. The decomposition now returns NotImplemented on a symbolic extent, so the op stays for partitioning to fall back to Torch. A static scatter_add is decomposed during export. Neither case reruns in compile.

One behaviour change for graphs that mix static and dynamic scatter_add: filter_decomposition_table used to keep all of them; the static ones are now decomposed.

API

export_with_tensorrt_decomps and maybe_run_decompositions now take CompilationSettings. trace and MutableTorchTensorRTModule build it from their keyword arguments with settings_from_kwargs.

Performance

Old: stock export, then run_decompositions, as on main. New: the one-shot export, after which run_decompositions is skipped. Each figure is the median of 3 runs, each in a fresh process.

Model export run_decompositions total
Flux.1-dev transformer (19 + 38 blocks) 3.19 s → 5.06 s 4.00 s → 0 s 7.34 s → 5.07 s (−2.26 s)
57-block DiT-style transformer, CUDA bf16 1.72 s → 2.66 s 2.44 s → 0 s 4.19 s → 2.67 s (−1.51 s)

The one-shot export is slower because it runs the decomposition functions inside the first trace. The saving is the second trace's fixed cost: fake-tensor conversion, functionalization and code generation for the whole program. Engine build time and inference latency are unchanged. On an 8-block model, a full compile showed only the skipped run_decompositions differing between the two flows, and the same inference latency within noise.

The decomposed graphs are identical between the two flows (5,830 ops on Flux.1-dev). After post_lowering, a smaller CUDA model also matched in op order, node names, graph signature and outputs. The only difference is metadata: one-shot nodes lack original_aten and seq_nr, which only the second trace adds. Nothing in Torch-TensorRT reads them.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

Tests: tests/py/dynamo/models/test_one_shot_decomp.py covers the declarations, the guard, training IR, _fused_rms_norm on CUDA, settings mismatches, and static and dynamic scatter_add. In the dynamo-lowering suite and the autocast and serde tests, the only failures also fail on main: the SDPA shared_bool_mask tests, 7 autocast tests, and test_save_load_ts / test_save_load_aoti. cudnn_grid_sampler also fails, inside stock torch.export before any Torch-TRT code runs, but it hasn't been checked on main.

🤖 Generated with Claude Code

dynamo.trace decomposes during export, so the passes in
pre_lowering_pass_list, which run afterwards, saw a decomposed graph:
replace_interleave_stack_with_cat found no aten.stack to rewrite, and
preserve_attention_output_layout found no scaled_dot_product_attention
with decompose_attention=True. Both silently did nothing.

Each pre-export pass now declares, for given settings, the ops it must
see undecomposed (needs_undecomposed_ops / needs_no_undecomposed_ops),
or ALL_OPS when it needs the graph as torch.export makes it. trace
defers those ops, together with SHAPE_DEPENDENT_DECOMPOSITIONS, and
compile decomposes them after the passes run. Autocast declares ALL_OPS,
which replaces its special cases in trace, MutableTorchTensorRTModule
and maybe_run_decompositions.

An undeclared pass counts as ALL_OPS and logs a warning, so forgetting
a declaration costs a second trace rather than a no-op pass, and a test
fails CI for any pass in ATEN_PRE_LOWERING_PASSES without one.
maybe_run_decompositions warns when trace's settings decomposed ops that
the compile settings need passes to see.

export_with_tensorrt_decomps and maybe_run_decompositions now take
CompilationSettings; trace and MutableTorchTensorRTModule build it from
their keyword arguments with settings_from_kwargs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@5had3z
5had3z requested a review from micwill755 November 17, 2026 18:14
@meta-cla meta-cla Bot added the cla signed label Nov 17, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 17, 2026
@github-actions
github-actions Bot requested a review from zewenli98 November 17, 2026 18:15
Signed-off-by: Bryce Ferenczi <[email protected]>
@narendasan

narendasan commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

So this is to allow post ATen lowering to dynamically insert exceptions to the default decomposition set?

Do we need both the @needs_no_ and the @needs_? can we assume if its not decorated it doesn't need anything?

@5had3z

5had3z commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

So this is to allow post ATen lowering to dynamically insert exceptions to the default decomposition set?

Do we need both the @needs_no_ and the @needs_? can we assume if its not decorated it doesn't need anything?

These are for the pre-export lowering passes (pre_lowering_pass_list), not post-lowering. This PR targets #4596, which applies the Torch-TRT decomposition table during dynamo.trace. As a result, the table has already run by the time the pre-export passes see the graph. Some of those passes need undecomposed ops under certain settings:

  • replace_interleave_stack_with_cat matches aten.stack.
  • preserve_attention_output_layout matches SDPA when decompose_attention is set.
  • rule_based_autocast needs the graph as stock torch.export produces it.

On #4596 alone, the first two silently become no-ops. Each pass now declares the ops it needs for the given settings. trace leaves those out of the table, and compile decomposes them after the passes run. A pass that needs the whole graph undecomposed, like autocast, makes trace fall back to a stock export.

On needing both decorators: if an undecorated pass were assumed to need nothing, forgetting the decorator would bring back exactly that silent no-op. So an undecorated pass is treated as unknown. It logs a warning and falls back to a stock export followed by a full run_decompositions, which is correct but slower. A test also fails CI for any pass in pre_lowering_pass_list without a declaration, and the warning could be made an error later. @needs_no_undecomposed_ops is the explicit "I checked; this pass works on a decomposed graph", and it keeps passes like remove_detach on the one-shot path.

I think we can also drop the settings fingerprint. compile would instead check whether the graph still has any op its decomposition table would rewrite, and run run_decompositions only if it does. That's one pass over the nodes with a dictionary lookup each: about 0.2 ms on a 4k-node graph, against several seconds for the re-trace. It also covers the cases the fingerprint misses, such as a graph edited after trace, or a change to the table's contents.

@narendasan

Copy link
Copy Markdown
Collaborator

Ah I see. So these decorators only apply to the fast single decomp path?

@5had3z

5had3z commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

Yeah, avoids creating a long list of conditions here and if it isn't marked at all it warns and runs the safe (slow) method.

…ions.

Replace the settings fingerprint with a check of the program itself:
compile runs run_decompositions unless the program is post-dispatch ATen
IR with no op that its decomp table would rewrite. Training IR from stock
torch.export always reruns, since run_decompositions functionalizes ops
outside the table. The one-shot export and each rerun record the ops of
the table they applied, so an op a table tried and left in place (such
as _fused_rms_norm on CUDA, whose CIA decomposition never applies) does
not force a pointless retrace. The check covers graph edits after trace
and changes to the table that the fingerprint missed, and costs under a
millisecond.

scatter_add's decomposition now declines on a symbolic index extent, so
the one-shot table keeps it: a static scatter_add is decomposed during
export and a dynamic one is left for partitioning, with no rerun in
compile. This removes SHAPE_DEPENDENT_DECOMPOSITIONS. In a graph that
mixes static and dynamic scatter_add, the static ones are now decomposed
instead of all being kept.

Pre-export passes now declare with a yes/no whether they need the graph
undecomposed (needs_undecomposed_graph / runs_on_decomposed_graph). If
any does under the given settings, trace falls back to a stock export.
Undeclared passes warn and count as needing it, and a test fails CI for
any pre-export pass without a declaration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@5had3z 5had3z changed the title Let pre-export passes declare the ops they need undecomposed. Decide decomposition reruns from the graph, with yes/no pass declarations. Nov 17, 2026

@micwill755 micwill755 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pass declarations look like a clear improvement over special-casing autocast in each caller, and treating an undeclared pass as needing the undecomposed graph is the right default.

Main asks:

  1. Record only the ops the table couldn't remove, so that ops inserted after trace still get decomposed. Otherwise, drop the "graph edited after trace" claim from the description.
  2. Warn or raise when trace defaults are combined with compile flags that need the undecomposed graph.
  3. Remove the scatter_add filter so both paths agree.

Also, many CI jobs are red. The lint job's failure is unrelated, in ExecuTorch on torch.jit.load. Could you confirm that the dynamo-lowering and dynamo-converters failures also happen on one-shot-decomp-in-trace, not just on main?

scatter_add_decomposition already declines (returns NotImplemented) on a
symbolic extent, on every path that applies the table, and per call. That
makes the graph-level filter_decomposition_table redundant, so remove it and
use the decomposition table as-is at each call site. The test now checks the
decline directly, including a graph mixing dynamic and static scatter_add.

Also:
- Warn once per pass (not on every call) when a pre-export lowering pass does
  not declare whether it needs the undecomposed graph.
- Inline the one-line settings -> get_decompositions helpers
  (_decomp_table, _trt_decomposition_table) and pass arguments positionally.
- Use ExportedProgram.dialect in _is_post_dispatch_aten instead of getattr.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
5had3z and others added 3 commits November 23, 2026 16:02
_record_applied recorded every key of the decomposition table, so a table op
that a pass added after trace was never treated as pending and compile skipped
run_decompositions for it. Record only the table ops still in the graph after
the table was applied (such as _fused_rms_norm on CUDA, or a declined
scatter_add), which rerunning would not remove.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
trace() with default settings decomposes during export. A later compile() whose
settings enable a pass that matches ops the decompositions rewrite (such as
replace_interleave_stack_with_cat) then finds nothing to rewrite, silently.
Warn in pre_export_lowering when the program is already ATen IR and the
settings need the graph before decompositions.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@5had3z
5had3z requested a review from micwill755 November 24, 2026 00:19

@micwill755 micwill755 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@5had3z
5had3z merged commit 4497363 into one-shot-decomp-in-trace Nov 25, 2026
102 of 117 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: runtime component: tests Issues re: Tests component: torch_compile

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants