feat: TorchTRT Annotation Layer for Cuda generated kernels#4199
feat: TorchTRT Annotation Layer for Cuda generated kernels#4199
Conversation
d3a0651 to
abaaf96
Compare
b41c684 to
3b4dc2b
Compare
narendasan
left a comment
There was a problem hiding this comment.
Cool I think this is getting really close. I think we just have a few naming things to make this more user friendly and I think we should let users provide PTX directly in addition to the cuda apis. Also did you add nvrtc as an optional dependency in the pyproject.toml) (maybe under an a extras called kernels)?
| @@ -0,0 +1,156 @@ | |||
| .. _torch_tensorrt_annotation_py: | |||
|
|
|||
| torch_tensorrt.annotation | |||
There was a problem hiding this comment.
I think this should be called torch_tensorrt.kernels
There was a problem hiding this comment.
Hey @narendasan should we just deprecate the naming related to annotation? I'm a little bit confused here since this annotation is really opaque to the users and there is no annotation module right now
| return params, extra | ||
|
|
||
|
|
||
| @tta.manual_cuda_kernel_plugin( |
There was a problem hiding this comment.
What is the distinction between manual and auto from a user perspective?
There was a problem hiding this comment.
To me it just seems like the function bodies are just manually configured here, why not just support additional kwargs in the same api?
There was a problem hiding this comment.
I’m planning to present how to manually write a CUDA kernel plugin. We have the option to completely remove this manual approach and rely entirely on automatic generation so users don't have to worry about it.
However, doing so means users lose control over launch functions and configurations. In this specific case, automatic generation doubles the number of launching threads, which could lead to memory inefficiencies and limit our ability to support dynamic cases.
@narendasan – We can drop the manual options for the sake of simplicity, but it comes at the cost of efficiency and flexibility. Which approach do you prefer?
| ) | ||
|
|
||
|
|
||
| def custom_plugin( |
There was a problem hiding this comment.
there is now a torch_tensorrt.annotations(kernels).custom_plugin and a torch_tensorrt.dynamo.conversion.plugins.custom_op. Why cant we just centralize on one?
There was a problem hiding this comment.
Or make it clear what is used for what by disambiguating the names
There was a problem hiding this comment.
@narendasan we could do that by just removing the annotation related stuff for now. I'm just putting that in the annotation in case the TensorRT team wants to develop some annotation related stuff in the future. Do we need to keep that stub for them now?
| # Numel("x") pass x.numel() to the kernel as an int extra. | ||
| # Elementwise(flat) 1-D launch over the flattened output; any input rank works. | ||
|
|
||
| tta.auto_cuda_kernel_plugin( |
There was a problem hiding this comment.
maybe we can call this something like torch_tensorrt.kernels.cuda_kernel_op
| aot_fn=_aot_repeat2, | ||
| supports_dynamic_shapes=True, | ||
| ) | ||
| def _repeat2_meta(x: torch.Tensor) -> torch.Tensor: |
There was a problem hiding this comment.
Why is the meta kernel the one that we decorate? to me the obvious thing to decorate is the jit_impl_fn
There was a problem hiding this comment.
I think the manual api as a decorator is somewhat confusing, imo we either do the workflow that we already have with multiple decorators (https://docs.pytorch.org/TensorRT/tutorials/_rendered_examples/dynamo/nvrtc_aot_plugin.html) or we dont decorate anything and just have a function that takes all of kernel source, meta, jit and aot as argument
| return | ||
| from torch_tensorrt.annotation._custom_plugin._nvrtc import compile_to_ptx | ||
|
|
||
| _ptx, device, kernel = compile_to_ptx( |
There was a problem hiding this comment.
Could we also expose a torch_tensorrt.kernels.ptx_op, that just takes externally created valid ptx?
Description
This PR introduces torch_tensorrt.annotation, an experimental module for registering hand-written CUDA C++ kernels as both PyTorch custom ops (for eager execution) and TensorRT Quick Deployable Plugins with AOT support (for torch_tensorrt.compile).
Usage
After this call, torch.ops.ann_ex.sigmoid is available in eager and is embedded as a TensorRT plugin during torch_tensorrt.compile. The meta function, eager
launch, AOT implementation, and PyTorch schema are all derived from the KernelSpec.
API Surface
The module exposes two primary entry points, layered by declarativeness:
auto_cuda_kernel_plugin is the recommended default. The caller supplies a KernelSpec dataclass describing the kernel's inputs, outputs (with a shape relation such
as SameAs or ReduceDims), scalar extras (Numel, DimSize), and launch geometry (Elementwise or Reduction). The framework derives the meta function, eager CUDA
launch, TensorRT AOT implementation, and PyTorch schema. This path covers pointwise kernels (1-D flat or N-D grid launches), reductions (with optional keepdim),
multi-input kernels, and scalar (non-tensor) kernel arguments via ScalarInput.
manual_cuda_kernel_plugin is the lower-level alternative for kernels outside the declarative DSL — shape-changing outputs, multi-output kernels, or non-standard
launch geometries. The caller provides eager_fn and aot_fn directly; the decorator still registers the PyTorch op, TRT plugin, AOT implementation, and converter
in a single call.
A Custom(fn=...) geometry is also available for callers who want the declarative path's schema/meta derivation but need to hand-write the TRT KernelLaunchParams.
Type of change
Checklist: