# Run-time test of N.q8 (int8 quantization: v / s rounded half to even, clamped to -128..127, as a byte) against # values computed by PyTorch: torch.clamp(torch.round(v / s), -128, 127).to(torch.int8), in float32. q8's rounding works # on F32, which the proof checker does not evaluate, so it is pinned by this test instead of a law. # Inputs are exact F32 bit patterns. Build and run from this folder: # bend q8_test.bend -o q8_test && ./q8_test (prints each mismatch; exits 1 if any) import Base import ../bang.bend as B import ../nn.bend as N type Case is Data: Case{v: U32, s: U32, want: U32, why: String} def cases() -> List: [ Case{1056964608, 1065353216, 0, "half rounds to even 0 (v 0.5, s 1.0)"}, Case{1069547520, 1065353216, 2, "half rounds to even 2 (v 1.5, s 1.0)"}, Case{1075838976, 1065353216, 2, "half rounds to even 2 (v 2.5, s 1.0)"}, Case{3204448256, 1065353216, 0, "-half rounds to 0 (v -0.5, s 1.0)"}, Case{3217031168, 1065353216, 254, "-1.5 rounds to -2 (v -1.5, s 1.0)"}, Case{3223322624, 1065353216, 254, "-2.5 rounds to -2 (v -2.5, s 1.0)"}, Case{1056964607, 1065353216, 0, "just below a half (v 0.4999999701976776, s 1.0)"}, Case{1056964609, 1065353216, 1, "just above a half (v 0.5000000596046448, s 1.0)"}, Case{3223322623, 1065353216, 254, "just above -2.5 (v -2.499999761581421, s 1.0)"}, Case{3223322625, 1065353216, 253, "just below -2.5 (v -2.500000238418579, s 1.0)"}, Case{1123876864, 1065353216, 126, "126.5 rounds to 126 (v 126.5, s 1.0)"}, Case{1124007936, 1065353216, 127, "127.5 rounds to 128, clamps to 127 (v 127.5, s 1.0)"}, Case{1124073472, 1065353216, 127, "clamps to 127 (v 128.0, s 1.0)"}, Case{1232348160, 1065353216, 127, "large clamps to 127 (v 1000000.0, s 1.0)"}, Case{3271557120, 1065353216, 128, "-128 kept (v -128.0, s 1.0)"}, Case{3271589888, 1065353216, 128, "-128.5 rounds to -128 (v -128.5, s 1.0)"}, Case{3271622656, 1065353216, 128, "clamps to -128 (v -129.0, s 1.0)"}, Case{3379831808, 1065353216, 128, "large negative clamps to -128 (v -1000000.0, s 1.0)"}, Case{0, 228737632, 0, "zero with the smallest scale (v 0.0, s 1.0000000031710769e-30)"}, Case{242443152, 228737632, 3, "tiny values at the smallest scale (v 3.0000000095132306e-30, s 1.0000000031710769e-30)"}, Case{1051931443, 1036831949, 4, "0.35 / 0.1 is exactly 3.5 in F32: half to even 4 (v 0.3499999940395355, s 0.10000000149011612)"}, Case{1050253722, 1008981770, 30, "an ordinary value (v 0.30000001192092896, s 0.009999999776482582)"}, Case{3236429824, 1030349233, 129, "an ordinary negative value (v -7.25, s 0.057100001722574234)"}, Case{1095447347, 1036831949, 127, "the row maximum maps to 127 (v 12.699999809265137, s 0.10000000149011612)"}, Case{3242930995, 1036831949, 129, "the negated row maximum maps to -127 (v -12.699999809265137, s 0.10000000149011612)"}, Case{2147483648, 1065353216, 0, "negative zero is byte 0 (v -0.0, s 1.0)"} ] def check_eq(ok: Bool, +want: U32, +got: U32, why: String) -> IO(Nat): match ok: case True{}: IO.pure(Nat, 0n) case False{}: do IO: IO.print("FAIL " ++ why ++ ": byte " ++ U32.show(got) ++ ", torch " ++ U32.show(want)) return 1n def check_got(+want: U32, why: String, +got: U32) -> IO(Nat): check_eq(U32.is_eq(got, want), want, got, why) @unsafe def check(c: Case) -> IO(Nat): match c: case Case{+v, +s, +want, why}: check_got(want, why, N.q8(B.fv(v), B.fv(s))) @unsafe def run(xs: List, +fails: Nat, +n: Nat) -> IO(Nat & Nat): match xs: case []: IO.pure(Nat & Nat, (fails, n)) case c <> t: do IO: f : Nat <- check(c) run(t, Nat.add(fails, f), 1n+n) def report_exit(ok: Bool) -> IO(Unit): match ok: case True{}: IO.pure(Unit, Unit{}) case False{}: IO.exit(1) def report(r: Nat & Nat) -> IO(Unit): match r: case (+fails, +n): do IO: IO.print("q8: " ++ Nat.show(Nat.sub(n, fails)) ++ " of " ++ Nat.show(n) ++ " cases match torch") report_exit(Nat.is_eq(fails, 0n)) @unsafe def main() -> IO(Unit): do IO: r : Nat & Nat <- run(cases(), 0n, 0n) report(r)