Repository navigation
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
Conversation
shoumikhin
force-pushed
the
collapse-expanded-high-rank
branch
4 times, most recently
from
November 20, 2026 12:24
98224f7 to
322e615
Compare
…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
force-pushed
the
collapse-expanded-high-rank
branch
from
November 23, 2026 16:34
a0d64da to
6d528c7
Compare
This branch has not been deployed
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.
What is wrong today
TensorRT tensors can have at most 8 dimensions. transformers 5.8 and later build Qwen2-VL image patches like this:
The 9-dimensional tensor keeps these ops in PyTorch and splits the graph.
collapse_high_rank_permuteshandles theolder form (one 10-dimensional permute), but not this one.
What this change does
A new opt-in setting,
collapse_high_rank_expands, rewrites anunsqueeze(d), anexpandthatrepeats only that new dimension
ktimes, an optionalclone, and reshapes asx.reshape(A, 1, B).expand(A, k, B).Ais the size of the dimensions beforedandBthe size of the rest. This gives the same values on 3dimensions.
collapse_high_rank_permutesdoes exactly what it did in 2.15. A chain that mixes both, such as a permute over 8dimensions 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_moduleskeeps its unsqueeze,the expand, a clone or a reshape in PyTorch, or when
torch_executed_opslists one of its ops. In those cases thegraph stays split anyway, and the rewrite would only send the larger tensor across an engine boundary.
What was tested
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.compileand serialized engines. Each guard test fails when its check is removed.tests/py/dynamo/loweringgives the same results ascurrent main, on Linux x86_64 with an A100.
min_block_size=1, on an A100: 1 engineinstead of 2 engines and a PyTorch part, 0.14 ms instead of 0.42 ms, same output. With only
collapse_high_rank_permuteson, the result is the same as with both settings off.