metrale-kernels
Path: crates/kernels/
Role: the bridge between the kernel source tree and the Rust workspace. Every PTX module every other crate launches is embedded here.
Key files: src/lib.rs (the registry types and the include! of the generated file), src/resolve.rs (which target serves a checkpoint), build.rs and its build_*.rs modules (which generate the registry).
The trick: auto-generated PTX embedding
crates/kernels/src/lib.rs includes one generated file:
#![allow(unused)] fn main() { include!(concat!(env!("OUT_DIR"), "/target_ptx.rs")); }
Everything inside target_ptx.rs — the per-target PTX bytes, ptx_modules() for the primary target, the all_ptx_sets() multi-target registry, TARGET_DEFAULTS and TARGET_SM_COUNT — is produced by build.rs at every Cargo build. You will not find target_ptx.rs in the repository; it is generated fresh into OUT_DIR each time.
all_ptx_sets() returns one TargetPtxSet per compiled target:
#![allow(unused)] fn main() { pub struct TargetPtxSet { pub target: KernelTarget, // (arch, model, quant) pub ptx_arch: &'static str, // HARDWARE.toml arch verbatim: sm_121f, sm_90a, … pub modules: Vec<(&'static str, &'static [u8])>, pub sampling: SamplingPresets, // from MODEL.toml pub behavior: ModelBehavior, // from MODEL.toml pub model_type_matches: Vec<ModelTypeMatch>, pub match_names: &'static [&'static str], // … } }
At startup, metrale_kernels::ptx_for_config(model_type, hidden_size, model_refs, pinned) picks the set that serves the checkpoint (src/resolve.rs: exact (model_type, hidden_size) declarations first, a collision broken by match_names, an unbroken tie an error, --kernel-target pins one), and the server passes its modules to MetraleCudaBackend::new, which uploads each PTX module with cuModuleLoadData.
What build.rs actually does
- Read
METRALE_TARGET_*—HW(defaultgb10),MODEL(default*) andQUANT(defaultnvfp4);*expands to every matching directory. - Resolve each target's sources — own leaf, own
common/, and the parent's pair whenHARDWARE.tomlsets[hardware] inherits, plus everyKERNEL.toml[sources] use. The resolver ismetrale-closure's layout module; an undeclared shadow or a[shadow]entry with nothing to shadow fails the build. - Stage each role's sources into
OUT_DIR(build_stage.rs), so a quoted#includeresolves against the headers of the layer that compiles it. - Resolve the compiler —
resolve_compute_target(vendor)(build_target.rs) returnsNvidiaTarget,AppleTarget,ScaleTargetorHipTarget. - Compile every source — flags come from
HARDWARE.toml, thecommon/and leafKERNEL.toml[build]tables, andMETRALE_EXTRA_NVCC_FLAGS. - Apply module-name overrides —
KERNEL.toml's[modules]section lets a file stem (e2m1_branchless.cu) expose itself under a shorter module name (e2m1). - Parse
MODEL.tomlintoSamplingPresetsandModelBehavior, andHARDWARE.toml[defaults]intoTARGET_DEFAULTS— the serving levers the target runs with. - Write
target_ptx.rs.
rerun-if-changed and rerun-if-env-changed directives on the kernel tree and the METRALE_TARGET_* variables mean Cargo re-runs the script only when an input changed.
METRALE_SKIP_BUILD=1 — the escape hatch
On a host with no nvcc, the crate would fail to build without this. When it is set, build.rs invokes no compiler and emits a stub target_ptx.rs with no targets, so the crate compiles cleanly. A macOS build without METRALE_TARGET_HW takes the same path. CI's lint and test jobs use it; so does a local cargo clippy. The Kernel Dispatch chapter covers the broader flow.
What gets added to this crate when you…
- …add a new
(hw, model, quant)leaf? Nothing undercrates/kernels/src/.build.rspicks it up on the nextcargo build; itsMODEL.tomldeclares whichmodel_type(andhidden_size) it serves. - …add a new kernel to an existing leaf? Drop the source in the leaf directory; if it replaces a
common/file, declare the shadow in the leaf'sKERNEL.toml[shadow]with a reason. For a non-stem module name, add a[modules]entry. - …add a new hardware vendor? Extend
resolve_compute_target(vendor)inbuild_target.rsto return your newComputeTargetimpl.
The rest of the kernel-engineering story is in the CUDA Kernel Engineering deep dive.