BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations28 of 28 in topic

855. LLM.int8() outlier decomposition matmul

HardPaper implementations Pro

Why it matters bitsandbytes load_in_8bit uses this method. It is the clearest example of why outliers make LLM quantization hard and how mixed precision fixes it.

Paper: "LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale", Dettmers, Lewis, Belkada and Zettlemoyer, 2022 (NeurIPS 2022), arXiv 2208.07339.

Large models have a few hidden dimensions with huge activations. LLM.int8() splits X @ W into two parts:
1. Outlier part: the columns of X that contain an outlier, and the matching rows of W, are multiplied in high precision (16-bit in the paper; plain floats here). In this problem a column is an outlier if any value in it has abs(value) >= threshold. The paper uses threshold 6.0.
2. Regular part: everything else uses vector-wise int8 absmax quantization. Each row of X_r gets its own scale sx = max(abs(row)) / 127, each column of W_r gets its own scale sw = max(abs(col)) / 127 (a scale of 0 is replaced by 1.0). Quantize with np.round(value / scale) to ints, multiply the int matrices, then dequantize: (Qx @ Qw) * sx[:, None] * sw[None, :].

The output is the sum of the two parts.

Write llm_int8_matmul(X, W, threshold=6.0).

Inputs: X is a list or ndarray of shape (n, h), W of shape (h, o).

Return a tuple (Y, outlier_cols): Y is an ndarray of floats with shape (n, o), outlier_cols a sorted list of Python ints. If every column is an outlier the regular part is zero; if none is, the outlier part is zero. No rounding of Y.

Functiondef llm_int8_matmul(X, W, threshold=6.0)

Example 1

Input: llm_int8_matmul([[1.0, 8.0], [0.5, -0.2]], [[1.0, 2.0], [0.5, 0.0]])

Output: ([[5.0, 2.0], [0.4, 1.0]], [1])

Example 2

Input: X = [[1.0, 2.0, 3.0]], W = [[1.0], [1.0], [1.0]]

Output: ([[6.0]], [])

Constraints

  • W has shape (h, o)
  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Find outlier columns with (abs(X) >= threshold).any(axis=0).

Hint 2

Multiply the outlier slice in floats: X[:, mask] @ W[mask, :].

Hint 3

Quantize the rest with per-row and per-column absmax scales, int matmul, then multiply by sx[:, None] * sw[None, :].

Common mistakes
  • Using one scale for the whole matrix.
  • Quantizing W per row instead of per column.
  • Dropping the outlier rows of W but not the outlier columns of X.
Understand the idea

One value of 8 in a row makes the absmax scale of that row 8 / 127, so small values in the same row round to 0 or 1 step. Pulling outlier columns out of the int8 path keeps the scale small for everything else. Vector-wise scales (one per row of X and one per column of W) mean each output entry X[i] . W[:, j] has its own pair of scales, which is the finest granularity that still allows a plain int matmul followed by one rescale.

Shapes and data flow
X: (n, h). W: (h, o). Outlier mask: (h,). sx: (n,). sw: (o,). Y: (n, o).
Topics
Paper implementations
Similar questions
  • FlashAttention-style tiled causal attention forward (Dao et al., 2022)Hard
  • Mamba-style selective state-space scan with zero-order-hold discretizationHard
  • Implement recurrent retention from RetNetHard
  • Implement differential attention with the lambda reparameterizationHard
All Paper implementations problems
Report
855 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def llm_int8_matmul(X, W, threshold=6.0):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS