Skip to content

fix(kernels): plugin kernel limits: 48 KiB shared memory, tensor memory, pointer alignment hint - #5016

Open
shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:kernels-48kib-smem-limit
Open

shoumikhin wants to merge 3 commits into
pytorch:mainfrom
shoumikhin:kernels-48kib-smem-limit

Conversation

@shoumikhin

@shoumikhin shoumikhin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What is wrong today

Shared memory above 48 KiB. TensorRT launches AOT plugin kernels without the cuFuncSetAttribute opt-in that CUDA needs above 48 KiB (#5010). So a kernel that needs more builds, then every run fails with an assertion that gives no reason.

Tensor memory. triton_op refused every kernel that uses tensor memory, as most Triton matmuls on sm_100 and sm_110 do.

Pointer alignment. A normal Triton launch tells the compiler which pointers are 16-byte aligned. triton_op compiles without it, so its kernels use scalar loads. A bf16 grouped GEMM on an A100 takes 6.61 ms per engine call without the hint, 2.55 ms with it.

What this change does

  • triton_op and cutedsl_op decline a kernel that needs more than 48 KiB, with a warning. The op then runs in PyTorch through its eager_fn where the graph allows it. Without an eager_fn, registration fails. For cutedsl_op the count includes static shared memory.

  • For every frontend, an aot_fn whose constant shared_mem plus static shared memory is above 48 KiB now fails the engine build, with the reason in the log. A custom tactic only gets a warning, since TensorRT can pick another one. If the static size cannot be read, the check warns and counts dynamic memory only.

  • triton_op accepts tensor memory up to an SM's 512 columns, or 576 on sm_107.

  • triton_op gets pointer_alignment, as cutedsl_op has. Values above 1 become Triton's divisibility hint, capped at 16.

    ttk.triton_op("my::op", kernel, signature, constexprs, grid, meta, pointer_alignment=16)

    Every pointer must then be aligned: a vector load through a misaligned one, such as x[1:], leaves the CUDA context unusable. Pipelining that the hint enables uses more shared memory.

What was tested

  • 49152 bytes is accepted, 49168 declined, at conversion and registration.
  • ptx_op engines: with a 16 KiB static array, 32 KiB of dynamic shared memory builds and runs, and 16 bytes more fails the build. With tactics of 64 KiB and 32 KiB, the engine uses the second.
  • A failed static size read warns and is retried.
  • Tensor memory at the limit is accepted, one column more is refused.
  • Alignments 8 and 16 give 8-byte and 16-byte loads, and an engine with alignment 16 matches eager.
  • Every new test fails without its change. The kernels tests pass on an A100. A tensor memory kernel ran on Jetson Thor.

@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: api [Python] Issues re: Python API labels Nov 12, 2026
@github-actions
github-actions Bot requested a review from narendasan November 12, 2026 03:26
@narendasan
narendasan requested a review from bowang007 November 12, 2026 23:41
@shoumikhin
shoumikhin force-pushed the kernels-48kib-smem-limit branch from 5240459 to 6806129 Compare November 14, 2026 17:17
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 kernels-48kib-smem-limit branch 2 times, most recently from 9812047 to 4f11878 Compare November 17, 2026 21:27
@shoumikhin shoumikhin changed the title fix(kernels): decline kernels above TensorRT's 48 KiB shared memory limit fix(kernels): decline kernels above TensorRT's 48 KiB shared memory limit, and accept kernels that use tensor memory Nov 17, 2026
@shoumikhin shoumikhin changed the title fix(kernels): decline kernels above TensorRT's 48 KiB shared memory limit, and accept kernels that use tensor memory fix(kernels): plugin kernel limits: 48 KiB shared memory, tensor memory, pointer alignment hint Nov 18, 2026
@shoumikhin
shoumikhin force-pushed the kernels-48kib-smem-limit branch from a1b2414 to 183f3e6 Compare November 18, 2026 19:23

@micwill755 micwill755 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Falling back to PyTorch is much better than the opaque pluginV3Runner assertion. The new engine tests failed in CI on the torch.jit / modelopt issue, so they haven't actually run. Can you get a green run before merge?

extra_args_fn,
)

alignments = _common.normalize_pointer_alignment(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A misaligned pointer leaves the CUDA context unusable. Could the runtime check input alignment, even just in debug mode? Otherwise, please file a follow-up issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. The check cannot go in the plugin, because TensorRT launches the precompiled kernel itself and no Python code runs per call. Inside the engine TensorRT always gave the plugin aligned buffers in my tests, so the risk is engine inputs, which the runtime binds as given: an offset view from the caller, or one made by a PyTorch op that runs before the engine. The docs now say this. The right fix is in the runtime: check each input against TensorRT's own 256-byte rule and copy the ones that fail, always on, since it costs one modulo per input. It changes input binding for every engine in both runtimes, and inputs that the engine writes in place need their own decision, so I opened #5086 to track it rather than change it in this kernels PR.

@shoumikhin
shoumikhin force-pushed the kernels-48kib-smem-limit branch 4 times, most recently from 94b7df1 to 6cc5d35 Compare November 20, 2026 15:44
@shoumikhin

Copy link
Copy Markdown
Contributor Author

@micwill755 You are right, the new engine tests have not run in CI yet. At this head the Triton and CuTe DSL kernel jobs still fail at the same point: importing ModelOpt calls torch.jit.script, which the current PyTorch nightly no longer has. 6 of those 8 failing tests fail the same way on main, and #5059 fixes it, but it is not merged yet. In the meantime I ran the whole tests/py/kernels folder at this head on an A100 with a recent PyTorch nightly, TensorRT 11.4 and Triton 3.9: 320 passed, 33 skipped (cuTile and NVRTC tests that need a newer driver), 0 failed, including both new engine tests. Once #5059 is in, I will rerun the kernel jobs.

…imit

CUDA lets a kernel use more than 48 KiB of dynamic shared memory only after
the launcher opts the kernel in with cuFuncSetAttribute. TensorRT launches
AOT plugin kernels without doing that (pytorch#5010). So a plugin kernel that needs
more than 48 KiB builds into an engine without complaint, and then every run
of the engine fails with a generic plugin assertion that gives no reason.
Large Triton tiles with software pipelining often need more than 48 KiB, so
this is easy to hit.

The capability validators of triton_op and cutedsl_op now decline such a
kernel, the same way they decline hardware-compatible engines. The op and
its eager_fn stay registered, the op runs in PyTorch where the graph
allows it, and
warnings at registration and at conversion name the cause and the fix
(fewer pipeline stages or smaller tiles). An op without an eager_fn could not run anywhere, so for it
registration fails with the same explanation instead. For cutedsl_op the
count also includes the kernel's static shared memory, which shares the
same limit, as the CUDA driver reports it when registration loads the PTX.
Kernels at or below 48 KiB are unchanged.

ptx_op and cuda_kernel_op get their launch parameters from an aot_fn, which
TensorRT calls only while it builds the engine. The AOT launch that every
frontend shares now checks the shared_mem it returns there, plus the
kernel's static shared memory as the CUDA driver reports it. A constant
total above 48 KiB fails the engine build, and the build log names the
limit and pytorch#5010, instead of building an engine that fails on every run.
When the driver cannot report the static size, for example without
cuda-python, the check warns that it counts the dynamic memory only, and
the next build reads the size again.
For a custom tactic it only logs a warning, because TensorRT can drop a
tactic whose launch fails and build with another one. A lone custom tactic
is not dropped, so that case still builds and then fails when it runs.

Signed-off-by: Anthony Shoumikhin <[email protected]>
A Triton kernel built for sm_100 or sm_110 keeps its matmul accumulator in
tensor memory. triton_op refused any such kernel, on the belief that a
TensorRT plugin launch cannot set that memory up.

A plugin launch does not need to set it up: the kernel allocates its tensor
memory itself with tcgen05.alloc. Triton's own loader only checks the request
against the tensor memory an SM has: 576 columns on sm_107 and 512 elsewhere.
Accept up to that and keep refusing anything larger, with a message that names
the limit.

Tested: tests/py/kernels/test_triton_op.py, including a triton_op registration
of a kernel with tensor memory, compiled for sm_100 on another GPU and small
enough for the 48 KiB limit; the new and changed tests fail without the
change. A grouped GEMM kernel built with tcgen05 this way ran inside a
TensorRT engine on Jetson Thor and matched the reference.

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

When Triton launches a kernel the normal way, it looks at each pointer
argument and tells the compiler which ones are 16-byte aligned. That hint
lets the compiler use vector loads, and for 1- and 2-byte elements also
asynchronous copies and software pipelining.

triton_op compiles the kernel ahead of time, before any pointer exists,
so it never passes the hint, and every triton_op kernel gets scalar memory
accesses only. Such a kernel can run several times slower inside a
TensorRT engine than the same kernel launched by Triton.

triton_op now takes pointer_alignment, with the same meaning and default
as in cutedsl_op: the byte alignment the kernel may assume for every
pointer, or a mapping from pointer names to alignments. Any alignment
above 1 becomes Triton's divisibility hint for that pointer, capped at 16
as Triton's own launcher does, and names that are not pointer parameters
are rejected. The default of 1 adds no hint, so existing registrations
compile exactly as before. Scalar arguments still get no hint, unlike in
Triton's launcher, so an access masked by a runtime size can stay scalar.
The declared alignment must hold for every pointer the kernel gets, and
a misaligned vector access is a sticky CUDA error. TensorRT does not document an
alignment for the buffers it allocates inside the engine, though in my
tests, including the engine test where TensorRT layers surround the
kernel, they were always aligned. TensorRT binds engine inputs as given,
so an offset view whose byte offset is not a multiple of the alignment
does not meet it. Values must be powers of two from 1 to 256. Both
frontends now share one pointer_alignment parser. Pipelining can take a
kernel over the 48 KiB shared memory limit, so the advice for that case
now also names pointer_alignment, and the registration log shows the
alignments in use.

Signed-off-by: Anthony Shoumikhin <[email protected]>
@shoumikhin
shoumikhin force-pushed the kernels-48kib-smem-limit branch from 6cc5d35 to 9ddd404 Compare November 21, 2026 12:53
@shoumikhin
shoumikhin force-pushed the kernels-48kib-smem-limit branch from 9ddd404 to 26cc2f8 Compare November 24, 2026 20:31

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