bloomery is an LLM inference engine for MoE models that splits the experts between GPU and CPU. Its GPU kernels are Rust, compiled with cuda-oxide. This page shows how a larger project is put together on it: how the kernels are laid out, how the host launches them and how the build is pinned. It is written for people who have finished a first cuda-oxide kernel and want to see the next step.
bloomery is still in active development, and this page describes the tree as it is now. Everything here runs on Ampere (sm_86), and on a DGX Spark's GB10 at sm_121a (contrib/dgx-spark.md).
The device code lives in three engine crates (gpu, gpu-deepseek41, gpu-vision): 69 #[cuda_module] modules with 274 #[kernel] entries, plus a few test kernels in gpu-gates. The kernels include quantized matrix-vector and matrix-matrix products (k-quants, int8 tensor-core GEMM), flash attention for decode and prefill, MoE routing, RoPE and norms. Every one of them is Rust.
| Crate | Holds |
|---|---|
crates/gpu |
kernels shared by every model, the device-callable cores (cores.rs), CUDA graphs, the fault word |
crates/gpu-deepseek41 |
kernels only DeepSeek-V4.1 runs |
crates/gpu-vision |
the vision encoder's kernels |
crates/gpu-gates |
the binaries and the GPU gates (tests) |
Three rules came from experience:
- One
#[cuda_module]per operation file. For example,rope_neox.rsholds the modulerope_neox_kernels, its launch arguments and its host launcher. Writing a new op touches its own file only. - Model-specific kernels live in their own crate. A build that does not need them does not compile them.
- Every kernel name carries a model prefix (
ds41_…,dflash_…). cuda-oxide derives a kernel's host symbol from the entry name alone, not from its crate or module, so two crates that declare the same entry name fail to link.
Arithmetic that several kernels share is written once, as #[inline(always)] pub fn in crates/gpu/src/cores.rs (for example q3k_row_dot). A kernel in another crate calls it as bloomery_gpu::cores::q3k_row_dot, and it inlines into the same instructions.
This is head_norm_neox_append from crates/gpu/src/rope_neox.rs, trimmed. It normalizes one attention head, applies RoPE and appends the key and value to the cache, one block per (token, head).
#[cuda_module]
mod rope_neox_kernels {
#[kernel]
#[launch_bounds(64)]
#[launch_contract(
domain = 1,
block = (64, 1, 1),
requires = (
q.len() >= m * n_head * 128,
table.len() >= ctx * 128,
pos.len() >= m,
cache_k.len() >= n_kv * ctx * 128,
// …one line per buffer
)
)]
pub fn head_norm_neox_append(
gq: &[f32], gk: &[f32], table: &[f32], pos: &[u32], v: &[f32],
eps: f32, n_head: u32, n_kv: u32, ctx: u32, m: u32,
fault: FaultSink,
mut q: DisjointSlice<f32>,
mut k: DisjointSlice<f32>,
mut cache_k: DisjointSlice<u16>,
mut cache_v: DisjointSlice<u16>,
) {
static mut WSUM: SharedArray<f64, 2> = SharedArray::UNINIT;
let b = thread::blockIdx_x() as usize;
// …
// SAFETY: t < m <= pos.len() by the launch contract.
let p = unsafe { *pos.get_unchecked(t) } as usize;
// …
let mut acc = f64::from(x0 * x0) + f64::from(x1 * x1);
acc += warp::shuffle_xor_f64(acc, 16);
// … down to 1, then one slot per warp in WSUM and a barrier
thread::sync_threads();
// …
}
}What we use and why:
#[launch_contract(requires = …)]does the bounds proof. It lists the length of every buffer as a product of the kernel's scalar arguments. The host checks it before the launch, so the body can useget_unchecked. Eachunsafeblock has a// SAFETY:comment that points at the contract line it relies on.#[launch_bounds]matches the block size. It caps the registers ptxas may use for that block size.- Outputs are
DisjointSlice. Each thread writes only its own positions, and the SAFETY comment says which ones. - Shared memory is a
static mut SharedArray, reached throughSharedArray::as_raw_mut_ptr. - Warp reductions use
cuda_device::warp. Here the sum of squares is taken in f64 to match the reference implementation's order.
requires accepts only + - *, so a count that needs a division or a ceiling is passed in as its own scalar argument. crates/gpu/src/lib.rs explains how n_sb, half_it and quad_it do this for the quantized products.
The host side of each module is a small struct that owns the loaded module. It does not own a stream: every call takes the engine's stream.
pub struct RopeNeoxKernels {
module: rope_neox_kernels::LoadedModule,
}
impl RopeNeoxKernels {
pub fn load(ctx: &Arc<CudaContext>) -> Result<RopeNeoxKernels, GpuError> {
// SAFETY: this package owns the embedded device bundle produced for
// the module above; the launcher checks its launch contract.
let module = unsafe { rope_neox_kernels::load(ctx)? };
Ok(RopeNeoxKernels { module })
}
pub fn enqueue_head_norm_neox_append(
&self, stream: &CudaStream, args: NeoxArgs<'_>,
) -> Result<(), GpuError> {
let what = "enqueue_head_norm_neox_append";
// 1. our own checks first, so an error names the argument
// ("cache_k.len() 1024 < 4096") and the entry point
// 2. usize -> u32 without `as`: a value that does not fit is an error
let grid = launch_u32(what, "grid", m * (n_head + n_kv))?;
// 3. prepare checks the launch contract, then the launch is enqueued
let prep = self
.module
.prepare_head_norm_neox_append(LaunchConfig1D::new(grid, THREADS_U32, 0))?;
self.module.head_norm_neox_append(
stream, &prep, gq, gk, table, pos, v, eps, n_head, n_kv, ctx, m, fault, q, k,
cache_k, cache_v,
)?;
Ok(())
}
}Modules are loaded once, when the model loads. The launchers allocate nothing, so a whole decode step can be captured into a CUDA graph and replayed. crates/gpu/src/graph.rs does capture, instantiate and launch through the driver bindings in cuda_core::sys.
A kernel cannot panic, and we do not want it to write a plausible value either. Every kernel that can meet undefined input (a NaN activation, a position past the cache) takes a FaultSink argument: a device address plus the layer number, passed by value. On bad input the kernel raises a fault code, keeps its memory accesses in bounds and writes NaN instead of a plausible result. The output head copies the fault word next to the token it writes, so the step's single readback carries it for free. The host then returns a named error. See crates/gpu/src/fault.rs.
The raise itself is a few lines of ptx_asm! (red.relaxed.gpu.global.min.u32), which names the global address space directly.
- Only
cargo oxidebuilds device crates. A plaincargo buildof such a crate compiles, but produces a binary without the device code. - cuda-oxide is pinned by git revision.
Cargo.tomldeclares the revision of NVIDIA/cuda-rust (formerly NVlabs/cuda-oxide) and takes the source from there as published: no fork. What the project needs beyond that is done in its own tree (THIRD_PARTY_NOTICES.md), and the backend is built from that revision's checkout without a patch.just denyfails if the revision floats. The Rust toolchain (the stable channel of that revision's ownrust-toolchain.toml) moves only when this pin moves. - One codegen backend, and one
cargo-oxide, per revision. On the build machine each revision's backend sits in its own directory with asource-rev.txtand aPROVENANCE, beside that revision'scargo-oxide.tools/box.shsetsCUDA_OXIDE_BACKENDto the backend, puts thatcargo-oxidefirst onPATH, and stops when the backend file is missing or names another revision or thecargo-oxidecargo finds is not at the revision. - The backend is built here, with the release profile. cargo-oxide builds its backend in the dev profile, where the device lowering of a crate as large as
bloomery-gpuruns several times slower; the profile changes the compiler's speed, not the PTX it writes.tools/oxide-backend.shbuilds the pin's checkout as published with cargo-oxide's own backend command plus--releaseand records the build inPROVENANCE. Up to the pin9df9e7ceit also applied one patch (NVIDIA/cuda-rust pull request #1648 by juliankang4: the CuTe dialect's verifier cost time cubic in the block count of a kernel, sobloomery-gpudid not finish); the pinab2b6738holds it (docs/upstream/nvlabs-ledger.md, row 51).tools/oxide-host.shrunstools/oxide-backend.sh --checkbefore everycargo oxidebuild and refuses by name a backend of another revision; the generated.cargo/cuda-oxide.tomlnames the same file for builds outsidetools/box.sh. - Host code gets the same CPU flags as a plain cargo build.
cargo oxidesets its ownCARGO_ENCODED_RUSTFLAGS, which hides.cargo/config.toml, and reads one flatextra-rustflagslist from.cargo/cuda-oxide.toml. Sotools/oxide-host.shwrites that file for the triple the build is for, from the one[target.<triple>] rustflagstable of.cargo/config.toml(a triple with no table is refused), andjust check-rustflagsholds the table, the generator and the file together. - The
llcis named and checked. cuda-oxide takesCUDA_OXIDE_LLC, else the Rust toolchain's own newerllc.tools/oxide-host.shrefuses an unset name, a file that does not run, a banner with no version and a major other than 21, and prints the path and version line it found.
Register use and spills do not change any output bit, only speed, so the bit-exact tests cannot see them. We check them at build time:
just ptx-scan <bin>prints, for every kernel entry, its registers, shared memory, ptxas spill bytes and the driver JIT's local bytes, with an md5 of its PTX. A change that should not touch device code (a rename, host-only work) must leave this table identical.just gate-ptx-spillcompares every entry's spill and local bytes with the pins intools/ref/ptx-shapes.tsv, or in the table oftools/ref/ptx-shapes/for the scan's arch and card capability. Any change, up or down, a new entry without a pin, or a pin whose entry disappeared is a failure. A non-zero pin carries a comment that gives its reason. A table may name the drivers itsjit_localcolumn is pinned on (# jit-driver:) and give a row a driver's own value (@<version>=<bytes>) where two drivers read it differently; a driver it does not list is compared onspilland the entry set only, and the run says so by name and counts it (ptx-spill summary … unheld=<n>;tools/gate-batch.shshowsunheld=<n>on the item and DONE lines and records no ledger row for that green).BLOOMERY_SPILL_STRICT=1turns it into a failure that names the driver and what to add; the maintainers' nightly and release batches set it (tools/ptx-spill-check.shheader).
A few small fixes found along the way went upstream: #1314 and #1321 (merged), #1329 and #1346 (open).
crates/gpu/src/rope_neox.rs: one module, two kernels, a host launcher. Small enough to read in one sitting.crates/gpu/src/cores.rs: device functions shared across crates.crates/gpu/src/fault.rs: the fault word and itsptx_asm!raise.crates/gpu/src/graph.rs: graph capture and replay through the driver API.crates/gpu-deepseek41/src/lib.rs: why a model's kernels get their own crate.docs/BUILD.md: toolchain versions and build commands.
Questions and corrections are welcome as issues.