Files
leleka/crates/cpu-sys/src/simd.rs
T
Pavlo Ratushnyi c1e7c02d1e STRA-338 size-rest: cpu-kernels, cpu-sys, gguf, gptoss and quant-core within 400 lines per file
Oversize files before/after: gptoss 2 -> 0 (lib.rs 1788 into 14 modules, synthetic_model.rs 922 into topic modules),
cpu-sys 2 -> 0 (lib.rs 1007, simd.rs 1503 with its inline x86 module split by kernel family), gguf 2 -> 0,
cpu-kernels 1 -> 0, quant-core 1 -> 0 (iq2s_grid.rs reflowed four entries per line, entries unchanged).
Modules are named after what they hold; public paths are re-exported unchanged; unsafe blocks and SAFETY comments moved
verbatim (cpu-sys 70 -> 70).

Census prod/test (fn>30 / cx>10 / nesting>4 / wide lines): cpu-sys 19/16, 3/0, 2/0, 5/2 -> 11/16, 3/0, 2/0, 3/2;
gptoss 13/16, 6/4, 1/0, 1/7 -> 12/14, 6/4, 1/0, 1/8; gguf 6/5, 6/0, 0/0, 3/1 -> 6/3, 6/0, 0/0, 3/1;
cpu-kernels and quant-core unchanged. Test ids and outcomes unchanged (recorded in docs/architecture/size-split-rest.md).
2026-11-06 16:28:24 +01:00

82 lines
4.1 KiB
Rust

//! Rust implementations of the CPU kernels, written with `std::arch` intrinsics.
//!
//! They mirror the C kernels in `csrc/` operation for operation (same accumulation structure, same
//! horizontal sums, same rounding), so results are bit-identical to them; a differential test checks
//! that. The portable functions are safe Rust. The x86-64 functions are `unsafe` only because the
//! caller must have verified the CPU features they are compiled for; the safe wrappers in the parent
//! module do that, and also check all slice lengths, so no function here re-checks them.
//!
//! Block formats follow ggml (MIT, llama.cpp commit 3cf03257f219afbe7334045ff7c6a06ac68c627d):
//! MXFP4 block = `e` scale byte + 16 bytes of nibbles (17 bytes), Q8_0 block = half scale + 32 int8
//! (34 bytes). The int8 and int4 dot products are adapted from Colibri `c/idot.h` (Apache-2.0).
// The portable reference loops walk blocks with `chunks_exact` on purpose: they mirror the C reference
// one for one, and the lengths are validated by the safe wrappers.
#![allow(clippy::chunks_exact_to_as_chunks)]
/// ggml's MXFP4 value table (twice the E2M1 value; the scale is halved to compensate).
pub const KVALUES_MXFP4: [i8; 16] = [0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12];
pub const MXFP4_BYTES: usize = 17;
pub const Q8_0_BYTES: usize = 34;
// ------------------------------------------------------------------- attention kernels ----------
//
// Single-precision helpers for attention over a key/value cache laid out with a fixed stride between
// positions (`stride` floats from one position to the next, one head's `hd` floats used per position).
// They work on a group of `heads` query heads that share the same keys and values (grouped-query
// attention), so each key and value row is read once for the whole group.
/// Positions handled per block: the rows of a block stay in the L1 cache while every head uses them.
pub const ATTN_BLOCK: usize = 16;
/// Bias that turns signed weights into unsigned ones for the VNNI tile kernels (`w + 128`).
pub const WEIGHT_BIAS: i32 = 128;
/// One quantized activation row for the tile kernels, in the layouts they read. All views describe the
/// same `blocks` 32-element blocks; build them with [`prepare_act`].
#[derive(Clone, Copy, Debug)]
pub struct ActRef<'a> {
/// One f32 scale per block.
pub scales: &'a [f32],
/// Each scale repeated 8 times (`blocks * 8` values), so a kernel can load it as a vector.
pub scales_bcast: &'a [f32],
/// The int8 quants, `blocks * 32` values.
pub quants: &'a [i8],
/// `blocks * 8` values: lane 0 of each group of 8 holds `-WEIGHT_BIAS * sum(quants of the block)`,
/// the others zero. Added to the first accumulation it removes the bias of the unsigned weights.
pub corr: &'a [i32],
}
/// One activation in the "q8r" layout for the multi-activation kernels: one f32 scale per block and the
/// int8 quants, `scales.len() * 32` of them.
#[derive(Clone, Copy, Debug)]
pub struct Q8rRef<'a> {
pub scales: &'a [f32],
pub quants: &'a [i8],
}
/// Most activations one multi-activation kernel call takes.
pub const MULTI_MAX: usize = 16;
// --------------------------------------------------------------------------- x86-64 ----------
#[cfg(target_arch = "x86_64")]
pub mod x86;
mod scalar_dots_and_formats;
use scalar_dots_and_formats::load_u16;
pub use scalar_dots_and_formats::{
attn_scores_f16_scalar, attn_scores_scalar, attn_weighted_sum_f16_scalar, attn_weighted_sum_scalar,
dot_mxfp4_q8_0_scalar, dot_mxfp4_q8r_scalar, dot_q8_q8r_scalar, mxfp4_q8_0_isums_scalar,
};
pub use scalar_dots_and_formats::{dot_i4i8_scalar, dot_i8i8_scalar};
pub use scalar_dots_and_formats::{e8m0_half, f16_to_f32, f32_to_f16};
mod scalar_tiles_and_elementwise;
use scalar_tiles_and_elementwise::dot_i4i8_tail;
pub use scalar_tiles_and_elementwise::{
dot_mxfp4_q8r_multi_scalar, dot_q8_0_q8r_multi_scalar, exp_shift_sum_scalar, f16_to_f32_slice_scalar,
f32_to_f16_slice_scalar, mxfp4_q8r_tile_scalar, prepare_act, q8_q8r_tile_scalar, quantize_q8_0_scalar,
swiglu_oai_scalar,
};
pub use scalar_tiles_and_elementwise::{dot_q8_0_q8_0_scalar, q8_0_q8_0_isums_scalar};