A source translator for kernels written against the Triton API to CUDA C++
Project description
tritoncu
A source translator for kernels written against the Triton API to CUDA C++. No GPU required at translation time. Generated .cu files are readable, compilable with NVCC 12.5.1 and embeddable in any CUDA project.
Tested on an NVIDIA Tesla T4 cloud GPU with the following configurations.
- Compute Capability: 7.5
- Max Threads per Block: 1024
- Max Grid Size: 2147483647 x 65535 x 65535
- Shared Memory per Block: 48 KB
- Total Global Memory: 14.56 GB
- Memory Clock Rate: 5.00 GHz
- Memory Bus Width: 256 bits
- Warp Size: 32
Refer to the tests for various kernels.
Tile-to-thread semantic translation
Triton programs operate over tiles, each kernel body sees an entire vector or matrix as if it were a single value. CUDA assigns one thread per scalar. tritoncu resolves this by allocating each tile as a stack array on the single CUDA thread mapped to that block, then expanding every tiled operation into an explicit for loop.
| Tile-based model | CUDA threading model |
|---|---|
tl.program_id(axis=0) |
blockIdx.x |
tl.arange(0, BLOCK_SIZE) |
int32_t arr[BLOCK_SIZE]; for (int i=0; i<BLOCK_SIZE; ++i) arr[i] = i; |
Tile variable x[BLOCK_SIZE] |
Stack array float x[BLOCK_SIZE] per thread |
tl.load(ptr + offsets, mask=m) |
Loop: out[i] = m[i] ? ((float*)ptr)[i] : other |
tl.store(ptr + offsets, val, mask=m) |
Loop: if (m[i]) ((float*)ptr)[i] = val[i] |
Elementwise x + y (tile + tile) |
Loop: out[i] = x[i] + y[i] |
Elementwise x * scalar (tile + scalar) |
Loop: out[i] = x[i] * scalar |
tl.where(cond, x, y) |
Loop: out[i] = cond[i] ? x[i] : y |
tl.sum(x, axis=0) |
Loop: acc += x[i] |
tl.max(x, axis=0) |
Loop: acc = x[i] > acc ? x[i] : acc |
tl.dot(a, b, acc) |
Triply-nested loop: c[m][n] += a[m*K+k] * b[k*N+n] |
tl.exp(x), tl.sqrt(x), etc. |
Loop: out[i] = expf(x[i]) / sqrtf(x[i]) |
tl.atomic_add(ptr, val) |
atomicAdd(&ptr[i], val) |
tl.debug_barrier() |
__syncthreads() |
tl.constexpr param |
const int kernel parameter |
tl.float32 pointer annotation |
float* kernel parameter |
x[:, None], x[None, :] |
Scalar index extraction, None/slice dims stripped |
Installation
pip install tritoncu
pip install cupy-cuda12x # optional, for launching
Usage
import tritoncu
import tritoncu.language as tl
@tritoncu.jit
def add_kernel(x_ptr, y_ptr, out_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(axis=0)
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask, other=0.0)
y = tl.load(y_ptr + offsets, mask=mask, other=0.0)
tl.store(out_ptr + offsets, x + y, mask=mask)
handle = add_kernel.compile(meta={"BLOCK_SIZE": 1024})
handle.get_source()
handle.get_kernel_source()
handle.write_to_disk("./out", "add", write_header=True)
Launching with CuPy
add_kernel[grid](*args, BLOCK_SIZE=1024)
Multi-kernel builds
from tritoncu import CudaSourceBuilder
builder = CudaSourceBuilder()
for name, src in handle.builder.kernels.items():
builder.add_kernel(name, src)
builder.write_to_disk("./out", "kernels")
Testing
python3 test.py
License
Apache v2.0 License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tritoncu-1.0.0.tar.gz.
File metadata
- Download URL: tritoncu-1.0.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ff049b034114a9cf742e3c6a5c5571eb23b01a28e0ac3a6f860b94fd7f8c5e4
|
|
| MD5 |
aa4cb862a91d97659955702cb6a9da9e
|
|
| BLAKE2b-256 |
082bdb1b6031e561c9776338ad25f45610a0496748393bf0e789bd3df5dce475
|
File details
Details for the file tritoncu-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tritoncu-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c6b61228fed68cf5d5c21d98253948e2e82b037d77e715a2b9cffa55cf4e532
|
|
| MD5 |
8a725dbaab05690f38f9d76fa7c9e946
|
|
| BLAKE2b-256 |
7dcdf1fa06d6e26e99777a805e7b20ade1b678eb775f1721a106ca277d8a77f3
|