mixle.engines.qlut module¶
Quantized function lookup tables – every scalar nonlinearity becomes table[code].
The corollary of quantization: once an operand is the integer code round(x/step), any scalar
function f is a precomputed table indexed by that code – no transcendental ever runs. This turns the
nonlinear ops of a model (activations: sigmoid/tanh/GELU/SiLU/softplus; the exp/log that convert between
the log (LNS) and linear domains; the Gaussian-log of mixle.engines.lns) into integer gathers.
Combined with integer-add products and the integer logsumexp, a fully-quantized forward pass has
no floating-point exp/log at all.
Nearest-code lookup has error <= (step/2) * sup|f'| on the tabulated range (e.g. sigmoid: 0.125*step).
Unbounded functions (GELU, softplus, ReLU) extrapolate linearly beyond the range using the boundary slope,
so the table need only cover the curved region. Measured 1.5-8x faster than the real transcendental.
- class QuantizedFunction(func, step, lo, hi)[source]
Bases:
objectA scalar function tabulated over quantized inputs;
f(x) ≈ table[round(x/step)]with linear tails.- lookup(code)[source]
Gather directly from integer codes already in the quantized domain (the pure-integer path).
- quantized_activation(name, step=0.01, span=20.0)[source]
A common NN activation as a quantized LUT (
sigmoid/tanh/gelu/silu/softplus/relu).
- quantized_exp(log_step=0.01, lo_log=-30.0)[source]
expfrom an LNS log-code (units oflog_step) back to the linear domain – a pure table gather.Call with integer log-codes via
QuantizedFunction.lookup(); this is the softmax/attention “back to linear” step, with no realexp.
- quantized_logsumexp(scores, *, bits=12, span=24.0, weights=None)[source]
Log-sum-exp via an integer histogram over a
span/2^bitsgrid plus ONE2^bits-entry exp table.Shift by the max, round every score to its grid level, count scores per level (an integer histogram –
np.bincount), then a single dot with the exp table and one finallog:2^bitsexp evaluations total (the table – 12 bits = 32 KB, cache-resident pertable_bytes()) no matter how many scores there are, instead of oneexpper score.weightsmakes the histogram weighted, which is exactly the group-attention cell form (experiments/group_attention/RESULTS.md): passing per-cell integer counts computes the LSE of the token-level attention masslog sum_c count_c * exp(s_c)without ever expanding cells back to tokens.-infscores (masked slots) are dropped, matching softmax semantics.Error: rounding moves each score by at most
delta/2 = span / 2^(bits+1), the grid term oflse_error_bound(). Scores more thanspanbelow the max are CLIPPED UP to the bottom bin instead of contributing their true (much smaller) mass. For typical, roughly-bounded score distributions that clipped mass is negligible, but it is NOT bounded by the grid term alone – a clipped score paired with a large weight can dominate the sum. Calllse_error_bound(bits, span, scores=scores, weights=weights)with the same data to get a bound that accounts for this rather than assuming the grid term is the whole story.
- lse_error_bound(bits, span, *, scores=None, weights=None)[source]
Upper bound on
|quantized_logsumexp(...) - exact LSE|.With no data (
scores=None), this is JUST the grid termspan / 2^(bits+1)– the ordinary nearest-code rounding error. That alone is a COMPLETE bound only if no score ever falls more thanspanbelow the running max.quantized_logsumexp()instead CLIPS such scores up to the bottom bin, and nothing inbits/spanlimits how much weight a clipped score can carry, so the omitted tail is not generally negligible – concretely,scores=[0, -25], weights=[1, 1e12], bits=12, span=24has actual error~0.96against a grid-only bound of~0.0029, a ~300x understatement.Pass the SAME
scores/weightsgiven to (or about to be given to)quantized_logsumexp()to get a bound that also accounts for the clipped tail: the grid term pluslog1p(clipped_mass / in_span_mass), whereclipped_massis the (elevated) linear-domain contribution of out-of-span scores andin_span_massis the linear-domain contribution of everything else. Derivation: the true linear sum is at least the true in-span mass (clipped terms only add) and at most the true in-span mass plus the FULLY elevated clipped mass (elevation only increases a clipped term’s contribution, never decreases it); the true in-span mass is within the grid term (in log-space) of the computed in-span mass by the ordinary nearest-code argument. Chaining those bounds and converting to log space gives the expression above. Verified empirically: it upper-bounds the exact error on the adversarial example above (bound~3.67vs actual~0.96), stays within ~1e-9 of the plain grid term when nothing clips (matching this function’s pre-existing behavior), and held with zero violations across 20000 randomized trials with huge-dynamic-range weights.
- error_bound(sup_abs_derivative, step)[source]
The nearest-code lookup error bound
(step/2) * sup|f'|(e.g. sigmoidsup|f'|=0.25).
- table_bytes(step, lo, hi, itemsize=8)[source]
Bytes a table spanning
[lo, hi]atstepoccupies (cache-residency check).