mixle.engines.bitpacked module¶
Packed binary / ternary COMPUTE: exact {-1,+1} and {-1,0,+1} dot products via popcount.
The one sub-byte path that is real arithmetic (not dequant-then-fp32): pack a vector to bits and
a . b = D - 2*popcount(a XOR b) (binary) / a two-plane popcount (ternary). The hardware popcount does
64 lanes per instruction with no rounding, and the packed data is 32x smaller than float64.
HONEST PERFORMANCE: whether this beats fp32 depends entirely on the fp32 baseline. Measured on Apple
silicon, a cache-resident GEMM goes through the AMX matrix coprocessor (Accelerate BLAS), which is so fast
that this popcount kernel is ~2x SLOWER – here the win is storage/bandwidth, not compute. On a CPU
without a matrix unit (popcount vs AVX-512 fp32), on memory-bound problems, or for genuinely binary/ternary
models where fp32 wastes 32x the bytes, it is a compute win. The kernel is always exact; pick it by the
regime, not blindly. The compiled extension is optional (build_kernels.compile_bitpacked_kernels); a
correct (slower) numpy bitwise_count fallback runs when it is absent.
- pack_pm1(x)[source]
Pack a
{-1,+1}(or{0,1}) array’s rows touint64words; last axis padded to a 64-multiple.
- binary_gemm(a_packed, b_packed, dim)[source]
Exact
A @ B.Tfor{-1,+1}matrices packed bypack_pm1().a_packedis(N, words)packed rows of A,b_packedis(M, words)packed rows of B (the operand whose columns are dotted),dimis the true bit length. Returnsint32(N, M).
- binary_dot(a, b)[source]
Exact dot products of a batch of
{-1,+1}vectorsa(N, D) againstb(M, D). Returns (N, M).
- ternary_gemm(a_sign, a_nz, b_sign, b_nz)[source]
Exact
{-1,0,+1}A @ B.Tfrom packed sign + nonzero-mask bit-planes (compiled path only).