use super::KernelError;
use super::gemv;
use super::primitives;
use super::quants::{BlockQ8_0, BlockQ8K, QuantFormat};
pub trait Backend {
fn gemv_q8_k(
&self,
format: QuantFormat,
weight: &[u8],
in_dim: usize,
out_dim: usize,
acts: &[BlockQ8K],
out: &mut [f32],
) -> Result<(), KernelError>;
fn gemv_q8_0(
&self,
weight: &[u8],
in_dim: usize,
out_dim: usize,
acts: &[BlockQ8_0],
out: &mut [f32],
) -> Result<(), KernelError>;
fn rmsnorm(
&self,
x: &[f32],
weight: &[f32],
eps: f32,
out: &mut [f32],
) -> Result<(), KernelError>;
fn rope_neox_heads(
&self,
x: &mut [f32],
n_heads: usize,
head_dim: usize,
position: u32,
theta_base: f32,
) -> Result<(), KernelError>;
fn softmax(&self, x: &mut [f32]) -> Result<(), KernelError>;
fn swiglu_combine(&self, gate: &mut [f32], up: &[f32]) -> Result<(), KernelError>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct CpuBackend;
impl Backend for CpuBackend {
fn gemv_q8_k(
&self,
format: QuantFormat,
weight: &[u8],
in_dim: usize,
out_dim: usize,
acts: &[BlockQ8K],
out: &mut [f32],
) -> Result<(), KernelError> {
gemv::gemv_q8_k(format, weight, in_dim, out_dim, acts, out)
}
fn gemv_q8_0(
&self,
weight: &[u8],
in_dim: usize,
out_dim: usize,
acts: &[BlockQ8_0],
out: &mut [f32],
) -> Result<(), KernelError> {
gemv::gemv_q8_0(weight, in_dim, out_dim, acts, out)
}
fn rmsnorm(
&self,
x: &[f32],
weight: &[f32],
eps: f32,
out: &mut [f32],
) -> Result<(), KernelError> {
primitives::rmsnorm(x, weight, eps, out)
}
fn rope_neox_heads(
&self,
x: &mut [f32],
n_heads: usize,
head_dim: usize,
position: u32,
theta_base: f32,
) -> Result<(), KernelError> {
primitives::rope_neox_heads(x, n_heads, head_dim, position, theta_base)
}
fn softmax(&self, x: &mut [f32]) -> Result<(), KernelError> {
primitives::softmax(x)
}
fn swiglu_combine(&self, gate: &mut [f32], up: &[f32]) -> Result<(), KernelError> {
primitives::swiglu_combine(gate, up)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn object_safe_and_delegates() {
let backend: &dyn Backend = &CpuBackend;
let x = [3.0f32, 4.0, 12.0, 84.0];
let w = [1.0f32, 0.5, 2.0, 1.0];
let mut via_trait = [0f32; 4];
backend.rmsnorm(&x, &w, 1e-6, &mut via_trait).unwrap();
let mut direct = [0f32; 4];
primitives::rmsnorm(&x, &w, 1e-6, &mut direct).unwrap();
assert_eq!(via_trait, direct);
let mut s = [0.0f32, 1.0, 2.0];
backend.softmax(&mut s).unwrap();
let total: f32 = s.iter().sum();
assert!((total - 1.0).abs() < 1e-6);
let mut gate = [1.0f32; 2];
assert_eq!(
backend.swiglu_combine(&mut gate, &[1.0; 3]).unwrap_err(),
KernelError::LengthMismatch {
what: "swiglu_combine: gate vs up",
left: 2,
right: 3,
}
);
let mut h = [1.0f32, 2.0, 3.0, 4.0];
backend.rope_neox_heads(&mut h, 1, 4, 0, 1e7).unwrap();
assert_eq!(h, [1.0, 2.0, 3.0, 4.0]);
let acts = vec![BlockQ8_0::default(); 8]; let weight = vec![0u8; 2 * 8 * 34];
let mut out = [1.0f32; 2];
backend.gemv_q8_0(&weight, 256, 2, &acts, &mut out).unwrap();
assert_eq!(out, [0.0, 0.0]);
let kacts = vec![BlockQ8K::default()];
let mut kout = [0f32; 1];
assert_eq!(
backend
.gemv_q8_k(QuantFormat::Q8_0, &[], 256, 1, &kacts, &mut kout)
.unwrap_err(),
KernelError::UnsupportedFormat {
what: "gemv_q8_k",
format: QuantFormat::Q8_0,
}
);
}
}