Skip to main content
Python plugins allow you to define custom TensorRT operations using Python, which are then compiled into the TensorRT engine.
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

Keep computations on GPU to avoid CPU-GPU transfers:
Use workspace for temporary buffers:
Annotate types for better error messages:

Plugin Examples

Custom Activation Function

Fused Operations

Debugging Plugins

Enable plugin debugging:
View plugin calls during inference:

Limitations

Performance: Python plugins may be slower than native C++ plugins due to Python overhead.Compatibility: Python plugins are only supported with the TensorRT backend.Deployment: Python plugins require the Python runtime at inference time.

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