Search

Search papers and lessons

Ternary Quantization

Level 4 · 11 min read

Ternary quantization stores each weight as -1, 0 or +1 plus one shared scale, about 1.58 bits per weight, so most multiplications in a layer become additions.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Linear algebra
  • Neural network basics
  • Efficiency
  • Probability
  • ReadReady to readNot yet

TL;DR

Ternary quantization gives every weight in a layer one of three values: -1, 0 or +1. The layer also keeps one positive number, a scale, shared by all its weights. Each weight then carries about 1.58 bits of information instead of 16, and the layer's Matrix Multiplication reduces to adding and subtracting inputs. It is the three-value case of Quantization, and in its strongest form the model is trained with the ternary weights from the start.

Builds on: Quantization, Straight-Through Estimator · Leads to: BitLinear, BitNet b1.58

Where it's used

Large language models trained from scratch. BitNet b1.58 (Ma and colleagues, 2024) builds the linear layers of a Transformer from ternary weights and trains the whole model that way. The paper reports that from 3 billion parameters up, its models match a full-precision model of the same size on perplexity and on a set of zero-shot tasks. In its Table 1, the 3B model runs with 2.71 times lower latency than the full-precision baseline, and the authors argue that the arithmetic suggests new hardware. Their results are measured under their own setup.

Shrinking a model that already exists. Ternary rounding can also be applied to a finished network, but that route usually loses far more quality than Post-Training Quantization to 8 or 4 bits. For that reason the ternary approaches that work best are the ones that train with the rounding in the loop.

Early convolutional networks. In 2016, Li and Liu's Ternary Weight Networks and Zhu and colleagues' Trained Ternary Quantization showed that image classifiers with ternary convolution weights could keep most of their accuracy. Their results suggested that the zero value is worth keeping, and the history below places them between the binary networks of 2016 and the language models of 2024.

How it works

Take one tiny linear layer with four weights, and follow one forward pass through it. The weights are w=[0.8,−0.2,0.1,−1.4]w = [0.8, -0.2, 0.1, -1.4] and the input is x=[2,3,−1,4]x = [2, 3, -1, 4].

  1. Start with full-precision weights. During training the layer keeps the real numbers ww. Check: there are four of them, and they can be any size.
  2. Measure the scale. Average the absolute values: (0.8+0.2+0.1+1.4)/4=2.5/4=0.625(0.8 + 0.2 + 0.1 + 1.4)/4 = 2.5/4 = 0.625. Call this γ\gamma. Check: γ\gamma is one positive number for the whole layer.
  3. Divide by the scale and round. Each weight divided by γ=0.625\gamma = 0.625 gives 1.281.28, −0.32-0.32, 0.160.16 and −2.24-2.24. Round each to the nearest integer: 11, 00, 00, −2-2. Check: all four are integers, but the last one is not yet ternary.
  4. Clip to the ternary range. Limit each value to the interval [−1,1][-1, 1]. The −2-2 becomes −1-1. The ternary weights are w~=[1,0,0,−1]\widetilde{w} = [1, 0, 0, -1]. Check: every entry is -1, 0 or +1.
  5. Compute the output with additions. Multiply the ternary weights by the input and sum: 1⋅2+0⋅3+0⋅(−1)+(−1)⋅4=−21 \cdot 2 + 0 \cdot 3 + 0 \cdot (-1) + (-1) \cdot 4 = -2. The zero weights drop their inputs entirely, and the other weights only add or subtract. Check: no multiplication by anything other than -1, 0 or 1 happens.
  6. Rescale once. Multiply the sum by the scale: γ⋅(−2)=0.625⋅(−2)=−1.25\gamma \cdot (-2) = 0.625 \cdot (-2) = -1.25. This is the layer's output. Check: one multiplication by γ\gamma per output, not one per weight.

The rounding in step 3 is a staircase, and the clip in step 4 flattens its ends:

−2−1012−1.5−1−0.500.511.5w / γternary weight
  • ternary rounding
Each scaled weight is rounded to the nearest integer and clipped to -1, 0 or +1. Values near zero become exactly zero.
flowchart LR
  W["Full-precision weights w"] --> G["Scale γ = mean of |w|"]
  G --> R["Divide by γ, round, clip to -1, 0, +1"]
  R --> M["Ternary weights times input: additions only"]
  X["Input x"] --> M
  M --> Y["Sum, then multiply once by γ"]

The layer needs about log⁡23≈1.58\log_2 3 \approx 1.58 bits per weight, because three values carry that much information. In practice, five ternary values pack into one byte, since 35=243≤2563^5 = 243 \le 256, which is 1.6 bits each.

The math

Definition: Ternary quantization (absmean form)

Given a real weight matrix WW with n⋅mn \cdot m entries, the scale is γ=1nm∑i,j∣Wij∣\gamma = \frac{1}{nm}\sum_{i,j} |W_{ij}|, and the ternary weights are W~ij=clip⁡(round⁡(Wij/(γ+ϵ)),−1,1)\widetilde{W}_{ij} = \operatorname{clip}\big(\operatorname{round}(W_{ij} / (\gamma + \epsilon)), -1, 1\big), where ϵ\epsilon is a tiny constant that stops division by zero. The layer output is y=γ W~xy = \gamma \, \widetilde{W} x.

Step 2 in symbols: the scale

γ=1nm∑i,j∣Wij∣\gamma = \frac{1}{nm}\sum_{i,j}|W_{ij}| is the mean absolute value of the entries. In the walkthrough, γ=(0.8+0.2+0.1+1.4)/4=0.625\gamma = (0.8 + 0.2 + 0.1 + 1.4)/4 = 0.625. Because γ\gamma is the average size, the ternary weights are scaled so that a typical weight rounds to ±1\pm 1 and a small one rounds to 00.

Steps 3 and 4 in symbols: round and clip

For each entry, round⁡(x)\operatorname{round}(x) means the nearest integer, and clip⁡(x,−1,1)\operatorname{clip}(x, -1, 1) means −1-1 if x<−1x \lt -1, 11 if x>1x \gt 1, and xx otherwise. The entry −1.4-1.4 divided by 0.6250.625 is −2.24-2.24. Its rounding is −2-2, and the clip makes it −1-1.

A weight becomes zero when ∣Wij∣/γ<0.5|W_{ij}|/\gamma \lt 0.5, that is when it is less than half the scale in size. In the example, 0.1/0.625=0.160.1/0.625 = 0.16, so the third weight becomes zero.

Step 5 in symbols: the product

The output of one row is yi=γ∑jW~ijxjy_i = \gamma \sum_j \widetilde{W}_{ij} x_j. Because each W~ij\widetilde{W}_{ij} is -1, 0 or +1, the sum ∑jW~ijxj\sum_j \widetilde{W}_{ij} x_j is a sum of some inputs, some negated, and some skipped. In the example, ∑jW~1jxj=2−4=−2\sum_j \widetilde{W}_{1j} x_j = 2 - 4 = -2, and y1=0.625⋅(−2)=−1.25y_1 = 0.625 \cdot (-2) = -1.25.

Training with the rounding in the loop

Rounding has a gradient of zero almost everywhere, so ordinary Backpropagation would never change the weights. The standard fix is the Straight-Through Estimator: the forward pass uses W~\widetilde{W}, and the backward pass treats the rounding as the identity, so the gradient with respect to W~\widetilde{W} is copied to WW. The optimizer then updates the full-precision WW, and the next forward pass rounds it again. The model learns weights whose rounded version works.

Worked examples

Example
1. Tiny: two weights

Take w=[0.4,−0.9]w = [0.4, -0.9]. Find the ternary weights and the scale.

  1. Scale: γ=(0.4+0.9)/2=0.65\gamma = (0.4 + 0.9)/2 = 0.65.
  2. Divide by the scale: 0.4/0.65=0.6150.4/0.65 = 0.615 and −0.9/0.65=−1.385-0.9/0.65 = -1.385.
  3. Round to the nearest integer: 0.6150.615 becomes 11, and −1.385-1.385 becomes −1-1.
  4. Clip to [−1,1][-1, 1]: nothing changes.

Result: w~=[1,−1]\widetilde{w} = [1, -1] and γ=0.65\gamma = 0.65. Both weights are nonzero, so this layer stores no zeros.

Example
2. Standard: a layer where zeros appear

Take w=[0.1,0.5,−0.7,0.3,−0.2,1.2]w = [0.1, 0.5, -0.7, 0.3, -0.2, 1.2]. Find the ternary weights.

  1. Scale: the absolute values sum to 0.1+0.5+0.7+0.3+0.2+1.2=3.00.1 + 0.5 + 0.7 + 0.3 + 0.2 + 1.2 = 3.0, so γ=3.0/6=0.5\gamma = 3.0/6 = 0.5.
  2. Divide by the scale: 0.20.2, 1.01.0, −1.4-1.4, 0.60.6, −0.4-0.4, 2.42.4.
  3. Round: 00, 11, −1-1, 11, 00, 22.
  4. Clip: the 22 becomes 11.

Result: w~=[0,1,−1,1,0,1]\widetilde{w} = [0, 1, -1, 1, 0, 1] with γ=0.5\gamma = 0.5. Two of the six weights are zero. Those inputs are skipped in the sum, which is where the speed and the sparsity come from.

Example
3. Realistic: memory for one 4096 by 4096 layer

A square layer in a transformer might be 4096 by 4096. It has 4096⋅4096=16,777,2164096 \cdot 4096 = 16{,}777{,}216 weights.

  1. In 16-bit floats, each weight takes 16 bits. Total: 16,777,216⋅16=268,435,45616{,}777{,}216 \cdot 16 = 268{,}435{,}456 bits. Divide by 8 to get 33,554,43233{,}554{,}432 bytes, which is 32 MiB.
  2. In ternary at 1.6 bits per weight (five trits per byte): 16,777,216⋅1.6=26,843,545.616{,}777{,}216 \cdot 1.6 = 26{,}843{,}545.6 bits. Divide by 8 to get 3,355,443.23{,}355{,}443.2 bytes, which is about 3.2 MiB.
  3. The ratio is 32/3.2=1032 / 3.2 = 10. The layer is about ten times smaller in storage, not counting the one scale γ\gamma (a single number).

Try it: a layer has 2000⋅500=1,000,0002000 \cdot 500 = 1{,}000{,}000 weights and is stored at 1.6 bits each. How many bytes does it take? Answer: 1,000,000⋅1.6=1,600,0001{,}000{,}000 \cdot 1.6 = 1{,}600{,}000 bits, and 1,600,000/8=200,0001{,}600{,}000 / 8 = 200{,}000 bytes.

Common mistakes and tradeoffs

It is tempting to think that ternary weights take 1.58 bits in memory on any hardware. In fact the 1.58 is the information content of three equally likely values. Ordinary hardware moves data in bytes and words, so a simple implementation stores two bits per weight, and only a packing scheme such as five trits per byte gets close to 1.6 bits.

It is tempting to think that the zero is wasted, and that binary weights of -1 and +1 would be better. In fact the zero is what lets a layer ignore an input completely. Binary weights have no such option, which is one reason ternary weights usually keep more accuracy at the same bit count.

It is tempting to think that rounding a trained model to ternary is enough. In fact the rounding error is large at three levels, and a model that was never trained with it loses much more quality than one rounded to 8 bits. Training with the rounding in the loop, through the Straight-Through Estimator, is what closes most of that gap.

It is tempting to think that the scale only matters for accuracy. In fact γ\gamma is also what keeps the output at the right size. Without it, a layer with ternary weights would produce outputs about 1/γ1/\gamma times the correct size (here 1.6 times too large), so the size of the output would not match what the rest of the network expects.

When to use it: training a large model where memory and data movement dominate the cost, and where you can train with the quantized weights. When not to use it: a small model where the full-precision embedding is most of the memory, or hardware that cannot exploit additions and zero-skipping. Alternatives: 8-bit or 4-bit Post-Training Quantization for a finished model, Quantization-Aware Training for 4-bit or 8-bit targets, and binary weights when every bit counts more than accuracy.

History

Binary weights came first. In 2015 Courbariaux, Bengio and David trained networks with weights of -1 and +1, using the straight-through trick to get gradients through the sign function. Hubara and colleagues extended this in 2016 with Binarized Neural Networks, which also binarized activations.

Ternary weights followed in the same year. Li and Liu's Ternary Weight Networks chose a threshold and a scale for each layer, and Zhu and colleagues' Trained Ternary Quantization learned the scale values during training. Both showed that the zero value gave accuracy that binary weights did not.

The idea caught on for language models in 2023 and 2024. The BitNet papers trained transformers whose linear layers used ternary weights, and the b1.58 paper made the three-value form, with the absmean scale, the standard recipe. It remains an active research area, and its hardware story is still being tested.

Recap

  • Quantization maps real numbers to a small set of levels. Ternary quantization uses three levels: -1, 0 and +1.
  • Each layer stores one scale γ\gamma, the mean absolute weight, and divides the weights by it before rounding.
  • Rounding and clipping give the ternary weights. Zeros let the layer skip inputs.
  • The layer's sum becomes additions and subtractions, and one multiplication by γ\gamma restores the output size.
  • The Straight-Through Estimator lets training update full-precision weights through the rounding.
  • Read next: BitLinear, which puts this layer inside a transformer, and the BitNet b1.58 paper.

Check yourself

QuestionRecall: which three values can a ternary weight take?Show answer

-1, 0 and +1. The zero is the third value, and it lets the layer drop an input from the sum.

QuestionA layer has the full-precision weights [0.6,−0.1,0.9,−1.8][0.6, -0.1, 0.9, -1.8]. What is the scale γ\gamma?Show answer

The absolute values sum to 0.6+0.1+0.9+1.8=3.40.6 + 0.1 + 0.9 + 1.8 = 3.4. Divide by 4 to get γ=0.85\gamma = 0.85.

QuestionWhy does a model trained with ternary weights usually do better than one rounded to ternary after training?Show answer

Training with the rounding in the loop lets the other weights adjust to the coarse values, so the model learns a solution that works after rounding. Rounding a finished model has no such adjustment, so the rounding error is larger and is not corrected.

QuestionA ternary layer has γ=0.5\gamma = 0.5, weights [1,0,−1][1, 0, -1] and inputs [4,7,2][4, 7, 2]. What is the output?Show answer

The sum is 1⋅4+0⋅7+(−1)⋅2=21 \cdot 4 + 0 \cdot 7 + (-1) \cdot 2 = 2. Multiply by γ\gamma: 0.5⋅2=10.5 \cdot 2 = 1. The output is 1. The input 7 is skipped because its weight is zero.

QuestionApplied scenario: a team wants a small model to run on a phone. They plan to put ternary weights on every layer, including the embedding table. What would you advise, and why?Show answer

Keep the embedding table at full precision, or quantize it to a higher bit width. The embedding is often a large share of a small model's memory. BitNet b1.58 keeps its embedding at full precision for this reason, and the embedding's share of memory is larger for smaller models, so ternary layers save less there. Check the memory of each part before deciding.

Sources