Python plugins are primarily used with the TensorRT backend. For the PyTorch backend, use custom CUDA kernels instead.
Overview
Python plugins provide a way to extend TensorRT-LLM with custom operations without writing C++ code.Creating a Python Plugin
Use the@python_plugin decorator from tensorrt_llm.plugin:
Plugin Registration
The@python_plugin decorator handles:
- Automatic registration with TensorRT plugin registry
- Type inference for inputs and outputs
- Shape inference during engine build
- Serialization for engine save/load
Advanced Plugin Example
Using Plugins in Models
Plugin Parameters
string
required
Unique name for the plugin
List[string]
required
List of output names
integer
default:"0"
Workspace memory size in bytes for intermediate calculations
string
default:"trtllm"
Plugin namespace for organization
string
default:"1"
Plugin version identifier
Shape Inference
Implement shape inference for dynamic shapes:Best Practices
Minimize Data Transfers
Minimize Data Transfers
Keep computations on GPU to avoid CPU-GPU transfers:
Workspace Management
Workspace Management
Use workspace for temporary buffers:
Type Safety
Type Safety
Annotate types for better error messages:
Plugin Examples
Custom Activation Function
Fused Operations
Debugging Plugins
Enable plugin debugging:Limitations
Migration to C++ Plugins
For production deployments, consider migrating to C++ plugins:Next Steps
Custom Kernels
Learn about CUDA kernel development
Model Architecture
Understand TensorRT-LLM architecture