Repository navigation
fix(partitioning): recompute large input-only values in each resource-split engine - #4961
Merged
Merged
Conversation
…-split engine validate_and_correct_subgraphs moves every node whose inputs are ready into the earliest subgraph, so values computed from the graph inputs alone (an attention mask, rotary cos/sin) land in the first engine and reach every later engine as inputs. On sm_110 (TensorRT 11.0 to 11.4) the fused attention kernel cannot be built for a layer fed that way: "uses too much shared data (0x39174 bytes, 0x38c00 max)", for every Gemma attention engine of pi0.5. ResourcePartitioner.recompute_input_only_nodes gives each subgraph that reads such a value its own copy of the weightless chain that computes it. Values of 64 KiB or less, and nodes that read a graph input directly, cross engines as they are, so each graph input keeps a single reader (GPU-resident ExecuTorch programs need that). Small constants (64 KiB, or 4 MiB for a constant-folded _frozen_param such as the fixed part of a mask) may be read by a recomputed chain; random ops are never recomputed. Both walks are iterative: which nodes are input-only is decided in one pass in graph order, and the chain to copy is collected with an explicit stack. Larger graphs have chains deeper than Python's recursion limit.
cehongwang
added a commit
that referenced
this pull request
Oct 30, 2026
#4961) is_cheap and copy recursed once per producer, two frames per level, so a chain about 500 nodes deep hit Python's recursion limit (evo1, ~16.5k nodes). Which nodes are input-only is now decided in one pass in graph order, and the chain to copy is collected with an explicit stack. The partition of the mask test is unchanged. The tests are #4961's, which don't need resource_partition_engine_cap, and the mapped-weights test compiles without TF32 (as in #4854).
3 of 7 tasks
…itioner-recompute # Conflicts: # py/torch_tensorrt/dynamo/partitioning/_resource_partitioner.py
cehongwang
added a commit
that referenced
this pull request
Nov 4, 2026
…r splits A model that stays one subgraph returns before the log, and the log no longer sits on the line after break_subgraphs, where #4961 adds its recompute step.
shoumikhin
reviewed
Nov 7, 2026
… with their producers, skip impure ops and collectives - Subgraphs are processed from the last one back, so every chain is copied from original nodes. Going forward, a value derived from one an earlier subgraph recomputed (and redirected to its copy) raised KeyError in the third subgraph. - A copy is input-only like its original, so the cleanup erases a copy nothing reads any more. Before, the copy of the mask made for a value that a later subgraph then recomputed stayed behind, as an engine input nothing used, or as a piece with no outputs, which fails to compile. - A getitem crosses with its producer: copying a large getitem of an input's unbind alone made the producer's engine output a tuple. - Impure nodes (random, in place, asserts) are never recomputed or erased, so an in-place plugin's write-back survives. - Collectives (_c10d_functional, and Torch-TensorRT's fused NCCL ops, which count as pure) are never recomputed. Each rank splits by its own memory reading, so copies could make ranks issue different numbers of collectives. - A constant folded without reading a parameter may be up to 4 MiB, whatever its dtype: constant folding marks such a value (folded_without_parameters), built from constructors, buffers or tensor constants, like a mask's fixed part, including a float causal triangle. One folded from a parameter is a weight transform (w.T, w.to(dtype)). A constant missing from the state dict isn't input-only. - Sizes are compared with statically_known_true, so symbolic shapes don't add guards. - The log counts the values and nodes recomputed; the thresholds' comments say why; names say what they hold. - The test file is test_001_* so CI's partitioning suite collects it, with a case per fix, threshold cases, a dynamic-shape variant and a causal-mask variant through real lowering. Each fix, undone, fails its test.
cehongwang
force-pushed
the
cehongw/resource-partitioner-recompute
branch
from
November 9, 2026 22:11
946142a to
39fe706
Compare
lanluo-nvidia
pushed a commit
that referenced
this pull request
Nov 10, 2026
7 tasks done
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.
Description
Part of #4867.
The resource partitioner splits in graph order, and a model computes the values that depend only on its inputs, such as an attention mask or rotary cos/sin, before its first layer. Those values therefore land in the first engine and reach every later engine as inputs. This still holds on
mainafter #4963, which removedvalidate_and_correct_subgraphs: with the recompute disabled, the mask test below fails on today'smain.Recomputing them in each engine removes that engine I/O on any GPU. It also fixes a build failure on Thor.
On a Jetson AGX Thor (sm_110), TensorRT 11.0 to 11.4 can't build an attention layer fed that way. Every Gemma attention engine of pi0.5 fails with:
and then "Could not find any implementation". It fails at optimization levels 1 and 3, and with every engine size tried (0.2 to 0.6 GiB). Two Gemma layers fail when they receive both the mask and cos/sin as inputs. One layer builds, either input alone builds, and the same layers build when they compute the mask and cos/sin themselves.
This PR adds
ResourcePartitioner.recompute_input_only_nodes, which runs afterbreak_subgraphs:RECOMPUTE_MIN_OUTPUT_BYTES: a padding mask, position ids) crosses engines as it is, and so does any node that reads a graph input directly. Each graph input keeps a single reader, which GPU-resident ExecuTorch programs (skip_h2d_for_method_inputs) require.folded_without_parameters): values built from constructors, buffers or tensor constants, such as the fixed part of pi0.5's 968×968 mask or a float causal triangle. A constant folded from a parameter is a weight transform (w.T,w.to(dtype)), so it counts as a weight. Weights are never recomputed, and neither are impure nodes (random, in place, asserts) or collectives (_c10d_functionaland the fused NCCL ops, which count as pure): each rank splits by its own memory reading, and every rank must issue the same collectives.statically_known_true, so dynamic shapes add no guards.Why the thresholds: values that grow with the sequence length stay under 64 KiB (pi0.5's padding mask is about 1 KiB). Values that grow with its square exceed it (its attention mask is 3.6 MiB, its cos/sin about 0.5 MiB), and those are the ones worth recomputing. The 4 MiB limit for constants folded without a parameter covers the 0.9 MiB folded mask; a constant folded from a parameter only gets the 64 KiB one.
Not covered: a value that reaches an engine across a split this pass doesn't make (an operator that runs in PyTorch), and a mask passed in as a graph input, still arrive as inputs. A TensorRT bug for the sm_110 failure should be filed with the two-layer repro.
Both walks over the graph are iterative. Which nodes are input-only is decided in one pass in graph order, and the chain to copy is collected with an explicit stack. Larger models have chains deeper than Python's recursion limit: an earlier, recursive version of this change failed with
RecursionErrorabout 500 levels deep on evo1 (about 16.5k nodes), as @shoumikhin reported.Graphs that resource partitioning doesn't split are unchanged.
Measured
pi0.5 (lerobot, bf16), ExecuTorch export on a Jetson AGX Thor with TensorRT 11.4, together with the other PRs in #4867:
That run used the recursive version. On the mask test below, the iterative version produced the same 5 subgraphs, with the same operations in each, before #4963's splitter was merged in. That test now splits into 8 subgraphs, and the fixes from review leave them unchanged.
Testing
tests/py/dynamo/partitioning/test_001_resource_partition_recompute.py. It's namedtest_001_*so CI's partitioning suite (tests/ci/suites.py) collects it:ResourcePartitioner.partition_graphat one 1 MiB layer per subgraph. No subgraph receives the mask, at most one reads the padding input, and the split module matches eager. It runs with static shapes, with a dynamic sequence length (256 to 1024), and with a fixed float causal part, which folds into a 1 MiB constant (7 of 9 subgraphs received the mask while folded float constants counted as weights). With the recompute disabled, the test fails on the mask check.RecursionError.KeyError, and the mask copy left in the emptied middle subgraph must be erased, with the subgraph dropped);constant_fold.On the C++ runtime, these 12 tests pass. Merged with today's
main, they,test_000_resource_partitioning.py,test_001_resource_partitioning.pyand the constant-folding tests intests/py/dynamo/loweringgive 95 passed. On the branch, adding global partitioning, the complex rewrite, decompositions and the specialized models gives 348 passed. Lint is clean.Type of change
Checklist: