mixle.engines.lns module¶
Logarithmic number system for mixle’s log-space compute – quantize by a fixed log constant ln(C).
mixle already works in log-space (log-densities, log-weights, log-sum-exp), so the natural quantization is
on the log value: store v as the integer k = round(ln(v) / s) with step s = ln(C), i.e.
v = C**k. Then the two hot operations become integer arithmetic:
multiplying probabilities = adding log-probs = adding the integers
k1 + k2– exact, no table.log-sum-exp (adding probabilities – the mixture / HMM / marginalization op) becomes
max(k1, k2) + LUT[|k1 - k2|]whereLUT[d] = round(log(1 + exp(-d*s)) / s)is a small precomputed integer table (the Gaussian logarithm). Noexp, nolog– integermax+ a gather. This is the transcendental reduction that dominates mixture scoring, and unlike a GEMM it has no BLAS to lose to: measured ~4x faster than float64logsumexpin pure numpy (more with a compiled integer kernel).
step is the precision dial – the fp1..fpN spectrum, but in the log domain where it is natural: each
unit is a factor of C = exp(step), so the relative precision of a stored value is ~``step/2`` and the
log-sum-exp error is bounded by ~``step``. Smaller step -> finer + wider integer range (int16 at step~0.1,
int32 at step~1e-3). The model’s log-parameters and the data terms are quantized by the SAME step, so the
whole score is integer arithmetic.
- class LogNumberSystem(step=1e-2)[source]
Bases:
objectQuantize log-space values to integers in units of
step = ln(C)and compute on the integers.- Parameters:
step (float)
- classmethod from_relative_precision(rel)[source]
Build a system whose stored values are accurate to ~``rel`` relative (
step = ln(1+rel)).- Parameters:
rel (float)
- Return type:
LogNumberSystem
- property max_logsumexp_error: float
Bound on the absolute log-sum-exp error from quantization + LUT rounding (~one step per fold).
- quantize(log_values)[source]
Round log-space values to integer multiples of
step(the stored representation).
- dequantize(k)[source]
Recover the float log-value
k * step.
- logadd(k1, k2)[source]
Integer Gaussian logarithm:
logsumexpof two quantized log-values ->max + LUT[|diff|].
- logsumexp(k, axis=-1)[source]
Integer log-sum-exp along
axisvia a pairwise tree oflogadd()(no exp/log).Uses the compiled one-pass tree kernel for the common 2-D last-axis reduction when available (bit-identical to the numpy tree, ~8x faster); falls back to vectorized numpy otherwise.