use super::CudaExecutor;
use serial_test::serial;
use trueno_gpu::driver::GpuBuffer;
use trueno_gpu::GpuError;
const ROWS: [u32; 6] = [1, 2, 3, 4, 8, 16];
const WARPS: &[u32] = &[2, 3, 4];
struct Quant {
name: &'static str,
block_bytes: usize,
block_values: u32,
scales: [u8; 4],
head: bool,
shapes: &'static [(u32, u32)],
warps: &'static [u32],
single: fn(
&mut CudaExecutor,
u64,
&GpuBuffer<f32>,
&GpuBuffer<f32>,
u32,
u32,
) -> Result<(), GpuError>,
batched: fn(
&mut CudaExecutor,
u64,
&GpuBuffer<f32>,
&GpuBuffer<f32>,
u32,
u32,
u32,
) -> Result<(), GpuError>,
}
const Q4K: Quant = Quant {
name: "q4k",
block_bytes: 144,
block_values: 256,
scales: [0x66, 0x2E, 0x66, 0x22],
head: true,
shapes: &[(64, 256), (48, 1024), (17, 3584), (40, 1000)],
warps: WARPS,
single: CudaExecutor::mwv_q4k_gemv_into,
batched: CudaExecutor::batched_mwv_q4k_gemv_into,
};
const Q6K: Quant = Quant {
name: "q6k",
block_bytes: 210,
block_values: 256,
scales: [0x66, 0x1E, 0, 0],
head: false,
shapes: &[(64, 256), (48, 1024), (17, 3584), (40, 512)],
warps: WARPS,
single: CudaExecutor::mwv_q6k_gemv_into,
batched: CudaExecutor::batched_mwv_q6k_gemv_into,
};
const Q5K: Quant = Quant {
name: "q5k",
block_bytes: 176,
block_values: 256,
scales: [0x66, 0x2E, 0x66, 0x22],
head: true,
shapes: &[(64, 256), (48, 1024), (17, 3584), (40, 1000)],
warps: &[3],
single: CudaExecutor::q5k_gemv_into,
batched: CudaExecutor::batched_q5k_gemv_into,
};
const Q8_0: Quant = Quant {
name: "q8_0",
block_bytes: 34,
block_values: 32,
scales: [0x66, 0x1E, 0, 0],
head: true,
shapes: &[(64, 256), (32, 2560), (17, 3584), (40, 1000)],
warps: &[3],
single: CudaExecutor::q8_0_gemv_into,
batched: CudaExecutor::batched_q8_0_gemv_into,
};
const Q6K_HW_DP4A: Quant = Quant {
name: "q6k_hw_dp4a",
block_bytes: 210,
block_values: 256,
scales: [0x66, 0x1E, 0, 0],
head: false,
shapes: &[(64, 256), (48, 1024), (17, 3584), (40, 512)],
warps: &[3],
single: hw_dp4a_q6k_single,
batched: CudaExecutor::batched_hw_dp4a_q6k_gemv_into,
};
fn hw_dp4a_q6k_single(
ex: &mut CudaExecutor,
w: u64,
x: &GpuBuffer<f32>,
y: &GpuBuffer<f32>,
n: u32,
k: u32,
) -> Result<(), GpuError> {
let bytes = (k / 32 * 36) as usize;
if ex
.workspace
.q8_activation_buf
.as_ref()
.map_or(0, GpuBuffer::len)
< bytes
{
ex.workspace.q8_activation_buf = Some(GpuBuffer::new(ex.context(), bytes)?);
}
ex.q8_activation_valid = false;
ex.hw_dp4a_q6k_gemv_into(w, x, y, n, k)
}
fn next(seed: &mut u32) -> u32 {
*seed ^= *seed << 13;
*seed ^= *seed >> 17;
*seed ^= *seed << 5;
*seed
}
fn weights(q: &Quant, n: u32, k: u32, mut seed: u32) -> Vec<u8> {
let blocks = (k.div_ceil(q.block_values) * n) as usize;
let fixed = if q.head && q.block_values == 256 {
4
} else {
2
};
let mut out = Vec::with_capacity(blocks * q.block_bytes);
for _ in 0..blocks {
if q.head {
out.extend_from_slice(&q.scales[..fixed]);
}
for _ in 0..q.block_bytes - fixed {
out.push(next(&mut seed) as u8);
}
if !q.head {
out.extend_from_slice(&q.scales[..fixed]);
}
}
out
}
fn activations(rows: u32, k: u32, mut seed: u32) -> Vec<f32> {
(0..rows * k)
.map(|_| (next(&mut seed) as f32 / u32::MAX as f32) * 2.0 - 1.0)
.collect()
}
fn single_rows(ex: &mut CudaExecutor, q: &Quant, w: u64, x: &[f32], n: u32, k: u32) -> Vec<f32> {
let mut want = vec![0.0f32; x.len() / k as usize * n as usize];
for (row, out_row) in x.chunks(k as usize).zip(want.chunks_mut(n as usize)) {
let input = GpuBuffer::from_host(ex.context(), row).expect("row up");
let out = GpuBuffer::new(ex.context(), n as usize).expect("row out");
(q.single)(ex, w, &input, &out, n, k).expect("single gemv");
ex.synchronize().expect("sync");
out.copy_to_host(out_row).expect("row down");
}
want
}
fn batched(
ex: &mut CudaExecutor,
q: &Quant,
w: u64,
x: &[f32],
m: u32,
n: u32,
k: u32,
) -> Vec<f32> {
let input = GpuBuffer::from_host(ex.context(), x).expect("rows up");
let out = GpuBuffer::new(ex.context(), (m * n) as usize).expect("rows out");
(q.batched)(ex, w, &input, &out, m, n, k).expect("batched gemv");
ex.synchronize().expect("sync");
let mut got = vec![0.0f32; (m * n) as usize];
out.copy_to_host(&mut got).expect("rows down");
got
}
fn assert_rows_bitwise(want: &[f32], got: &[f32], n: u32, what: &str) {
let bits = |v: &[f32]| v.iter().map(|f| f.to_bits()).collect::<Vec<_>>();
let rows = want.chunks(n as usize).zip(got.chunks(n as usize));
for (r, (want, got)) in rows.enumerate() {
assert_eq!(
bits(got),
bits(want),
"{what}: row {r} is not the single-row GEMV"
);
}
assert!(
want.iter().any(|v| v.abs() > 1e-3),
"{what}: the single-row outputs are all ~0, so equality proves nothing"
);
}
fn check_quant(ex: &mut CudaExecutor, q: &Quant) -> usize {
let mut checked = 0usize;
for (i, &(n, k)) in (0u32..).zip(q.shapes) {
let name = format!("batched_mwv_{}_5303_{i}", q.name);
ex.load_quantized_weights(&name, &weights(q, n, k, 0x9E37 + i))
.expect("load weights");
let w = ex.get_quantized_weight_ptr(&name).expect("weight ptr");
for &warps in q.warps {
ex.gpu_profile.mwv_warps = warps;
for m in ROWS {
let x = activations(m, k, n ^ k ^ m ^ warps);
let want = single_rows(ex, q, w, &x, n, k);
let got = batched(ex, q, w, &x, m, n, k);
let what = format!("{} n={n} k={k} warps={warps} m={m}", q.name);
assert_rows_bitwise(&want, &got, n, &what);
checked += 1;
}
}
}
checked
}
#[test]
#[serial]
fn batched_mwv_q4k_rows_are_bitwise_the_single_row_gemv() {
let mut ex = crate::cuda_executor_or_skip!(0);
let checked = check_quant(&mut ex, &Q4K);
assert_eq!(checked, Q4K.shapes.len() * Q4K.warps.len() * ROWS.len());
}
#[test]
#[serial]
fn batched_mwv_q6k_rows_are_bitwise_the_single_row_gemv() {
let mut ex = crate::cuda_executor_or_skip!(0);
let checked = check_quant(&mut ex, &Q6K);
assert_eq!(checked, Q6K.shapes.len() * Q6K.warps.len() * ROWS.len());
}
#[test]
#[serial]
fn batched_q5k_rows_are_bitwise_the_single_row_gemv() {
let mut ex = crate::cuda_executor_or_skip!(0);
let checked = check_quant(&mut ex, &Q5K);
assert_eq!(checked, Q5K.shapes.len() * Q5K.warps.len() * ROWS.len());
}
#[test]
#[serial]
fn batched_hw_dp4a_q6k_rows_are_bitwise_the_single_row_gemv() {
let mut ex = crate::cuda_executor_or_skip!(0);
let checked = check_quant(&mut ex, &Q6K_HW_DP4A);
assert_eq!(
checked,
Q6K_HW_DP4A.shapes.len() * Q6K_HW_DP4A.warps.len() * ROWS.len()
);
}
#[test]
fn the_batch_rows_differ() {
let x = activations(16, 256, 7);
let rows: Vec<&[f32]> = x.chunks(256).collect();
for (a, row_a) in rows.iter().enumerate() {
for row_b in &rows[a + 1..] {
assert_ne!(row_a, row_b);
}
}
}
#[test]
#[serial]
fn batched_q8_0_rows_are_bitwise_the_single_row_gemv() {
let mut ex = crate::cuda_executor_or_skip!(0);
let checked = check_quant(&mut ex, &Q8_0);
assert_eq!(checked, Q8_0.shapes.len() * Q8_0.warps.len() * ROWS.len());
}