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 and the input is .
- Start with full-precision weights. During training the layer keeps the real numbers . Check: there are four of them, and they can be any size.
- Measure the scale. Average the absolute values: . Call this . Check: is one positive number for the whole layer.
- Divide by the scale and round. Each weight divided by gives , , and . Round each to the nearest integer: , , , . Check: all four are integers, but the last one is not yet ternary.
- Clip to the ternary range. Limit each value to the interval . The becomes . The ternary weights are . Check: every entry is -1, 0 or +1.
- Compute the output with additions. Multiply the ternary weights by the input and sum: . 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.
- Rescale once. Multiply the sum by the scale: . This is the layer's output. Check: one multiplication by per output, not one per weight.
The rounding in step 3 is a staircase, and the clip in step 4 flattens its ends:
- ternary rounding
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 bits per weight, because three values carry that much information. In practice, five ternary values pack into one byte, since , which is 1.6 bits each.
The math
Given a real weight matrix with entries, the scale is , and the ternary weights are , where is a tiny constant that stops division by zero. The layer output is .
Step 2 in symbols: the scale
is the mean absolute value of the entries. In the walkthrough, . Because is the average size, the ternary weights are scaled so that a typical weight rounds to and a small one rounds to .
Steps 3 and 4 in symbols: round and clip
For each entry, means the nearest integer, and means if , if , and otherwise. The entry divided by is . Its rounding is , and the clip makes it .
A weight becomes zero when , that is when it is less than half the scale in size. In the example, , so the third weight becomes zero.
Step 5 in symbols: the product
The output of one row is . Because each is -1, 0 or +1, the sum is a sum of some inputs, some negated, and some skipped. In the example, , and .
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 , and the backward pass treats the rounding as the identity, so the gradient with respect to is copied to . The optimizer then updates the full-precision , and the next forward pass rounds it again. The model learns weights whose rounded version works.
Worked examples
Take . Find the ternary weights and the scale.
- Scale: .
- Divide by the scale: and .
- Round to the nearest integer: becomes , and becomes .
- Clip to : nothing changes.
Result: and . Both weights are nonzero, so this layer stores no zeros.
Take . Find the ternary weights.
- Scale: the absolute values sum to , so .
- Divide by the scale: , , , , , .
- Round: , , , , , .
- Clip: the becomes .
Result: with . Two of the six weights are zero. Those inputs are skipped in the sum, which is where the speed and the sparsity come from.
A square layer in a transformer might be 4096 by 4096. It has weights.
- In 16-bit floats, each weight takes 16 bits. Total: bits. Divide by 8 to get bytes, which is 32 MiB.
- In ternary at 1.6 bits per weight (five trits per byte): bits. Divide by 8 to get bytes, which is about 3.2 MiB.
- The ratio is . The layer is about ten times smaller in storage, not counting the one scale (a single number).
Try it: a layer has weights and is stored at 1.6 bits each. How many bytes does it take? Answer: bits, and 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 is also what keeps the output at the right size. Without it, a layer with ternary weights would produce outputs about 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 , 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 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 . What is the scale ?Show answer
The absolute values sum to . Divide by 4 to get .
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 , weights and inputs . What is the output?Show answer
The sum is . Multiply by : . 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.