#[derive(Debug, Clone)]
pub struct NpuReadoutWeights {
pub weights_i8: Vec<i8>,
pub scale: f64,
pub zero_point: i64,
pub input_dim: usize,
pub output_dim: usize,
}
#[must_use]
#[expect(
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
reason = "NPU API"
)]
pub fn quantize_affine_i8_f64(values: &[f64]) -> (Vec<i8>, f64, i64) {
if values.is_empty() {
return (Vec::new(), 1.0, 0);
}
let min_val = values.iter().copied().fold(f64::INFINITY, f64::min);
let max_val = values.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let range = max_val - min_val;
let scale = if range > 0.0 { range / 254.0 } else { 1.0 };
let zero_point = (round(-min_val / scale) - 127.0) as i64;
let quantized: Vec<i8> = values
.iter()
.map(|&x| {
let q_f64 = round(x / scale) + zero_point as f64;
let q = round(q_f64).clamp(-128.0, 127.0) as i64;
q.clamp(-128, 127) as i8
})
.collect();
(quantized, scale, zero_point)
}
#[inline]
fn round(x: f64) -> f64 {
(x + 0.5_f64.copysign(x)).trunc()
}
#[must_use]
#[expect(
clippy::cast_precision_loss,
reason = "i8→i64 and i64→f64 casts are lossless for the value ranges involved (i8: ±127, zero_point: small)"
)]
pub fn dequantize_affine_i8_f64(quantized: &[i8], scale: f64, zero_point: i64) -> Vec<f64> {
quantized
.iter()
.map(|&q| (q as i64 - zero_point) as f64 * scale)
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quantize_round_trip_max_error() {
let values: Vec<f64> = (0..100).map(|i| (i as f64).mul_add(0.15, -5.0)).collect();
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
let dequantized = dequantize_affine_i8_f64(&quantized, scale, zero_point);
let range = values
.iter()
.copied()
.fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), x| {
(a.min(x), b.max(x))
});
let range_size = range.1 - range.0;
let max_error = values
.iter()
.zip(dequantized.iter())
.map(|(a, b)| (a - b).abs())
.fold(0.0_f64, f64::max);
assert!(
max_error < 0.01 * range_size,
"max_error {} should be < 1% of range {}",
max_error,
0.01 * range_size
);
}
#[test]
fn test_quantize_known_values() {
let values = vec![0.0, 1.0, -1.0, 0.5, -0.5];
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
let dequantized = dequantize_affine_i8_f64(&quantized, scale, zero_point);
for (orig, deq) in values.iter().zip(dequantized.iter()) {
let err = (orig - deq).abs();
assert!(err < 0.1, "orig {orig} deq {deq} err {err}");
}
}
#[test]
fn test_quantize_all_zero() {
let values = vec![0.0; 10];
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
assert_eq!(quantized.len(), 10);
assert!(quantized.iter().all(|&q| q == quantized[0]));
let dequantized = dequantize_affine_i8_f64(&quantized, scale, zero_point);
assert!(dequantized.iter().all(|&x| x.abs() < 1e-10));
}
#[test]
fn test_quantize_single_value() {
let values = vec![1.5; 5];
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
assert_eq!(quantized.len(), 5);
let dequantized = dequantize_affine_i8_f64(&quantized, scale, zero_point);
for (orig, deq) in values.iter().zip(dequantized.iter()) {
assert!(
(orig - deq).abs() < 1.0,
"single-value quantize: orig {orig} deq {deq}"
);
}
}
#[test]
fn test_quantize_large_range() {
let values: Vec<f64> = (-50..50).map(|i| i as f64 * 10.0).collect();
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
let dequantized = dequantize_affine_i8_f64(&quantized, scale, zero_point);
let range = 1000.0; let max_error = values
.iter()
.zip(dequantized.iter())
.map(|(a, b)| (a - b).abs())
.fold(0.0_f64, f64::max);
assert!(max_error < 0.01 * range);
}
#[test]
fn test_quantize_empty() {
let values: Vec<f64> = vec![];
let (quantized, scale, zero_point) = quantize_affine_i8_f64(&values);
assert!(quantized.is_empty());
assert!((scale - 1.0).abs() < 1e-10);
assert_eq!(zero_point, 0);
}
}