use super::{LUT_SIZE, Requantizer};
fn frexp(x: f64) -> (f64, i32) {
if x == 0.0 || !x.is_finite() {
return (x, 0);
}
let bits = x.to_bits();
let biased = ((bits >> 52) & 0x7ff) as i32;
if biased == 0 {
let (m, e) = frexp(x * f64::from_bits(0x4350_0000_0000_0000)); return (m, e - 54);
}
let mantissa = f64::from_bits((bits & !(0x7ff << 52)) | (1022 << 52));
(mantissa, biased - 1022)
}
pub fn quantize_multiplier(ratio: f64) -> (i32, i32) {
if ratio == 0.0 {
return (0, 0);
}
let (mantissa, mut exponent) = frexp(ratio);
let mut q = (mantissa * (1i64 << 31) as f64).round() as i64;
if q == 1i64 << 31 {
q >>= 1;
exponent += 1;
}
(q as i32, -exponent)
}
pub fn build_requantizer(
input_scale: f32,
weight_scale: f32,
output_scale: f32,
output_zero_point: i32,
qmin: i32,
qmax: i32,
) -> Requantizer {
let ratio = (f64::from(input_scale) * f64::from(weight_scale)) / f64::from(output_scale);
let (multiplier, shift) = quantize_multiplier(ratio);
Requantizer {
multiplier,
shift,
zero_point: output_zero_point as i8,
qmin: qmin as i8,
qmax: qmax as i8,
}
}
pub fn quantize(x: f32, scale: f32, zero_point: i32, qmin: i32, qmax: i32) -> i8 {
let q = (f64::from(x) / f64::from(scale)).round() + f64::from(zero_point);
let mut qi = q as i64;
if qi < i64::from(qmin) {
qi = i64::from(qmin);
}
if qi > i64::from(qmax) {
qi = i64::from(qmax);
}
qi as i8
}
pub fn dequantize(q: i8, scale: f32, zero_point: i32) -> f32 {
scale * (i32::from(q) - zero_point) as f32
}
fn saturate_round_to_i8(v: f32) -> i8 {
(f64::from(v).round() as i64).clamp(-128, 127) as i8
}
fn build_lut(
input_scale: f32,
input_zero_point: i32,
output_scale: f32,
output_zero_point: i32,
f: impl Fn(f32) -> f32,
) -> [i8; LUT_SIZE] {
core::array::from_fn(|index| {
let x = index as i32 - 128;
let x_real = input_scale * (x - input_zero_point) as f32;
let y_q = output_zero_point as f32 + f(x_real) / output_scale;
saturate_round_to_i8(y_q)
})
}
pub fn build_sigmoid_lut(
input_scale: f32,
input_zero_point: i32,
output_scale: f32,
output_zero_point: i32,
) -> [i8; LUT_SIZE] {
build_lut(
input_scale,
input_zero_point,
output_scale,
output_zero_point,
|x| 1.0 / (1.0 + (-x).exp()),
)
}
pub fn build_tanh_lut(
input_scale: f32,
input_zero_point: i32,
output_scale: f32,
output_zero_point: i32,
) -> [i8; LUT_SIZE] {
build_lut(
input_scale,
input_zero_point,
output_scale,
output_zero_point,
f32::tanh,
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quantize_multiplier_cases() {
assert_eq!(quantize_multiplier(0.0), (0, 0));
assert_eq!(quantize_multiplier(1.0), (1 << 30, -1));
assert_eq!(quantize_multiplier(0.5), (1 << 30, 0));
assert_eq!(quantize_multiplier(1.0 - 1e-10), (1 << 30, -1));
let unity = build_requantizer(1.0, 1.0, 1.0, 0, -128, 127);
assert_eq!(unity.apply(42), 42);
let half = build_requantizer(1.0, 0.5, 1.0, 0, -128, 127);
assert_eq!(half.apply(100), 50);
}
#[test]
fn frexp_matches_c() {
for (x, m, e) in [
(1.0, 0.5, 1),
(0.75, 0.75, 0),
(3.0, 0.75, 2),
(-3.0, -0.75, 2),
(f64::MIN_POSITIVE, 0.5, -1021),
(f64::MIN_POSITIVE / 4.0, 0.5, -1023),
] {
assert_eq!(frexp(x), (m, e), "frexp({x})");
}
}
#[test]
fn quantize_rounds_half_away_and_saturates() {
assert_eq!(quantize(0.5, 1.0, 0, -128, 127), 1);
assert_eq!(quantize(-0.5, 1.0, 0, -128, 127), -1);
assert_eq!(quantize(1000.0, 1.0, 0, -128, 127), 127);
assert_eq!(quantize(-1000.0, 1.0, 0, -128, 127), -128);
assert_eq!(dequantize(-128, 0.5, -128), 0.0);
}
}