Skip to content

Runtime: check engine input alignment before binding, so a misaligned input cannot crash an aligned plugin kernel #5086

Open

Description

@shoumikhin

triton_op and cutedsl_op take a pointer_alignment argument. When it is above 1, the kernel is compiled to assume that every pointer it gets is aligned to that many bytes, so it can use wide vector loads. If a pointer is not aligned, the kernel fails with cudaErrorMisalignedAddress. That error is sticky: the CUDA context of the whole process stays broken afterwards.

Nothing checks this before the launch today. Inside the engine it is fine: in my tests on TensorRT 11.4, the plugin always got aligned buffers, also next to slices and concatenations. The problem is engine inputs. The runtime binds a contiguous input at the address it already has, so an offset view reaches the kernel as it is. Two ways this happens:

# op is a triton_op registered with pointer_alignment=16, and model.forward(x) returns op(x)

# 1. The caller passes an offset view.
trt = torch_tensorrt.compile(model, inputs=[torch.randn(4, 1024, device="cuda")], min_block_size=1)
buf = torch.randn(4 * 1024 + 8, device="cuda")
trt(buf[1 : 1 + 4 * 1024].view(4, 1024))  # CUDA error: misaligned address

# 2. A PyTorch op before the engine makes one, with no help from the caller.
class M(torch.nn.Module):
    def forward(self, x):
        return op(x[1:].reshape(4, 1024))

x = torch.randn(4 * 1024 + 1, device="cuda")
trt = torch_tensorrt.compile(
    M(), inputs=[x], min_block_size=1, torch_executed_ops={"torch.ops.aten.slice.Tensor"}
)
trt(x)  # the slice runs in PyTorch, its output is the engine input: misaligned address

Both run correctly with pointer_alignment=1. Case 1 also crashes when the input goes through a reshape inside the engine before the plugin, because TensorRT passes the same buffer through. With CUDA graphs enabled it runs correctly, because the runtime copies inputs into its own buffers.

The check cannot go in the plugin. TensorRT launches the precompiled kernel itself, and no Python code runs per call. It has to go in the runtime, where engine inputs are bound.

TensorRT already documents that I/O tensor addresses must be 256-byte aligned (setTensorAddress), but it does not enforce it: the engine above accepted an address 4 bytes off. So the runtime can enforce that rule for every engine input, with no plugin metadata:

  1. In both the C++ and the Python runtime, after an input is made contiguous, check data_ptr() % 256. This costs almost nothing per call.
  2. If it is not aligned, copy the input into a fresh buffer and bind that. Only inputs that already break TensorRT's rule pay for the copy.
  3. An input the engine writes in place (an aliased input) cannot be copied. Today an offset view of such a buffer, for example a slice of a cache, can work. So this case needs a decision: raise a clear error that names the input, or only raise when the engine contains an aligned plugin.

Recording, per plugin, which engine inputs reach it unchanged is not a good alternative: TensorRT decides which layers pass a buffer through, so the converter cannot know it reliably.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions