use crate::error::{QuantError, QuantResult};
use crate::traits::QuantKernel;
use crate::types::QuantTensor;
const Q6_K_BLOCK_SIZE: usize = 256;
const Q6_K_BLOCK_BYTES: usize = 210;
pub struct Q6KRef;
impl QuantKernel for Q6KRef {
fn dequant_block(&self, block: &[u8], output: &mut [f32]) -> QuantResult<()> {
if block.len() < Q6_K_BLOCK_BYTES {
return Err(QuantError::BufferTooSmall {
needed: Q6_K_BLOCK_BYTES,
available: block.len(),
});
}
if output.len() < Q6_K_BLOCK_SIZE {
return Err(QuantError::BufferTooSmall {
needed: Q6_K_BLOCK_SIZE,
available: output.len(),
});
}
let ql = &block[0..128];
let qh = &block[128..192];
let scales = &block[192..208];
let d = f16_to_f32(u16::from_le_bytes([block[208], block[209]]));
for group in 0..2 {
let ql_off = group * 64;
let qh_off = group * 32;
let sc_off = group * 8;
let out_off = group * 128;
for l in 0..32 {
let is = l / 16;
let q1 = ((ql[ql_off + l] & 0x0F) | ((qh[qh_off + l] & 3) << 4)) as i32 - 32;
let q2 =
((ql[ql_off + l + 32] & 0x0F) | (((qh[qh_off + l] >> 2) & 3) << 4)) as i32 - 32;
let q3 = ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4)) as i32 - 32;
let q4 =
((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4)) as i32 - 32;
let s0 = scales[sc_off + is] as i8 as f32;
let s1 = scales[sc_off + is + 2] as i8 as f32;
let s2 = scales[sc_off + is + 4] as i8 as f32;
let s3 = scales[sc_off + is + 6] as i8 as f32;
output[out_off + l] = d * s0 * q1 as f32;
output[out_off + l + 32] = d * s1 * q2 as f32;
output[out_off + l + 64] = d * s2 * q3 as f32;
output[out_off + l + 96] = d * s3 * q4 as f32;
}
}
Ok(())
}
fn gemv(
&self,
quant_matrix: &QuantTensor,
input: &[f32],
output: &mut [f32],
) -> QuantResult<()> {
let n_rows = quant_matrix.shape[0];
let n_cols = if quant_matrix.shape.len() > 1 {
quant_matrix.shape[1]
} else {
quant_matrix.n_elements() / n_rows
};
if input.len() < n_cols {
return Err(QuantError::DimensionMismatch {
expected: n_cols,
got: input.len(),
});
}
if output.len() < n_rows {
return Err(QuantError::DimensionMismatch {
expected: n_rows,
got: output.len(),
});
}
let blocks_per_row = n_cols.div_ceil(Q6_K_BLOCK_SIZE);
let row_bytes = blocks_per_row * Q6_K_BLOCK_BYTES;
for (row, out) in output.iter_mut().enumerate().take(n_rows) {
let row_start = row * row_bytes;
let mut sum = 0.0f32;
for blk in 0..blocks_per_row {
let bo = row_start + blk * Q6_K_BLOCK_BYTES;
let data = &quant_matrix.data;
let ql = &data[bo..bo + 128];
let qh = &data[bo + 128..bo + 192];
let scales = &data[bo + 192..bo + 208];
let d = f16_to_f32(u16::from_le_bytes([data[bo + 208], data[bo + 209]]));
let inp = &input[blk * Q6_K_BLOCK_SIZE..];
let cols_in_block = (n_cols - blk * Q6_K_BLOCK_SIZE).min(Q6_K_BLOCK_SIZE);
for group in 0..2 {
let ql_off = group * 64;
let qh_off = group * 32;
let sc_off = group * 8;
let in_off = group * 128;
for l in 0..32 {
let is = l / 16;
let q1 =
((ql[ql_off + l] & 0x0F) | ((qh[qh_off + l] & 3) << 4)) as i32 - 32;
let q2 = ((ql[ql_off + l + 32] & 0x0F) | (((qh[qh_off + l] >> 2) & 3) << 4))
as i32
- 32;
let q3 = ((ql[ql_off + l] >> 4) | (((qh[qh_off + l] >> 4) & 3) << 4))
as i32
- 32;
let q4 = ((ql[ql_off + l + 32] >> 4) | (((qh[qh_off + l] >> 6) & 3) << 4))
as i32
- 32;
let s0 = d * scales[sc_off + is] as i8 as f32;
let s1 = d * scales[sc_off + is + 2] as i8 as f32;
let s2 = d * scales[sc_off + is + 4] as i8 as f32;
let s3 = d * scales[sc_off + is + 6] as i8 as f32;
let c0 = in_off + l;
let c1 = in_off + l + 32;
let c2 = in_off + l + 64;
let c3 = in_off + l + 96;
if c0 < cols_in_block {
sum += s0 * q1 as f32 * inp[c0];
}
if c1 < cols_in_block {
sum += s1 * q2 as f32 * inp[c1];
}
if c2 < cols_in_block {
sum += s2 * q3 as f32 * inp[c2];
}
if c3 < cols_in_block {
sum += s3 * q4 as f32 * inp[c3];
}
}
}
}
*out = sum;
}
Ok(())
}
fn gemm(
&self,
quant_matrix: &QuantTensor,
input: &[f32],
output: &mut [f32],
m: usize,
n: usize,
k: usize,
) -> QuantResult<()> {
for row in 0..m {
let input_row = &input[row * k..(row + 1) * k];
let output_row = &mut output[row * n..(row + 1) * n];
self.gemv(quant_matrix, input_row, output_row)?;
}
Ok(())
}
fn block_size(&self) -> usize {
Q6_K_BLOCK_SIZE
}
fn block_bytes(&self) -> usize {
Q6_K_BLOCK_BYTES
}
fn name(&self) -> &'static str {
"Q6_K"
}
}
fn f16_to_f32(bits: u16) -> f32 {
half::f16::from_bits(bits).to_f32()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dequant_zero_scale() {
let block = vec![0u8; Q6_K_BLOCK_BYTES];
let kernel = Q6KRef;
let mut output = vec![0.0f32; 256];
kernel.dequant_block(&block, &mut output).unwrap();
for &v in &output {
assert!((v).abs() < 1e-5, "expected 0, got {v}");
}
}
#[test]
fn test_dequant_simple() {
let mut block = vec![0u8; Q6_K_BLOCK_BYTES];
for i in 0..64 {
block[128 + i] = 0xAA;
}
for i in 0..16 {
block[192 + i] = 1;
}
let d_bits = half::f16::from_f32(1.0).to_bits();
block[208] = (d_bits & 0xFF) as u8;
block[209] = ((d_bits >> 8) & 0xFF) as u8;
let kernel = Q6KRef;
let mut output = vec![0.0f32; 256];
kernel.dequant_block(&block, &mut output).unwrap();
for (i, &v) in output.iter().enumerate() {
assert!((v).abs() < 0.01, "weight[{i}] = {v}, expected 0");
}
}
#[test]
fn test_gemv_q6_k() {
let mut block = vec![0u8; Q6_K_BLOCK_BYTES];
for (i, b) in block.iter_mut().enumerate().take(128) {
*b = ((i * 7 + 3) & 0xFF) as u8;
}
for i in 0..64 {
block[128 + i] = ((i * 13 + 5) & 0xFF) as u8;
}
for i in 0..16 {
block[192 + i] = (i as i8 * 3 - 8) as u8;
}
let d_bits = half::f16::from_f32(0.5).to_bits().to_le_bytes();
block[208] = d_bits[0];
block[209] = d_bits[1];
let kernel = Q6KRef;
let mut dequant = vec![0.0f32; 256];
kernel.dequant_block(&block, &mut dequant).unwrap();
let input: Vec<f32> = (0..256).map(|i| (i as f32 * 0.01) - 1.28).collect();
let expected: f32 = dequant.iter().zip(input.iter()).map(|(w, x)| w * x).sum();
let tensor = QuantTensor::new(block, vec![1, 256], oxillama_gguf::GgufTensorType::Q6K);
let mut output = vec![0.0f32; 1];
kernel.gemv(&tensor, &input, &mut output).unwrap();
assert!(
(output[0] - expected).abs() < 0.1,
"gemv={}, expected={}",
output[0],
expected
);
}
}