Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
trtx-rs
⚠️ EXPERIMENTAL - NOT FOR PRODUCTION USE
This project is in early experimental development. The API is unstable and will change. This is NOT production-ready software. Use at your own risk.
Published on crates.io to reserve the crate names.
Safe Rust bindings to NVIDIA TensorRT-RTX for high-performance deep learning inference.
Overview
This project provides ergonomic Rust bindings to TensorRT-RTX, enabling efficient inference of deep learning models on NVIDIA GPUs with minimal overhead.
Features
- Safe API: RAII-based memory management and type-safe abstractions
- Two-phase workflow: Separate build (AOT) and inference (runtime) phases
- Zero-cost abstractions: Minimal overhead over C++ API
- Comprehensive error handling: Proper Rust error types for all operations
- Flexible logging: Customizable log handlers for TensorRT messages
Project Structure
trtx-rs/
├── trtx-sys/ # Raw FFI bindings (unsafe)
└── trtx/ # Safe Rust wrapper (use this!)
Prerequisites
Required
- NVIDIA TensorRT-RTX: Download and install from NVIDIA Developer
- CUDA Runtime: Version compatible with your TensorRT-RTX installation
- NVIDIA GPU: Compatible with TensorRT-RTX requirements
Environment Setup
Set the installation path if TensorRT-RTX is not in a standard location:
Development Without TensorRT-RTX (Mock Mode)
If you're developing on a machine without TensorRT-RTX (e.g., macOS, or for testing), you can use the mock feature:
# Build with mock mode
# Run examples with mock mode
# Run tests with mock mode
Mock mode provides stub implementations that allow you to:
- Verify the API compiles correctly
- Test your application structure
- Develop without needing an NVIDIA GPU
- Run CI/CD pipelines on any platform
Note: Mock mode only validates structure and API usage. For actual inference, you need real TensorRT-RTX.
Installation
Add to your Cargo.toml:
[]
= "0.1"
Usage
Build Phase (Creating an Engine)
use ;
use ;
Inference Phase (Running Inference)
use ;
use fs;
Custom Logging
use ;
;
API Overview
Core Types
Logger: Captures TensorRT messages with custom handlersBuilder: Creates optimized inference enginesNetworkDefinition: Defines the computational graphBuilderConfig: Configures optimization parametersRuntime: Deserializes engines for inferenceCudaEngine: Optimized inference engineExecutionContext: Manages inference execution
Error Handling
All fallible operations return Result<T, Error>:
use Error;
match builder.create_network
Safety
Safe Operations
Most operations are safe and use RAII for resource management:
- Creating loggers, builders, runtimes
- Building and serializing engines
- Deserializing engines
- Creating execution contexts
Unsafe Operations
CUDA-related operations require unsafe:
set_tensor_address: Must point to valid CUDA device memoryenqueue_v3: Requires valid CUDA stream and properly bound tensors
Building from Source
# Clone the repository
# Option 1: Build with TensorRT-RTX (requires NVIDIA GPU)
# Option 2: Build in mock mode (no GPU required)
Examples
See the trtx/examples/ directory for complete examples:
basic_build.rs: Building an engine from scratchinference.rs: Running inference with a pre-built engine
Architecture
trtx-sys (FFI Layer)
- Raw
bindgen-generated bindings - C wrapper functions for exception handling
- No safety guarantees
- Internal use only
trtx (Safe Wrapper)
- RAII-based resource management
- Type-safe API
- Lifetime tracking
- Comprehensive error handling
- User-facing API
Troubleshooting
Build Errors
Cannot find TensorRT headers:
Linking errors:
Runtime Errors
CUDA not initialized: Ensure CUDA runtime is properly initialized before creating engines or contexts.
Invalid tensor addresses: Verify that all tensor addresses point to valid CUDA device memory with correct sizes.
Development
Pre-commit Hooks
To ensure code quality, set up the pre-commit hook:
The hook will automatically run cargo fmt and cargo clippy before each commit.
Manual Checks
You can also run checks manually using the Makefile:
GPU Testing
The project includes CI workflows for testing with real NVIDIA GPUs:
- Mock mode CI: Runs on every push (ubuntu, macos) - tests API without GPU
- GPU tests: Runs on self-hosted Windows runner with T4 GPU - tests real TensorRT-RTX
To set up a GPU runner for real hardware testing, see GPU Runner Setup Guide.
The GPU tests workflow:
- Builds without mock mode (uses real TensorRT-RTX)
- Verifies CUDA and GPU availability
- Runs tests and examples with actual GPU acceleration
- Can be triggered manually or runs automatically on code changes
Contributing
Contributions are welcome! Please see DESIGN.md for architecture details.
License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Acknowledgments
- NVIDIA for TensorRT-RTX
- The Rust community for excellent FFI tools
Status
This project is in early development. APIs may change before 1.0 release.
Implemented
- ✅ Core FFI layer with mock mode support
- ✅ Logger interface with custom handlers
- ✅ Builder API for engine creation
- ✅ Runtime and engine deserialization
- ✅ Execution context
- ✅ Error handling with detailed messages
- ✅ ONNX parser bindings (nvonnxparser integration)
- ✅ CUDA memory management (malloc, memcpy, free wrappers)
- ✅ rustnn-compatible executor API (ready for integration)
- ✅ RAII-based resource management
Planned
- ⬜ Dynamic shape support
- ⬜ Optimization profiles
- ⬜ Weight refitting
- ⬜ INT8 quantization support
- ⬜ Comprehensive examples with real models
- ⬜ Performance benchmarking
- ⬜ Documentation improvements