use half::f16;
use super::fit::{make_qx_quants, nearest_int, GROUP_MAX_EPS};
use crate::{Q6_K_BLOCK_BYTES, Q6_K_BLOCK_ELEMS};
const GROUP_ELEMS: usize = 16;
const GROUPS: usize = Q6_K_BLOCK_ELEMS / GROUP_ELEMS;
const NMAX: i32 = 32;
const RMSE_TYPE: i32 = 1;
#[doc(hidden)]
pub fn probe_q6_k_group(xs: &[f32]) -> (f32, [i8; GROUP_ELEMS]) {
assert_eq!(xs.len(), GROUP_ELEMS);
let mut l = [0i8; GROUP_ELEMS];
let scale = make_qx_quants(xs, &mut l, NMAX, RMSE_TYPE, None);
(scale, l)
}
pub fn encode_block_q6_k(block: &[f32; Q6_K_BLOCK_ELEMS], out: &mut Vec<u8>) {
let mut l = [0i8; Q6_K_BLOCK_ELEMS];
let mut scales = [0f32; GROUPS];
let mut max_scale = 0f32;
let mut max_abs_scale = 0f32;
for (ib, out_scale) in scales.iter_mut().enumerate() {
let lo = GROUP_ELEMS * ib;
let scale = make_qx_quants(
&block[lo..lo + GROUP_ELEMS],
&mut l[lo..lo + GROUP_ELEMS],
NMAX,
RMSE_TYPE,
None,
);
*out_scale = scale;
let abs_scale = scale.abs();
if abs_scale > max_abs_scale {
max_abs_scale = abs_scale;
max_scale = scale;
}
}
if max_abs_scale < GROUP_MAX_EPS {
out.resize(out.len() + Q6_K_BLOCK_BYTES, 0);
return;
}
let iscale = -128.0f32 / max_scale;
let d = f16::from_f32(1.0 / iscale);
let mut qscales = [0i8; GROUPS];
for (ib, q) in qscales.iter_mut().enumerate() {
*q = nearest_int(iscale * scales[ib]).min(127) as i8;
}
for (j, &sc) in qscales.iter().enumerate() {
let dj = d.to_f32() * sc as f32;
if dj == 0.0 {
continue;
}
for ii in 0..GROUP_ELEMS {
let idx = GROUP_ELEMS * j + ii;
l[idx] = (nearest_int(block[idx] / dj).clamp(-32, 31) + 32) as i8;
}
}
out.reserve(Q6_K_BLOCK_BYTES);
let mut ql = [0u8; Q6_K_BLOCK_ELEMS / 2];
let mut qh = [0u8; Q6_K_BLOCK_ELEMS / 4];
for (half, j) in (0..Q6_K_BLOCK_ELEMS).step_by(128).enumerate() {
let (qlo, qho) = (half * 64, half * 32);
for i in 0..32 {
let q1 = (l[j + i] & 0xF) as u8;
let q2 = (l[j + i + 32] & 0xF) as u8;
let q3 = (l[j + i + 64] & 0xF) as u8;
let q4 = (l[j + i + 96] & 0xF) as u8;
ql[qlo + i] = q1 | (q3 << 4);
ql[qlo + i + 32] = q2 | (q4 << 4);
qh[qho + i] = ((l[j + i] >> 4) as u8)
| (((l[j + i + 32] >> 4) as u8) << 2)
| (((l[j + i + 64] >> 4) as u8) << 4)
| (((l[j + i + 96] >> 4) as u8) << 6);
}
}
out.extend_from_slice(&ql);
out.extend_from_slice(&qh);
out.extend_from_slice(&qscales.map(|v| v as u8));
out.extend_from_slice(&d.to_le_bytes());
}
pub fn encode_row_q6_k(src: &[f32], out: &mut Vec<u8>) -> Option<()> {
let (blocks, rest) = src.as_chunks::<Q6_K_BLOCK_ELEMS>();
if !rest.is_empty() {
return None;
}
out.reserve(blocks.len() * Q6_K_BLOCK_BYTES);
for block in blocks {
encode_block_q6_k(block, out);
}
Some(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dequant_q6_k;
use crate::encode::testdata::k_quant_fixture;
const LLAMA_CPP_Q6_K_GOLDEN: [u8; 12 * Q6_K_BLOCK_BYTES] = [
0xa4, 0x58, 0xde, 0x50, 0x5e, 0x68, 0xb7, 0x71, 0xa6, 0x0d, 0xf7, 0x1d, 0x04, 0xd5, 0x2c,
0x7b, 0x8e, 0xfb, 0xe1, 0x3b, 0xa1, 0x00, 0x96, 0x52, 0x08, 0x88, 0x39, 0x95, 0xa4, 0x16,
0x1c, 0x72, 0x0d, 0x43, 0x9f, 0x22, 0x0d, 0xb7, 0x05, 0x2a, 0x19, 0xc3, 0x88, 0x2b, 0xe4,
0x8f, 0x65, 0x00, 0xdf, 0xf6, 0xf6, 0xc2, 0x74, 0xc2, 0x53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, 0x80, 0x20, 0x10, 0xd0, 0x60, 0x50, 0x20, 0x60, 0xa0, 0x30, 0xf0, 0x60, 0x40,
0x70, 0x10, 0x00, 0xd0, 0x80, 0x80, 0xe0, 0x10, 0xf0, 0xd0, 0xa0, 0x10, 0x30, 0xb0, 0xa0,
0x10, 0xc0, 0xb0, 0x00, 0x45, 0x1b, 0x9b, 0x3c, 0xc7, 0x11, 0x61, 0xc1, 0x0a, 0x59, 0xc5,
0x60, 0xaf, 0x2f, 0x9c, 0xdb, 0xa5, 0x49, 0x4d, 0xcf, 0x0b, 0xa6, 0x02, 0xb3, 0x95, 0xf0,
0x15, 0x41, 0x11, 0xde, 0x03, 0xb6, 0x07, 0xc2, 0x72, 0x88, 0xa1, 0xe3, 0x57, 0x80, 0xfe,
0xc0, 0x8c, 0x86, 0x30, 0xae, 0xe8, 0x45, 0xe5, 0x0f, 0x9e, 0x99, 0xd1, 0xfb, 0x33, 0x08,
0x07, 0x69, 0x11, 0xc8, 0xed, 0x12, 0x4b, 0x7f, 0x50, 0x40, 0x10, 0x00, 0x10, 0x00, 0x10,
0x00, 0x00, 0x40, 0x10, 0x50, 0x40, 0x40, 0x10, 0x40, 0x10, 0x40, 0x40, 0x00, 0x00, 0x00,
0x10, 0x40, 0x00, 0x10, 0x40, 0x00, 0x10, 0x00, 0x00, 0x50, 0x06, 0x4f, 0x8e, 0xc1, 0xc3,
0x7c, 0xad, 0x01, 0x5d, 0x2a, 0x75, 0x71, 0x24, 0xfd, 0x82, 0x5d, 0x26, 0xcb, 0xfd, 0x6e,
0x92, 0xfa, 0x3b, 0x22, 0x17, 0xef, 0xae, 0x00, 0x8e, 0x42, 0xeb, 0xfc, 0x00, 0x00, 0x42,
0x42, 0x1f, 0x1f, 0xe2, 0xe0, 0x0a, 0x0b, 0x1a, 0x1a, 0xc9, 0x36, 0x94, 0x80, 0xdd, 0x83,
0xa0, 0x6f, 0xb6, 0x7d, 0x4e, 0xe1, 0x0e, 0xa0, 0x72, 0x8f, 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, 0x9f, 0x52, 0x00, 0x3b, 0x28,
0x93, 0x06, 0x1c, 0xd6, 0x99, 0xcd, 0x68, 0xec, 0xf4, 0x95, 0x54, 0x66, 0xd0, 0x00, 0xa8,
0x96, 0x3a, 0xc4, 0x41, 0xb3, 0x6a, 0x51, 0x96, 0x16, 0x65, 0x4a, 0x35, 0xf9, 0x21, 0xcf,
0xe0, 0x40, 0xb5, 0xb6, 0xb6, 0xac, 0xf2, 0xb0, 0x33, 0xf2, 0x82, 0x10, 0x7c, 0x54, 0xf2,
0x37, 0x94, 0xc1, 0xd8, 0x36, 0xfe, 0xbe, 0x3f, 0xee, 0x10, 0x10, 0x60, 0x47, 0xae, 0x9e,
0x47, 0x67, 0x8f, 0x4b, 0x27, 0x60, 0x40, 0xb8, 0x60, 0xcb, 0xae, 0x8e, 0x50, 0x13, 0x2a,
0xc0, 0x5a, 0xe3, 0x3f, 0xa9, 0x97, 0xa8, 0xc3, 0x35, 0xae, 0x08, 0x34, 0x92, 0x01, 0x16,
0x11, 0xbe, 0x45, 0xdc, 0x74, 0xad, 0x83, 0x08, 0x31, 0xbe, 0xb9, 0x10, 0x96, 0x9a, 0x45,
0x65, 0x99, 0x0d, 0x03, 0x5a, 0x6a, 0xb2, 0xee, 0x49, 0xd5, 0x87, 0x88, 0xe9, 0xe6, 0x3a,
0x66, 0x61, 0x9c, 0xb8, 0x62, 0x28, 0x78, 0x59, 0x1e, 0x7a, 0x51, 0xad, 0xae, 0x27, 0xd2,
0x92, 0x47, 0x04, 0xb7, 0x66, 0x99, 0x69, 0x66, 0x87, 0x05, 0x6e, 0x1a, 0x89, 0x61, 0x15,
0xa3, 0x88, 0x9a, 0xbb, 0xc1, 0x4a, 0x5b, 0x3a, 0x6e, 0xa1, 0x8f, 0x25, 0x89, 0x6c, 0x9c,
0x8e, 0xbb, 0xa4, 0x80, 0x99, 0x84, 0xa7, 0x58, 0x6a, 0x98, 0x71, 0x74, 0x8d, 0x35, 0x01,
0x81, 0x8f, 0xdc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29,
0x79, 0xd1, 0xae, 0xd8, 0xab, 0xce, 0x35, 0x65, 0xb6, 0xbc, 0x29, 0xc4, 0x65, 0xa9, 0x6a,
0x41, 0x55, 0x5a, 0x06, 0xaf, 0xb6, 0x52, 0x8b, 0x68, 0x5d, 0x51, 0x92, 0x8b, 0x9f, 0x6b,
0x7c, 0xb5, 0x91, 0x4d, 0x67, 0x8e, 0x6d, 0x33, 0x67, 0x4c, 0xa4, 0x6b, 0x80, 0xbe, 0x01,
];
#[test]
fn q6_k_matches_llama_cpp_quantize_row_q6_k_ref() {
let x = k_quant_fixture();
let mut got = Vec::new();
encode_row_q6_k(&x, &mut got).unwrap();
assert_eq!(got.len(), LLAMA_CPP_Q6_K_GOLDEN.len());
for (b, (g, w)) in got
.as_chunks::<Q6_K_BLOCK_BYTES>()
.0
.iter()
.zip(LLAMA_CPP_Q6_K_GOLDEN.as_chunks::<Q6_K_BLOCK_BYTES>().0)
.enumerate()
{
assert_eq!(g, w, "super-block {b} disagrees with llama.cpp");
}
}
#[test]
fn a_row_that_is_not_a_whole_number_of_super_blocks_is_refused() {
let mut out = Vec::new();
assert!(encode_row_q6_k(&[0.5; Q6_K_BLOCK_ELEMS + 1], &mut out).is_none());
assert!(encode_row_q6_k(&[0.5; 16], &mut out).is_none());
assert!(encode_row_q6_k(&[], &mut out).is_some());
}
#[test]
fn an_all_zero_super_block_is_all_zero_bytes_the_way_llama_cpp_writes_it() {
let mut out = Vec::new();
encode_row_q6_k(&[0.0; Q6_K_BLOCK_ELEMS], &mut out).unwrap();
assert_eq!(out, vec![0u8; Q6_K_BLOCK_BYTES]);
}
#[test]
fn every_element_lands_on_its_nearest_representable_level() {
let x = k_quant_fixture();
let mut bytes = Vec::new();
encode_row_q6_k(&x, &mut bytes).unwrap();
let back = dequant_q6_k(&bytes).unwrap();
assert_eq!(back.len(), x.len());
for (b, block) in bytes.as_chunks::<Q6_K_BLOCK_BYTES>().0.iter().enumerate() {
let d = f16::from_le_bytes([block[208], block[209]]).to_f32();
for g in 0..GROUPS {
let sc = block[192 + g] as i8;
let dg = d * sc as f32;
for ii in 0..GROUP_ELEMS {
let idx = b * Q6_K_BLOCK_ELEMS + GROUP_ELEMS * g + ii;
let chosen = (x[idx] - back[idx]).abs();
for k in -32..=31i32 {
let level = dg * k as f32;
assert!(
(x[idx] - level).abs() >= chosen,
"block {b} group {g} element {ii}: {} is closer to {} than to the \
chosen {}",
x[idx],
level,
back[idx]
);
}
}
}
}
}
#[test]
fn the_group_scales_are_stored_signed() {
let x = k_quant_fixture();
let mut bytes = Vec::new();
encode_row_q6_k(&x, &mut bytes).unwrap();
let any_negative = bytes
.as_chunks::<Q6_K_BLOCK_BYTES>()
.0
.iter()
.flat_map(|b| b[192..208].iter())
.any(|&v| (v as i8) < 0);
assert!(
any_negative,
"no group scale is negative; this fixture no longer exercises the signed path"
);
}
}