(very WIP!) Fast LLM inference in Rust & CUDA doll.tngl.io/firecrest

kernels: generate the Rust launch wrappers from the kernel signatures #10

Open do.llth.ing
Open opened by do.llth.ing

Written by Claude Opus 5.5.

Problem #

Each engine's gpu crate hand-writes a Rust wrapper for every CUDA kernel it launches. The wrapper is an unsafe fn that mirrors the kernel's C signature argument by argument. These wrappers and their call sites are most of the roughly 670 unsafe sites in the GPU crates, and the reason clippy::too_many_arguments is allowed workspace-wide. Nothing checks a wrapper against its kernel: reviewing #5 meant comparing 22 wrapper families against qwen36.cu by hand. Constants shared by a kernel and its Rust side (head dims, rows per block) are duplicated the same way.

Proposal #

Generate the launchers from the kernel sources at build time.

  • A strict, documented convention for launchable entry points, for example an annotation macro next to extern "C" __global__ whose parameters use a small set of types:
    • device pointers to T (const or not);
    • scalar integers and floats;
    • a few POD structs declared in shared .cuh headers.
  • The build script, firecrest-kernels or each engine's after the kernels move (#9), parses those signatures and emits a Rust module per kernel file. That module holds:
    • a typed launcher per entry, taking &DeviceBuffer<T>/&mut DeviceBuffer<T> and scalars, with grid, block and stream;
    • the #define constants marked for export;
    • #[repr(C)] mirrors of the shared structs, with size and offset assertions on both sides.
  • The launch unsafe then lives in one generated place. It carries one // SAFETY: argument for the whole pattern; a kernel's own bounds contract stays documented at its definition. Hand-written kernels.rs files disappear.
  • A proc-macro (#[kernel(...)] on a Rust declaration, checked against the .cu) is the alternative if parsing in a build script proves too fragile. Its drawback is a second crate and slower builds.

Acceptance #

  • One engine (smollm3 or qwen3.6) ported first, with no change to its outputs.
  • The convention is documented next to the kernels.
  • A mismatch between a wrapper and its kernel becomes a build error.
Sign up or Sign in to add to the discussion.