Repository navigation
Decide decomposition reruns from the graph, with yes/no pass declarations. - #5066
Conversation
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]>
Signed-off-by: Bryce Ferenczi <[email protected]>
|
So this is to allow post ATen lowering to dynamically insert exceptions to the default decomposition set? Do we need both the |
These are for the pre-export lowering passes (
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. |
|
Ah I see. So these decorators only apply to the fast single decomp path? |
|
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]>
micwill755
left a comment
There was a problem hiding this comment.
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:
- 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.
- Warn or raise when trace defaults are combined with compile flags that need the undecomposed graph.
- 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]>
_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]>
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]>
Description
Proposed change to #4596, which applies the Torch-TRT decomposition table during
dynamo.traceso thatcompilecan skiprun_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_listrun aftertrace, so with one-shot decomposition they saw an already decomposed graph:replace_interleave_stack_with_catfound noaten.stackto rewrite.preserve_attention_output_layoutfound noscaled_dot_product_attentionunderdecompose_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:
If any pass needs it,
tracefalls back to a stocktorch.export, andcompiledecomposes after the passes have run, exactly as before #4596. This replaces autocast's special cases intrace,MutableTorchTensorRTModuleandmaybe_run_decompositions. The three flags that need the undecomposed graph arereplace_interleave_stack_with_cat,decompose_attentionandenable_autocast. All default toFalse, 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_PASSESwithout a declaration.compiledecides from the graph, not a settings fingerprintmaybe_run_decompositionsskipsrun_decompositionsonly when both of these hold:torch.exportreturns training IR, which can hold in-place ops outside the table, such asmul_. Those still needrun_decompositionsto functionalize them.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_normon 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 betweentraceandcompilesettings 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_addscatter_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 returnsNotImplementedon a symbolic extent, so the op stays for partitioning to fall back to Torch. A staticscatter_addis decomposed during export. Neither case reruns incompile.One behaviour change for graphs that mix static and dynamic
scatter_add:filter_decomposition_tableused to keep all of them; the static ones are now decomposed.API
export_with_tensorrt_decompsandmaybe_run_decompositionsnow takeCompilationSettings.traceandMutableTorchTensorRTModulebuild it from their keyword arguments withsettings_from_kwargs.Performance
Old: stock export, then
run_decompositions, as on main. New: the one-shot export, after whichrun_decompositionsis skipped. Each figure is the median of 3 runs, each in a fresh process.run_decompositionsThe 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_decompositionsdiffering 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 lackoriginal_atenandseq_nr, which only the second trace adds. Nothing in Torch-TensorRT reads them.Type of change
Checklist:
Tests:
tests/py/dynamo/models/test_one_shot_decomp.pycovers the declarations, the guard, training IR,_fused_rms_normon CUDA, settings mismatches, and static and dynamicscatter_add. In thedynamo-loweringsuite and the autocast and serde tests, the only failures also fail on main: the SDPAshared_bool_masktests, 7 autocast tests, andtest_save_load_ts/test_save_load_aoti.cudnn_grid_sampleralso fails, inside stocktorch.exportbefore any Torch-TRT code runs, but it hasn't been checked on main.🤖 Generated with Claude Code