Files
leleka/crates/model-format/src/kar_kernel_model.rs
T

121 lines
4.2 KiB
Rust

//! 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<L): window_words u32, from word start/32,
//! each plane `plane_stride_words` (multiple of 4) apart
//! ```
//!
//! Element `start+i` lives at bit `(head32+i) % 32` of staged word
//! `(head32+i) / 32` and uses staged scale `(head_group+i) / group`, where
//! `head32 = start % 32` and `head_group = start % group`. Planes are MSB
//! first: `code = (code << 1) | bit` over planes `0..L`.
use crate::kar::{KarNestedRecord, NESTING_TOPDOWN};
use std::fmt;
pub const MIN_LEVEL: u8 = 2;
pub const MAX_LEVEL: u8 = 5;
/// Threads per block for both kernels (kar_expert.cu uses 256).
pub const BLOCK_THREADS: u32 = 256;
/// Largest element range of one launch; the kar_expert.cu bound `hidden*ff`.
pub const MAX_RANGE_ELEMENTS: u64 = 16_777_216;
pub const MAX_TOKENS: u32 = 8;
pub const MAX_COLUMNS: u64 = 1 << 20;
/// 2^5 f16 entries, the largest book a level-5 table needs.
pub const BOOK_REGION_BYTES: u64 = 64;
pub const PACKET_ALIGN: u64 = 16;
/// Device status word bits (OR-ed with atomicOr on the device).
pub const STATUS_BAD_ARGUMENT: u32 = 1;
/// A used f16 scale or any entry of the level book was Inf/NaN. The CPU
/// oracle returns an error in exactly this case.
pub const STATUS_NONFINITE_INPUT: u32 = 2;
/// A multiply result was not finite.
pub const STATUS_NONFINITE_OUTPUT: u32 = 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KernelModelError {
Rejected(&'static str),
}
type Result<T> = std::result::Result<T, KernelModelError>;
/// 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<ByteRange>,
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::*;