mixle.engines.formats module¶
Numeric format codecs across mixle’s precision spectrum: low-bit float, fixed-point, and codebook/VQ – the compression + “fixed precision” end, each with a provable error bound so a precision-allocation pass can pick the smallest format that preserves accuracy.
A NumericFormat is a lossy codec: quantize maps a float64 array to a compact
representation, dequantize maps it back, and max_abs_error / max_rel_error bound the round
trip. The bound is the “logic” a caller uses to spend minimal bits while keeping error under a target
(see min_float_mantissa_bits()). Fixed-point and codebook codecs store an actually smaller array
(real compression, vectorized in numpy); the float codec rounds to an n-bit float’s representable set
to measure that band’s accuracy (true sub-byte bit-packing is the Cython/C tail).
- class NumericFormat[source]
Bases:
objectBase codec:
quantize/dequantizeplus round-trip error bounds and a storage bit count.- name = 'identity'
- bits_per_value = 64.0
- measured_max_abs_error(x)[source]
Empirical max absolute round-trip error over
x(codecs also expose analytic bounds).
- class FloatFormat(mantissa_bits, exp_bits=11)[source]
Bases:
NumericFormatA float with
mantissa_bitsof mantissa – roundsxto that band’s representable set.Below ~52 mantissa bits this is lossy (fp4/fp8/fp16 storage); the round trip measures the band’s accuracy.
max_rel_error == 2**-(mantissa_bits+1)from round-to-nearest.- classmethod fp(total_bits)[source]
Build an
n-bit float format with an IEEE-like exponent/mantissa split (n = 1..1024+).- Parameters:
total_bits (int)
- Return type:
FloatFormat
- property max_rel_error: float
- class FixedPointFormat(frac_bits, int_bits=31)[source]
Bases:
NumericFormatFixed-point: store
round(x * 2**frac_bits)as an integer; real compression + a hard error bound.int_bitssets the representable magnitude[-2**int_bits, 2**int_bits); out-of-range clamps.max_abs_error == 2**-(frac_bits+1)(round-to-nearest), independent ofx.- property max_abs_error: float
- class CodebookFormat(codebook)[source]
Bases:
NumericFormatScalar vector-quantization: store an index into a learned codebook;
log2(K)bits per value.The genuine pure-numpy compression codec – quantize gathers the nearest code (an unsigned index array), dequantize gathers the code values back. Fit the codebook to data with
fit().- Parameters:
codebook (Any)
- classmethod fit(data, n_codes, iters=25, seed=0)[source]
Learn
n_codescodes by 1-D k-means (Lloyd) ondata; codes are the cluster means.
- compress(x)[source]
Quantize
xand bit-pack the indices to bytes: returns(packed_uint8, count).For
K <= 16codes the indices are sub-byte and pack8//bitsper byte, realizing the advertised compression (e.g. 16 codes -> 4-bit indices -> 2 values/byte -> 16x vs float64).
- min_float_mantissa_bits(target_rel_error)[source]
Smallest mantissa-bit count whose round-to-nearest relative error meets
target_rel_error.The error-tracing primitive: given a tolerated relative error, return the minimal float precision that preserves it – i.e. spend the fewest bits the accuracy budget allows.