AI Logic

Model Compression

AWQ (Activation-aware Weight Quantization)

Updated:

How AWQ works. The left side shows the flow for quantizing one layer: measure the per-input-channel activation magnitude from calibration data, search for Ξ± with s = s_X raised to the power Ξ±, and quantize WΒ·diag(s) to INT4. The right side shows the channel with large values in activation X divided by s, and the row with the same index in weight W multiplied by s before rounding

Background and Overview

AWQ (Activation-aware Weight Quantization) keeps accuracy even with 4-bit and 3-bit weights by using activations to find the important weights and scaling those weights up before quantization. It was proposed by Lin et al. in 2023 and presented at MLSys 2024 (Best Paper Award).

When a large language model (LLM) runs on local hardware such as a PC or smartphone, the stage that generates text one token at a time is especially slow. The paper's analysis shows that this stage is bound not by computation but by how fast weights can be read from memory, and that the weights are orders of magnitude larger than the activations. Weight-only 4-bit quantization (W4A16) is therefore effective.

AWQ starts from the observation that not all weights are equally important.

  • Keeping just 0.1–1% of the weights in 16 bits recovers much of the accuracy lost by 3-bit quantization.
  • However, this works only when the kept weights belong to input channels with large activations; selecting by the magnitude of the weights themselves is barely better than selecting at random.

Keeping only some weights in 16 bits is awkward for hardware. AWQ instead obtains the same effect with an equivalent transformation: it multiplies the weights of important channels by ss before quantization and multiplies the input by 1/s1/s. All weights stay in the same 4-bit format, so computation remains efficient.

Theory (Scale Up Important Channels, Then Round)

Consider quantization that rounds a group of weights w\mathbf{w} onto a grid with spacing Ξ”\Delta (Eq. (1) in the paper).

Q(w)=Ξ”β‹…Round ⁣(wΞ”)Q(\mathbf{w}) = \Delta \cdot \mathrm{Round}\!\left(\frac{\mathbf{w}}{\Delta}\right)

Ξ”\Delta is the maximum absolute value in the group, max⁑(∣w∣)\max(|\mathbf{w}|), divided by the number of grid levels determined by the bit width NN.

If a weight ww is multiplied by s>1s > 1 before quantization and the input xx is multiplied by 1/s1/s, the result equals the original wxwx, but the error changes as follows (Eq. (3) in the paper).

Err(Q(w) x)=Ξ”β‹…RoundErr ⁣(wΞ”)β‹…x,Err ⁣(Q(ws) xs)=Ξ”β€²β‹…RoundErr ⁣(wsΞ”β€²)β‹…xβ‹…1s\mathrm{Err}\big(Q(w)\,x\big) = \Delta \cdot \mathrm{RoundErr}\!\left(\frac{w}{\Delta}\right) \cdot x, \qquad \mathrm{Err}\!\left(Q(w s)\,\frac{x}{s}\right) = \Delta' \cdot \mathrm{RoundErr}\!\left(\frac{w s}{\Delta'}\right) \cdot x \cdot \frac{1}{s}

The rounding error RoundErr\mathrm{RoundErr} is unchanged on average (about 0.25), and scaling up a single weight usually does not change the group maximum, so Ξ”β€²β‰ˆΞ”\Delta' \approx \Delta. As a result, the error of the scaled weight shrinks by roughly a factor of 1/s1/s. As an analogy, it is like writing only the important digits in large print before making a reduced copy: the copy is equally coarse, but the digits written larger are misread less often.

If ss is too large, however, the group maximum changes, Ξ”β€²\Delta' grows, and the error of the other weights in the same group increases. In the paper's OPT-6.7B experiment, scaling the 1% of channels with the largest activations by s=2s = 2 gave the best perplexity (23.54 β†’ 11.92), while s=4s = 4 degraded it to 12.36.

AWQ therefore chooses a per-input-channel scale ss that minimizes the deviation of the layer output after quantization (Eqs. (4) and (5) in the paper).

sβˆ—=argmin⁑sβˆ₯Q(Wβ‹…diag(s))(diag(s)βˆ’1β‹…X)βˆ’WXβˆ₯,s=sXΞ±\mathbf{s}^* = \operatorname*{argmin}_{\mathbf{s}} \left\| Q\big(\mathbf{W} \cdot \mathrm{diag}(\mathbf{s})\big)\big(\mathrm{diag}(\mathbf{s})^{-1} \cdot \mathbf{X}\big) - \mathbf{W}\mathbf{X} \right\|, \qquad \mathbf{s} = \mathbf{s}_{\mathbf{X}}^{\alpha}

The procedure is as follows.

  1. Run a small amount of calibration data through the model and collect the input X\mathbf{X} of each layer. To avoid bias toward a particular application, the paper takes general text of the kind used for pretraining (the Pile).
  2. For each input channel, compute the mean absolute activation sX\mathbf{s}_{\mathbf{X}}.
  3. Try 20 values of Ξ±\alpha in the range 0–1 and pick the s=sXΞ±\mathbf{s} = \mathbf{s}_{\mathbf{X}}^{\alpha} that gives the smallest output deviation after quantization. Ξ±=0\alpha = 0 means no scaling, and Ξ±=1\alpha = 1 means the strongest scaling.
  4. Also tune the weight clipping range so that the mean squared quantization error becomes small.
  5. Quantize Wβ‹…diag(s)\mathbf{W} \cdot \mathrm{diag}(\mathbf{s}) to INT4 (or INT3) in groups of 128.
  6. Fold diag(s)βˆ’1\mathrm{diag}(\mathbf{s})^{-1} into the coefficients of the preceding operation (such as a normalization layer). No extra computation is added at inference time.

AWQ uses neither backpropagation nor the error-compensating regression of methods like GPTQ. The only quantity measured from data is the average activation magnitude per channel, so it needs little data and is less prone to overfitting the calibration set.

Experimental Results

Table 1 of the paper compares which weights to keep in 16 bits for 3-bit OPT-6.7B with group size 128 (WikiText perplexity; lower is better).

Selection0.1%1%3%
By activation magnitude11.5811.3911.36
By weight magnitude23.4122.3722.45
Random23.5424.2324.22

The 16-bit model scores 10.86, and simple round-to-nearest (RTN) on all weights scores 23.54. Selecting by activation gives a large improvement while keeping only 0.1%.

The main results (Table 4: Llama 2 WikiText2 perplexity, group size 128) are as follows.

Model16-bitRTN 4-bitGPTQ 4-bitAWQ 4-bitRTN 3-bitGPTQ 3-bitAWQ 3-bit
Llama 2-7B5.475.735.695.606.666.436.24
Llama 2-13B4.884.984.984.975.525.485.32
Llama 2-70B3.323.463.423.413.983.883.74

The paper also reports the following.

  • It needs little data. On 3-bit OPT-6.7B, AWQ with 16 sequences achieved better perplexity than GPTQ with 192.
  • It also works for instruction-tuned and vision-language models. The paper evaluates Vicuna, OpenFlamingo-9B, and LLaVA-13B and reports good quantization results.
  • Combined with a dedicated inference framework (TinyChat), it is faster. With 4-bit weights, it ran more than 3Γ— faster than the 16-bit Hugging Face implementation on both desktop and mobile GPUs.

Practical Considerations

  • Only the weights are quantized (W4A16). Activations stay in 16 bits, so the amount of computation does not decrease. The speedup comes from reading fewer weight bytes from memory, and it requires dedicated kernels for low-bit weights.
  • It assumes an architecture where the scale 1/s1/s can be folded into the preceding operation. The paper absorbs the input-side scale into the operation just before, such as a normalization layer. For layers with nothing to fold into, computation increases accordingly.
  • Choose the group size and bit width to balance accuracy against memory. The paper uses a group size of 128 in almost all experiments.

Scaling too aggressively actually hurts accuracy. The error of the important channels decreases, but the grid for the other channels in the same group becomes coarser. This is why AWQ searches for Ξ±\alpha; a fixed large factor can degrade results, as in the paper's s=4s = 4 example.

Comparison with Other Methods

The table compares methods for reducing LLM weights to low bit widths along the same dimensions.

MethodHow important weights are protectedComputation during quantizationData required
RTN (round-to-nearest)Not protectedAlmost noneNone
GPTQCompensates rounding error by adjusting the remaining weightsRegression using the HessianSmall (128 sequences in the paper)
AWQScales up weights of channels with large activations before roundingScale search (no backpropagation)Less than GPTQ (outperformed it with 16 sequences in the paper)
SmoothQuantMigrates the magnitude of activation outliers into the weights (goal: 8-bit activations as well)Scales set by a formulaSmall

AWQ and SmoothQuant both use the equivalent transformation of "scaling up the weights and scaling down the input per input channel." They differ in purpose: SmoothQuant flattens activation outliers to make 8-bit activations feasible, while AWQ reduces the error of important weights when quantizing weights to 4 bits.

  • To run an LLM on local hardware with 4-bit weights, AWQ is a good fit. It needs little data and tends to preserve accuracy across models for many applications.
  • To push to 3 bits or below, or when a GPTQ pipeline is already in place, GPTQ is also a candidate. The paper shows that combining AWQ and GPTQ improves results further.
  • To speed up computation by making both weights and activations 8-bit, choose SmoothQuant.

PyTorch example

Show code
python
import torch

# AWQ scale search for a single linear layer (small implementation for illustration)
# W: weights of shape (out, in); X: layer input of shape (tokens, in), collected from calibration data
# y = X @ W.T. A scale s is applied per input channel (column of W)


def quantize_groupwise(W, bits=4, group_size=128):
    """Group-wise symmetric quantization (the form of Eq. (1) in the paper; Delta is set from the group's max absolute value)"""
    out_f, in_f = W.shape
    Wg = W.reshape(out_f, in_f // group_size, group_size)
    delta = Wg.abs().amax(dim=-1, keepdim=True).clamp(min=1e-8) / (2 ** (bits - 1) - 1)
    q = torch.clamp(torch.round(Wg / delta), -(2 ** (bits - 1)) + 1, 2 ** (bits - 1) - 1)
    return (q * delta).reshape(out_f, in_f)  # dequantized values


@torch.no_grad()
def awq_search_scale(W, X, bits=4, group_size=128, n_grid=20):
    s_x = X.abs().mean(dim=0)              # mean activation magnitude per input channel
    ref = X @ W.T                          # output before quantization
    best = (float("inf"), None, None)
    for i in range(n_grid):                # search alpha over 20 points in [0, 1)
        alpha = i / n_grid
        s = s_x.pow(alpha).clamp(min=1e-4)
        W_q = quantize_groupwise(W * s, bits, group_size)   # Q(W Β· diag(s))
        out = (X / s) @ W_q.T                               # (diag(s)^-1 Β· X)
        loss = (out - ref).pow(2).mean().item()
        if loss < best[0]:
            best = (loss, alpha, s)
    return best


torch.manual_seed(0)
in_f, out_f, tokens = 256, 128, 512
W = torch.randn(out_f, in_f) * 0.02
X = torch.randn(tokens, in_f)
X[:, :3] *= 20                             # only a few input channels have large activations

ref = X @ W.T
rtn = X @ quantize_groupwise(W, bits=3).T  # simple rounding (same as alpha = 0)
loss, alpha, s = awq_search_scale(W, X, bits=3)
print("RTN error:", (rtn - ref).pow(2).mean().item())
print("AWQ error:", loss, " chosen alpha:", alpha)
# At inference, store the quantized W * s and fold 1/s into the coefficients of the previous layer (e.g., normalization)

FAQ

Q. Why are important weights selected by activation magnitude rather than weight magnitude?
A.

Because the effect on the output is determined by the product of the weight and the input. For channels with large inputs (activations), even a small deviation in the weight changes the output a lot. In the paper's experiments on 3-bit OPT-6.7B, keeping the 1% of channels with the largest activations in 16 bits improved perplexity from 23.54 to 11.39, whereas keeping the 1% selected by weight magnitude only reached 22.37.

Q. Should I use AWQ or GPTQ?
A.

Both can bring weights to 4 bits without training, and in the paper's comparison AWQ gave slightly better perplexity on LLaMA and Llama 2. AWQ uses neither backpropagation nor regression and needs less data, so it suits situations where the calibration data and the application are likely to differ, such as instruction-tuned models. The paper also shows that combining the two improves results further.

Q. Does multiplying by the scale s add computation at inference?
A.

Hardly any. The ss applied to the weights is folded into the weights before quantization. The 1/s1/s applied to the input can be folded into the coefficients of the preceding operation (such as a normalization layer), so it does not need to be computed separately at inference.

References

  1. J. Lin, J. Tang, H. Tang, S. Yang, W.-M. Chen, W.-C. Wang, G. Xiao, X. Dang, C. Gan, and S. Han, "AWQ: Activation-aware Weight Quantization for On-Device LLM Compression and Acceleration," MLSys, 2024. arXiv:2306.00978.
  2. mit-han-lab, "llm-awq" (GitHub).