use super::blocks::{QK_K, QK8_0, f16_at, get_scale_min_k4, q4k, q5k, q6k, q8_0, scales_at};
use super::{KernelError, QuantFormat};
fn blocks(
bytes: &[u8],
format: QuantFormat,
) -> Result<std::slice::ChunksExact<'_, u8>, KernelError> {
let block_bytes = format.block_bytes();
if !bytes.len().is_multiple_of(block_bytes) {
return Err(KernelError::RowBytesNotBlockMultiple {
format,
len: bytes.len(),
block_bytes,
});
}
Ok(bytes.chunks_exact(block_bytes))
}
pub fn dequantize_row_q4_k(bytes: &[u8]) -> Result<Vec<f32>, KernelError> {
let blocks = blocks(bytes, QuantFormat::Q4_K)?;
let mut out = Vec::with_capacity(blocks.len() * QK_K);
for block in blocks {
let d = f16_at(block, q4k::D);
let min = f16_at(block, q4k::DMIN);
let scales = scales_at(block, q4k::SCALES);
let qs = &block[q4k::QS..q4k::BYTES];
for span in 0..4 {
let (sc1, m1) = get_scale_min_k4(2 * span, scales);
let (sc2, m2) = get_scale_min_k4(2 * span + 1, scales);
let d1 = d * f32::from(sc1);
let n1 = min * f32::from(m1);
let d2 = d * f32::from(sc2);
let n2 = min * f32::from(m2);
let q = &qs[32 * span..32 * span + 32];
for &b in q {
out.push(d1 * f32::from(b & 0xF) - n1);
}
for &b in q {
out.push(d2 * f32::from(b >> 4) - n2);
}
}
}
Ok(out)
}
pub fn dequantize_row_q5_k(bytes: &[u8]) -> Result<Vec<f32>, KernelError> {
let blocks = blocks(bytes, QuantFormat::Q5_K)?;
let mut out = Vec::with_capacity(blocks.len() * QK_K);
for block in blocks {
let d = f16_at(block, q5k::D);
let min = f16_at(block, q5k::DMIN);
let scales = scales_at(block, q5k::SCALES);
let qh = &block[q5k::QH..q5k::QS];
let qs = &block[q5k::QS..q5k::BYTES];
for span in 0..4 {
let (sc1, m1) = get_scale_min_k4(2 * span, scales);
let (sc2, m2) = get_scale_min_k4(2 * span + 1, scales);
let d1 = d * f32::from(sc1);
let n1 = min * f32::from(m1);
let d2 = d * f32::from(sc2);
let n2 = min * f32::from(m2);
let u1 = 1u8 << (2 * span);
let u2 = 2u8 << (2 * span);
let q = &qs[32 * span..32 * span + 32];
for (l, &b) in q.iter().enumerate() {
let hi = if qh[l] & u1 != 0 { 16 } else { 0 };
out.push(d1 * f32::from((b & 0xF) + hi) - n1);
}
for (l, &b) in q.iter().enumerate() {
let hi = if qh[l] & u2 != 0 { 16 } else { 0 };
out.push(d2 * f32::from((b >> 4) + hi) - n2);
}
}
}
Ok(out)
}
pub fn dequantize_row_q6_k(bytes: &[u8]) -> Result<Vec<f32>, KernelError> {
let blocks = blocks(bytes, QuantFormat::Q6_K)?;
let n_blocks = blocks.len();
let mut out = vec![0f32; n_blocks * QK_K];
for (i, block) in blocks.enumerate() {
let d = f16_at(block, q6k::D);
let y = &mut out[i * QK_K..(i + 1) * QK_K];
for half in 0..2 {
let ql = &block[q6k::QL + 64 * half..];
let qh = &block[q6k::QH + 32 * half..];
let sc = &block[q6k::SCALES + 8 * half..];
let y = &mut y[128 * half..];
for l in 0..32 {
let is = l / 16;
let q1 = (i32::from(ql[l] & 0xF) | ((i32::from(qh[l]) & 3) << 4)) - 32;
let q2 = (i32::from(ql[l + 32] & 0xF) | ((i32::from(qh[l] >> 2) & 3) << 4)) - 32;
let q3 = (i32::from(ql[l] >> 4) | ((i32::from(qh[l] >> 4) & 3) << 4)) - 32;
let q4 = (i32::from(ql[l + 32] >> 4) | ((i32::from(qh[l] >> 6) & 3) << 4)) - 32;
y[l] = d * f32::from(sc[is] as i8) * q1 as f32;
y[l + 32] = d * f32::from(sc[is + 2] as i8) * q2 as f32;
y[l + 64] = d * f32::from(sc[is + 4] as i8) * q3 as f32;
y[l + 96] = d * f32::from(sc[is + 6] as i8) * q4 as f32;
}
}
}
Ok(out)
}
pub fn dequantize_row_q8_0(bytes: &[u8]) -> Result<Vec<f32>, KernelError> {
let blocks = blocks(bytes, QuantFormat::Q8_0)?;
let mut out = Vec::with_capacity(blocks.len() * QK8_0);
for block in blocks {
let d = f16_at(block, q8_0::D);
for &b in &block[q8_0::QS..q8_0::BYTES] {
out.push(d * f32::from(b as i8));
}
}
Ok(out)
}