Torch-TensorRT JIT Backend Guide
Torch-TensorRT JIT Backend Guide
The Torch-TensorRT JIT backend integrates TensorRT acceleration through torch.compile(backend="torch_tensorrt"). This provides a seamless JIT (Just-In-Time) compilation experience without needing intermediate model formats.
Overview
- JIT Compilation: Compiles at runtime using
torch.compile - No Intermediate Formats: No ONNX export or separate engine files
- PyTorch Native: Stays within the PyTorch ecosystem
- Dynamic Recompilation: Automatically recompiles on shape changes
- FP16 Support: Built-in mixed precision support
Quick Start
Configuration Options
TorchTensorRTJitBackendConfig
compile_config
TensorRT compilation settings from torch_tensorrt:
By default, the engine matches the model’s loaded dtype. To request FP16 kernels via the legacy weak-typing path (deprecated in TensorRT 10.12), pair enabled_precisions with use_explicit_typing=False:
Common options:
workspace_size: Maximum workspace memory in bytesenabled_precisions: Kernel dtype precisions TRT may pick (requiresuse_explicit_typing=False; legacy weak typing)use_explicit_typing: Respect graph dtypes (defaultTrue); setFalseto enable legacy weak typing
fullgraph
Require the entire function to be captured in a single graph.
Use cases:
False(default): Allow partial compilationTrue: Ensure complete compilation or fail
dynamic
Enable dynamic shape tracing:
Options:
True: Generate dynamic kernels up-frontFalse: Always specializeNone(default): Auto-detect and recompile
autocast_enabled
Enable automatic mixed precision:
JIT vs AOT Torch-TensorRT
Understanding JIT/AOT Terminology
It’s important to distinguish between two uses of “JIT” and “AOT” in AITune:
AITune Tuning Modes
-
Ahead-of-Time Tuning: The declarative approach using
inspect(),wrap(), andtune()- You explicitly select modules to tune
- Full control over the tuning process
- Works with any backend (JIT or AOT)
-
Just-in-Time Tuning: The automatic approach using environment variables or imports
- No code changes required
- AITune automatically discovers and tunes modules
- Works with any backend (JIT or AOT)
Torch-TensorRT Backend Types
-
TorchTensorRTJitBackend (this page): Uses
torch.compile(backend="torch_tensorrt")- Compiles at runtime on first inference
- Does not save compiled artifacts separately
- Recompiles automatically on shape changes
-
TorchTensorRTAotBackend: Uses
torch.export.export()andtorch_tensorrt.dynamo.compile()- Compiles during the
tune()call - Saves compiled model to disk
- Fixed compilation (no automatic recompilation)
- Compiles during the
Combining Them
You can use any combination:
Key Takeaway: AITune’s tuning mode (JIT/AOT) is independent from the backend type (JIT/AOT). Choose based on your needs:
- Tuning mode: How you want to control tuning (automatic vs explicit)
- Backend type: How the model gets compiled and stored (runtime vs saved)
Best Practices
- Use FP16: Enable FP16 for better performance
- Dynamic Shapes: Enable if input sizes vary frequently
- Fullgraph for Production: Use
fullgraph=Trueto catch issues early - Warmup: Run a few inference calls before benchmarking
Troubleshooting
Issue: Compilation fails
Solution: Try with partial compilation:
Issue: Slow first inference
Cause: JIT compilation happens on the first run.
Solution: This is expected. Subsequent inferences will be fast.
Next Steps
- Compare with Torch-TensorRT AOT Backend
- Learn about TensorRT Backend
- Explore Tune Strategies