jax-triton
The jax-triton repository contains integrations between JAX
and Triton, including support for Gluon dialect.
Documentation can be found here.
This is not an officially supported Google product.
Quickstart
The main function of interest is jax_triton.triton_call for applying Triton
functions to JAX arrays, including inside jax.jit-compiled functions. For
example, we can define a kernel from the Triton
tutorial:
import triton
import triton.language as tl
@triton.jit
def add_kernel(
x_ptr,
y_ptr,
length,
out_ptr, # out_shape pointers follow the inputs.
block_size: tl.constexpr, # constexpr params can go anywhere.
):
"""Adds two vectors output = x + y."""
pid = tl.program_id(axis=0)
block_start = pid * block_size
offsets = block_start + tl.arange(0, block_size)
mask = offsets < length
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
output = x + y
tl.store(out_ptr + offsets, output, mask=mask)
Then we can apply it to JAX arrays using jax_triton.triton_call:
import jax
import jax.numpy as jnp
import jax_triton as jt
def add(x: jax.Array, y: jax.Array) -> jax.Array:
block_size = 8
return jt.triton_call(
x,
y,
x.size,
kernel=add_kernel,
out_type=jax.typeof(x),
grid=(x.size // block_size,),
block_size=block_size)
x_val = jnp.arange(8)
y_val = jnp.arange(8, 16)
print(add(x_val, y_val))
print(jax.jit(add)(x_val, y_val))
One could also use in-out parameters for kernels by passing a read-write
Ref (created via jax.new_ref). The kernel mutates the Ref in place, so it
is not listed in out_shape:
@triton.jit
def add_inplace_y_kernel(
x_ptr, # input vector
y_inout_ptr, # explicit in-out vector (could be anywhere)
length,
block_size: tl.constexpr,
):
"""Adds two vectors in place: y = x + y."""
pid = tl.program_id(axis=0)
block_start = pid * block_size
offsets = block_start + tl.arange(0, block_size)
mask = offsets < length
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_inout_ptr + offsets, mask=mask)
output = x + y
tl.store(y_inout_ptr + offsets, output, mask=mask)
# jitting isn't mandatory, but makes invocation more efficient.
@jax.jit
def add_inplace_y(x: jax.Array, y_ref) -> None:
block_size = 8
jt.triton_call(
x,
y_ref, # read-write Ref argument, mutated in place
x.size,
kernel=add_inplace_y_kernel,
out_type=(), # no allocated outputs; the Ref is mutated in place
grid=(x.size // block_size,),
block_size=block_size)
x_val = jnp.arange(8)
y_ref = jax.new_ref(jnp.arange(8, 16))
add_inplace_y(x_val, y_ref)
print(y_ref[...])
See the examples directory, especially fused_attention.py and the fused attention ipynb.
Some other use-cases are also covered in tests.
Installation
$ pip install jax-triton
Make sure you have a CUDA- or ROCm- compatible jax installed. For example you
could run:
$ pip install "jax[cuda13]"
Development
To develop jax-triton, you can clone the repo with:
$ git clone https://github.com/jax-ml/jax-triton.git
and do an editable install with:
$ cd jax-triton
$ pip install -e .
To run the jax-triton tests, you'll need pytest:
$ pip install pytest
$ pytest tests/
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 jax_triton-0.4.0.tar.gz.
File metadata
- Download URL: jax_triton-0.4.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32577b14168b762b18fe1835995f6994ee3c2cb5e94fc9eae88b331197ac5ee3
|
|
| MD5 |
249c6cc404a5cf9c2d5a2400a681074c
|
|
| BLAKE2b-256 |
3d4ab418d92281313c30f176f34bfe56638c788f03eeafa5b15a28117daaae2f
|
Provenance
The following attestation bundles were made for jax_triton-0.4.0.tar.gz:
Publisher:
pypi-publish.yml on jax-ml/jax-triton
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jax_triton-0.4.0.tar.gz -
Subject digest:
32577b14168b762b18fe1835995f6994ee3c2cb5e94fc9eae88b331197ac5ee3 - Sigstore transparency entry: 2455948045
- Sigstore integration time:
-
Permalink:
jax-ml/jax-triton@2e271999faaa95af2cc6c08d2a93e787746cab02 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/jax-ml
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2e271999faaa95af2cc6c08d2a93e787746cab02 -
Trigger Event:
release
-
Statement type:
File details
Details for the file jax_triton-0.4.0-py3-none-any.whl.
File metadata
- Download URL: jax_triton-0.4.0-py3-none-any.whl
- Upload date:
- Size: 23.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76f7178dcf49cd65cb3c8d6a1edca30415c54ae3ddae6555ae2b432a79db3d61
|
|
| MD5 |
3532a7a44a243d9ca0f5874f42735b62
|
|
| BLAKE2b-256 |
df5d93c19c7ac9387e082356ee070b0ea589837829d01122d59b382428ddedad
|
Provenance
The following attestation bundles were made for jax_triton-0.4.0-py3-none-any.whl:
Publisher:
pypi-publish.yml on jax-ml/jax-triton
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jax_triton-0.4.0-py3-none-any.whl -
Subject digest:
76f7178dcf49cd65cb3c8d6a1edca30415c54ae3ddae6555ae2b432a79db3d61 - Sigstore transparency entry: 2455948494
- Sigstore integration time:
-
Permalink:
jax-ml/jax-triton@2e271999faaa95af2cc6c08d2a93e787746cab02 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/jax-ml
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@2e271999faaa95af2cc6c08d2a93e787746cab02 -
Trigger Event:
release
-
Statement type: