Skip to main content
TensorRT-LLM allows you to write custom CUDA kernels to optimize specific operations for your use case.

Overview

Custom kernels can be integrated in two ways:
  1. Custom Operations: PyTorch custom ops for the PyTorch backend
  2. TensorRT Plugins: Native TensorRT plugins for the TensorRT backend

Custom PyTorch Operations

For the PyTorch backend, use PyTorch’s custom op registration:

Example: Custom Attention Kernel

Integration with TensorRT-LLM

Add your custom op to the auto_deploy custom_ops directory:
Register the operation:

TensorRT Plugins (Legacy)

For the TensorRT backend, implement a TensorRT plugin:

Performance Considerations

Ensure memory accesses are coalesced for maximum bandwidth:
Use shared memory for frequently accessed data:
Maximize occupancy by balancing register usage and thread blocks:

Testing Custom Kernels

Examples in TensorRT-LLM

Study existing custom ops:
  • tensorrt_llm/_torch/custom_ops/fused_moe/ - Fused MoE kernel
  • tensorrt_llm/_torch/cuda_tile_kernels/ - CUDA tile kernels
  • tensorrt_llm/kernels/ - C++ CUDA kernels

Next Steps

Profiling

Profile your custom kernels

Optimization Guide

Optimize kernel performance