Repository navigation
fix(dynamo): int64 sum and prod, truncating division sign, uint8 select and cast in the engine - #5019
Open
shoumikhin wants to merge 6 commits into
Open
shoumikhin wants to merge 6 commits into
shoumikhin wants to merge 6 commits into
Conversation
This was referenced Nov 12, 2026
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
from
November 14, 2026 17:16
9b0c128 to
f4ee2a5
Compare
shoumikhin
added a commit
to shoumikhin/TensorRT
that referenced
this pull request
Nov 15, 2026
…5021 on main Brings the grouped GEMM kernel on this branch to the current version of pytorch#5021 on main. The kernel is opt-in through a grouped_mm_kernel setting (default False). It declines, with one warning that names the reason, when Triton, the driver's PTX limit, the GPU, hardware compatibility or Windows cross-compilation rule it out. A build failure is cached and logged once. Rows are masked so invalid offsets, past mat1 or negative, never read or write outside mat1 or the output. The dense converter's warning suggests the setting only when it is off and the kernel would likely take the op, checked without starting Triton. The tests are the same as on main, including engine-only exports run in a fresh process that loads only TensorRT and PyTorch, and a .pte run by the ExecuTorch runtime. It adapts to the release branch's kernel helpers. The plugin launch is still built in the kernel module, since this branch has no _common.make_pointer_aot_fn. The 48 KiB shared memory limit is a named constant in kernels/_common.py, used by the triton_op check, its error message and this kernel, as on main. compile_triton_to_ptx here hints every pointer as 16-byte aligned; it gets an optional per-pointer pointer_alignment, with the old behaviour as the default, so the kernel can leave the offsets unhinted as it does on main. This branch is release/2.15 (5aca374) with these release/2.15 ports of pull requests on main, oldest first: - pytorch#5014: fix(conversion): leave large aten._grouped_mm to PyTorch instead of the dense lowering - pytorch#5016: fix(kernels): reject AOT kernels that need more than 48 KiB of shared memory - pytorch#5017: fix(kernels): give triton_op kernels Triton's pointer alignment hint - pytorch#5021: feat(conversion): run aten._grouped_mm in TensorRT with a grouped GEMM kernel - pytorch#5021: fix(conversion): build the grouped GEMM plugin launch directly on release/2.15 - pytorch#5020: feat(dynamo): convert stable sort of index tensors with a counting sort - pytorch#5020: fix(dynamo): counting sort: add the key count to the engine cache key, convert single rows only - pytorch#5019: fix(dynamo): keep a select of a uint8 tensor inside the TensorRT engine - pytorch#5022: fix(dynamo): keep a cast to uint8 inside the TensorRT engine - pytorch#5023: Compute antialiased resize weights like PyTorch's CUDA kernel - pytorch#5024: Convert antialiased bilinear resize by default - pytorch#5021: fix(conversion): compile the grouped GEMM kernel without tensor memory on sm_100 and newer - pytorch#5015: fix(dynamo): release weights before serializing the engine, not after - pytorch#5021: feat(conversion): make the grouped GEMM kernel opt-in, as in pytorch#5021 on main Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin
added a commit
to shoumikhin/TensorRT
that referenced
this pull request
Nov 15, 2026
…5021 on main Brings the grouped GEMM kernel on this branch to the current version of pytorch#5021 on main. The kernel is opt-in through a grouped_mm_kernel setting (default False). It declines, with one warning that names the reason, when Triton, the driver's PTX limit, the GPU, hardware compatibility or Windows cross-compilation rule it out. A build failure is cached and logged once. Rows are masked so invalid offsets, past mat1 or negative, never read or write outside mat1 or the output. The dense converter's warning suggests the setting only when it is off and the kernel would likely take the op, checked without starting Triton. The tests are the same as on main, including engine-only exports run in a fresh process that loads only TensorRT and PyTorch, and a .pte run by the ExecuTorch runtime. It adapts to the release branch's kernel helpers. The plugin launch is still built in the kernel module, since this branch has no _common.make_pointer_aot_fn. The 48 KiB shared memory limit is a named constant in kernels/_common.py, used by the triton_op check, its error message and this kernel, as on main. compile_triton_to_ptx here hints every pointer as 16-byte aligned; it gets an optional per-pointer pointer_alignment, with the old behaviour as the default, so the kernel can leave the offsets unhinted as it does on main. This branch is release/2.15 (5aca374) with these release/2.15 ports of pull requests on main, oldest first: - pytorch#5014: fix(conversion): leave large aten._grouped_mm to PyTorch instead of the dense lowering - pytorch#5016: fix(kernels): reject AOT kernels that need more than 48 KiB of shared memory - pytorch#5017: fix(kernels): give triton_op kernels Triton's pointer alignment hint - pytorch#5021: feat(conversion): run aten._grouped_mm in TensorRT with a grouped GEMM kernel - pytorch#5021: fix(conversion): build the grouped GEMM plugin launch directly on release/2.15 - pytorch#5020: feat(dynamo): convert stable sort of index tensors with a counting sort - pytorch#5020: fix(dynamo): counting sort: add the key count to the engine cache key, convert single rows only - pytorch#5019: fix(dynamo): keep a select of a uint8 tensor inside the TensorRT engine - pytorch#5022: fix(dynamo): keep a cast to uint8 inside the TensorRT engine - pytorch#5023: Compute antialiased resize weights like PyTorch's CUDA kernel - pytorch#5024: Convert antialiased bilinear resize by default - pytorch#5021: fix(conversion): compile the grouped GEMM kernel without tensor memory on sm_100 and newer - pytorch#5015: fix(dynamo): release weights before serializing the engine, not after - pytorch#5021: feat(conversion): make the grouped GEMM kernel opt-in, as in pytorch#5021 on main Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
from
November 15, 2026 20:50
f4ee2a5 to
6f8a2a6
Compare
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
from
November 17, 2026 21:27
6f8a2a6 to
0f59194
Compare
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
8 times, most recently
from
November 23, 2026 07:07
41d78f6 to
9aba887
Compare
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
from
November 24, 2026 04:40
9aba887 to
eabc19a
Compare
torch.div(x, y, rounding_mode="trunc") took the sign of the quotient from the product x * y, and that product can overflow. For an int32 x of 2**20 divided by 2048 it wraps to a negative number, so the result was -512 instead of 512. For float32 a tiny product rounds to 0, so 3e-30 divided by 1e-30 gave 0 instead of 3. A float divisor of 0 also gave a sign of 0, so 1.0 divided by 0.0 gave NaN instead of infinity. For integers the converter now multiplies the sign of x by the sign of y, which cannot overflow. For floats it takes the sign of the quotient x / y, which keeps its sign when the product would round to 0 and is a signed infinity when y is 0. Signed-off-by: Anthony Shoumikhin <[email protected]>
PyTorch sums and multiplies bool and integer tensors in int64 unless a dtype is given. The sum and prod converters did not: - sum reduced int32 in int32, so a total past 2**31 wrapped or saturated, depending on the GPU. - prod cast int8 and int32 to float32, so a product past 2**24 was rounded. - the dtype argument of aten.sum and aten.prod was ignored. Only the output_dtype of prims.sum was read, and it was applied after the sum, so a sum with dtype=torch.int32 past 2**24 was rounded. Now the converters reduce in the dtype PyTorch gives the result: the dtype argument if there is one, else int64 for a bool or integer input (prims.sum gives its output_dtype, else the input dtype). An integer result is always reduced in int64 and then cast, which wraps like PyTorch. TensorRT reduces int64 exactly. A capability validator keeps on the PyTorch path a reduction of an int8 graph input, also after casts and data moves as in torch.sum(x.int()), since TensorRT treats an int8 engine input as quantized and the build fails. An int8 value made inside the graph stays inside its engine, so x.to(torch.int8).float().sum() still builds one engine as before. The validator also keeps a result in uint8 or bool on the PyTorch path, which the next engine can take as an input. An int8, int16 or float64 result could not feed another engine, so the converter computes it in a dtype the engine holds, and x.sum(-1, dtype=torch.float64).float() still builds one engine. For int8 and int16 that is int64 of the input cast to int64, which truncates a float like PyTorch's cast to the dtype: a float 1.7 counts as 1. A value or total outside the int8 or int16 range does not wrap, as before. float64 becomes float32, so a product does not wrap the way int64 would, and a float16 sum does not overflow. An integer sum to float64 is reduced exactly in int64, and stays int64 unless truncate_double asks for float32. Every float64 sum and prod still runs in TensorRT, with or without truncate_double, as before. The unary converters that cast int8 and int32 to float32 before a TensorRT layer that only takes floats (exp, log, sqrt, rsqrt, sin, erf, isinf and the like) now cast int64 too, and sigmoid and tanh cast integers the same way. ceil, floor and round return an integer input unchanged, as PyTorch does, instead of rounding it through float32, which also failed to build for an int8 engine input. Otherwise an integer sum or product, now int64, could no longer feed them: x / torch.sqrt(mask.sum(-1)) and torch.sigmoid(x.prod(1)) failed to compile. The converters that such a value now reaches are fixed the same way: - mean casts int64 to float32, as it does int8 and int32, so mask.sum(-1).mean(dtype=torch.float32) keeps its fraction. - where keeps int64 when either value is int64, as PyTorch does, instead of narrowing both to int32, so a product past 2**31 passes through it unchanged. A 0-dim int64 value still does not widen an int32 tensor, as in PyTorch. - relu takes an int32 or int64 input as an elementwise max with 0, since TensorRT RELU rejects int32 on some versions, so torch.relu(torch.round(x)) still compiles for an int32 x. - max and average pooling cast an int32 or int64 input to float32, and the result back to the input type, since TensorRT pooling takes neither. Integer graphs exported on the CPU, such as max_pool2d(x.prod(-1), 2), still build. - torch.cond casts its two branch results to one dtype when TensorRT holds them in two, as for an int64 sum in one branch and an int32 argmax in the other. Signed-off-by: Anthony Shoumikhin <[email protected]>
Follow-up to the int64 sum and prod change: - New tests: uint8 sums and products past 2**31, both for an input and for a slice reduced inside the engine; an int32 sum with dim=[]; and prod over a dim with dtype=torch.float32 on float16. Each fails on main. - The int32 prod test now uses 50001, so the product of all values fits in int64 instead of relying on int64 overflow. - The int32 sum with dtype=torch.int32 now totals past 2**31, so it checks that the result wraps like PyTorch. It fails on main. - The validator test also checks that a float64 sum of int32, bool or float32 stays in TensorRT with truncate_double, so the validator refuses no more than it should. It now also checks the refusals of a bool result and of a prod of an int8 input. - The where test with a 0-dim int64 value also puts that value first. - prims.sum of int32 keeps the int32 dtype of the exported graph. - sigmoid and tanh are tested on int8 and int32 as well as int64. Signed-off-by: Anthony Shoumikhin <[email protected]>
…ngine With truncate_double=True, `frames[1:3].double()` on a uint8 input failed to compile with "TypeError: Unsupported tensorrt dtype", and the PyTorch fallback did not catch it. The same happened after reshape, flip and the other ops that only move data, and for an aten.copy of such a value into a float64 tensor. The _to_copy validator accepts the copy because of the graph's dtype: float32 holds every uint8 value exactly. But the interpreter widens uint8 data to int32 before an op that only moves it, and impl.cast.to_copy decided from that int32, which float32 does not hold exactly, so it asked TensorRT for float64. The _to_copy and aten.copy converters now turn a float64 copy into a float32 copy themselves with truncate_double. Their validators allow that only when the graph's source dtype fits in float32 exactly. A copy of an engine input still decides from the TensorRT dtype, which is uint8 there and gives the same answer. Signed-off-by: Anthony Shoumikhin <[email protected]>
Picking one frame out of a uint8 batch, as in `frames[1].float() / 255`, split the graph: the partitioner refused aten.select.int on a uint8 tensor, so the select ran in PyTorch between two engines. TensorRT allows uint8 only at engine inputs and outputs, but select only picks existing values. Like slice and index_select, it now goes in UINT8_DATA_MOVEMENT_OPS: the interpreter widens its uint8 input to int32, and a uint8 engine output is cast back to uint8. The select validator that refused uint8 is removed, since the gather no longer sees uint8, and so is the select entry in the interpreter's value-picking list, which can no longer be reached. Signed-off-by: Anthony Shoumikhin <[email protected]>
Rounding video frames to uint8 levels, as in `frames.round().clamp(0, 255).to(torch.uint8)`, split the graph: the cast to uint8 ran in PyTorch between two engines, even when the uint8 values were only sliced, concatenated and cast back to float. TensorRT allows uint8 only at engine inputs and outputs, so the cast now makes the widened int32 value that the engine already uses for uint8 data. It matches PyTorch exactly: PyTorch converts through int64, truncating toward zero, then wraps modulo 256 (255.9 gives 255, 256.7 gives 0, -1.5 gives 255). The TensorRT cast to int64 truncates the same way, and the existing wrap_to_uint8 helper does the wrap. A bool is already 0 or 1, so it is cast straight to int32. A uint8 engine output is still cast back to uint8 at the output. impl.cast.UINT8_CAST_SOURCES lists the source dtypes this applies to: bool, int32, int64, float16, bfloat16 and float32. The _to_copy validator reads it, so makes_uint8_tensor now leaves every _to_copy to the validator, as it does for clone and where. Only the _to_copy converters call the new impl.cast.to_uint8. A cast from any other dtype still runs in PyTorch. For int8 that is required: TensorRT treats an int8 engine input as quantized, so the math after the cast fails the build. Other casts or data moves in between do not change that, as in x.to(torch.int32).to(torch.uint8) on an int8 input, so the validator follows the source back through them. It refuses any int8 value there, not only a graph input as for sum and prod, because an int8 value made by an op that runs in PyTorch reaches the engine of the cast as an input. With require_full_compilation the graph is one engine, so only an int8 graph input counts. For float64 with truncate_double, float32 would round 255.99999999 up to 256, which wraps to 0. The wrap needs an exact int64 floor division. TensorRT before 10.14 gets it wrong for large values (2**40 + 300 keeps 300 instead of 44), so with those versions the cast still runs in PyTorch, except from a bool, which needs no wrap. The tests that expect the cast in the engine are skipped there. Values outside the int64 range, infinity and NaN are unspecified, since PyTorch's own result is undefined for them and differs between CPU and CUDA. Signed-off-by: Anthony Shoumikhin <[email protected]>
shoumikhin
force-pushed
the
uint8-select-in-engine
branch
from
November 24, 2026 06:48
eabc19a to
995c55b
Compare
micwill755
self-requested a review
November 25, 2026 23:41
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
Integer
sumandprodcan be wrong. PyTorch sums and multiplies bool and integer tensors in int64. The converters do not:sumwraps past 2**31: 4 values of2**30sum to 0.prodcasts int32 to float32, so 4097 * 4097 gives 16785408 instead of 16785409.dtypeargument is ignored, sotorch.sum(half_x, dtype=torch.float32)can overflow to inf.Truncating division takes its sign from
x * y, which overflows:torch.div(x, 2048, rounding_mode="trunc")gives -512 for an int32xof 2**20. Dividing a float by 0 gives NaN, not infinity.uint8 camera frames split the graph. For example, with uint8
frames:TensorRT allows uint8 only at engine inputs and outputs, so the select and the cast ran in PyTorch.
What this change does
sumandprodreduce integers in int64, then cast to thedtypeargument, else int64.prims.sumkeeps itsoutput_dtype, else the graph dtype.An int8 or int16 result is int64, with floats truncated like PyTorch but no wrap. A float64 result is float32, except that an integer
sumis exact in int64.A reduction of an int8 graph input stays in PyTorch, even after casts and moves, since TensorRT treats an int8 engine input as quantized. A uint8 or bool result also stays in PyTorch.
Ops that an int64 result reaches handle it: unary float ops,
meanand pooling cast it to float32,wherekeeps int64,reluis a max with 0, andtorch.condgives both branches one dtype.ceil,floorandroundreturn integers unchanged.Truncating division takes the sign from each integer operand, or from the float quotient.
uint8 data that is only moved stays in the engine, as int32:
aten.select.intjoins the uint8 data movement ops, likeslice. TensorRT reads only the selected frame, with no int32 copy of the input.truncate_double.What was tested