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.
Performance depends on the fp32 baseline. On Apple silicon, cache-resident GEMM goes through the AMX
matrix coprocessor (Accelerate BLAS), which can make this popcount kernel slower; the advantage there is
storage and bandwidth rather than compute. On CPUs without a matrix unit, memory-bound problems, or
native binary/ternary models where fp32 wastes 32x the bytes, it can be a compute win. The kernel is
always exact; select it for the measured regime. The compiled extension is optional
(build_kernels.compile_bitpacked_kernels); a correct but 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).Both operands’ rank, packed word count, and word count implied by
dimare validated up front: the compiled kernel derives its loop bound froma_packedalone and has no way to notice a shorterb_packedon its own (MXR-080-0131), so a caller-visibleValueErrorhere is what stands between a mismatched pair and an out-of-bounds read in thenogilkernel.
- 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).Each operand’s sign and nonzero-mask planes must have identical shape (they describe the same rows), and the two operands’ packed word counts must match. The compiled kernel derives its loop bound from
a_signalone and indexes into the other three planes without any bounds check of its own (MXR-080-0131), so this validation is what turns a shorter plane into a clean exception instead of an out-of-bounds read.