Repository navigation
feat(quantization): add 1.0-bit BNN codec and vectorized XNOR-Popcount GEMM engine - #2460
Open
presentyourlove wants to merge 1 commit into
Open
presentyourlove wants to merge 1 commit into
presentyourlove wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implements a pure 1.0-bit Binary Neural Network (BNN) Codec (
BNNCodec) and a vectorized Bitwise XNOR + Popcount Matrix Multiplication Engine (BNNGemmEngine), replacing floating-point multipliers with single-cycle 64-parameter logic bitwise operations.Background & Motivation
While ternary quantization (such as BitNet 1.58b${-1, 0, +1}$ ) slashes weight bitwidths, it still retains zero values requiring integer arithmetic. On legacy CPUs (e.g. without AVX2) and ultra-low-power microcontrollers, integer add/sub ALUs remain a throughput and energy bottleneck.${-1, +1}$ , matrix dot products reduce to pure bitwise logic:
$$\text{BitwiseDot}(A, B) = 2 \times \text{Popcount}(\text{XNOR}(A, B)) - N$$
By restricting weights and activations strictly to binary
A single 64-bit general-purpose CPU register executes 64 parameter multiplications and additions in a single machine cycle, slashing inference energy consumption by >95% compared to FP16.
Key Changes
BNN Data Models (
src/exo/shared/types/bnn.py):BNNPackedTensor: Immutable data container packing 64 binary parameters intouint64words (1.0-bit effective bits per parameter, 16.0x compression vs FP16).BNNTelemetry: Tracks uncompressed FP16 equivalents, popcount operations count, and energy reduction ratios (>95%).BNN Codec & XNOR-Popcount GEMM Engine (
src/exo/worker/engines/quantization/bnn_kernel.py):BNNCodec:binarize_and_pack: Binarizes weights viauint64word with boundary padding.unpack_to_floats: Fast register-level lossless reconstruction.BNNGemmEngine:execute_bnn_gemm: Vectorized bitwise XNOR and Popcount execution implementingModule Exports (
src/exo/worker/engines/quantization/__init__.py):BNNCodecandBNNGemmEngine.Unit Tests (
src/exo/shared/tests/test_bnn_kernel.py):Verification
.venv/Scripts/pytest.exe src/exo/shared/tests/test_bnn_kernel.py # 4 passed in 0.21s.venv/Scripts/ruff.exe check src/exo/shared/types/bnn.py src/exo/worker/engines/quantization/bnn_kernel.py src/exo/worker/engines/quantization/__init__.py src/exo/shared/tests/test_bnn_kernel.py # All checks passed!Related Issues
人人都能跑llm.md(Pure 1.0-bit Binary Neural Network & XNOR-Popcount Kernel)