use super::iq_shared::KVALUES_IQ4NL;
use crate::error::{QuantError, QuantResult};
use crate::traits::QuantKernel;
use crate::types::QuantTensor;
const IQ4_NL_BLOCK_SIZE: usize = 32;
const IQ4_NL_BLOCK_BYTES: usize = 18;
pub struct Iq4NlRef;
impl QuantKernel for Iq4NlRef {
fn dequant_block(&self, block: &[u8], output: &mut [f32]) -> QuantResult<()> {
if block.len() < IQ4_NL_BLOCK_BYTES {
return Err(QuantError::BufferTooSmall {
needed: IQ4_NL_BLOCK_BYTES,
available: block.len(),
});
}
if output.len() < IQ4_NL_BLOCK_SIZE {
return Err(QuantError::BufferTooSmall {
needed: IQ4_NL_BLOCK_SIZE,
available: output.len(),
});
}
let d = half::f16::from_le_bytes([block[0], block[1]]).to_f32();
for i in 0..(IQ4_NL_BLOCK_SIZE / 2) {
let byte = block[2 + i];
let lo = (byte & 0x0F) as usize;
let hi = ((byte >> 4) & 0x0F) as usize;
output[i * 2] = d * KVALUES_IQ4NL[lo] as f32;
output[i * 2 + 1] = d * KVALUES_IQ4NL[hi] 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(IQ4_NL_BLOCK_SIZE);
let row_bytes = blocks_per_row * IQ4_NL_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 block_offset = row_start + blk * IQ4_NL_BLOCK_BYTES;
let block = &quant_matrix.data[block_offset..block_offset + IQ4_NL_BLOCK_BYTES];
let d = half::f16::from_le_bytes([block[0], block[1]]).to_f32();
let input_offset = blk * IQ4_NL_BLOCK_SIZE;
for i in 0..(IQ4_NL_BLOCK_SIZE / 2) {
let byte = block[2 + i];
let lo = (byte & 0x0F) as usize;
let hi = ((byte >> 4) & 0x0F) as usize;
let idx = input_offset + i * 2;
if idx + 1 < n_cols {
sum += KVALUES_IQ4NL[lo] as f32 * d * input[idx];
sum += KVALUES_IQ4NL[hi] as f32 * d * input[idx + 1];
} else if idx < n_cols {
sum += KVALUES_IQ4NL[lo] as f32 * d * input[idx];
}
}
}
*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 {
IQ4_NL_BLOCK_SIZE
}
fn block_bytes(&self) -> usize {
IQ4_NL_BLOCK_BYTES
}
fn name(&self) -> &'static str {
"IQ4_NL"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::QuantKernel;
fn make_iq4_nl_block(scale: f32, nibble_bytes: [u8; 16]) -> [u8; IQ4_NL_BLOCK_BYTES] {
let mut block = [0u8; IQ4_NL_BLOCK_BYTES];
let d_le = half::f16::from_f32(scale).to_le_bytes();
block[0] = d_le[0];
block[1] = d_le[1];
block[2..18].copy_from_slice(&nibble_bytes);
block
}
#[test]
fn test_dequant_block_known_values() {
let mut nibbles = [0x00u8; 16];
nibbles[0] = 0x50;
let block = make_iq4_nl_block(1.0, nibbles);
let mut out = [0.0f32; 32];
Iq4NlRef.dequant_block(&block, &mut out).unwrap();
assert!(
(out[0] - (-127.0f32)).abs() < 0.1,
"out[0] = {}, expected -127",
out[0]
);
assert!(
(out[1] - (-35.0f32)).abs() < 0.1,
"out[1] = {}, expected -35",
out[1]
);
}
#[test]
fn test_dequant_block_all_zero_index() {
let block = make_iq4_nl_block(2.0, [0x00u8; 16]);
let mut out = [0.0f32; 32];
Iq4NlRef.dequant_block(&block, &mut out).unwrap();
for &v in &out {
assert!((v - (-254.0f32)).abs() < 0.5, "expected -254, got {v}");
}
}
#[test]
fn test_dequant_block_all_max_index() {
let block = make_iq4_nl_block(1.0, [0xFFu8; 16]);
let mut out = [0.0f32; 32];
Iq4NlRef.dequant_block(&block, &mut out).unwrap();
for &v in &out {
assert!((v - 113.0f32).abs() < 0.1, "expected 113, got {v}");
}
}
#[test]
fn test_dequant_block_buffer_too_small_block() {
let small = [0u8; 10];
let mut out = [0.0f32; 32];
let result = Iq4NlRef.dequant_block(&small, &mut out);
assert!(matches!(result, Err(QuantError::BufferTooSmall { .. })));
}
#[test]
fn test_dequant_block_buffer_too_small_output() {
let block = make_iq4_nl_block(1.0, [0x00u8; 16]);
let mut out = [0.0f32; 10];
let result = Iq4NlRef.dequant_block(&block, &mut out);
assert!(matches!(result, Err(QuantError::BufferTooSmall { .. })));
}
#[test]
fn test_gemv_sum_equals_dequant_dot_ones() {
let block = make_iq4_nl_block(1.0, [0x88u8; 16]);
let tensor = QuantTensor::new(
block.to_vec(),
vec![1, 32],
oxillama_gguf::GgufTensorType::Iq4Nl,
);
let mut dequant = [0.0f32; 32];
Iq4NlRef.dequant_block(&block, &mut dequant).unwrap();
let expected: f32 = dequant.iter().sum();
let input = vec![1.0f32; 32];
let mut out = [0.0f32; 1];
Iq4NlRef.gemv(&tensor, &input, &mut out).unwrap();
assert!(
(out[0] - expected).abs() < 1e-4,
"gemv={}, expected sum={}",
out[0],
expected
);
}
#[test]
fn test_gemv_two_rows() {
let block0 = make_iq4_nl_block(1.0, [0x88u8; 16]);
let block1 = make_iq4_nl_block(1.0, [0xFFu8; 16]);
let mut data = Vec::with_capacity(IQ4_NL_BLOCK_BYTES * 2);
data.extend_from_slice(&block0);
data.extend_from_slice(&block1);
let tensor = QuantTensor::new(data, vec![2, 32], oxillama_gguf::GgufTensorType::Iq4Nl);
let input = vec![1.0f32; 32];
let mut out = [0.0f32; 2];
Iq4NlRef.gemv(&tensor, &input, &mut out).unwrap();
assert!((out[0] - 32.0f32).abs() < 0.1, "row0={}", out[0]);
assert!((out[1] - 3616.0f32).abs() < 0.1, "row1={}", out[1]);
}
#[test]
fn test_block_size_and_bytes() {
assert_eq!(Iq4NlRef.block_size(), 32);
assert_eq!(Iq4NlRef.block_bytes(), 18);
assert_eq!(Iq4NlRef.name(), "IQ4_NL");
}
}