PaperLeet

SmoothQuant Equivalent Activation and Weight Smoothing

SmoothQuant · ICML 2023 · Medium · 40 minutes

Compute input-channel smoothing factors and preserve linear-layer outputs and gradients under reciprocal scaling.

Calibration maxima, offline scales and differentiable equivalent transforms; the experiment simulates symmetric INT8 without integer GEMM or LayerNorm fusion.

Functions to implement

SmoothQuant Equivalent Activation and Weight Smoothing

1. Problem

Compute input-channel smoothing factors and preserve linear-layer outputs and gradients under reciprocal scaling.

Calibration maxima, offline scales and differentiable equivalent transforms; the experiment simulates symmetric INT8 without integer GEMM or LayerNorm fusion.

Source: SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language Models · Section 4, Equations (3) and (4); official smooth.py offline scale construction.

2. Core equations

$$ s_j=\max\left(\frac{(\max_i|X_{ij}|)^\alpha}{\max(\max_o|W_{jo}|,\epsilon)^{1-\alpha}},\epsilon\right),\quad \widehat X=X/s,\quad \widehat W=sW,\quad \widehat X\widehat W=XW. $$

3. From equations to code

  • smooth_scales(x: Tensor, weight: Tensor, alpha: float = 0.5, eps: float = 1e-5) -> Tensor — Detached positive per-input-channel calibration scales.
  • smooth_transform(x: Tensor, weight: Tensor, scales: Tensor) -> tuple[Tensor, Tensor] — Differentiable reciprocally rescaled activations and weights.

Reduce activation maxima over samples and weight maxima over output channels to obtain the same input-channel vector. Divide activations and multiply weights by that vector. Calibration statistics are detached while the equivalent matrix product preserves training gradients.

4. Conventions

x:(N,D), weight:(D,O), alpha in [0,1], eps>0. Clamp weight-channel absolute maxima to eps, then clamp final scales to eps. Zero activation channels follow this formula. Calibration scales are detached; smooth_transform is differentiable in x, weight and scale. Inputs are unchanged.

5. Experiment view

Use 17 samples with one activation channel amplified by 100 and correspondingly small weights; sweep alpha and compare direct and smoothed INT8 reconstruction errors.

The chart is generated by the actual submitted kernel during T3 and by the reference during export. Values are synthetic teaching measurements, not GPU throughput or full-model quality claims.

6. Run your implementation

Complete the functions in the Python editor on the right, then click Run tests. Inspect each failed check, update your implementation, and run again. Passing every required check marks the problem as solved and unlocks the notes.

7. Further study

Read the cited section and explain which dimensions or scheduling constraints the kernel preserves. Then extend the experiment beyond the stated fixture while retaining the existing contract. Larger models, real devices, and communication costs require separate measurement.

Read paper · arXiv:2211.10438

Related exercises