How flagos-ai/libtriton_jit works
Purpose and Entry Points
libtriton_jit is a C++20 runtime that invokes Python-defined Triton JIT kernels from C++/libtorch code, reducing Python wrapper, JIT-dispatch, and launch overhead. Its primary API is TritonJITFunction::get_instance(...); operator()(...), declared in include/triton_jit/triton_jit_function.h and implemented in src/triton_jit_function.cpp. Consumer-facing kernel definitions pair Python Triton source with C++ Torch operators, e.g. operators/pointwise/add/add.py and operators/pointwise/add/add_op.cpp.
Runtime Specialization and Kernel Cache
A call to TritonJITFunction captures typed C++ arguments through a variadic template. It combines call-site types, tensor metadata, constexpr values, and backend specialization rules into a full string signature. This determines the compiled-kernel variant and its per-function in-memory cache. The signature/argument utilities and metadata boundary live in include/triton_jit/jit_utils.h, include/triton_jit/kernel_metadata.h, and src/kernel_metadata.cpp.
Python/Triton Compilation Boundary
On a cache miss, the C++ runtime executes scripts/standalone_compile.py, which imports the user’s Python module and delegates compilation to Triton’s Python compiler. The returned compiled artifact path is then loaded by the runtime. Python kernel modules must be directly importable, so relative imports are not viable. This is the key C++/embedded-Python boundary; Triton’s persistent CacheManager owns compiler-wide artifact caching.
Backend-Agnostic Kernel Lifecycle
TritonKernel provides the backend-neutral orchestration: load compiled artifacts, prepare launch state, and launch after compiler-only arguments have been filtered from runtime arguments. Its interface is in include/triton_jit/triton_kernel.h; common policy requirements are defined by include/triton_jit/backend_policy.h. Backend selection is compile-time, rather than a runtime plugin system.
Hardware Backend Policies
Each supported accelerator implements the policy contract for driver/API-specific loading and launching: CUDA, MUSA, NPU/ACL, IX, MACA, MLU, GCU, and HIP-compatible HCU. Representative definitions are include/triton_jit/backends/cuda_backend.h, include/triton_jit/backends/npu_backend.h, and include/triton_jit/backends/hcu_backend.h. NPU-specific ABI argument packing is separated in include/triton_jit/backends/npu_arg_buffer.h. Adding a backend means supplying a conforming policy and CMake integration.
Torch Operator Integration
Operators preprocess Torch tensors, allocate/result-shape outputs, establish device/stream context, invoke Triton kernels, and register callable operations with PyTorch’s dispatcher. Shared operator registration and backend dispatch support are in operators/common/op_registration.h and operators/common/backend_ops.h. Operator families—pointwise, matmul, reduction, indexing, normalization, and fused model kernels—are composed through operators/CMakeLists.txt, with each implementation maintaining a Python kernel plus C++ wrapper boundary.
Build, Distribution, and Dependencies
CMake selects one backend and links Python, libtorch, pybind11, Triton-adjacent dependencies, and vendor SDKs via the root CMakeLists.txt, src/CMakeLists.txt, and backend modules such as cmake/BackendCUDA.cmake. The deliverable is a C++ library with installed headers, compile script, and CMake package metadata. Debian/RPM packaging and CI publish paths are represented by packaging/debian/rules, packaging/rpm/libtriton-jit.spec, and .github/workflows/upload-nexus.yml.
Main entry points: backend_policy.h Backend policy contract · CMakeLists.txt CMake build · triton_jit_function.cpp Function runtime · add_op.cpp Add Torch wrapper
Components 21
Consumer API
- C++ / libtorch consumer
JIT Runtime
- TritonJITFunction · Typed call capture, specialization, and per-function variant cache.
include/triton_jit/triton_jit_function.h - Function runtime
src/triton_jit_function.cpp - Signature & metadata · Builds signatures from types, tensors, constexpr values, and backend rules.
include/triton_jit/jit_utils.h - Kernel metadata
include/triton_jit/kernel_metadata.h - Metadata implementation
src/kernel_metadata.cpp - TritonKernel · Loads artifacts, filters compiler-only arguments, prepares and launches.
include/triton_jit/triton_kernel.h
Python Compilation
- Standalone compiler · Imports the user module and invokes Triton's Python compiler.
scripts/standalone_compile.py - Python Triton kernel · Must be directly importable by the compiler process.
operators/pointwise/add/add.py - Triton CacheManager · Owns compiler-wide compiled-artifact caching.
Backend Policies
- Backend policy contract
include/triton_jit/backend_policy.h - CUDA policy
include/triton_jit/backends/cuda_backend.h - NPU / ACL policy
include/triton_jit/backends/npu_backend.h - NPU argument buffer
include/triton_jit/backends/npu_arg_buffer.h - HCU / HIP policy
include/triton_jit/backends/hcu_backend.h
Torch Operators
- Add Torch wrapper · Prepares tensors and invokes the specialized kernel.
operators/pointwise/add/add_op.cpp - Operator registration
operators/common/op_registration.h - Operator families · Builds pointwise, matmul, reduction, indexing, normalization, and fused kernels.
operators/CMakeLists.txt
Build & Distribution
- CMake build · Selects one backend and links Python, libtorch, pybind11, Triton dependencies, and SDKs.
CMakeLists.txt - CUDA CMake module
cmake/BackendCUDA.cmake - Package publishing
.github/workflows/upload-nexus.yml
Connections 22
- C++ / libtorch consumer → TritonJITFunction: calls
- TritonJITFunction → Function runtime: implements
- Function runtime → Signature & metadata: specializes from arguments
- Signature & metadata → Kernel metadata: uses metadata
- Kernel metadata → Metadata implementation: implemented by
- Function runtime → Standalone compiler: cache miss compiles
- Standalone compiler → Python Triton kernel: imports
- Standalone compiler → Triton CacheManager: reads and writes artifacts
- Function runtime → TritonKernel: loads cached variant
- TritonKernel → Backend policy contract: orchestrates through
- Backend policy contract → CUDA policy: implemented by
- Backend policy contract → NPU / ACL policy: implemented by
- Backend policy contract → HCU / HIP policy: implemented by
- NPU / ACL policy → NPU argument buffer: packs ABI arguments
- Add Torch wrapper → TritonJITFunction: invokes
- Add Torch wrapper → Python Triton kernel: pairs with
- Add Torch wrapper → Operator registration: registers
- Operator families → Add Torch wrapper: builds
- CMake build → Operator families: includes
- CMake build → CUDA CMake module: selects backend integration
- CMake build → Backend policy contract: chooses implementation at compile time
- CMake build → Package publishing: produces publishable artifacts