Let the input hidden states be X∈Rs×h (s is the number of tokens and h the hidden dimension) and the weights W∈Rh×o (o is the output dimension). LLM.int8() computes XW in the following steps.
- Inspect each column (hidden dimension) of X and collect the dimensions containing at least one value whose magnitude is at or above a threshold α into the set of outlier dimensions O. In the paper, α=6.0 was sufficient.
- Extract the O columns of X and the O rows of W and compute their matrix product in 16-bit floating point.
- For the remaining part, compute a constant cx from the maximum absolute value of each row of X and a constant cw for each column of W, and quantize each to 8-bit integers in [−127,127].
- Compute the matrix product of the 8-bit integers, accumulating in 32-bit integers.
- Divide each element of the result by its row constant and column constant (dequantize) to return to 16 bits.
- Add the results of steps 2 and 5 to obtain the output.
The quantization in step 3 is absmax quantization, which divides by the maximum absolute value and multiplies by 127 (Section 2.1 of the paper). When the whole of X is quantized with a single constant, the formula is as follows.
Xi8=⌊maxij∣Xij∣127X⌉
Vector-wise quantization keeps this constant for each row or column. Since element (i,j) of the output is the inner product of row i of X and column j of W, dividing by the product of the row constant cx,i and the column constant cw,j restores the original scale. For the whole matrix, this means dividing by the outer product of the constants cx⊗cw (Eq. (7) in the paper).
XW≈cx⊗cw1∘(Xi8Wi8)
Here ∘ denotes element-wise multiplication, and the division is also element-wise. cx is a vector of length s and cw a vector of length o.
The mixed-precision decomposition that separates the outlier dimensions can be written as follows (Eq. (8) in the paper).
XW≈h∈O∑Xf16hWf16h+S⋅h∈/O∑Xi8hWi8h
The first term is the 16-bit matrix product over the outlier dimensions only, the second term is the 8-bit matrix product over the rest, and S is the dequantization factor. According to the paper, models up to 13B parameters have at most 7 outlier dimensions, so the extra memory for keeping them in 16 bits is only about 0.1%.
A matrix product is a sum over the hidden dimension h. Therefore, splitting the hidden dimensions into "dimensions with outliers" and "dimensions without," computing them separately, and adding them at the end gives the same result as computing without splitting. This is why the decomposition does not change the meaning of the computation.