import numpy as np import pytest from quantize import (QMAX, QMIN, affine_qparams, dequantize, int8_linear, int8_matmul, quantize, symmetric_scale) def test_worked_example() -> None: s, z = affine_qparams(-1.0, 3.0) assert s == pytest.approx(4.0 / 255.0) assert z == -64 q = quantize(np.array([0.5]), s, z) assert int(q[0]) == -32 assert dequantize(q, s, z)[0] == pytest.approx(0.50196, abs=1e-5) def test_zero_is_exact_and_range_covers_zero() -> None: for lo, hi in [(-1.0, 3.0), (0.2, 5.0), (-4.0, -0.5), (-0.37, 0.91)]: s, z = affine_qparams(lo, hi) assert dequantize(quantize(np.array([0.0]), s, z), s, z)[0] == 0.0 def test_round_trip_error_is_at_most_half_a_step() -> None: rng = np.random.default_rng(0) x = rng.uniform(-1.0, 3.0, 10_000) s, z = affine_qparams(-1.0, 3.0) err = dequantize(quantize(x, s, z), s, z) - x assert np.max(np.abs(err)) <= s / 2 + 1e-12 def test_out_of_range_saturates() -> None: s, z = affine_qparams(-1.0, 3.0) q = quantize(np.array([-10.0, 10.0]), s, z) assert q.tolist() == [QMIN, QMAX] def test_integer_matmul_equals_float_on_dequantised_values() -> None: rng = np.random.default_rng(1) w = rng.normal(0, 0.3, (16, 40)) x = rng.uniform(-1, 3, (8, 40)) sx, zx = affine_qparams(-1, 3) sw = symmetric_scale(w) qx, qw = quantize(x, sx, zx), quantize(w, sw) acc = int8_matmul(qx, zx, qw) assert acc.dtype == np.int32 ref = dequantize(qx, sx, zx) @ dequantize(qw, sw).T assert np.allclose(sx * sw * acc, ref, rtol=0, atol=1e-12) def test_worst_case_accumulator_does_not_overflow() -> None: k = 4096 qx = np.full((1, k), QMAX, dtype=np.int8) qw = np.full((1, k), QMIN + 1, dtype=np.int8) # -127, symmetric weights acc = int8_matmul(qx, QMIN, qx * 0 + qw) # (127 - (-128)) * (-127) per term assert int(acc[0, 0]) == k * 255 * -127 def test_quantised_layer_close_to_float() -> None: rng = np.random.default_rng(0) w = rng.normal(0.0, 0.2, (64, 128)) b = rng.normal(0.0, 0.1, 64) x = rng.uniform(-1.0, 3.0, (256, 128)) y = x @ w.T + b rel = {} for pc in (False, True): e = int8_linear(x, w, b, (-1.0, 3.0), per_channel=pc) - y rel[pc] = np.sqrt(np.mean(e**2)) / np.sqrt(np.mean(y**2)) assert rel[False] < 0.02 and rel[True] < 0.02 assert rel[True] <= rel[False] def test_per_channel_scales_shape() -> None: w = np.array([[0.5, -1.0], [0.01, 0.02]]) s = symmetric_scale(w, axis=1) assert s.shape == (2,) assert s[0] == pytest.approx(1.0 / 127) and s[1] == pytest.approx(0.02 / 127)