I8Hard★ 大模型量化必考 · AWQ 核心Part I · 推理生成、解码与量化
AWQ 激活感知缩放与 Group-Wise INT4 量化
按分组缩小权重量化粒度,并用激活异常通道反比缩放因子降低 INT4 舍入误差在关键通道上的输出放大。
⏱️ 时间复杂度:
O(B * D_in * D_out)💾 空间复杂度:
分组缩放因子额外开销约 0.5 bit/参数🎯 免登录实战自测闭环(先做再看答案)
按「机制回忆 → 关键行填空 → 边界反例 → 追问与错题记录」顺序自测,进度自动保存在本地。
Step 1 · 机制口述与数值稳定性自检
Step 2 · 核心代码关键行补全
import numpy as np
def groupwise_int4_quantize_dequant(w: np.ndarray, group_size: int = 4) -> np.ndarray:
"""
Symmetric group-wise INT4 quantization & dequantization over weight shape (in_dim, out_dim).
Splits input channels into blocks of `group_size` so outliers stay localized.
"""
in_dim, out_dim = w.shape
assert in_dim % group_size == 0
num_groups =
wg = w.reshape(num_groups, group_size, out_dim)
max_abs = np.max(np.abs(wg), axis=1, keepdims=True)
scales = np.where(max_abs > 0, max_abs / 7.0, 1.0)
q = np.clip(np.round(wg / scales), -7, 7)
return (q * scales).reshape(in_dim, out_dim)
def awq_salient_channel_mse(x: np.ndarray, w: np.ndarray, scale_vec: np.ndarray, group_size: int = 4) -> float:
"""
Evaluate AWQ activation-aware scaling: multiply salient input channels of W by s > 1
before INT4 group quantization and divide X by s at runtime.
"""
s = scale_vec[:, np.newaxis] # (in_dim, 1)
w_scaled_q = groupwise_int4_quantize_dequant(w * s, group_size=group_size)
x_scaled = x / scale_vec[np.newaxis, :]
y_ref = x @ w
y_awq = x_scaled @ w_scaled_q
return
Step 3 · 反例与边界断言核对
💡
实战一句话记忆口诀
权重按组算步长,激活大处权重放大,输入反除保等价,关键通道少舍入
📐 核心数学公式与机理推导
### 为什么不直接按权重大小保护,而看激活分布?
Lin 等人在 2023 年(AWQ)发现:大模型权重里只有约 0.1%~1% 的“显著通道(Salient Channels)”对精度极其致命,而这些关键通道由输入激活幅值较大的维度决定,而非权重本身的幅值。
1. Group-Wise 切组:每隔 (如 64 或 128)个元素共享一个 Scale,把单点极大权重的影响锁在小组内;
2. 等价通道缩放:对第 个显著通道权重放大 (同时推理时对激活缩小 ,数学输出完全等价),使得组内量化步长相对显著权重变细,有效降低该关键通道的相对舍入误差。
Lin 等人在 2023 年(AWQ)发现:大模型权重里只有约 0.1%~1% 的“显著通道(Salient Channels)”对精度极其致命,而这些关键通道由输入激活幅值较大的维度决定,而非权重本身的幅值。
1. Group-Wise 切组:每隔 (如 64 或 128)个元素共享一个 Scale,把单点极大权重的影响锁在小组内;
2. 等价通道缩放:对第 个显著通道权重放大 (同时推理时对激活缩小 ,数学输出完全等价),使得组内量化步长相对显著权重变细,有效降低该关键通道的相对舍入误差。
🔄 张量维度与形状流转说明
x:(B,D_in), w:(D_in,D_out) -> w*s 分组算 INT4 scale -> q*scale -> (x/s) @ w_q -> 低误差重构输出
🛡️ 数值稳定性自检与边界要点
- 显著缩放因子 不可调得过大,否则同一个量化组内的其余普通权重会被新拉高的
max_abs挤占动态范围而丢失精度 - 与 GPTQ 需要计算 Hessian 逆矩阵逐列更新不同,AWQ 只需在前向校准样本上搜索通道激活均值与最优缩放幂次
💻 完整参考代码实现
import numpy as np
def groupwise_int4_quantize_dequant(w: np.ndarray, group_size: int = 4) -> np.ndarray:
"""
Symmetric group-wise INT4 quantization & dequantization over weight shape (in_dim, out_dim).
Splits input channels into blocks of `group_size` so outliers stay localized.
"""
in_dim, out_dim = w.shape
assert in_dim % group_size == 0
num_groups = in_dim // group_size
wg = w.reshape(num_groups, group_size, out_dim)
max_abs = np.max(np.abs(wg), axis=1, keepdims=True)
scales = np.where(max_abs > 0, max_abs / 7.0, 1.0)
q = np.clip(np.round(wg / scales), -7, 7)
return (q * scales).reshape(in_dim, out_dim)
def awq_salient_channel_mse(x: np.ndarray, w: np.ndarray, scale_vec: np.ndarray, group_size: int = 4) -> float:
"""
Evaluate AWQ activation-aware scaling: multiply salient input channels of W by s > 1
before INT4 group quantization and divide X by s at runtime.
"""
s = scale_vec[:, np.newaxis] # (in_dim, 1)
w_scaled_q = groupwise_int4_quantize_dequant(w * s, group_size=group_size)
x_scaled = x / scale_vec[np.newaxis, :]
y_ref = x @ w
y_awq = x_scaled @ w_scaled_q
return float(np.mean((y_ref - y_awq) ** 2))
🧪 本地测试与正确性断言
可直接复制到本地 Python / Jupyter 验证:
import numpy as np
np.random.seed(7)
x = np.random.randn(8, 8)
x[:, 0] *= 25.0 # salient outlier channel 0
w = np.random.randn(8, 6) * 0.5
naive_mse = awq_salient_channel_mse(x, w, np.ones(8), group_size=4)
scales = np.ones(8)
scales[0] = 4.0 # boost salient channel before quantization
awq_mse = awq_salient_channel_mse(x, w, scales, group_size=4)
assert awq_mse < naive_mse * 0.5, f"AWQ scaling should cut quantization error: awq={awq_mse}, naive={naive_mse}"
print("✓ AWQ activation-aware group INT4 quantization assertion passed")🎯 核心工程追问与标准应答
Q1:AWQ 与 GPTQ 在 4-bit 权重量化原理与校准稳健性上有何核心区别?
GPTQ 基于二阶 Hessian 逆矩阵逐列量化并补偿残差误差,容易对校准数据集过拟合;AWQ 不修改权重优化残差,而是根据激活统计为关键通道求解等价缩放因子,泛化更好且实现更轻量。
⚡
想检验本题的肌肉记忆?进入 3 阶闪卡主动回忆盲测
隐藏答案遮罩,自测:审题锚点 ➔ 数理公式机理 ➔ 工业标杆代码与连环追问。3 分钟攻破认知盲区!