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 to uint64 words; last axis padded to a 64-multiple.

Parameters:

x (Any)

Return type:

ndarray

binary_gemm(a_packed, b_packed, dim)[source]

Exact A @ B.T for {-1,+1} matrices packed by pack_pm1().

a_packed is (N, words) packed rows of A, b_packed is (M, words) packed rows of B (the operand whose columns are dotted), dim is the true bit length. Returns int32 (N, M).

Parameters:
Return type:

ndarray

binary_dot(a, b)[source]

Exact dot products of a batch of {-1,+1} vectors a (N, D) against b (M, D). Returns (N, M).

Parameters:
Return type:

ndarray

ternary_gemm(a_sign, a_nz, b_sign, b_nz)[source]

Exact {-1,0,+1} A @ B.T from packed sign + nonzero-mask bit-planes (compiled path only).

Parameters:
Return type:

ndarray

pack_ternary(x)[source]

Pack a {-1,0,+1} array’s rows into (sign, nonzero) bit-plane uint64 words for ternary_gemm().

Parameters:

x (Any)

Return type:

tuple[ndarray, ndarray]