Search

Search papers and lessons

BitLinear

Level 5 · 14 min read

BitLinear is a drop-in replacement for a linear layer whose weights are stored as one bit each, so most of its multiplications become additions; it was the layer that made 1-bit Transformers trainable from scratch.

On this page

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

TL;DR

BitLinear is a layer that takes the place of an ordinary linear layer inside a Transformer. Its weights are stored as just +1 or -1, and its input is rounded to 8-bit integers, so the big matrix multiplication needs almost no real multiplication. It was introduced in 2023 to train language models whose weights are 1 bit from the start, which cuts memory and energy use a great deal.

Builds on: Quantization, Straight-Through Estimator, Layer Normalization · Leads to: BitNet, BitNet b1.58 and the later ternary language models

Where it's used

Training language models from scratch at 1 bit. The BitNet paper (Wang et al., 2023) replaced the weight projections in the attention and feed-forward blocks of a Transformer with BitLinear, and trained autoregressive language models from 125 million to 30 billion parameters on an English corpus. The change was a swap of one layer type. The other components stayed at higher precision (8-bit in the paper's experiments), so the recipe needs no new architecture.

Serving large models with less memory. A 6.7-billion-parameter model has about 6.7 billion weights. At 16 bits each that is about 13.4 GB of weights; at 1 bit each it is about 0.84 GB. Memory bandwidth, the time spent moving weights from memory to the processor, is often the limit when a large model answers one request at a time. Smaller weights mean less of that movement.

Cheaper arithmetic on energy-limited hardware. The paper's energy estimate for a 6.7-billion-parameter model (Table 1) gives 4.41 J for the multiplications of a 32-bit Transformer at 7 nm, and 0.02 J for BitNet's multiplications in the same setting. The additions that remain are cheap too, which matters on battery-powered hardware.

How it works

Take one tiny layer with a 2 by 2 weight matrix and one input vector, and follow one forward pass. The weights are W=(0.5−1.52.0−0.5)W = \begin{pmatrix} 0.5 & -1.5 \\ 2.0 & -0.5 \end{pmatrix} and the input is x=[4,−2]x = [4, -2].

  1. Start with the real-valued weights. During training the layer keeps the four numbers in WW at full precision. Check: the matrix has four entries and they can be any size.
  2. Centre the weights. Subtract the mean of all entries. The mean is (0.5−1.5+2.0−0.5)/4=0.5/4=0.125(0.5 - 1.5 + 2.0 - 0.5)/4 = 0.5/4 = 0.125, so W−0.125W - 0.125 is (0.375−1.6251.875−0.625)\begin{pmatrix} 0.375 & -1.625 \\ 1.875 & -0.625 \end{pmatrix}. Check: the four new entries add to zero, since 0.375−1.625+1.875−0.625=00.375 - 1.625 + 1.875 - 0.625 = 0.
  3. Keep only the sign. Each entry becomes +1 if it is positive and -1 otherwise. The binary matrix is W~=(+1−1+1−1)\widetilde{W} = \begin{pmatrix} +1 & -1 \\ +1 & -1 \end{pmatrix}. Check: every entry is +1 or -1, so storing it needs one bit per entry.
  4. Measure one scale for the weights. Average the absolute values of the original weights: β=(0.5+1.5+2.0+0.5)/4=4.5/4=1.125\beta = (0.5 + 1.5 + 2.0 + 0.5)/4 = 4.5/4 = 1.125. Check: β\beta is one positive number for the whole layer. It will be used once, at the end, to put the output back on the right scale.
  5. Normalize the input. This is the layer normalization step: subtract the mean of xx and divide by its spread. The mean of xx is (4−2)/2=1(4 - 2)/2 = 1, and the variance is ((4−1)2+(−2−1)2)/2=9((4-1)^2 + (-2-1)^2)/2 = 9, so the square root is 3 and LN(x)=[(4−1)/3,(−2−1)/3]=[1,−1]\mathrm{LN}(x) = [(4-1)/3, (-2-1)/3] = [1, -1]. Check: the new vector has mean 0 and variance 1.
  6. Round the input to 8-bit integers. Find the largest absolute value in the normalized input, γ=1\gamma = 1. Multiply every entry by Qb/γQ_b/\gamma, where Qb=128Q_b = 128 for 8 bits, and clip to the range [−128,128][-128, 128]. The input becomes x~=[128,−128]\widetilde{x} = [128, -128]. Check: both entries are integers in the 8-bit range. (The real formula keeps a tiny margin ϵ\epsilon inside the range; we ignore it here.)
  7. Multiply with additions only. Each output is a sum of the input entries, each added or subtracted according to its weight sign. Row 1 gives (+1)(128)+(−1)(−128)=256(+1)(128) + (-1)(-128) = 256. Row 2 gives (+1)(128)+(−1)(−128)=256(+1)(128) + (-1)(-128) = 256. Check: every step is an addition or subtraction.
  8. Rescale once. Multiply the sums by βγ/Qb=1.125⋅1/128=0.0087890625\beta\gamma/Q_b = 1.125 \cdot 1 / 128 = 0.0087890625. Each output becomes 256⋅0.0087890625=2.25256 \cdot 0.0087890625 = 2.25, so y=[2.25,2.25]y = [2.25, 2.25]. Check: one multiplication per output, not one per weight.

For comparison, the same layer without any rounding gives W LN(x)=[2.0,2.5]W\,\mathrm{LN}(x) = [2.0, 2.5]. The 1-bit layer lands close to it, not on it. On a matrix this small the error is large; on a layer with thousands of inputs the errors average out, which is why the design is useful at scale.

−1.5−1−0.500.511.5−100−50050100normalized input valuequantized value
  • 8-bit absmax, γ = 1
With the largest input scaled to 1, absmax maps values linearly onto the 8-bit range from -128 to 128 and clips anything beyond it.
flowchart LR
  W["Real weights W"] --> C["Centre and take sign: W~ in {-1, +1}"]
  X["Input x"] --> LN["Layer norm: LN(x)"]
  LN --> Q["8-bit absmax: x~"]
  C --> M["W~ times x~: additions only"]
  Q --> M
  M --> S["Rescale by beta gamma / Q_b"]
  S --> Y["Output y"]

The math

Definition: BitLinear (BitNet, 2023)

For a weight matrix W∈Rn×mW \in \mathbb{R}^{n \times m} and an input xx, let α\alpha be the mean of the entries of WW, W~=Sign⁡(W−α)\widetilde{W} = \operatorname{Sign}(W - \alpha) the matrix of signs, β\beta the mean absolute value of the entries of WW, LN(x)\mathrm{LN}(x) the layer-normalized input, and x~=Quant⁡(LN(x))\widetilde{x} = \operatorname{Quant}(\mathrm{LN}(x)) the input rounded to bb bits by absmax. The layer output is y=W~ x~⋅βγQb.y = \widetilde{W}\,\widetilde{x} \cdot \frac{\beta\gamma}{Q_b}.

Step 2 in symbols: centring

α=1nm∑i,jWij\alpha = \frac{1}{nm}\sum_{i,j} W_{ij}. Subtracting α\alpha moves the weights so that their mean is zero, which lets the signs split them evenly. In the example, α=0.125\alpha = 0.125.

Step 3 in symbols: the sign

Sign⁡(u)={+1if u>0,−1if u≤0.\operatorname{Sign}(u) = \begin{cases} +1 & \text{if } u \gt 0, \\ -1 & \text{if } u \le 0. \end{cases}

Applied entry by entry to W−αW - \alpha. Note that zero maps to -1, as in the paper. For instance, if α=0.2\alpha = 0.2 and Wij=0.5W_{ij} = 0.5, then Wij−α=0.3>0W_{ij} - \alpha = 0.3 \gt 0 and the sign is +1.

Step 4 in symbols: the weight scale

β=1nm∥W∥1=1nm∑i,j∣Wij∣\beta = \frac{1}{nm}\lVert W\rVert_1 = \frac{1}{nm}\sum_{i,j}|W_{ij}|. The sign matrix has entries of size 1, while the real weights have typical size β\beta. Multiplying by β\beta at the end restores that size. In the example, β=1.125\beta = 1.125.

Step 5 in symbols: layer normalization

LN(x)=x−E(x)Var⁡(x)+ϵ\mathrm{LN}(x) = \frac{x - \mathbb{E}(x)}{\sqrt{\operatorname{Var}(x) + \epsilon}}

Here E(x)\mathbb{E}(x) is the mean of the entries of xx and Var⁡(x)\operatorname{Var}(x) their variance, with ϵ\epsilon a tiny constant that avoids dividing by zero. The paper's reason for this step: with standard initialization (Kaiming or Xavier), the full-precision output variance is about 1. Normalizing before the 8-bit rounding keeps the quantized output near that value, since the paper's estimate is Var(y)≈E[LN(x~)2]=1\mathrm{Var}(y) \approx \mathbb{E}[\mathrm{LN}(\widetilde{x})^2] = 1.

Step 6 in symbols: absmax rounding

With b=8b = 8 bits, Qb=2b−1=128Q_b = 2^{b-1} = 128, and

x~=Quant⁡(LN(x))=Clip⁡ ⁣(LN(x)×Qbγ,  −Qb+ϵ,  Qb−ϵ),γ=∥LN(x)∥∞.\widetilde{x} = \operatorname{Quant}(\mathrm{LN}(x)) = \operatorname{Clip}\!\left(\mathrm{LN}(x) \times \frac{Q_b}{\gamma},\; -Q_b + \epsilon,\; Q_b - \epsilon\right), \qquad \gamma = \lVert \mathrm{LN}(x) \rVert_\infty.

Here ∥⋅∥∞\lVert \cdot \rVert_\infty is the largest absolute value of the entries, and Clip⁡(u,a,c)=max⁡(a,min⁡(c,u))\operatorname{Clip}(u, a, c) = \max(a, \min(c, u)). The input is scaled so its largest entry lands on ±Qb\pm Q_b, then clipped. In the example, γ=1\gamma = 1, so the values are ±128\pm 128.

Steps 7 and 8 in symbols: the product and the rescale

The matrix product is ∑jW~ij x~j\sum_j \widetilde{W}_{ij}\,\widetilde{x}_j. Because each W~ij\widetilde{W}_{ij} is ±1\pm 1, every term is ±x~j\pm \widetilde{x}_j, so the product is a signed sum of inputs. In the example, ∑jW~1jx~j=128+128=256\sum_j \widetilde{W}_{1j}\widetilde{x}_j = 128 + 128 = 256. The output is y1=256⋅βγQb=256⋅1.125⋅1128=2.25y_1 = 256 \cdot \frac{\beta\gamma}{Q_b} = 256 \cdot \frac{1.125 \cdot 1}{128} = 2.25.

Two details from the paper matter in practice. During training, γ\gamma is computed once per tensor. During inference it is computed per token, which the paper says helps both stability and efficiency. The paper also quantizes activations to 8 bits and leaves lower precision to future work.

Worked examples

Example
1. A single weight row, by hand

Treat one row, [0.5,−1.5][0.5, -1.5], as a whole layer with one output, so β\beta uses only its two entries. The input after normalization and rounding is x~=[128,−128]\widetilde{x} = [128, -128]. Use only the weight signs, not the real values, in the sum.

  1. The row's mean is (0.5−1.5)/2=−0.5(0.5 - 1.5)/2 = -0.5, so the centred row is [1.0,−1.0][1.0, -1.0].
  2. The signs of [1.0,−1.0][1.0, -1.0] are [+1,−1][+1, -1].
  3. The signed sum is (+1)(128)+(−1)(−128)=128+128=256(+1)(128) + (-1)(-128) = 128 + 128 = 256.
  4. The scale is β=(0.5+1.5)/2=1.0\beta = (0.5 + 1.5)/2 = 1.0 and γ=1\gamma = 1, so the output is 256⋅1.0⋅1128=2.0256 \cdot \frac{1.0 \cdot 1}{128} = 2.0.
  5. Interpretation: the layer turned a sign pattern and an 8-bit input into one real number, using additions for the big sum and a single multiplication at the end.
Example
2. Rounding a general input

Input after normalization: LN(x)=[0.3,−0.6,0.0]\mathrm{LN}(x) = [0.3, -0.6, 0.0]. Find the 8-bit values x~\widetilde{x}.

  1. The largest absolute value is γ=∣−0.6∣=0.6\gamma = |-0.6| = 0.6.
  2. The scale factor is Qb/γ=128/0.6≈213.3Q_b / \gamma = 128 / 0.6 \approx 213.3.
  3. Multiply each entry: 0.3⋅213.3=640.3 \cdot 213.3 = 64, −0.6⋅213.3=−128-0.6 \cdot 213.3 = -128, 0.0⋅213.3=00.0 \cdot 213.3 = 0.
  4. Each value is already inside [−128,128][-128, 128], so clipping changes nothing.
  5. Interpretation: the entry of largest size maps to the edge of the 8-bit range, and the others keep their ratios. The scale γ\gamma is applied back at the end, in the rescale step.
Example
3. Memory of one realistic layer

A 6.7-billion-parameter language model has hidden size 4096 (Table 6 of the paper lists this for the 6.7B model), so its projection layers are 4096 by 4096. Compare the memory of one such layer's weights at 16 bits and at 1 bit. Use 1 megabyte = 1,048,576 bytes and ignore the few scalar values α\alpha, β\beta, γ\gamma.

  1. The layer has 4096⋅4096=16,777,2164096 \cdot 4096 = 16{,}777{,}216 weights.
  2. At 16 bits each, that is 16,777,216⋅2=33,554,43216{,}777{,}216 \cdot 2 = 33{,}554{,}432 bytes.
  3. Divided by 1,048,576, that is 32 megabytes.
  4. At 1 bit each, the weights take 16,777,216/8=2,097,15216{,}777{,}216 / 8 = 2{,}097{,}152 bytes.
  5. Divided by 1,048,576, that is 2 megabytes. The ratio is 32/2=1632 / 2 = 16.
  6. Interpretation: each projection shrinks sixteenfold in storage. The paper's energy and memory claims come from this kind of saving, applied to every projection in the network.

Try it: a feedforward projection in a model of hidden size 4096 has size 4096 by 11008 (11008 is the inner width). How many megabytes does it take at 16 bits, and how many at 1 bit? Use the same conventions as above.

Answer: 4096⋅11008=45,088,7684096 \cdot 11008 = 45{,}088{,}768 weights. At 16 bits that is 90,177,53690{,}177{,}536 bytes, which is 86 megabytes. At 1 bit it is 5,636,0965{,}636{,}096 bytes, which is 5.375 megabytes. The ratio is 16 again.

Common mistakes and tradeoffs

It is tempting to think a 1-bit network does all its arithmetic in 1 bit. In fact the weights are 1 bit, but the activations are 8 bits and the scales β\beta and γ\gamma are real numbers. The additions are cheap because the weights are binary; the input still needs enough precision to carry information.

It is tempting to think you can round a finished model to 1 bit and keep its quality. In fact the paper's post-training absmax baseline at 1-bit weights reached a Perplexity of 3.5×10233.5 \times 10^{23}, and its zero-shot average fell to 44.6, close to the 43.8 of random guessing. BitNet, trained with the rounding in the loop, reached a perplexity of 17.07 at 6.7B parameters, against 15.19 for the FP16 Transformer, with a zero-shot average of 55.9. The network has to learn weights whose signs work after rounding, which is why the training recipe matters (see Quantization-Aware Training).

It is tempting to think the latent weights must be binary too. In fact the optimizer updates a full-precision copy, and the binary weights are recomputed from it on every forward pass. The paper notes that a small update to the latent weights often changes no 1-bit weight at all, which is why the copy is kept.

It is tempting to think the centring and the layer norm are decoration. They are part of the design. Centring makes the signs split evenly, and the layer norm keeps the output variance near 1 after rounding. In the paper's ablation (Table 4), its configuration (absmax with SubLN placement) gets a zero-shot average of 52.3, against 51.3 for absmax with a standard Pre-LN placement and 50.2 for the elastic activation quantizer of an earlier method with Pre-LN.

When to use it. Use BitLinear when you can train from scratch or continue training, and when memory or energy at inference is the limit. The paper's Figure 1 says the cost savings become more significant as the model grows. Quality is not free at 6.7B, though: the perplexity gap to the FP16 Transformer is 17.07 against 15.19, so compare on your own task before committing. The paper also keeps the input and output embedding layers at high precision, because sampling from the output needs accurate probabilities.

When not to use it. If you already have a trained 16-bit model and no training budget, post-training quantization is simpler (see Post-Training Quantization), at a cost in accuracy at low bit widths.

Alternatives. Ternary weights in {−1,0,+1}\{-1, 0, +1\} (see Ternary Quantization, and BitNet b1.58 for the language-model version) give a little more capacity per weight, at about 1.58 bits; Mixed-Precision Training keeps sensitive layers at higher precision.

History

For years, binary neural networks were studied mainly on convolutional image classifiers, as the BitNet paper notes. XNOR-Net (Rastegari and colleagues, 2016) used binary weights and inputs in convolution layers, and the large language model setting was a different problem.

The training trick came from earlier work. The Straight-Through Estimator was described by Bengio, Léonard and Courville in 2013 as a way to pass gradients through a hard step. It is what lets the rounding sit inside the forward pass during training.

In October 2023, BitNet (Wang and colleagues) introduced BitLinear as a drop-in replacement for the linear layer and reported scaling results up to 30 billion parameters. A 2024 follow-up, BitNet b1.58, moved the weights to the ternary values {−1,0,+1}\{-1, 0, +1\}. The idea is easy to try because it changes one layer type, not the whole training system.

Recap

  • A linear layer multiplies a weight matrix by an input. BitLinear replaces the weights with their signs, ±1\pm 1, after centring them.
  • The input is normalized with layer normalization and rounded to 8 bits with absmax, so the product is a sum of signed inputs.
  • One real-valued scale, βγ/Qb\beta\gamma/Q_b, is applied to each output at the end.
  • The network is trained with the Straight-Through Estimator and a full-precision latent weight, so it learns weights that survive the rounding.
  • Read next: the BitNet paper, which builds the whole model from this layer, and BitNet b1.58 for the three-value variant. Quantization-Aware Training explains the training recipe.

Check yourself

QuestionRecall: what are the only two values a BitLinear weight can take?Show answer

+1 and -1. The sign of the centred weight, Sign⁡(Wij−α)\operatorname{Sign}(W_{ij} - \alpha), decides which. There is no zero.

QuestionFor the weights used in the walkthrough, the mean is α=0.125\alpha = 0.125. What is the sign of the entry Wij=0.1W_{ij} = 0.1?Show answer

Compute 0.1−0.125=−0.0250.1 - 0.125 = -0.025. This is not greater than zero, so the sign is -1. A value that sits just below the mean is still mapped to -1, which is why the centring step matters.

QuestionWhy does the layer divide by the scale γ\gamma before rounding the input, and then multiply by βγ/Qb\beta\gamma/Q_b at the end?Show answer

Dividing by γ\gamma and multiplying by QbQ_b puts the largest input entry exactly on the edge of the 8-bit range, so the rounding uses the full range and loses as little as possible. The factor γ/Qb\gamma/Q_b undoes this scaling at the end, so the output is on the same scale as the real-valued product. β\beta does the same for the weights.

QuestionA layer has 1024 inputs and 1024 outputs. How many bits does its weight matrix need at 1 bit per weight, ignoring scalars?Show answer

1024⋅1024=1,048,5761024 \cdot 1024 = 1{,}048{,}576 weights, each one bit, so 1,048,5761{,}048{,}576 bits, which is 131,072 bytes or 128 kilobytes (with 1 kilobyte = 1024 bytes).

QuestionA team wants a 1-bit model for a phone and has a trained 16-bit model with no training budget left. Should they convert it with BitLinear? Explain.Show answer

No. BitLinear needs training with the rounding in the loop, as the paper does from scratch. Rounding a finished 16-bit model to 1 bit without that training loses almost all accuracy; the paper's post-training 1-bit baseline collapsed. Post-training quantization at 8 or 4 bits is the practical choice in that situation.

Sources