Migrate activation outliers with SmoothQuant scales

MediumQuantizationPro

Why it matters SmoothQuant is how int8 W8A8 inference handles LLM activation outlier channels.

SmoothQuant moves difficulty from activations to weights: divide activation channel j by s_j and multiply weight row j by s_j.

Write smooth_scales(X, W, alpha=0.5). X is (n, k) activations, W is (k, m) weights. s_j = max|X[:, j]|^alpha / max|W[j, :]|^(1 - alpha). Return (s, X / s, W * s[:, None]). The product of the returned X and W must equal X @ W.

Signaturedef smooth_scales(X, W, alpha=0.5)

Example

X = [[4, 1]], W = [[1], [1]], alpha 0.5 gives s [2, 1].

Understand the idea

Activations in LLMs have a few channels with huge outliers, which makes 8-bit activation quantisation lose precision. Weights are much smoother. SmoothQuant divides each activation channel by a scale s_j and multiplies the matching weight row by the same s_j, so the matrix product does not change but the outliers shrink. alpha decides how much difficulty moves to the weights. Worked example: X = [[8, -1], [2, 0.5]], W = [[2, 1], [0.5, 4]], alpha 0.5. Channel 0: max|X| = 8, max|W row 0| = 2, so s_0 = sqrt(8) / sqrt(2) = 2. Channel 1: max|X| = 1, max|W row 1| = 4, so s_1 = 1 / 2 = 0.5. New X = [[4, -2], [1, 1]] and new W = [[4, 2], [0.25, 2]]. Check row 0, column 0: before 8 * 2 + (-1) * 0.5 = 15.5; after 4 * 4 + (-2) * 0.25 = 15.5. Rule: s_j = max|X[:, j]|^alpha / max|W[j, :]|^(1 - alpha); return s, X / s and W * s[:, None]. The scale belongs to the shared input channel, which is a row of W, not a column. Used in: W8A8 LLM inference.

Shapes and data flow

X (n, k) / s (k,) per column
W (k, m) * s[:, None] per row
(X / s) @ (s W) = X W
solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: examplesmooth_scales([[4, 1]], [[1], [1]])
  • Sample 2: product preservedfloat(np.abs(np.matmul(*smooth_scales(_X, _W)[1:]) - _X @ _W).max())