Bridge the gap between PyTorch Triton research and high-throughput C++ inference engines.
Automated Dual-Pass Tracing • SymPy AST Grid Transpilation • Zero-Copy VRAM IO-Binding • Pure C++ Plugin Synthesis
KernelLens is an open-source compiler framework designed to transform PyTorch models containing custom @triton.jit kernels into standalone, high-performance C++ shared libraries (.so).
It generates native ONNX Runtime (OrtCustomOp) and NVIDIA TensorRT 10.x (nvinfer1::IPluginV2DynamicExt) plugins automatically—eliminating thousands of lines of C++ boilerplate, manual nvcc build scripts and host-side memory copies.
+-----------------------------------------------------------------------------------+
| KernelLens Pipeline Flow |
+-----------------------------------------------------------------------------------+
| PyTorch Module (@triton.jit) |
| │ |
| ▼ |
| Phase 1: Dual-Pass Tracing & AST Inspection (Intercept PTX, Smem, Grids) |
| │ |
| ▼ |
| Phase 2: Base ONNX Graph Transformation (Inject Custom Domain Nodes) |
| │ |
| ▼ |
| Phase 3: Automated C++/CUDA Code Synthesis (Generate OrtCustomOp & TRT Plugins) |
| │ |
| ▼ |
| Phase 4: Zero-Copy C++ Runtime Execution (Direct VRAM Pointer Binding) |
+-----------------------------------------------------------------------------------+
-
Zero C++ Boilerplate: Automatically inspects Triton kernel signatures and synthesizes production-ready C++ plugin code (
.cu,.cpp,.h) and compiled shared libraries (.so). -
SymPy Dynamic Grid AST Transpilation: Parses Triton grid launchers (e.g.,
triton.cdiv(M, BLOCK_SIZE)) into pure C++ integer arithmetic evaluated dynamically at inference time ($< 0.5 \ \mu\text{s}$ CPU overhead). -
Zero-Copy VRAM Pointer Mapping: Directly binds PyTorch / C++ VRAM memory addresses (
data_ptr()) into ONNX Runtime and TensorRT execution contexts, achieving$O(1)$ allocation overhead. -
16-Byte Memory Alignment Guards: Automatically injects dynamic 16-byte address check guards in C++ to prevent illegal unaligned vector load traps (
LDG.128) on sub-aligned tensor slices. -
Toolchain & Hardware Hardening: Features automatic PTX ISA version clamping (
.version 9.3$\rightarrow$ .version 9.0) for NVIDIA Blackwell (sm_120a) GPU driver compatibility. -
FP8 & Sub-Byte (INT4) Support: Native 1-byte pointer mapping (
GetTensorData<uint8_t>()) for quantized Tensor Core acceleration. -
Cold-Start Disk Caching: Caches compiled C++ dynamic libraries (
.so) and TensorRT engines (.engine) for instant cold-start loading in production.
KernelLens can be installed directly from PyPI:
# Core compiler (FX Graph tracing & C++ code synthesis)
pip install kernel-lens
# Install with target inference backends
pip install kernel-lens[ort] # ONNX Runtime support
pip install kernel-lens[trt] # NVIDIA TensorRT support
pip install kernel-lens[all] # Full backend suite- OS: Linux (x86_64)
- Python: 3.10+
- CUDA Toolkit: 12.0+
- PyTorch: 2.0+
- Triton: 2.1+
Take any standard PyTorch nn.Module with a custom @triton.jit kernel and compile it for production in 3 lines of Python:
import torch
import torch.nn as nn
import triton
import triton.language as tl
import kernel_lens as kl
# 1. Define a custom Triton kernel and Module
@triton.jit
def rmsnorm_kernel(x_ptr, weight_ptr, out_ptr, stride_x_b, stride_x_m, stride_out_b, stride_out_m, N: tl.constexpr, eps: tl.constexpr, BLOCK_SIZE: tl.constexpr):
pid_b = tl.program_id(0)
pid_m = tl.program_id(1)
row_x = x_ptr + pid_b * stride_x_b + pid_m * stride_x_m
row_out = out_ptr + pid_b * stride_out_b + pid_m * stride_out_m
cols = tl.arange(0, BLOCK_SIZE)
mask = cols < N
x = tl.load(row_x + cols, mask=mask, other=0.0)
w = tl.load(weight_ptr + cols, mask=mask, other=1.0)
var = tl.sum(x * x, axis=0) / N
rsqrt = tl.rsqrt(var + eps)
tl.store(row_out + cols, x * rsqrt * w, mask=mask)
class RMSNormModule(nn.Module):
def __init__(self, dim: int):
super().__init__()
self.weight = nn.Parameter(torch.ones(dim, dtype=torch.float32))
def forward(self, x: torch.Tensor):
B, M, N = x.shape
out = torch.empty_like(x)
rmsnorm_kernel[(B, M)](x, self.weight, out, x.stride(0), x.stride(1), out.stride(0), out.stride(1), N=N, eps=1e-6, BLOCK_SIZE=triton.next_power_of_2(N))
return out
# 2. Compile to native C++ backends
x = torch.randn(4, 512, 4096, device="cuda", dtype=torch.float32)
model = RMSNormModule(4096).cuda()
compiled_model = kl.compile(
model,
(x,),
name="llama3_rmsnorm",
backends=["onnx", "tensorrt"]
)
# 3. Execute zero-copy inference in pure C++ engines
ort_output = compiled_model.run((x,), backend="onnx")
trt_output = compiled_model.run((x,), backend="tensorrt")Evaluated on NVIDIA GPU across state-of-the-art LLM operators:
| Operator / Workload | PyTorch Eager | torch.compile |
Native Triton | KernelLens TRT | KernelLens ORT | Speedup vs Eager |
|---|---|---|---|---|---|---|
| Mamba SSM Recurrence ( |
||||||
| Fused Cross-Entropy ( |
||||||
| LLaMA 3 RMSNorm ( |
||||||
| PaLM SwiGLU Activation ( |
||||||
| Qwen 2.5 RoPE Embedding ( |
All runs achieve exact numerical parity against native Triton execution.
KernelLens Compiler Pipeline
[ PyTorch Model ] ──► Dual-Pass Tracing ──► AST Grid Inspection ──► Manifest Builder
│
[ C++ Exec Engine ] ◄── Dynamic Plugin Load ◄── nvcc / g++ Build ◄───────┴── Synthesized C++/CUDA Code
- Phase 1: Dual-Pass Tracing: Intercepts eager Triton launches to record PTX strings, shared memory bytes and scalar arguments. A secondary PyTorch FX proxy pass captures dynamic grid expressions.
- Phase 2: Base ONNX Graph Transformation: Injects custom domain nodes (
triton_custom::<kernel_name>) and binds CPU scalar parameters (OrtMemTypeCPUInput). - Phase 3: Code Synthesis & Toolchain Hardening: Emits complete C++ source files implementing
Ort::CustomOpandnvinfer1::IPluginV2DynamicExt. Injects 16-byte alignment guards and applies PTX version clamping. - Phase 4: Zero-Copy Runtime Execution: Dynamically loads shared libraries via
ctypes.CDLLand binds GPU VRAM pointers directly to execution contexts.
Once compiled, KernelLens persists plugins to disk. Reload existing engines instantly without recompilation:
# Load previously compiled C++ plugins directly from disk
engine = kl.load("llama3_rmsnorm")
output = engine.run((x,), backend="tensorrt")Enable verbose tripwire logging to inspect generated C++ grid calculations and GPU memory addresses before cuLaunchKernel:
KERNEL_LENS_DEBUG=1 python my_inference_script.pyFollow these steps to reproduce the empirical benchmarks and verify system correctness:
Clone the repository and install dependencies in editable mode:
git clone https://github.com/Just1truc/kernel_lens.git
cd kernel_lens
pip install -e .[all]Run the research model operator test suite evaluating LLaMA 3 RMSNorm, SwiGLU, RoPE and Liger-Kernel Fused Cross-Entropy:
python3 tests/test_research_kernels.pyExpected Output:
[Passed] Llama 3 RMSNorm Kernel (MaxDiff: 0.000000e+00)
[Passed] SwiGLU Fused Activation Kernel (MaxDiff: 0.000000e+00)
[Passed] RoPE Positional Embedding Kernel (MaxDiff: 0.000000e+00)
[Passed] Fused Softmax & Cross Entropy Loss Kernel (MaxDiff: 0.000000e+00)
ALL RECENT RESEARCH TRITON KERNELS PASSED WITH KERNEL LENS!
Run the 4-layer LLaMA Transformer decoder pipeline benchmark comparing PyTorch Eager, torch.compile, ONNX Runtime and TensorRT 10.x C++ plugins:
python3 tests/benchmark_e2e_llama.pyExpected Output:
--- MEASURED REAL EMPIRICAL END-TO-END RESULTS ---
Configuration | TTFT (S=512) | ITL (S=1) | Total (128 tok) | Peak VRAM
-----------------------------------------------------------------------------------------------
PyTorch Eager + Triton | 42.77 ms | 2.68 ms | 0.384 s | 453.2 MB
torch.compile (Inductor) | 41.81 ms | 2.31 ms | 0.335 s | 451.2 MB
KernelLens C++ Plugins (ORT) | 45.38 ms | 2.97 ms | 0.423 s | 440.3 MB
KernelLens TensorRT 10.x Plugin | 43.90 ms | 2.23 ms | 0.328 s | 440.3 MB
Saved real end-to-end benchmark results to measured_e2e_llama.json
Verify dynamic scalar parameters, 4D high-rank launch grids and nested stores:
python3 tests/test_architectural_fixes.py
python3 tests/test_edge_cases.pyExpected Output:
ALL ARCHITECTURAL FIX VERIFICATION TESTS PASSED SUCCESSFULLY!
Verify clean PDF generation of the 19-page research paper:
pdflatex -interaction=nonstopmode architecture_report.texExpected Output: Output written on architecture_report.pdf (19 pages)
If you use KernelLens in your research, please cite our publication:
Duc, J. (2026). KernelLens: Deploying Triton Kernels to C++ Inference Engines (Version 1.1.9). Galiad Research. https://doi.org/10.5281/zenodo.23194523
@article{duc2026kernellens,
title={KernelLens: Deploying Triton Kernels to C++ Inference Engines},
author={Duc, Justin},
institution={Galiad Research},
year={2026},
version={1.1.9},
doi={10.5281/zenodo.23194523},
url={https://doi.org/10.5281/zenodo.23194523}
}KernelLens is open-sourced under the MIT License.