03-memory-footprint-estimation showed that int8 storage costs a quarter of float32's. Getting there requires actually mapping real-valued numbers onto the 256 discrete integers int8 can hold — not by truncating decimals (that would throw away almost all the useful range), but by finding an affine transformation that uses the entire integer range for whatever the data's own range actually is.
Implement compute_scale_zero_point(x, num_bits=8), which finds the one scale and one zero_point that map x's actual [min, max] onto the integer type's full representable range, and quantize(x, num_bits=8), which applies that mapping and rounds to the nearest integer.
num_bits=8 means the target range is [-128, 127] (signed int8).scale: a positive float, the size of one integer "step" in the original float units.zero_point: an integer in the target range, the integer value that represents float 0.0.x.max() == x.min()) must not divide by zero — fall back to scale=1.0.quantize returns (q, scale, zero_point), q as np.int8, values always clipped into the valid range even after rounding.Open one at a time. Each gives away a little more than the last.
scale = (x.max() - x.min()) / (qmax - qmin) — the float range divided by the integer range gives exactly the size of one integer step.
Once scale is known, x.min() in float units should land exactly on qmin in integer units: zero_point = round(qmin - x.min() / scale), clipped into range in case of rounding at the boundary.
No results yet
Run your code to check it against the examples, or submit to run every test.