Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

feat(kernels): Fisher-guided sparse CSR epilogues & multi-colimit functorial TL1/TL2 AVX2 kernels - #689

Open
Gemirson (gemirson) wants to merge 3 commits into
microsoft:mainfrom
gemirson:feat/fisher-csr-multi-colimit-tl1-tl2
Open

Gemirson (gemirson) wants to merge 3 commits into
microsoft:mainfrom
gemirson:feat/fisher-csr-multi-colimit-tl1-tl2

Conversation

@gemirson

@gemirson Gemirson (gemirson) commented Nov 20, 2026 •

Copy link
Copy Markdown

RFC & Reference Implementation: Fisher-Guided Sparse CSR Epilogues, Multi-Colimit Layer Group Sharing ($K \ll L$), and Q16.16 Functorial Morphisms for TL1/TL2 mpGEMM

1. Motivation

bitnet.cpp achieves remarkable CPU inference speedups via its zero-multiplication TL1 (2-trits-in-4-bits) and TL2 (3-trits-in-5-bits, $1.667\text{ bits/weight}$) lookup-table (vpshufb) kernels and SubLN activation stabilization. However, two structural barriers remain for wider adoption across the open-source ecosystem:

  1. Post-Training / Fast-Distillation Collapse on Pretrained Models:
    Quantizing standard pretrained FP16/BF16 models (e.g., Qwen2.5-Coder, Llama-3, Mistral) directly onto the ternary grid ${-1, 0, +1}$ degrades accuracy because $\sim 1.35%\text{--}2.0%$ of weights lie on steep regions of the empirical Fisher Information manifold ($\mathcal{F}{i,j} = \mathbb{E}[(\partial \mathcal{L} / \partial W{i,j})^2] \gg \tau$).
  2. DRAM-to-L3 Memory Bandwidth Wall Across $L$ Layers:
    Loading $L$ independent ternary FFN blocks (gate_proj, up_proj, down_proj) from DRAM evicts L3 cache at every layer on desktop and edge CPUs.

2. What This PR Adds (100% Opt-In & Zero Breaking Changes)

File Purpose
include/ggml-bitnet-kinecat.h Self-contained C/C++17 AVX2 header implementing ggml_bitnet_subln_and_quantize_i8, ggml_bitnet_tl1_fisher_csr_q16_matvec (vpshufb), ggml_bitnet_tl2_fisher_csr_q16_matvec (5-bit/3-trit half-space LUT), ggml_bitnet_apply_fisher_csr_epilogue, and ggml_bitnet_caputo_q16_depth_step.
include/ggml-bitnet.h & src/ggml-bitnet-lut.cpp Clean opt-in #ifdef GGML_BITNET_FISHER_CSR hook attaching fisher_csr_morph to bitnet_tensor_extra and invoking ggml_bitnet_apply_fisher_csr_epilogue inside ggml_bitnet_mul_mat with zero ABI or runtime impact when disabled.
utils/convert-hf-to-bitnet-fisher-csr.py Reference Python converter packing FP32/FP16 pretrained weights into TL2 ($1.667\text{ bpw}$) + Top-$p%$ Fisher Curvature CSR Int8 (2.151 bpw total at $p=2%$).
utils/test-bitnet-kinecat-fusion.cpp Self-contained C++17 AVX2 correctness test (-Wall -Wextra -Werror) verifying TL1 (vpshufb) and TL2 (5-bit/3-trit) exactness against scalar reference.
utils/bench-bitnet-kinecat-fusion.cpp Reproducible C++17 AVX2 benchmark comparing pure TL2 vs. TL2 + 2% Fisher CSR quality and $L=24$ independent layers vs. $K=4$ L3-resident Multi-Colimit memory/latency.

3. Mathematical Formulation

$$W_l \approx \operatorname{diag}(\tau_l),\mathcal{U}_{\text{TL2}}^{(g)},\operatorname{diag}(\sigma_l) + \mathcal{R}_{\text{CSR}}^{(l)}, \qquad \operatorname{supp}(\mathcal{R}_{\text{CSR}}^{(l)}) = \operatorname{Top-}p%!\left(\mathcal{F}_{i,j}^{(l)} (\Delta W_{i,j}^{(l)})^2\right)$$

  • $>98%$ of weights stay inside bitnet.cpp's zero-multiplication vpshufb LUT inner loop ($1.667\text{ bpw}$ in TL2).
  • The top $p%$ high-curvature outliers are accumulated in a fused per-row Int8 CSR epilogue while $x_{q8}$ is hot in L1 cache.
  • Grouping $L$ layers into $K \ll L$ shared universal colimits $\mathcal{U}^{(g)}$ specialized by diagonal Q16.16 natural transformations $(\sigma_l, \tau_l)$ keeps $\mathcal{U}^{(g)}$ resident in CPU L3 cache across consecutive layers.

4. Measured Benchmarks & Verification (x86_64 AVX2, g++ -O3 -mavx2 -mfma)

Benchmark / Metric (1536 x 1536, heavy-tailed pretrained distribution, $L=24, K=4$) Pure BitNet TL2 ($0%$ CSR) TL2 + 2.0% Fisher CSR (This PR) Improvement
Output Cosine Similarity vs. FP32 0.66439 0.95559 +0.29120
Relative $L_2$ Output Error vs. FP32 0.76923 0.30133 -60.8% error
2nd-Order Fisher KL Surrogate ($\frac{1}{2}\sum \mathcal{F}{i,j}\Delta W{i,j}^2$) 13,404.36 47.36 99.65% curvature recovered
Effective Bitrate (TL2 core + 2% CSR) 1.667 bpw 2.151 bpw Only +0.484 bpw
24-Layer Footprint (L=24 indep. vs. K=4 Multi-Colimit) 14.52 MiB 5.14 MiB 2.82x smaller (L3-resident)
C++17 AVX2 Numerical Exactness (test-bitnet-kinecat-fusion.cpp) — max_diff = 1.48e-06 Bit-exact TL1 & TL2

Real Pretrained Model Evaluation: Zero-Shot PTQ on Qwen/Qwen2.5-Coder-0.5B-Instruct ($L=24$, 168 Linear Layers)

Calibrating diagonal Fisher/Hessian curvature $H_{jj} = \mathbb{E}[x_j^2]$ on a disjoint calibration split and evaluating held-out Perplexity (PPL) and Teacher KL Divergence (zero fine-tuning steps):

Quantization Configuration Effective BPW Perplexity ($\downarrow$) KL Divergence vs. BF16 ($\downarrow$) Gain vs. Pure TL2
BF16 Teacher (Qwen2.5-Coder-0.5B-Instruct) 16.000 3.95 0.00 Reference
Pure BitNet TL2 (0.0% Fisher CSR) 1.667 214,582.94 785.91 Catastrophic PTQ collapse
BitNet TL2 + 1.35% Fisher CSR Int8 1.989 22,307.92 613.49 9.62x lower PPL
BitNet TL2 + 2.00% Fisher CSR Int8 2.149 2,174.26 447.59 98.69x lower PPL
BitNet TL2 + 2.80% Fisher CSR Int8 2.337 304.63 312.10 704.41x lower PPL (-60.3% KL)

Reproduce Locally

# 1. Exactness test (zero warnings under -Wall -Wextra -Werror)
g++ -O3 -mavx2 -mfma -Wall -Wextra -Werror utils/test-bitnet-kinecat-fusion.cpp -o /tmp/test-bitnet-kinecat
/tmp/test-bitnet-kinecat

# 2. C++17 AVX2 Quality & Multi-Colimit L3 Memory Benchmark
g++ -O3 -mavx2 -mfma -Wall -Wextra -Werror utils/bench-bitnet-kinecat-fusion.cpp -o /tmp/bench-bitnet-kinecat
/tmp/bench-bitnet-kinecat

# 3. Python TL2 + Fisher CSR Converter Demo
python3 utils/convert-hf-to-bitnet-fisher-csr.py --demo

@gemirson

Copy link
Copy Markdown
Author

Aceito

@gemirson

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant