Search

Search papers and lessons

LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale

Tim Dettmers, Mike Lewis, Younes Belkada et al. · 2022

LLM.int8() runs most of a large language model's matrix products in 8-bit integers and the rare huge-valued input features in 16-bit, so a 175B-parameter model loses no accuracy at half the memory.

On this page

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

TL;DR

Large language models need a lot of GPU memory, and most of it holds the weights of their matrix multiplications. LLM.int8() stores those weights and the inputs to them as 8-bit integers instead of 16-bit floats, and it handles the few input features with very large values separately in 16-bit. The result is a model that uses about half the memory and gives the same output quality, tested up to 175 billion parameters.

Builds on: Quantization, Transformer · Leads to: Post-Training Quantization, which applies this kind of scheme after training

Why it mattered

Before this paper, 8-bit quantization of transformers had been shown to work only for models up to about 350 million parameters. Larger models lost accuracy when quantized, and that loss was usually treated as the price of size. The paper showed that the loss came from a specific cause, and that a fix could keep full accuracy at 175B parameters.

The authors released the method as the bitsandbytes library, and its 8-bit path was integrated into Hugging Face Transformers, so any hosted model with linear layers could be loaded in 8-bit with a flag. The paper's own example is running OPT-175B or BLOOM on a single server with consumer GPUs. Later work on low-precision inference of large models builds on its finding that a few systematic outlier features dominate the quality of a large model.

The problem

A transformer spends most of its time in matrix multiplications. The paper says that for models of 6.7B parameters and above, the feed-forward and attention projection layers hold about 95 percent of the parameters and take 65 to 85 percent of the computation. Storing those weights in 8 bits would halve their memory, if quantization did not hurt accuracy.

It did hurt, and the damage grew with size. Standard 8-bit schemes, which scale each tensor by its largest absolute value (absmax quantization), work for small models. At about 6.7B parameters, the paper finds a phase shift: outlier features of magnitude up to about 20 times the typical value appear in every layer. A single absmax scale for a whole tensor then leaves most of the 8-bit levels empty, and the small values round to zero. Quality falls off sharply, as shown in the paper's Figure 1 for zero-shot accuracy.

The two prerequisites explain the setting. Quantization is the mapping from floats to a small set of integer levels, and the question is how many levels a value gets. A Transformer computes many dot products between a row of hidden states and a column of weights, and the quantization error of each dot product adds up across the hidden dimension.

How it works

The method runs one linear layer as follows. The paper's notation: X is the input hidden state, a matrix with s rows (tokens) and h columns (feature dimensions), and W is the weight matrix with h rows and o columns.

  1. Find the outlier feature dimensions. Scan the input X and mark each feature dimension j that has at least one value of magnitude 6.0 or more. The paper calls this set O. In its experiments the set has at most 7 members for models up to 13B, which is about 0.1 percent of the features.
  2. Split the input and the weights. Columns in O form a 16-bit piece, and the remaining columns form an 8-bit piece. The 16-bit piece is multiplied in floating point, so outliers keep full precision.
  3. Quantize the 8-bit piece by row and column. Each row of the input gets its own scale, chosen from the largest value in that row. Each column of the weights gets its own scale from its largest value. This is vector-wise quantization. Each scale maps its vector onto the integers from -127 to 127.
  4. Multiply in integers. The two 8-bit matrices are multiplied, and the sums are accumulated in 32-bit integers.
  5. Dequantize. Divide each integer sum by the product of its row scale and column scale to get back an approximate float.
  6. Add the two pieces. The 16-bit result and the dequantized 8-bit result are summed to give the layer's output.
flowchart LR
  X[16-bit input X] --> D{split features}
  D -->|outlier columns, 0.1 percent| H[16-bit matmul]
  D -->|other 99.9 percent| Q[row-wise scale to int8]
  W[16-bit weights W] --> E{split rows}
  E -->|outlier rows| H
  E -->|other rows| R[column-wise scale to int8]
  Q --> M[int8 matmul, int32 sums]
  R --> M
  M --> DQ[divide by row and column scales]
  H --> SUM[add]
  DQ --> SUM
  SUM --> Y[16-bit output]

Tiny instance, traced

Take one token with input row x = [0.5, -1.0, 8.0, 2.0] and one output column with weights w = [0.2, 0.4, 0.1, -0.3]. The exact dot product is 0.5 times 0.2, plus (-1.0) times 0.4, plus 8.0 times 0.1, plus 2.0 times (-0.3), which is 0.1 - 0.4 + 0.8 - 0.6 = -0.1. The method reproduces this value as follows. The threshold is 6.0.

  1. Outlier dimensions: only index 2 has magnitude at least 6.0. The outlier term is 8.0 times 0.1 = 0.8, computed in 16 bits.
  2. The regular inputs are x_reg = [0.5, -1.0, 2.0] (indices 0, 1, 3), and the matching weights are w_reg = [0.2, 0.4, -0.3].
  3. Input scale: the largest regular magnitude is 2.0, so c_x = 127 divided by 2.0 = 63.5.
  4. Weight scale: the largest regular weight magnitude is 0.4, so c_w = 127 divided by 0.4 = 317.5.
  5. Quantize: X_i8 = round(63.5 times [0.5, -1.0, 2.0]) = round([31.75, -63.5, 127]) = [32, -64, 127], rounding halves away from zero. W_i8 = round(317.5 times [0.2, 0.4, -0.3]) = round([63.5, 127, -95.25]) = [64, 127, -95].
  6. Integer dot product: 32 times 64, plus (-64) times 127, plus 127 times (-95) = 2048 - 8128 - 12065 = -18145.
  7. Dequantize: divide by c_x times c_w = 63.5 times 317.5 = 20161.25. Result: -18145 divided by 20161.25, about -0.900.
  8. Add the outlier term: -0.900 + 0.800 = -0.100.
  9. Interpretation: the 8-bit part is -0.900 and the 16-bit outlier part is 0.800. Together they give the exact -0.1. The outlier kept its 16-bit value, so the one large term was not rounded.
Example
Try it: which features are outliers?

A row of the input is x = [3.0, -7.0, 0.5, 5.5] and the threshold is 6.0. Which dimensions go to the 16-bit path, and which go to the 8-bit path?

Answer: only dimension 1 (the value -7.0) is an outlier. Dimensions 0, 2 and 3 go to the 8-bit path, where their scale is set by the largest of their values, 5.5.

The math

We write the layer as before: XX is a matrix in Rs×h\mathbb{R}^{s \times h} and WW is a matrix in Rh×o\mathbb{R}^{h \times o}. The output is C=XWC = XW.

Step 3 in symbols: the vector-wise scales

For each row i of X, define the scale

cx,i=127max⁡j∣Xij∣c_{x,i} = \frac{127}{\max_j |X_{ij}|}

where the maximum runs over the 8-bit feature columns only. For each column k of W, define

cw,k=127max⁡i∣Wik∣c_{w,k} = \frac{127}{\max_i |W_{ik}|}

The quantized matrices are X_i8 = round(c_x times X) and W_i8 = round(W times c_w), where each scale multiplies its own row or column. Rounding goes to the nearest integer.

Step 4 and 5 in symbols: integer product and dequantization

The integer product is C_i32 = X_i8 W_i8, with entries summed in 32 bits. Dequantization divides each entry by its pair of scales:

Cik≈Ciki32cx,i cw,kC_{ik} \approx \frac{C^{i32}_{ik}}{c_{x,i}\, c_{w,k}}

Equivalently, the whole matrix is divided elementwise by the outer product of the row-scale vector and the column-scale vector. The paper writes this as S times C_i32, where S is the reciprocal of that outer product.

Step 2 and 6 in symbols: the decomposition

Let O be the set of outlier feature indices, those j with max over i of |X_ij| at least 6.0. Then

C≈∑j∈OX:,j Wj,:  +  ∑j∉OX:,j Wj,:C \approx \sum_{j \in O} X_{:,j}\, W_{j,:} \;+\; \sum_{j \notin O} X_{:,j}\, W_{j,:}

The first sum is computed in 16 bits. The second is computed with the 8-bit path above. The only error in the second sum comes from rounding to 8 bits.

Why the split matters

With a single scale for a row that contains a value 20 times larger than the rest, the step size is 20 times larger, and most typical values round to zero. Taking the outlier columns out of the 8-bit path keeps the step size small for everything else. Only about 0.1 percent of the values change precision, so the memory saving stays close to one half.

Results

The paper's main quantitative claims come from two experiments: C4 perplexity for dense models from 125M to 13B parameters, and zero-shot accuracy for OPT models up to 175B. Lower perplexity is better.

Example
Reading the 13B row of the perplexity table

Table 1 reports C4 validation perplexity for a 13B model. The 32-bit float baseline is 12.45. The vector-wise absmax 8-bit method gives 16.48, and the full LLM.int8() gives 12.45.

  1. Gap of the simple 8-bit method: 16.48 minus 12.45 = 4.03.
  2. Relative gap: 4.03 divided by 12.45, which is about 0.32, or 32 percent worse than the float model.
  3. Gap of LLM.int8(): 12.45 minus 12.45 = 0.00.
  4. Interpretation: at 13B the simple 8-bit method is clearly worse, and the decomposition closes the entire gap. At 6.7B the simple method gives 14.13 against 13.30 in float, and LLM.int8() gives 13.24, so the decomposed model matches float quality at that size.
Example
Realistic: memory of a 176B-parameter model

BLOOM-176B has about 176 billion weights. Storing 8 bits per weight instead of 16 roughly halves the weight memory.

  1. 16-bit storage: 176 billion weights times 2 bytes = 352 billion bytes, or about 352 GB.
  2. 8-bit storage: 176 billion weights times 1 byte = about 176 GB.
  3. Compare with consumer hardware: eight 24 GB GPUs give 8 times 24 = 192 GB of memory.
  4. Check the margin: 176 GB fits in 192 GB, leaving about 16 GB for activations and overhead. This is a rough estimate, since real deployments also need memory for the key-value cache and other buffers.
  5. Interpretation: a model that needed about 352 GB in 16 bits can fit across one server of consumer GPUs once its weights are 8-bit. This is the accessibility claim the paper makes, and the memory saving is what makes it possible.

What the results do not show

It is tempting to think that 8-bit inference always makes a model faster. In fact the paper reports that the quantization steps add overhead. For models up to about 6.7B parameters, inference was slower than the 16-bit baseline. A matrix multiplication speed-up of about two times appeared only for very large layers, such as those in 175B-scale models. The main gain is memory, and the speed depends on the layer size and on how well the kernels are written.

It is tempting to think that the outliers are noise to be removed. In fact the paper shows the opposite: zeroing the outlier dimensions lowers the top-1 attention softmax probability mass by more than 20 percent and raises perplexity by 600 to 1000 percent. The method keeps the outliers at full precision because they carry signal the model uses.

It is tempting to think that a single scale per tensor is enough when the model is large. In fact one large value sets the step size for the whole tensor. Row and column scales limit the damage to one token or one output, and the decomposition removes the few features that remain extreme.

The paper also states its limits. It studies only the Int8 data type, not 8-bit floats. It covers models up to 175B parameters, and emergent features might disrupt the method at larger scales. It does not quantize the attention function itself, and it studies inference rather than training, leaving 8-bit training to later work.

When to use it: inference of large dense transformers when memory is the limit and accuracy must match the 16-bit model.

When not to use it: small models that already fit, where the overhead outweighs the savings, and training.

Alternatives: lower-bit post-training quantization, which saves more memory but accepts some accuracy loss, and Mixed-Precision Training, which keeps 16-bit numbers during training.

Reading guide

  • Section 1, Introduction (needs Quantization and Transformer): the claim, the outlier figure, and the list of contributions. Read Figure 1 first.
  • Section 2, Background (needs Quantization, Floating-Point Numbers and Dot Product): the absmax and zeropoint formulas. You can skip the zeropoint details on a first read, since the final method uses absmax.
  • Section 3, Int8 Matrix Multiplication at Scale (needs Matrix Multiplication and Dot Product): vector-wise quantization in 3.1 and the decomposition in 3.2. The equations there are the method.
  • Section 4, Emergent Outliers (needs Transformer, Softmax and Perplexity): how the outliers were found, using the criteria of 25 percent of layers and 6 percent of positions, and the perplexity analysis.
  • Section 5, Results and Table 1 (needs Perplexity and Neural Scaling Laws for the scaling trend): read the 13B and 6.7B rows first.
  • Appendix D (needs Matrix Multiplication): the speed measurements, which are the place to check the limits of the speed claim.

Check yourself

QuestionIn one sentence, what outlier problem makes simple 8-bit quantization fail for large transformers?Show answer

Above about 6.7B parameters, a few feature dimensions carry values much larger than typical in every layer. A single scale per tensor is then set by those values, so most of the typical values round to zero.

QuestionIn the tiny instance, the 8-bit part gives an integer dot product of -18145 before dequantization, with scales 63.5 and 317.5. What is the dequantized value of that part?Show answer

Divide -18145 by 63.5 times 317.5 = 20161.25. The result is about -0.900. Adding the 16-bit outlier term 0.800 gives -0.100, which matches the exact dot product.

QuestionA model with 6.7B parameters has 14.13 perplexity with the simple 8-bit method and 13.30 in float. Why does the decomposed method matter at this size?Show answer

The simple 8-bit method is measurably worse at 6.7B, and LLM.int8() recovers the float quality, 13.24 against 13.30. The decomposition is what closes the gap, so the simple method is not enough at this scale.

QuestionWhy does the method keep outlier features in 16 bits rather than clipping them to the 8-bit range?Show answer

The paper finds that outliers carry important signal: zeroing them reduces top-1 attention probability mass by more than 20 percent and raises perplexity by hundreds of percent. Clipping would change those values and damage the model, so the method keeps them at full precision instead.

Sources

Read the full paper

Tim Dettmers, Mike Lewis, Younes Belkada et al., 2022. arXiv:2208.07339