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

461. Group-wise int4 quantization like GPTQ and AWQ checkpoints

MediumQuantization Pro

One scale for a whole weight matrix wastes precision when a few values are large. 4-bit LLM checkpoints use one scale per small group of weights instead.

Write quantize_int4_groups(w, group) for a 1D weight vector whose length is a multiple of group. For each group, use symmetric quantization:
scale = max(|w|) / 7 (if the max is 0, use scale 1.0)
q = clip(round(w / scale), -8, 7) as integers
Return (q, scales, dequantized) where dequantized = q * scale per group. Use np.round (round half to even).

Functiondef quantize_int4_groups(w, group)

Example 1

Input: quantize_int4_groups([0.7, -0.35, 0.0, 1.4], 2)

Output: ([7, -4, 0, 7], [0.1, 0.2], [0.7, -0.4, 0.0, 1.4])

Example 2

Input: quantize_int4_groups(np.array([0.01, -0.02, 0.03, 0.005, 2.0, -1.0, 0.5, 0.25]), 4)[0]

Output: [2, -5, 7, 1, 7, -4, 2, 1]

Example 3

Input: quantize_int4_groups(np.array([0.01, -0.02, 0.03, 0.005, 2.0, -1.0, 0.5, 0.25]), 4)[1]

Output: [0.004286, 0.285714]

Constraints

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

Treat each group as its own small vector with its own scale, which is easy if you reshape so that each group is one row.

Hint 2

Reshape to (n_groups, group), compute max abs per row divided by 7 as the scale, replace zero scales with 1.0, divide, round, clip to [-8, 7], then multiply back and flatten.

Hint 3

Use keepdims=True on the max so the scale broadcasts over each row, and np.where(s == 0, 1.0, s) for all-zero groups. Cast q with .astype(int) and return scales as a 1D array like s[:, 0].

Understand the idea

With only 4 bits there are just 16 levels, -8 to 7. One scale for a whole vector wastes them when a few weights are large, because small weights all round to 0. Group-wise quantization gives each small group of weights its own scale. Like using a different ruler for each drawer.

Worked example

w = [1.4, -0.6, 0.25, 0.07, -0.02, 0.0], group 3. Group 1: max |w| = 1.4, scale = 1.4 / 7 = 0.2, q = round([7, -3, 1.25]) = [7, -3, 1], dequantized [1.4, -0.6, 0.2]. Group 2: max 0.07, scale = 0.01, q = [7, -2, 0], dequantized [0.07, -0.02, 0]. With one global scale of 0.2, group 2 would become all zeros.

per group, scale = max|w| / 7 (1.0 if the max is 0); q = clip(round(w / scale), -8, 7); dequantized = q x scale. np.round uses round half to even. 4-bit LLM checkpoints store int4 codes plus one scale per group of 32 to 128 weights
Topics
Quantization
Similar questions
  • Quantize weights to int8 and measure the errorMedium
  • Search for an activation-aware per-channel scale before int4 roundingMedium
  • Round float32 to bfloat16Medium
  • Migrate activation outliers with SmoothQuant scalesMedium
All Quantization problems
Report
461 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def quantize_int4_groups(w, group):
# your code here
pass
​
quantize_int4_groups(np.array([0.01, -0.02, 0.03, 0.005, 2.0, -1.0, 0.5, 0.25]), 4)[0]

This case uses generated data, so it runs as written. Press + to add your own case.

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