Convert between floats and integer codes
For a simple symmetric quantiser choose a scale and an integer limit Store and reconstruct Specify a tie rule for rounding. For example, Python’s round-to-nearest sends an exact tie to the even integer. With the integer codes fit in a signed 8-bit field.
If rounding alone produces a code in the allowed range, the absolute reconstruction error is at most If clipping changes the code, the error can exceed that bound. Fixing a narrow representable range improves resolution for small values at a given number of codes and can clip more outliers. Allowing per-group scales can adapt the resolution to groups with different magnitudes. Every scale also needs to be stored or recovered.
With and an input of maps to code and reconstructs as An input of maps to code after clipping and reconstructs as with error
Bound the effect on a prediction
Let be a weight vector, its reconstruction, and an input vector. With no clipping and common scale the triangle inequality gives
This is an absolute bound in exact arithmetic, for quantised weights and original inputs. Actual implementation may introduce more error during multiplication or accumulation. A very small original dot product can result in large relative error, even with a small absolute error.
For weights inputs and scale compare the quantised dot product with the unquantised dot product.
from fractions import Fraction as F
weights = [F(3, 10), F(-11, 20), F(1, 5)]
inputs = [2, -1, 4]
scale, limit = F(1, 4), 3
codes = [max(-limit, min(limit, round(w / scale))) for w in weights]
reference = sum(x * w for x, w in zip(inputs, weights))
approximation = sum(x * q * scale for x, q in zip(inputs, codes))
print(codes)
print(reference, approximation, abs(reference - approximation))
print(scale * sum(map(abs, inputs)) / 2)[1, -2, 1]
39/20 2 1/20
7/8To evaluate quantisation over a set of inputs, report the scale rule, tie rule, reference precision, absolute error and the downstream metric. Check that the held-out evaluation matches across runs. In the toy example the absolute error is well within the bound of Real input distributions can have very different errors from one chosen vector.
Exercises
At scale reconstruct the integer code
Show solution✓ Checked
Multiply the code by the scale, giving
At scale which code does get with ties to even and
Show solution✓ Checked
The scaled input is Its nearest even integer is The reconstructed value is and the absolute error is
For input and scale what absolute error bound follows when no weights clip?
Show solution✓ Checked
The one-norm is Multiply by half of to obtain