use crate::activation::{Activation, Relu, Sigmoid, Tanh};
use crate::lut::{SIGMOID_Q8_8, SIGMOID_Q16_16, TANH_Q8_8, TANH_Q16_16};
use crate::qformat::{Q8_8, Q16_16};
fn congruent(x: i64, y: i64, bits: u32) -> bool {
(x.wrapping_sub(y)).rem_euclid(1i64 << bits) == 0
}
macro_rules! format_proofs {
($module:ident, $s:ty, $frac:expr) => {
mod $module {
use super::congruent;
use crate::qformat::{Fixed, QValue};
type Q = QValue<$s, $frac>;
const FRAC: u32 = $frac;
const BITS: u32 = <$s>::BITS;
fn any() -> Q {
Q::from_raw(kani::any())
}
fn wide(q: Q) -> i64 {
i64::from(q.raw())
}
fn bound_for_sign(a: Q) -> i64 {
if a.raw() > 0 {
i64::from(<$s>::MAX)
} else if a.raw() < 0 {
i64::from(<$s>::MIN)
} else {
0
}
}
#[kani::proof]
fn add_and_sub_wrap() {
let (a, b) = (any(), any());
let mut c = a;
c += b;
assert!(congruent(wide(a + b), wide(a) + wide(b), BITS));
assert!(congruent(wide(c), wide(a) + wide(b), BITS));
assert!(congruent(wide(a - b), wide(a) - wide(b), BITS));
}
#[kani::proof]
fn mul_floors_then_wraps() {
let (a, b) = (any(), any());
let p = i128::from(wide(a)) * i128::from(wide(b));
let f: i64 = kani::any();
let scaled = i128::from(f) * (1i128 << FRAC);
kani::assume(scaled <= p && p - scaled < (1i128 << FRAC));
assert!(congruent(wide(a * b), f, BITS));
}
#[kani::proof]
fn div_never_panics() {
let _ = any() / any();
let _ = any().div_int(kani::any());
}
#[kani::proof]
fn from_int_scales_then_wraps() {
let n: i32 = kani::any();
assert!(congruent(
wide(Q::from_int(n)),
i64::from(n) * (1i64 << FRAC),
BITS
));
}
#[kani::proof]
fn pow2_neg_is_exact_or_zero() {
let n: u32 = kani::any();
let r = wide(Q::pow2_neg(n));
if n > FRAC {
assert!(r == 0);
} else {
assert!(congruent(r << n, 1i64 << FRAC, BITS + n));
}
}
#[kani::proof]
fn zero_tolerance_matches_definition() {
let a = any();
let tolerance = 1i64 << if FRAC > 7 { FRAC - 7 } else { 1 };
assert!(a.is_within_zero_tolerance() == (wide(a).abs() < tolerance));
}
#[kani::proof]
fn comparisons_are_on_the_raw_value() {
let (a, b) = (any(), any());
assert!((a < b) == (a.raw() < b.raw()));
assert!((a == b) == (a.raw() == b.raw()));
}
}
};
}
format_proofs!(q16_16, i32, 16);
format_proofs!(q8_8, i16, 8);
format_proofs!(q31_1, i32, 1);
format_proofs!(q1_31, i32, 31);
format_proofs!(q15_1, i16, 1);
format_proofs!(q1_15, i16, 15);
macro_rules! activation_proofs {
($name:ident, $q:ty, $s:ty, $act:ty, $table:expr) => {
#[kani::proof]
fn $name() {
let x = <$q>::from_raw(kani::any());
let y = <$act>::activate(x).raw();
let first = $table[0] as $s;
let last = $table[$table.len() - 1] as $s;
assert!(first <= y && y <= last);
let _ = <$act>::derivative(<$q>::from_raw(kani::any()));
}
};
}
activation_proofs!(tanh_q16_16_in_range, Q16_16, i32, Tanh, TANH_Q16_16);
activation_proofs!(
sigmoid_q16_16_in_range,
Q16_16,
i32,
Sigmoid,
SIGMOID_Q16_16
);
activation_proofs!(tanh_q8_8_in_range, Q8_8, i16, Tanh, TANH_Q8_8);
activation_proofs!(sigmoid_q8_8_in_range, Q8_8, i16, Sigmoid, SIGMOID_Q8_8);
#[kani::proof]
fn relu_definition() {
let x = Q16_16::from_raw(kani::any());
let y = Relu::activate(x);
assert!(y.raw() == x.raw().max(0));
let d = Relu::derivative(y);
assert!(d == Q16_16::ZERO || d == Q16_16::ONE);
let x = Q8_8::from_raw(kani::any());
assert!(Relu::activate(x).raw() == x.raw().max(0));
}
mod int8 {
use crate::int8::{
LUT_SIZE, QDense, QDensePerChannel, QSoftmax, Requantizer, apply_lut,
exp_on_negative_values, multiply_by_quantized_multiplier, one_over_one_plus_x,
rounding_divide_by_pot, rounding_high_mul_divide_by_pot,
};
fn two_step(x: i32, m: i32, e: i32) -> i32 {
let h = if x == i32::MIN && m == i32::MIN {
i32::MAX
} else {
let ab = i64::from(x) * i64::from(m);
let nudge: i64 = if ab >= 0 { 1 << 30 } else { 1 - (1 << 30) };
((ab + nudge) / (1i64 << 31)) as i32
};
let mask = ((1u32 << e) - 1) as i32;
let threshold = (mask >> 1) + i32::from(h < 0);
(h >> e) + i32::from((h & mask) > threshold)
}
#[kani::proof]
fn fused_requantize_matches_two_step() {
let (x, m, e): (i32, i32, i32) = (kani::any(), kani::any(), kani::any());
kani::assume(m >= 0 && (2..=31).contains(&e));
assert!(rounding_high_mul_divide_by_pot(x, m, e) == two_step(x, m, e));
}
#[kani::proof]
fn requantizer_never_panics() {
let (x, m, s): (i32, i32, i32) = (kani::any(), kani::any(), kani::any());
let _ = rounding_divide_by_pot(x, s);
let _ = multiply_by_quantized_multiplier(x, m, s);
let rq = Requantizer {
multiplier: m,
shift: s,
zero_point: kani::any(),
qmin: kani::any(),
qmax: kani::any(),
};
let y = rq.apply(x);
if rq.qmin <= rq.qmax {
assert!(rq.qmin <= y && y <= rq.qmax);
}
}
#[kani::proof]
fn dense_never_panics() {
let weights: [[i8; 3]; 2] = kani::any();
let biases: [i32; 2] = kani::any();
let with_bias: bool = kani::any();
let layer = QDense {
weights: &weights,
biases: if with_bias { Some(&biases) } else { None },
input_zero_point: kani::any(),
requantizer: Requantizer {
multiplier: kani::any(),
shift: kani::any(),
zero_point: kani::any(),
qmin: kani::any(),
qmax: kani::any(),
},
};
let input: [i8; 3] = kani::any();
let _ = layer.forward(&input);
}
#[kani::proof]
fn dense_per_channel_never_panics() {
let weights: [[i8; 3]; 2] = kani::any();
let biases: [i32; 2] = kani::any();
let requantizers: [Requantizer; 2] = [
Requantizer {
multiplier: kani::any(),
shift: kani::any(),
zero_point: kani::any(),
qmin: kani::any(),
qmax: kani::any(),
},
Requantizer {
multiplier: kani::any(),
shift: kani::any(),
zero_point: kani::any(),
qmin: kani::any(),
qmax: kani::any(),
},
];
let layer = QDensePerChannel {
weights: &weights,
biases: if kani::any() { Some(&biases) } else { None },
input_zero_point: kani::any(),
requantizers: &requantizers,
};
let input: [i8; 3] = kani::any();
let _ = layer.forward(&input);
}
#[kani::proof]
fn softmax_parts_never_panic() {
let _ = exp_on_negative_values(kani::any());
let _ = one_over_one_plus_x(kani::any());
}
#[kani::proof]
fn softmax_never_panics() {
let softmax = QSoftmax {
input_multiplier: kani::any(),
input_left_shift: kani::any(),
diff_min: kani::any(),
};
let input: [i8; 2] = kani::any();
let _ = softmax.forward(&input);
}
#[kani::proof]
fn lut_never_panics() {
let lut: [i8; LUT_SIZE] = [0; LUT_SIZE];
let _ = apply_lut(kani::any(), &lut);
}
}