XLA and PJRT¶
ruxla runs IR with XLA from Rust through PJRT plugins: the IR is lowered
to StableHLO (ruxla-stablehlo), compiled by a CPU or CUDA plugin, and executed (ruxla-pjrt). This is
what jit, vmap(..).call_xla, and grad(..).call_xla use. XLA is optional: native execution, the
interpreter, export to JAX, and ahead-of-time compilation do not need it.
Getting the plugins¶
Usually nothing needs to be done: without configuration, ruxla downloads the CPU plugin into its cache
on first use (x86-64 and AArch64 Linux, Apple silicon macOS) and, on Linux, takes the CUDA plugin of
the JAX installed for Python (jax[cuda13] or jax[cuda12]); see the order below. On other platforms
(Windows, Intel macOS), give a plugin with set_plugin_path or RUXLA_PJRT_CPU_PLUGIN; without one,
jit reports an error that says so, and everything that does not run XLA from Rust still works. In a clone, the script below puts the plugins in .cache/pjrt/
instead (the tests look there):
- CPU: a prebuilt
libpjrt_cpu.so(libpjrt_cpu.dylibon macOS) fromzml/pjrt-artifacts, checked against a pinned SHA-256, in.cache/pjrt/. - CUDA: a symbolic link
.cache/pjrt/xla_cuda_plugin.soto the plugin of thejax[cuda13]installed in.venv.
Choosing the platform and the plugin¶
Jitted functions run on one platform per process (ruxla::runtime):
runtime::set_platform(Platform::Cpu | Platform::Cuda), if called;- otherwise the environment variable
RUXLA_PLATFORM(cpuorcuda); - otherwise the CPU.
The plugin of a platform comes from:
runtime::set_plugin_path(platform, path), if called;- otherwise
RUXLA_PJRT_CPU_PLUGINorRUXLA_PJRT_CUDA_PLUGIN; - otherwise the directory
RUXLA_PJRT_PLUGIN_DIR(fileslibpjrt_cpu.so,libpjrt_cpu.dylibon macOS, andxla_cuda_plugin.so;Platform::plugin_filegives the name); - otherwise the cache
RUXLA_CACHE_DIR/pjrt(by default~/.cache/ruxla/pjrt); - otherwise, for CUDA on Linux, the plugin of the JAX installed for
RUXLA_PYTHON(orpython3); - otherwise, for the CPU on x86-64 and AArch64 Linux and Apple silicon macOS, the prebuilt plugin
downloaded into the cache (with
curlandtar, checked against a pinned SHA-256);RUXLA_PJRT_DOWNLOAD=0turns the download off.
use ruxla::runtime::{self, Platform};
runtime::set_plugin_path(Platform::Cpu, ".cache/pjrt/libpjrt_cpu.so");
// runtime::set_platform(Platform::Cuda);
If no plugin is found, jit(..).call panics and try_call returns an error that says which
variable to set.
Running with XLA¶
use ruxla::prelude::*;
#[derive(Clone, Debug, Tree)]
struct Particle<B: Backend> {
x: Array<B, f32>,
v: Array<B, f32>,
steps: I32<B>,
}
fn step<B: Backend>(p: Particle<B>, dt: F32<B>) -> Particle<B> {
let v = jnp::where_(p.x.lt(0.0), &-&p.v, &p.v);
Particle { x: &p.x + &(&v * dt), v, steps: p.steps + 1 }
}
fn simulate<B: Backend>(p: Particle<B>, dt: F32<B>) -> Particle<B> {
lax::fori_loop(0, 10, |_, p| step(p, dt), p)
}
let f = jit(|(p, dt): (Particle<Trace>, F32<Trace>)| simulate(p, dt));
let out = f.call(&(particle, F32::new(0.1))); // the same values as simulate::<Native>
- Compiled executables are cached in memory, keyed by the platform and the serialized IR (which includes the input types), so a function is traced and compiled once per input type.
device_putandcall_on_devicekeep values on the device;call_on_device_donatingreuses input buffers (see Transformations).- Random numbers agree with the native backend bit for bit. Float results can differ in the last bits (XLA fuses operations into FMAs and has its own transcendental functions).
Lowering details¶
threefry2x32is lowered as JAX lowers it: a loop on the CPU, an unrolled function on the GPU.- Linear algebra (
cholesky,triangular_solve,lu,qr,eigh,svd, ...) has no StableHLO operation. With the PJRT plugins, ruxla registers its own FFI handlers (ruxla-pjrt'slinalg), which use the same kernels as the native backend, so the results are bit-identical. On CUDA,cholesky,triangular_solve, andqruse XLA's own implementations on the device, and LU,eigh, andsvdcall cuSOLVER on the device; the remaining decompositions are computed on the host. - When a function is embedded in JAX instead (
ruxla_call), linear algebra lowers to the custom calls JAX itself uses (jaxlib's LAPACK handlers on the CPU, cuSOLVER on the GPU).
The PJRT bindings¶
ruxla-pjrt can also be used directly: load a plugin, create a client, compile StableHLO text, and move
buffers.
use ruxla_pjrt::{ElementType, Plugin};
let plugin = Plugin::load(ruxla_pjrt::CPU_PLUGIN_FILE)?; // libpjrt_cpu.so or .dylib
let client = plugin.client()?;
let exe = client.compile(
r#"
func.func public @main(%x: tensor<2xf32>) -> tensor<2xf32> {
%0 = stablehlo.add %x, %x : tensor<2xf32>
return %0 : tensor<2xf32>
}
"#,
)?;
let x = client.buffer_from_host(&[1.0f32, 2.0], &[2])?;
let y = exe.execute(&[&x])?;
assert_eq!(y[0].element_type()?, ElementType::F32);
assert_eq!(y[0].to_vec::<f32>()?, [2.0, 4.0]);
The C API bindings (crates/ruxla-pjrt/src/sys.rs) are generated from XLA's pjrt_c_api.h with
scripts/gen-pjrt-bindings.sh.
Tests¶
Tests that need a plugin are #[ignore]d:
They include the OpInfo checks that run every primitive's samples natively, on the interpreter, and on
XLA and compare them (ruxla-testing).