Extreme Quantization: Binary, Ternary & 1-bit LLMs

Pushing quantization to the limit: binary neural networks (XNOR-Net), ternary weights, BitNet, 1.58-bit LLMs, the scaling laws for extreme quantization, and when sub-4-bit models are viable.

Advanced

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Binary Neural Networks
  5. XNOR-Net: Binary Weights and Activations
  6. Ternary Quantization
  7. BitNet: 1-bit Transformer
  8. BitNet b1.58: Ternary LLMs
  9. Scaling Laws for Extreme Quantization
  10. Hardware Implications
  11. Common Pitfalls
  12. Summary
  13. Exercises

Learning Objectives

  1. Derive binary network forward/backward passes with sign function and STE.
  2. Explain why XNOR replaces multiplication for binary matmul.
  3. Describe BitNet's training strategy for 1-bit transformers.
  4. Analyze the width-vs-precision tradeoff at extreme quantization.
  5. Evaluate when 1-bit models are practical vs. when they fail.

Notation

  • sign(x)={+1x01x<0\text{sign}(x) = \begin{cases}+1 & x \geq 0 \\ -1 & x < 0\end{cases}
  • {1,0,+1}\{-1, 0, +1\} — ternary weight values
  • BPW — bits per weight

Core Intuition

At 1-2 bits, we're no longer "quantizing" in the traditional sense — we're fundamentally changing what weights represent. A binary weight {1,+1}\{-1, +1\} means matrix multiplication becomes addition/subtraction (no multiplier needed). A ternary weight {1,0,+1}\{-1, 0, +1\} adds sparsity. The key insight (BitNet): if you make the model WIDER (more parameters), you can compensate for the precision loss and match FP16 quality at the same compute budget.

Binary / Ternary Quantization

Weights → {-1, 0, +1}-1-100+1+1+1+1-1+1-1+1XNOR shortcuta ⊙ b → popcount(XNOR)10 active ops (32× faster)MSE: 0.2494
Threshold
0.15
+10-1
Explore: Ternary weights {-1, 0, +1} enable XNOR-popcount matrix multiply — extreme compression at the cost of approximation error controlled by the zero threshold.

Binary Neural Networks

Binary weights: wb=sign(w){1,+1}w_b = \text{sign}(w) \in \{-1, +1\}.

Forward pass:

y=α(wbTx)=α(isign(wi)xi),(1)y = \alpha \cdot (w_b^T x) = \alpha \cdot \left(\sum_i \text{sign}(w_i) \cdot x_i\right), \tag{1}

where α=1niwi\alpha = \frac{1}{n}\sum_i |w_i| (mean magnitude as scaling factor).

Matmul simplification: Since wb{1,+1}w_b \in \{-1, +1\}:

wbx={+xwb=+1xwb=1(2)w_b \cdot x = \begin{cases}+x & w_b = +1 \\ -x & w_b = -1\end{cases} \tag{2}

No multiplication needed — only additions and subtractions.

Training (STE):

Lw=Lwb1w1.(3)\frac{\partial\mathcal{L}}{\partial w} = \frac{\partial\mathcal{L}}{\partial w_b} \cdot \mathbf{1}_{|w| \leq 1}. \tag{3}

XNOR-Net: Binary Weights and Activations

Both weights AND activations binarized:

wb=sign(w),xb=sign(x).(4)w_b = \text{sign}(w), \quad x_b = \text{sign}(x). \tag{4}

XNOR trick: For binary values {1,+1}\{-1, +1\}:

wbxb=2XNOR(w~,x~)1,(5)w_b \cdot x_b = 2 \cdot \text{XNOR}(\tilde{w}, \tilde{x}) - 1, \tag{5}

where w~=(wb+1)/2{0,1}\tilde{w} = (w_b + 1)/2 \in \{0, 1\}.

Matmul becomes bitwise XNOR + popcount:

wbTxb=n2popcount(w~x~).(6)w_b^T x_b = n - 2 \cdot \text{popcount}(\tilde{w} \oplus \tilde{x}). \tag{6}

Speedup: 58x theoretical speedup over FP32 matmul (single XNOR + popcount vs nn multiply-accumulates).

Quality: 10-15% accuracy loss on ImageNet (too much for practical use in 2016, but a landmark result).


Ternary Quantization

Ternary weights: wt{1,0,+1}w_t \in \{-1, 0, +1\} with learned threshold Δ\Delta:

wt={+1w>Δ0wΔ1w<Δ(7)w_t = \begin{cases}+1 & w > \Delta \\ 0 & |w| \leq \Delta \\ -1 & w < -\Delta\end{cases} \tag{7}

Advantages over binary:

  • Zero values introduce sparsity → skip computation.
  • Three levels better approximate Gaussian weight distribution.
  • Much smaller quality loss than binary (2-5% vs 10-15% on ImageNet).

Bits per weight: log2(3)1.58\log_2(3) \approx 1.58 bits (between binary and 2-bit).


BitNet: 1-bit Transformer

BitNet (Wang et al., 2023): Replace all linear layers in a transformer with binary weights:

W^=sign(W)W1nm.(8)\hat{\mathbf{W}} = \text{sign}(\mathbf{W}) \cdot \frac{\|\mathbf{W}\|_1}{nm}. \tag{8}

Key modifications:

  1. Binary weights with mean-absolute-value scaling.
  2. Activations quantized to INT8 (not binary — too much quality loss).
  3. LayerNorm before every linear layer (stabilizes binary training).
  4. Larger hidden dimensions to compensate for precision loss.

Training: Standard QAT with STE from scratch. NOT post-training quantization.

Result: At same perplexity, BitNet uses 2-3x fewer FLOPs than FP16 (because the model is wider but weights are 1-bit).


BitNet b1.58: Ternary LLMs

BitNet b1.58 (Ma et al., 2024): Ternary weights {1,0,+1}\{-1, 0, +1\}:

W^=RoundClip(Wγ,1,1),γ=W1nm.(9)\hat{\mathbf{W}} = \text{RoundClip}\left(\frac{\mathbf{W}}{\gamma}, -1, 1\right), \quad \gamma = \frac{\|\mathbf{W}\|_1}{nm}. \tag{9}

1.58 bits: log2(3)=1.58\log_2(3) = 1.58 bits per weight.

Key results (3B scale):

  • Matches FP16 LLaMA perplexity at same model size.
  • 3.55x memory reduction.
  • 2.71x faster on specialized hardware (addition-only).
  • Energy: 41.2x less for matrix multiplication.

Why ternary works better than binary: The zero value introduces explicit sparsity. Feature selection (zeroing irrelevant connections) is a powerful inductive bias.


Scaling Laws for Extreme Quantization

The width-precision tradeoff:

For a fixed compute budget CC:

  • FP16 model with width dd: performance f(d,16)\sim f(d, 16).
  • 1.58-bit model with width d=d16/1.58d' = d \cdot \sqrt{16/1.58}: similar performance at same compute.

Scaling law (empirical):

Loss(N,b)Loss(Nb/bref,bref),(10)\text{Loss}(N, b) \approx \text{Loss}(N \cdot b / b_{\text{ref}}, b_{\text{ref}}), \tag{10}

where bb is bit-width and NN is parameter count. Fewer bits per parameter can be offset by more parameters.

Implication: At sufficient scale, 1.58-bit models can match 16-bit models in quality while being much more efficient for inference (memory and energy).


Hardware Implications

Binary/ternary matmul: Replace multiply-accumulate with:

  • Binary: XNOR + popcount.
  • Ternary: Addition + subtraction (skip zeros).

No floating-point unit needed: Entire inference with integer arithmetic. Enables deployment on:

  • Edge devices without FPU.
  • FPGAs with lookup tables.
  • Custom ASICs optimized for ternary.

Memory: 1.58-bit 70B model: 70B×1.58/81470\text{B} \times 1.58/8 \approx 14 GB (vs 140 GB in FP16).


Common Pitfalls

Pitfall 1. Post-training quantization to binary/ternary. This NEVER works — always catastrophic quality loss. Must train with QAT from scratch.

Pitfall 2. Keeping model width the same as FP16. A 1-bit model with the same architecture as a 16-bit model has 4-5x less effective capacity. Must increase width.

Pitfall 3. Binarizing ALL components including embeddings and LayerNorm. Keep embeddings in higher precision (INT8/FP16); LayerNorm in FP32. Only linear layer weights should be extreme-quantized.


Summary

  • Binary (1-bit): {1,+1}\{-1, +1\}; XNOR matmul; 58x theoretical speedup; significant quality loss.
  • Ternary (1.58-bit): {1,0,+1}\{-1, 0, +1\}; add/subtract + sparsity; matches FP16 at scale.
  • BitNet b1.58: State-of-the-art ternary LLM; 3.5x smaller, 2.7x faster.
  • Key principle: Compensate precision with width — more parameters at fewer bits.
  • Must use QAT from scratch; post-training binarization always fails.
  • Hardware: Enables multiplier-free inference (addition only).

Exercises

Exercise 1. For XNOR-Net: compute the binary matmul of wb=[+1,1,+1,1]w_b = [+1, -1, +1, -1] and xb=[+1,+1,1,+1]x_b = [+1, +1, -1, +1] using bitwise operations.

Exercise 2. For BitNet b1.58 with d=4096d=4096: compute the model size in bytes and compare to FP16 at the same dd.

Exercise 3. Using the scaling law (equation 10): what width dd' should a 1.58-bit model have to match a d=4096d=4096 FP16 model in quality?

Exercise 4. Derive the energy savings of ternary matmul vs FP16 matmul (hint: energy of addition vs multiplication in 45nm CMOS).

Exercise 5. Design a BitNet-style training curriculum: specify the initialization, learning rate schedule, and warmup strategy for training a 7B ternary model from scratch.