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

456. Quantize activations to uint8 with a scale and zero point

EasyQuantization
Asked atNVIDIA

Activations after ReLU or GELU are mostly positive, so a symmetric int8 grid wastes half its codes. Asymmetric quantization maps the range [lo, hi] onto 0..255 and stores a zero point so that the real value 0.0 lands exactly on a code. Padding and ReLU zeros then dequantize to exactly 0.

Write quantize_uint8(x) for a 1D float array x. Steps:
- lo = min(x.min(), 0.0), hi = max(x.max(), 0.0). The range must always contain 0.
- If hi == lo (all zeros), return (array of zeros as int, 1.0, 0).
- scale = (hi - lo) / 255
- zero_point = int(np.round(-lo / scale)), clipped to [0, 255]
- q = clip(np.round(x / scale) + zero_point, 0, 255) as int
Return (q, scale, zero_point). Dequantization is (q - zero_point) * scale.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef quantize_uint8(x)

Example 1

Input: x = [-1.0, 0.0, 2.0]

Output: scale = 3/255, zero_point = 85, q = [0, 85, 255]

Example 2

Input: x = [2.740919, 0.2, 1.118099, 0.2, 0.247351, 0.484403, 0.2, 0.468068, 0.2, 4.023, 0.925787, 0.347369]

Output: ([174, 13, 71, 13, 16, 31, 13, 30, 13, 255, 59, 22], 0.015776, 0)

Example 3

Input: x = [0.0, 0.0, 0.0]

Output: ([0, 0, 0], 1.0, 0)

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Stretch the range to include 0 before computing the scale.

Hint 2

The zero point is the code that the value 0.0 maps to.

Common mistakes
  • The buggy version uses the raw min and max.
Understand the idea

Asymmetric quantization maps a real range [lo, hi] onto the 256 codes 0 to 255. A zero point says which code stands for the real value 0. Forcing the range to include 0 guarantees that 0.0 lands exactly on a code, so ReLU zeros and padding come back as exact zeros.

Worked example

x = [-0.5, 2.0]. lo = -0.5, hi = 2.0, scale = 2.5 / 255 = 0.009804. zero_point = round(0.5 / 0.009804) = round(51) = 51. q(-0.5) = round(-51) + 51 = 0. q(2.0) = round(204) + 51 = 255. q(0.0) = 51, which dequantizes to (51 - 51) * scale = 0 exactly. For all-positive data like [0.5, 1.0], lo is still 0, so the zero point is 0.

lo = min(min(x), 0), hi = max(max(x), 0); if equal return zeros with scale 1 and zero point 0; scale = (hi - lo) / 255; zero_point = round(-lo / scale) clipped to [0, 255]; q = clip(round(x / scale) + zero_point, 0, 255)

Used for int8 activation quantization in mobile and server inference.

Topics
Quantization
Companies1
  • NVIDIA the topic appears in interview reports NVIDIA Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Per-channel vs per-tensor int8: measure the errorEasy
  • Unpack int4 weights from bytesEasy
  • Quantize with stochastic rounding from given random drawsEasy
  • Work out effective bits per weight and model size for group quantizationEasy
All Quantization problems
Report
456 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def quantize_uint8(x):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

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