//! Literal CPU transcription of the planned CUDA KAR level-decode kernels. //! //! This module is the macOS-verifiable specification of //! `crates/cuda-driver/src/kar_decode_levels.cu`: the same window planning, //! packet layout, thread/block decomposition, plane-word addressing and group //! scale lookup, written as plain Rust loops with the same integer widths and //! the same guard order. Passing its tests proves the *addressing and //! arithmetic specification*; it does not prove that any CUDA build of the //! kernel matches it. That needs an nvcc build and a GPU run (still open). //! //! Format ground truth: `r08/docs/format-kar-v2.md` as mirrored by //! `control-plane/src/kar_codec.rs::decode_range`, the inspector in `kar.rs`, //! and `gpu/kar/src/kar_expert.cu` (decode then plain F32 row multiply). //! //! Window model. A decode request is a flat element range `[start, start+count)` //! of one NESTED record at runtime level `L` in 2..=5. Only the bytes that //! range can touch are staged into one device "packet": //! //! ```text //! packet+0 book: 2^L f16 (level-L table), 64-byte region //! packet+64 scales: window_groups f16, from group start/group //! packet+planes_offset plane p (p = std::result::Result; /// One archive byte range and where it lands in the device packet. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ByteRange { pub archive_offset: u64, pub len: u64, pub packet_offset: u64, } /// The scalar arguments of `leleka_kar_decode_levels_f32`, in launch order /// after the three pointers (packet, out, status). #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DecodeParams { pub level: u32, pub staged_planes: u32, pub count: u32, pub head32: u32, pub head_group: u32, pub group: u32, pub scales_offset: u32, pub planes_offset: u32, pub plane_stride_words: u32, pub window_words: u32, pub window_groups: u32, pub packet_bytes: u32, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct WindowPlan { pub level: u8, pub group: u32, pub start: u64, pub count: u32, pub first_word: u64, pub first_group: u64, pub book: ByteRange, pub scales: ByteRange, /// Exactly `level` entries. pub planes: Vec, pub params: DecodeParams, } /// A whole-row window plus the geometry the multiply kernel needs. #[derive(Debug, Clone, PartialEq, Eq)] pub struct RowPlan { pub window: WindowPlan, pub columns: u32, pub rows: u32, } const NAN_BITS: u32 = 0x7fc0_0000; /// Largest SwiGLU width of one launch. pub const MAX_SWIGLU_VALUES: u64 = 1 << 20; #[cfg(test)] #[path = "../tests/kar_kernel_model/mod.rs"] mod tests; mod window_plan; pub use window_plan::*; mod decode_params_valid; pub use decode_params_valid::*;