Files
leleka/crates/cpu-sys/build.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 c1444d98ac perf: run the CPU kernels from Rust std::arch instead of C (STRA-214)
The MXFP4, Q8_0, int8 and int4 kernels, the Q8_0 quantizer and the half conversions are now Rust
(cpu-sys/src/simd.rs), written operation for operation after the C versions. The C code stays only behind
the test-only feature c-reference, and tests/rust_vs_c.rs checks bit-identical results at the portable,
AVX2 and AVX-VNNI levels. A runtime build compiles no C.

Paired decode on gpt-oss-20b (40 tokens, 10 threads, 5 alternating rounds): median 98 ms per token for
both builds, identical tokens and first-step log-probabilities.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 18:32:31 +02:00

86 lines
2.9 KiB
Rust

//! Builds C code only for test features: the original C kernels (`c-reference`, one translation unit
//! per ISA level, each with its own flags) and the pinned ggml quantizers (`ggml-oracle`). A runtime
//! build compiles no C at all. No CMake: the `cc` crate drives MSVC or a GNU/clang compiler.
fn base() -> cc::Build {
let mut build = cc::Build::new();
build.include("csrc").opt_level(2).warnings(true);
build
}
fn main() {
println!("cargo:rerun-if-changed=csrc");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo::rustc-check-cfg=cfg(leleka_avxvnni)");
if std::env::var_os("CARGO_FEATURE_GGML_ORACLE").is_some() {
build_ggml_oracle();
}
if std::env::var_os("CARGO_FEATURE_C_REFERENCE").is_none() {
return;
}
base()
.file("csrc/dot_scalar.c")
.file("csrc/mxfp4_scalar.c")
.file("csrc/q8_scalar.c")
.compile("leleka_cpu_scalar");
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
if arch != "x86_64" {
return;
}
let msvc = std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc");
// "AVX2 level" means AVX2 + FMA + F16C (Haswell and later); MSVC's /arch:AVX2 enables all three.
let mut avx2 = base();
avx2.file("csrc/dot_avx2.c")
.file("csrc/mxfp4_avx2.c")
.file("csrc/q8_avx2.c");
if msvc {
avx2.flag("/arch:AVX2");
} else {
avx2.flag("-mavx2").flag("-mfma").flag("-mf16c");
}
avx2.compile("leleka_cpu_avx2");
// AVX-VNNI needs compiler support (MSVC with /arch:AVX2 provides the intrinsic; GCC 11+ and
// clang 12+ need -mavxvnni). If the compiler lacks it, the level is simply not built and the
// Rust side reports it as unsupported.
let mut vnni = base();
vnni.file("csrc/dot_avxvnni.c")
.file("csrc/mxfp4_avxvnni.c")
.file("csrc/q8_avxvnni.c");
let flags_ok = if msvc {
vnni.flag("/arch:AVX2");
true
} else {
vnni.flag("-mavx2").flag("-mfma").flag("-mf16c");
vnni.is_flag_supported("-mavxvnni").unwrap_or(false) && {
vnni.flag("-mavxvnni");
true
}
};
if flags_ok {
vnni.compile("leleka_cpu_avxvnni");
println!("cargo:rustc-cfg=leleka_avxvnni");
} else {
println!("cargo:warning=compiler lacks -mavxvnni; AVX-VNNI kernels are not built");
}
}
/// Test-only: the pinned ggml reference quantizers, compiled from the vendored copy in the control
/// plane. Enabled by the `ggml-oracle` feature; nothing of it reaches a runtime build.
fn build_ggml_oracle() {
let ggml = "../../control-plane/third_party/ggml";
println!("cargo:rerun-if-changed={ggml}");
cc::Build::new()
.file(format!("{ggml}/ggml-quants.c"))
.file("csrc/ggml_oracle.c")
.include(ggml)
.flag_if_supported("-std=c11")
.warnings(false)
.opt_level(2)
.compile("leleka_ggml_oracle");
}