Files
leleka/crates/igpu/src/params.rs
T

197 lines
5.1 KiB
Rust

//! Uniform parameter blocks of the kernels, the attention shape and the MoE plan.
#[allow(unused_imports)]
use super::*;
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct Dims {
pub(super) m: u32,
pub(super) n: u32,
pub(super) nb: u32,
pub(super) flags: u32,
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct NormParams {
pub(super) m: u32,
pub(super) e: u32,
pub(super) nb: u32,
pub(super) eps: f32,
}
/// What attention needs to know about its operands (see [`Recorder::attention`]).
#[derive(Clone, Copy, Debug)]
pub struct AttentionShape {
/// Tokens in this batch, at positions `pos0..pos0 + m`.
pub m: usize,
pub pos0: usize,
/// Sliding-window length, or 0 for full attention.
pub window: usize,
/// KV width (`kv heads * 64`) and query width (`heads * 64`).
pub kv_width: usize,
pub q_width: usize,
pub scale: f32,
}
/// The buffers of an attention call (see [`Recorder::attention`]).
#[derive(Clone, Copy)]
pub struct AttentionBufs<'a> {
pub queries: &'a Buf,
pub keys: &'a Buf,
pub values: &'a Buf,
pub sinks: &'a Buf,
pub out: &'a Buf,
}
/// A weight matrix and the quantized activations it multiplies.
#[derive(Clone, Copy)]
pub struct Operands<'a> {
pub w: &'a GpuMatrix,
pub a: &'a GpuActivations,
}
/// The output buffer of a product with its optional bias.
#[derive(Clone, Copy)]
pub struct ProductOutput<'a> {
pub out: &'a Buf,
pub bias: Option<&'a Buf>,
}
/// The shape of a RoPE pass: `token_count` tokens of `head_count` heads of `head_dim` floats each.
#[derive(Clone, Copy, Debug)]
pub struct RopeShape {
pub token_count: usize,
pub head_count: usize,
pub head_dim: usize,
}
/// The shape of a KV store: `row_count` rows of `kv_width` floats into the cache at positions `first_position..`.
#[derive(Clone, Copy, Debug)]
pub struct KvStoreShape {
pub row_count: usize,
pub kv_width: usize,
pub first_position: usize,
}
/// The buffers of the weighted combine of expert outputs: the compact `down` rows, the experts' weights and the
/// residual `x` they are added to.
#[derive(Clone, Copy)]
pub struct CombineBufs<'a> {
pub down: &'a Buf,
pub expert_weights: &'a Buf,
pub x: &'a Buf,
}
/// The buffers of an RMSNorm-and-quantize pass.
#[derive(Clone, Copy)]
pub struct NormBufs<'a> {
pub x: &'a Buf,
pub weight: &'a Buf,
pub normalized: &'a Buf,
pub act: &'a GpuActivations,
}
/// The shape of an RMSNorm-and-quantize pass: `row_count` rows of `width` floats, with the `epsilon`.
#[derive(Clone, Copy, Debug)]
pub struct NormShape {
pub epsilon: f32,
pub row_count: usize,
pub width: usize,
}
/// The buffers of a SwiGLU-and-quantize pass.
#[derive(Clone, Copy)]
pub struct SwigluBufs<'a> {
pub gate: &'a Buf,
pub up: &'a Buf,
pub act: &'a GpuActivations,
}
/// The buffers of a router pass: the input rows, the router weights and bias, and the chosen ids and weights.
#[derive(Clone, Copy)]
pub struct RouterBufs<'a> {
pub h: &'a Buf,
pub weights: &'a Buf,
pub bias: &'a Buf,
pub ids: &'a Buf,
pub expert_weights: &'a Buf,
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct AttentionParams {
pub(super) m: u32,
pub(super) pos0: u32,
pub(super) window: u32,
pub(super) kw: u32,
pub(super) qw: u32,
pub(super) scale: f32,
pub(super) pad: [u32; 2],
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct Quad {
pub(super) a: u32,
pub(super) b: u32,
pub(super) c: u32,
pub(super) d: u32,
}
/// Experts the router chooses among, and how many it picks per token (gpt-oss-20b).
pub const ROUTER_EXPERTS: usize = 32;
pub const ROUTER_USED: usize = 4;
/// The activation's two constants (`alpha`, `limit`).
#[derive(Clone, Copy, Debug)]
pub struct SwigluParams {
pub alpha: f32,
pub limit: f32,
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(super) struct SwigluUniform {
pub(super) rows: u32,
pub(super) n: u32,
pub(super) nb: u32,
pub(super) alpha: f32,
pub(super) limit: f32,
pub(super) pad: [u32; 3],
}
/// Which tokens go to which expert for one layer, laid out for the grouped products: entries (a token's
/// choice of an expert) sorted by expert, tokens ascending within an expert, cut into tiles of at most one
/// workgroup's rows.
pub struct MoePlan {
pub(super) tiles: Buf,
pub(super) rowmap: Buf,
pub(super) entry_of: Buf,
pub(super) tile_count: usize,
pub(super) entries: usize,
pub(super) experts: usize,
pub(super) tokens: usize,
pub(super) used: usize,
}
impl MoePlan {
/// Number of (token, expert) entries.
pub fn entries(&self) -> usize {
self.entries
}
/// Row counts per expert, for tests and diagnostics.
pub fn tiles(&self) -> usize {
self.tile_count
}
}
impl Quad {
/// A block holding `a` and `b`, the rest zero.
pub(super) fn pair(a: u32, b: u32) -> Self {
Self { a, b, c: 0, d: 0 }
}
}