Skip to content

feat(lowering): add collapse_high_rank_expands to run unsqueeze and expand chains over 8 dims, as in newer Qwen2-VL image patches, in TensorRT - #5028

Open
shoumikhin wants to merge 5 commits into
pytorch:mainfrom
shoumikhin:collapse-expanded-high-rank
Open

shoumikhin wants to merge 5 commits into
pytorch:mainfrom
shoumikhin:collapse-expanded-high-rank

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

TensorRT tensors can have at most 8 dimensions. transformers 5.8 and later build Qwen2-VL image patches like this:

patches = images.reshape(b, c, gh // m, m, p, gw // m, m, p).permute(0, 2, 5, 3, 6, 1, 4, 7)
patches = patches.unsqueeze(6).expand(-1, -1, -1, -1, -1, -1, t, -1, -1)  # 9 dims
patches = patches.reshape(b, gh * gw, c * t * p * p)

The 9-dimensional tensor keeps these ops in PyTorch and splits the graph. collapse_high_rank_permutes handles the
older form (one 10-dimensional permute), but not this one.

What this change does

A new opt-in setting, collapse_high_rank_expands, rewrites an unsqueeze(d), an expand that
repeats only that new dimension k times, an optional clone, and reshapes as x.reshape(A, 1, B).expand(A, k, B).
A is the size of the dimensions before d and B the size of the rest. This gives the same values on 3
dimensions.

trt_mod = torch_tensorrt.dynamo.compile(ep, arg_inputs=[x], collapse_high_rank_expands=True)

collapse_high_rank_permutes does exactly what it did in 2.15. A chain that mixes both, such as a permute over 8
dimensions that feeds an expand, needs both settings.

Like the permute case, it needs static shapes. Only reshapes and clones may read the expand, and each reshape must
come back to at most 8 dimensions. The chain is also left alone when torch_executed_modules keeps its unsqueeze,
the expand, a clone or a reshape in PyTorch, or when torch_executed_ops lists one of its ops. In those cases the
graph stays split anyway, and the rewrite would only send the larger tensor across an engine boundary.

What was tested

  • New tests: the patch layout, the new shapes and values, eager results for several dims and sizes, chains that must
    stay (other readers, a reader over 8 dimensions, any node of the chain in a kept module, chains within 8
    dimensions, dynamic shapes, excluded ops), each setting running only its own rewrite, a permute before the chain,
    stacked repeats, and torch.compile and serialized engines. Each guard test fails when its check is removed.
  • The permute tests from current main pass unchanged, and tests/py/dynamo/lowering gives the same results as
    current main, on Linux x86_64 with an A100.
  • These patches for a 448x448 image plus a patch embedding, float16, min_block_size=1, on an A100: 1 engine
    instead of 2 engines and a PyTorch part, 0.14 ms instead of 0.42 ms, same output. With only
    collapse_high_rank_permutes on, the result is the same as with both settings off.

@meta-cla meta-cla Bot added the cla signed label Nov 12, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation 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: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Nov 12, 2026
@github-actions
github-actions Bot requested a review from narendasan November 12, 2026 23:14
@shoumikhin
shoumikhin force-pushed the collapse-expanded-high-rank branch 4 times, most recently from 98224f7 to 322e615 Compare November 20, 2026 12:24
…wer Qwen2-VL image patches, in TensorRT

Newer transformers releases build Qwen2-VL image patches with an 8-D permute and then an unsqueeze and expand that
copy each image into the temporal patch, which makes a 9-D tensor. TensorRT tensors have at most 8 dims, so these ops
ran in PyTorch and split the graph. The collapse_high_rank_permutes setting, which already handles the older patch
layout, now also rewrites an unsqueeze(d), an expand of dim d alone, and reshapes as reshape(A, 1, B) and
expand(A, k, B), which gives the same values on 3 dims. One setting covers both, because a chain can mix the two.

Like the permute case, it needs static shapes and starts from the unsqueeze's own value. Each reshape that reads the
expand must come back to at most 8 dims, or start a permute chain that is rewritten too and comes back to at most 8
dims, so the engine never returns the expanded tensor while the graph stays split. For the same reason a repeat is
skipped when torch_executed_modules keeps one of those last readers in PyTorch. Repeats are rewritten before
permutes, and the last first, so stacked repeats and a permute over 8 dims that feeds one are rewritten too. Each
case is skipped when torch_executed_ops lists one of its own ops. The static-shape check, the reshape-only check, the
value update and the "can this permute chain be collapsed" check are now helpers shared by both cases.

Signed-off-by: Anthony Shoumikhin <[email protected]>
…odules, and drop permute-chain readers

With collapse_high_rank_permutes on, a repeat (an unsqueeze and expand over 8 dims) was rewritten even when
torch_executed_modules kept a clone of the expand in PyTorch. The graph then stayed split at that clone, and the
engine before it returned the expanded tensor, k times larger, instead of its input. The same happened, with an
extra small engine fed the expanded tensor, when the kept module held only the expand. The module check now covers
the expand itself, its clones and its reshape readers.

A reshape reader over 8 dims is no longer accepted when it starts a permute chain. No known model reads the expand
that way (the Qwen2-VL patches read it back to 3 dims), and that case had the same gap at the reader that starts the
chain. Such a repeat now stays as it is today, with the same values. The helpers that only served that case are
folded back into their callers.

New tests: a kept module around the unsqueeze, around the expand, around a contiguous() of the expand, or around the
reader, each of which stops the rewrite on its own; an expand with 2 readers where only the second is over 8 dims;
and the new value of a clone of the expand.

Signed-off-by: Anthony Shoumikhin <[email protected]>
…build fails

By default the torch_tensorrt backend of torch.compile catches a failed TensorRT conversion and runs the PyTorch
graph instead. So the collapse_high_rank_permutes torch.compile case passed even when conversion failed after the
rewrite. Set pass_through_build_failures, so that such a failure fails the test.

Signed-off-by: Anthony Shoumikhin <[email protected]>
… plainly

The first line of the pass docstring now names the unsqueeze, expand chains too. The comment on erasing the
unsqueeze now says that a permute before it needs it gone as well. The _reshaped_clones docstring now says clearly
that clones may read the node. The user-facing sentence about the repeat rewrite, in the settings, the compile
functions and the user guide, is split into shorter sentences.

Signed-off-by: Anthony Shoumikhin <[email protected]>
…, collapse_high_rank_expands

collapse_high_rank_permutes shipped in 2.15 as a rewrite of reshape, permute, reshape chains only. The commits
before this one made it also rewrite unsqueeze and expand chains. A model that turned it on in 2.15 would then get a
second rewrite after an upgrade, and could only stop it by keeping every expand in PyTorch.

The unsqueeze and expand rewrite now has its own opt-in setting, collapse_high_rank_expands, declared like the other
opt-in rewrites: a default, a settings field, an argument of the compile functions, a part of the engine cache key,
and a row in the user guide. collapse_high_rank_permutes does what it did in 2.15. A chain that mixes both, such as a
permute over 8 dims that feeds an unsqueeze and expand, needs both settings.

The rewrite function and its counter are now named after the expand, the counts use plain loops, and the docs say
that transformers 5.8 and later build the Qwen2-VL patches this way.

New test: each setting runs only its own rewrite, including the 2.15 setup (collapse_high_rank_permutes alone) on
the newer patch layout. The permute tests still run with collapse_high_rank_permutes alone, and the expand tests
turn on collapse_high_rank_expands.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the collapse-expanded-high-rank branch from a0d64da to 6d528c7 Compare November 23, 2026 16:34
@shoumikhin shoumikhin changed the title feat(lowering): run unsqueeze and expand chains over 8 dims, as in newer Qwen2-VL image patches, in TensorRT feat(lowering): add collapse_high_rank_expands to run unsqueeze and expand chains over 8 dims, as in newer Qwen2-VL image patches, in TensorRT Nov 23, 2026

This branch has not been deployed

No deployments
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: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant