Skip to content

feat(conversion): convert small antialiased bilinear downscales by default - #5024

Closed
shoumikhin wants to merge 2 commits into
pytorch:mainfrom
shoumikhin:antialias-bilinear-default
Closed

shoumikhin wants to merge 2 commits into
pytorch:mainfrom
shoumikhin:antialias-bilinear-default

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

This sits on top of #5023, so the first commit here is #5023. Only the last commit is new.

What is wrong today

Antialiased resize (F.interpolate with antialias=True) runs in TensorRT only with convert_antialiased_resize=True. Otherwise it splits the graph. This is common at the start of image models, which resize camera frames. Exporting frameworks often cannot pass extra settings.

What this change does

convert_antialiased_resize becomes Optional[bool], with None as the new default:

  • None converts float32 bilinear downscales with at most 6 input pixels per output pixel along each axis (up to about 3x). On A100, H100 and Jetson Orin GPUs with TensorRT 11.4 and a PyTorch 2.16 build for CUDA 13.2, its output matched PyTorch's bit for bit, also at 6 nonzero taps. Elsewhere it can differ by a few float32 roundings; set False for PyTorch's exact output.
  • True converts every antialiased resize, as before.
  • False keeps them all in PyTorch, as before. A falsy value such as 0 means False. Settings pickled before this setting existed load as False.

The limit and the per-axis check are DEFAULT_MAX_ANTIALIAS_TAPS and antialias_converts_by_default in impl/upsample.py. Without settings, the validator uses the defaults. The docs name the overload that torch_executed_ops matches, torch.ops.aten._upsample_bilinear2d_aa.default.

None also keeps in PyTorch an antialiased resize that has another antialiased resize before it in the graph. TensorRT fuses such a chain into one slow kernel: a 3-level 1080p pyramid took 37 s to build, against 1.8 s now. On TensorRT-RTX, None converts nothing, since it was not measured there.

Upscales, larger downscales, half-precision inputs and bicubic resize stay in PyTorch. They were not always bit for bit: a 4x downscale was within 5e-5 on 0 to 255, a 1.25x upscale one float32 rounding off.

Speed, for a resize followed by a 3x3 convolution: 0.18 ms in one engine against 0.35 ms split for 2x3x288x512 to 234x416, and 0.80 ms against 1.31 ms for 8x3x1080x1920 to 360x640. A resize alone is slower in TensorRT; the gain is the avoided split. Memory: for the second case the engine needs about 21 MiB of activation memory, about what PyTorch allocates for the resize with False.

Tests

  • test_antialias2d_matches_pytorch runs the default at 3 and 6 nonzero taps within 1 float32 rounding (a mask-after-multiply change fails it), and other cases within 1e-4.
  • test_antialias2d_chain_default: a resize after another one stays in PyTorch by default. test_antialias2d_validator_default_tensorrt_rtx: by default nothing converts on TensorRT-RTX.
  • test_antialias2d_validator_default: by default 6 taps convert, also with align_corners or a 0.5 scale; 7 taps (either axis or align_corners), 8 taps with a scale, an upscale, float16 and float64 do not. True converts 7 taps, 0 nothing.
  • test_antialias2d_setting_is_bool, test_antialias2d_old_pickle_keeps_resize_in_pytorch: the two False cases above. test_antialias2d_default_float64_log: no wrong truncate_double hint.
  • test_antialias2d_engines: by default 1 engine, with False the split; bicubic stays in PyTorch.
  • All of test_upsample_aten.py passes on A100. Default-path cases skip on TensorRT-RTX.

@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: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters 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 zewenli98 November 12, 2026 15:20
@shoumikhin
shoumikhin force-pushed the antialias-bilinear-default branch from 6af773c to 35f05f9 Compare November 14, 2026 17:20
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
shoumikhin force-pushed the antialias-bilinear-default branch 2 times, most recently from 4c6b69f to aa242f6 Compare November 17, 2026 21:29
…DA kernel

The antialiased resize converter computed its filter weights in float64.
PyTorch's CUDA kernel computes them in float32, and its compiler fuses
some multiply and add steps. So for float32 inputs the two results
differed by up to about 4e-3 on pixel values from 0 to 255.

That is normally harmless, but images are often resized and then rounded
to uint8. Many exact resize results end in .5, so a small difference
moves a pixel by one level. At a 288x512 to 234x416 downscale, 0.2% to
0.7% of the pixels changed.

For float32 inputs, compute the weights the way the kernel does: float32
at every step, the same fused multiply-adds, and the weights summed in
order. The fused multiply-adds use a float64 product on every NumPy
version. float64 inputs keep float64 weights, as in PyTorch. A window
whose weights sum to 0 keeps them undivided, like the kernel. Before,
such a window got NaN weights, for example in a bicubic 10x10 to 20x20
resize with scales of 1.0, or very large ones when nonzero bicubic
weights cancel out.

Also mask the pixels past a window's end before the multiply instead of
after it, so TensorRT can fuse the multiply and the sum like the kernel.

Also put the dtype of each graph input into the engine cache key. With
truncate_double=True a float64 input reaches the engine as float32, so a
float32 and a float64 version of the same resize got the same key. Their
weights now differ, and sometimes so does the number of taps, so reusing
the cached float32 engine for the float64 graph failed at refit.

The weights come from the height and width in the graph, which the
validator requires to be static. TensorRT can still see one of them as
dynamic, for example after a fixed crop of an image with a dynamic
height, and the conversion then failed. The converter now first
reshapes the input to the static sizes, as the adaptive max pooling
converter does.

The new weights equal PyTorch's CUDA weights exactly, which a new test
reads out by resizing an identity matrix. On A100 and H100 GPUs with
TensorRT 11.4, the TensorRT output was bit for bit the same as PyTorch's
for bilinear downscales by up to about 3 (at most 6 input pixels per
output pixel per axis) and for some bicubic sizes. Other cases, such as
bilinear upscales and larger downscales, stayed within a few float32
roundings.

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

Antialiased resize (F.interpolate with antialias=True) only ran in
TensorRT with convert_antialiased_resize=True. Otherwise it stayed in
PyTorch and split the graph, often right at the start of an image model.

convert_antialiased_resize is now Optional[bool], and None is the new
default:
- None converts float32 bilinear downscales with at most 6 input pixels
  per output pixel along each axis, up to about 3x. In our measurements
  on A100, H100 and Jetson Orin GPUs with TensorRT 11.4 and a PyTorch
  2.16 build for CUDA 13.2, TensorRT summed the taps in the same order as
  PyTorch's CUDA kernel there, and the output was bit for bit the same.
  Elsewhere it can differ by a few float32 roundings. Upscales,
  half-precision inputs, larger downscales and bicubic resize stay in
  PyTorch. So does a resize that depends on another antialiased resize:
  TensorRT can fuse the two into one kernel that builds and runs slowly.
  A 3-level 1080p image pyramid took 37 s to build that way and ran 8
  times slower than in PyTorch. On TensorRT-RTX, where the output was
  not measured, None converts nothing.
- True converts every antialiased bilinear and bicubic resize, as before.
  The output can then differ from PyTorch by a few roundings.
- False keeps them all in PyTorch, as before. A falsy value such as 0
  means False, and settings pickled before this setting existed load as
  False, so refit partitions them as before.

A test checks that the TensorRT output of the sizes None converts stays
within one float32 rounding step of PyTorch's. With the pixels masked
after the multiply instead, it is off by 2 to 4 steps there.

The limit and the per-axis check live in impl/upsample.py as
DEFAULT_MAX_ANTIALIAS_TAPS and antialias_converts_by_default. A validator
called without settings now uses the defaults, and the documented opt-out
names the operator overload that torch_executed_ops matches.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the antialias-bilinear-default branch from aa242f6 to 8a0d65d Compare November 18, 2026 08:09
@shoumikhin

Copy link
Copy Markdown
Contributor Author

Folded into #5023.

@shoumikhin shoumikhin closed this Nov 18, 2026
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: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants