Skip to content

fix(partitioning): recompute large input-only values in each resource-split engine - #4961

Merged
cehongwang merged 3 commits into
mainfrom
cehongw/resource-partitioner-recompute
Nov 10, 2026
Merged

cehongwang merged 3 commits into
mainfrom
cehongw/resource-partitioner-recompute

Conversation

@cehongwang

@cehongwang cehongwang commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

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 main after #4963, which removed validate_and_correct_subgraphs: with the recompute disabled, the mask test below fails on today's main.

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:

ptxas error: Entry function '_gemm_mha_v2_...' uses too much shared data (0x39174 bytes, 0x38c00 max)

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 after break_subgraphs:

  • Each subgraph that reads a large weightless value computed from the graph inputs alone gets its own copy of the chain that computes it.
  • A value of at most 64 KiB (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.
  • A recomputed chain may read constants of up to 64 KiB, or up to 4 MiB, whatever the dtype, for a constant folded without reading a parameter. Constant folding marks those (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_functional and the fused NCCL ops, which count as pure): each rank splits by its own memory reading, and every rank must issue the same collectives.
  • A getitem crosses with its producer, so an engine never outputs a tuple.
  • Subgraphs are processed from the last one back, so every chain is copied from original nodes.
  • The originals and copies that nothing reads any more are removed, and a subgraph left empty is dropped.
  • Sizes are compared with 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 RecursionError about 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:

  • Without this change, every prefix engine with Gemma attention fails to build, as above.
  • With it, the export builds its 8 prefix engines and peaks at 4,983 MiB above idle. A chunk takes 117.9 ms, and the largest action error against PyTorch is 0.029.

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 named test_001_* so CI's partitioning suite (tests/ci/suites.py) collects it:

  • Mask: 8 attention-like layers share one 512×512 mask built from a padding input. They're lowered and split by ResourcePartitioner.partition_graph at 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.
  • Deep chain: a 2,000-node input-only chain, twice Python's recursion limit, is built in one subgraph and read in the next. It's recomputed there, and the graph still computes the same values. The recursive version fails this test with RecursionError.
  • One case per rule. Undoing each rule makes its own test fail, and only that one:
    • a value derived, in a later subgraph, from one that subgraph recomputes, read by a third subgraph (going forward raises KeyError, and the mask copy left in the emptied middle subgraph must be erased, with the subgraph dropped);
    • a getitem of an input's unbind;
    • a random value, which is passed rather than recomputed;
    • a fused NCCL all-reduce, which is passed rather than recomputed;
    • a 64×64 float mask (16 KiB), which crosses as it is, against a 256×256 one (256 KiB), which is recomputed;
    • a 1 MiB float constant folded from constructors (a causal triangle), which may be recomputed, against one folded from a parameter, which counts as a weight, both folded by the real 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.py and the constant-folding tests in tests/py/dynamo/lowering give 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

  • 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

…-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.
@meta-cla meta-cla Bot added the cla signed label Oct 30, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Oct 30, 2026
@github-actions
github-actions Bot requested a review from zewenli98 October 30, 2026 23:32
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).
…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.
@cehongwang cehongwang added this to the v2.15.0 milestone Nov 5, 2026

@shoumikhin shoumikhin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some notes inline.

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci: full 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: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants