AWQ › Worked Example: Tracing AWQ Through a Two-Channel Layer

Michael BrenndoerferJanuary 14, 20263 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 321AWQ › Worked Example: Tracing AWQ Through a Two-Channel Layer
Reading section 4 of 11 · Worked Example: Tracing AWQ Through a Two-Channel Layer · 3 minSection 4 of 11 · 3 min

Book contents

Section 4 of 11 · 3 min read

AWQ › Worked Example: Tracing AWQ Through a Two-Channel LayerOne section · Sections in this chapter4 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's trace the AWQ algorithm step by step through a minimal example to make the mathematics concrete. We will work with a tiny linear layer with just two input channels and show exactly how quantization error changes when we apply AWQ versus naive round-to-nearest quantization.

Setup. Consider a linear layer with a single-row weight matrix w=[0.5,2.0]\mathbf{w} = [0.5, 2.0] (one output, two inputs). Suppose the calibration dataset gives us average activation magnitudes aˉ0=10.0\bar{a}_0 = 10.0 for channel 0 and aˉ1=0.1\bar{a}_1 = 0.1 for channel 1.

Step 1: Identify salient channels. The maximum activation magnitude across channels is max⁡kaˉk=10.0\max_k \bar{a}_k = 10.0 (channel 0). The normalized magnitudes are aˉ0/10.0=1.0\bar{a}_0 / 10.0 = 1.0 and aˉ1/10.0=0.01\bar{a}_1 / 10.0 = 0.01.

Step 2: Compute scaling factors with α=0.5\alpha = 0.5. Applying the AWQ formula:

s0=(1.0)0.5=1.0s1=(0.01)0.5=0.1\begin{aligned} s_0 &= (1.0)^{0.5} = 1.0 \\ s_1 &= (0.01)^{0.5} = 0.1 \end{aligned}

Channel 0 (the salient channel) gets scaling factor 1.0. Channel 1 (the non-salient channel) gets scaling factor 0.1.

Step 3: Transform weights. Multiply each weight by its channel's scaling factor:

w~0=0.5×1.0=0.5w~1=2.0×0.1=0.2\begin{aligned} \tilde{w}_0 &= 0.5 \times 1.0 = 0.5 \\ \tilde{w}_1 &= 2.0 \times 0.1 = 0.2 \end{aligned}

After scaling, the transformed weights are [0.5,0.2][0.5, 0.2]. Notice that the max weight has dropped from 2.0 to 0.5 because we compressed the unimportant channel materially.

Step 4: Quantize the transformed weights. With a 3-bit symmetric quantizer (7 levels for simplicity), the quantization scale is:

sq=max⁡∣w~∣2b−1−1=0.53≈0.1667s_q = \frac{\max|\tilde{w}|}{2^{b-1} - 1} = \frac{0.5}{3} \approx 0.1667

Quantizing each transformed weight:

w^0=round(0.5/0.1667)×0.1667=round(3.0)×0.1667=0.5w^1=round(0.2/0.1667)×0.1667=round(1.2)×0.1667≈0.1667\begin{aligned} \hat{w}_0 &= \text{round}(0.5 / 0.1667) \times 0.1667 = \text{round}(3.0) \times 0.1667 = 0.5 \\ \hat{w}_1 &= \text{round}(0.2 / 0.1667) \times 0.1667 = \text{round}(1.2) \times 0.1667 \approx 0.1667 \end{aligned}

Step 5: Dequantize and undo channel scaling. The effective weights recovered after inverse scaling are:

w0eff=0.5/1.0=0.5w1eff=0.1667/0.1=1.667\begin{aligned} w^{\text{eff}}_0 &= 0.5 / 1.0 = 0.5 \\ w^{\text{eff}}_1 &= 0.1667 / 0.1 = 1.667 \end{aligned}

Step 6: Compute weighted quantization error. The output-weighted errors are:

AWQ Error0=∣0.5−0.5∣×10.0=0.0AWQ Error1=∣2.0−1.667∣×0.1=0.333×0.1=0.033\begin{aligned} \text{AWQ Error}_0 &= |0.5 - 0.5| \times 10.0 = 0.0 \\ \text{AWQ Error}_1 &= |2.0 - 1.667| \times 0.1 = 0.333 \times 0.1 = 0.033 \end{aligned}

Comparison with RTN. Without AWQ, standard RTN quantizes the original weights [0.5,2.0][0.5, 2.0] with scale sq=2.0/3≈0.667s_q = 2.0/3 \approx 0.667:

w0RTN=round(0.5/0.667)×0.667=round(0.75)×0.667=1×0.667=0.667w1RTN=round(2.0/0.667)×0.667=round(3.0)×0.667=2.0\begin{aligned} w^{\text{RTN}}_0 &= \text{round}(0.5 / 0.667) \times 0.667 = \text{round}(0.75) \times 0.667 = 1 \times 0.667 = 0.667 \\ w^{\text{RTN}}_1 &= \text{round}(2.0 / 0.667) \times 0.667 = \text{round}(3.0) \times 0.667 = 2.0 \end{aligned}

The RTN weighted errors are:

RTN Error0=∣0.5−0.667∣×10.0=0.167×10.0=1.67RTN Error1=∣2.0−2.0∣×0.1=0.0\begin{aligned} \text{RTN Error}_0 &= |0.5 - 0.667| \times 10.0 = 0.167 \times 10.0 = 1.67 \\ \text{RTN Error}_1 &= |2.0 - 2.0| \times 0.1 = 0.0 \end{aligned}

The comparison is stark. RTN spends most of its quantization effort on channel 1 (the large weight) but channel 1 multiplies near-zero activations, so that accuracy is wasted. Meanwhile, channel 0 (the small weight that multiplies large activations) suffers a relative error of 33% that gets amplified by the activation magnitude of 10, creating an output error of 1.67. AWQ nearly eliminates this output error by giving channel 0 all the precision it needs, accepting a larger relative error on channel 1 where it barely matters.

This example captures the needed logic of AWQ in numerical form: precision is a resource to be allocated intelligently, not distributed uniformly.