← 知识图谱 ← 返回学习站

推理优化

Large Transformer Model Inference Optimization
Lilian Weng · 2023-01 · 预计阅读 32 分钟

Large transformer models are powerful but very expensive to train and use. The extremely high inference cost, in both time and memory, is a big bottleneck for adopting a powerful transformer for solving real-world tasks at scale. This post looks into several approaches for making transformer inference more efficient.

大型 Transformer 模型强大但训练和使用都极其昂贵。推理在时间和内存上的极高成本,是把强大 Transformer 用于大规模真实任务的主要瓶颈。本文探讨让 Transformer 推理更高效的几种方法——这些技术(KV Cache、量化、蒸馏、剪枝、稀疏化、MoE)共同支撑了今天 LLM 的低成本部署。

推理为什么难?

Two main factors contribute to the inference challenge: Large memory footprint—both model parameters and intermediate states (e.g. KV cache) are needed in memory; for a batch size of 512 and context length of 2048, the KV cache totals 3TB, 3x the model size! And low parallelizability—autoregressive decoding is hard to parallelize.

两个主要因素造成推理挑战:巨大的内存占用——模型参数和中间状态(如 KV cache)都需驻留内存;例如批大小 512、上下文长度 2048 时,KV cache 总计 3TB,是模型大小的 3 倍!低并行度——自回归解码难以并行。

Goals for inference optimization: reduce memory footprint (fewer GPUs); reduce computation complexity (fewer FLOPs); reduce inference latency (run faster). Methods include parallelism, memory offloading, smart batching, network compression (pruning, quantization, distillation), and architecture-specific improvements.

推理优化的目标:减少内存占用(少用 GPU)、降低计算复杂度(少 FLOPs)、降低延迟(跑更快)。方法包括并行、内存卸载、智能批处理、网络压缩(剪枝、量化、蒸馏)以及针对架构的改进。

💡 AI 解读

"KV cache 是模型大小 3 倍"这个数字是理解 LLM 推理成本的关键。很多人以为瓶颈在参数存储,其实推理时真正的内存大头是 KV cache——每生成一个 token,就要为所有历史 token 缓存 Key/Value 向量。这就是为什么长上下文推理极其昂贵,也是 PagedAttention(vLLM)、KV 量化、滑动窗口注意力等技术的存在意义。自回归的低并行度则是另一个根本约束:生成本质是串行的,无法像训练那样大规模并行,所以单 token 延迟极难压缩。

蒸馏

Knowledge Distillation builds a smaller, cheaper "student" model by transferring skills from a pre-trained expensive "teacher." The distillation loss minimizes the difference between two softmax outputs with a high temperature T, combined with a supervised objective. DistilBERT reduces BERT parameters by 40% while maintaining 97% performance and running 71% faster.

知识蒸馏(KD)通过把预训练的昂贵"教师"模型的技能转移到更小更便宜的"学生"模型来加速推理。蒸馏损失最小化高温 T 下两个 softmax 输出的差异,并结合监督目标。DistilBERT 把 BERT 参数减少 40%,保持 97% 性能,速度快 71%

💡 AI 解读

蒸馏是推理优化中性价比最高的技术之一,也是今天大模型生态的隐形支柱——几乎所有"小而强"的模型背后都有一个大教师的影子。关键洞察:软标签(soft labels,温度 softmax 后的概率分布)比硬标签(one-hot)携带更多信息,因为它编码了类间相似度。实践启示:蒸馏可与量化、剪枝叠加(教师全精度稠密,学生量化稀疏);且不需要原始训练数据,维基百科甚至随机 token 都能work。这也是为何开源社区能把 70B 模型的能力压进 7B——蒸馏功不可没。

量化

Two common approaches: Post-Training Quantization (PTQ) converts weights to lower precision after training (cheap); Quantization-Aware Training (QAT) applies quantization during training (better performance but needs resources). A key challenge: simple 8-bit PTQ leads to significant performance drop mainly due to the high dynamic ranges of activation. As models grow to billions of parameters, outlier features of high magnitude emerge in all layers—some dimensions can be ~100× larger than most others.

两种常见方法:训练后量化(PTQ)训练后再把权重转成低精度(便宜);量化感知训练(QAT)训练时就量化(性能更好但需资源)。关键挑战:简单的 8 位 PTQ 会导致显著性能下降,主要因为激活值动态范围大。随着模型增长到数十亿参数,所有层都出现高幅度异常值特征——某些维度可能比其他值大 100 倍

💡 AI 解读

"激活值异常值"是 LLM 量化的核心难题,也是 GPTQ、AWQ、SmoothQuant 等技术存在的根本原因。反直觉的是:模型越大,异常值问题越严重(6.7B 以上的 OPT 才出现)。少数维度的大异常值会主导量化误差,让大部分正常值被"连累"丢失精度。解决思路分两派:混合精度(异常维度保留 FP16,如 LLM.int8())和平滑迁移(把激活的难度转移到权重,如 SmoothQuant)。理解这一点就能看懂为何现代量化方案几乎都在围绕"如何处理那几个异常维度"做文章。

训练后量化(PTQ)

Mixed-precision: LLM.int8() (Dettmers et al. 2022) keeps outlier activation features (20× larger) in FP16 while quantizing the rest to INT8. Fine-grained granularity: Q-BERT uses group-wise (per-head) quantization; ZeroQuant uses group-wise weights + token-wise activation with fused kernels. Second-order: GPTQ treats the weight matrix as row vectors and iteratively quantizes greedily using Hessian matrices—reducing OPT-175B weights to 3-4 bits.

混合精度:LLM.int8()(Dettmers 等 2022)把异常激活特征(大 20 倍)保留在 FP16,其余量化到 INT8。细粒度:Q-BERT 用分组(按头)量化;ZeroQuant 用分组权重 + token 级激活并融合算子。二阶信息:GPTQ 把权重矩阵视为行向量,用 Hessian 矩阵贪心地迭代量化——能把 OPT-175B 权重压到 3-4 位。

Outlier smoothing: SmoothQuant (Xiao & Lin 2022) smartly smooths outlier features from activations to weights via a mathematically equivalent transformation, then enables W8A8 quantization on both. A hyperparameter α controls how much difficulty migrates from activations to weights; α=0.5 is a sweet spot. Because both resulting matrices become easy to quantize, SmoothQuant has better hardware efficiency than mixed-precision.

异常值平滑:SmoothQuant(Xiao & Lin 2022)通过数学等价变换把激活的异常值特征智能地平滑到权重,然后对两者都做 W8A8 量化。超参数 α 控制从激活迁移多少难度到权重;α=0.5 是甜点。由于变换后两个矩阵都易量化,SmoothQuant 比混合精度有更好的硬件效率。

量化感知训练(QAT)

QAT fuses quantization into pre-training or fine-tuning. One approach treats the full-precision model as teacher and the lower-precision model as student, optimized with distillation loss—Wikipedia or even random tokens can give decent gains. Layer-by-layer Knowledge Distillation (LKD) quantizes the network layer by layer, using the unquantized version as teacher.

QAT 把量化融入预训练或微调。一种方法把全精度模型当教师、低精度模型当学生,用蒸馏损失优化——维基百科甚至随机 token 都能带来不错的收益。逐层知识蒸馏(LKD)逐层量化网络,用未量化版本当教师。

剪枝

Network pruning reduces model size by trimming unimportant weights while capacity remains. Unstructured pruning drops any weight but doesn't work well with modern hardware. Structured pruning maintains dense matrix form with zeros. Magnitude pruning is simplest yet effective—weights with smallest absolute values are trimmed. Iterative pruning alternates prune/retrain multiple times. The Lottery Ticket Hypothesis motivates this: a dense network contains a sparse "winning ticket" subnetwork that can achieve optimal performance in isolation.

网络剪枝通过修剪不重要的权重来减小模型大小,同时保持容量。非结构化剪枝可丢弃任意权重,但与现代硬件配合不佳。结构化剪枝保持含零的稠密矩阵形式。幅度剪枝最简单却有效——剪掉绝对值最小的权重。迭代剪枝多次交替剪枝/重训练。彩票假说为此提供动机:稠密网络中存在一个稀疏的"中奖彩票"子网络,能独立达到最优性能。

For retraining, weight rewinding (reinitialize to earlier training values) outperforms fine-tuning, and learning rate rewinding matches or outperforms weight rewinding. Zhu & Gupta found large sparse models achieve better performance than small but dense counterparts.

关于重训练,权重回退(重置为早期训练的值)优于微调,学习率回退与权重回退相当或更优。Zhu & Gupta 发现大的稀疏模型比小而稠密的模型性能更好

💡 AI 解读

彩票假说是剪枝领域的里程碑思想:训练好的大网络里,早就藏着一个能独立工作的稀疏小网络——我们只是训练了多余的权重。这暗示过参数化其实是"搜索"的过程,而非最终需要。实践要点:①幅度剪枝这种最简单的方法常常胜过复杂方法;②大稀疏优于小稠密,所以"剪大模型"比"训练小模型"划算;③重训练时学习率回退是性价比最高的策略。不过剪枝在现代 LLM 上的工程价值不如量化——因为稀疏矩阵在 GPU 上的加速依赖 N:M 等特定模式。

稀疏化

N:M sparsity (N out of every M consecutive elements are zeros) works well with modern GPUs—Nvidia A100 supports 2:4 sparsity. Permuting columns can provide more options to maintain large-magnitude parameters. SR-STE (Sparse-refined STE) extends straight-through estimation to prevent large mask changes. Top-KAST preserves constant sparsity in both forward and backward passes without dense forward passes. The Scaling Transformer sparsifies both self-attention and FFN, achieving 37× speedup for single-example inference; combined with LSH attention and FFN recurrence it becomes Terraformer.

N:M 稀疏(每 M 个连续元素中 N 个为零)与现代 GPU 配合良好——Nvidia A100 支持 2:4 稀疏。列置换能提供更多选择以保留大幅值参数。SR-STE 扩展直通估计以防止掩码剧变。Top-KAST 在前向和反向都保持恒定稀疏度而无需稠密前向。Scaling Transformer 同时稀疏化自注意力和 FFN,单样本推理加速 37 倍;结合 LSH 注意力和 FFN 循环后成为 Terraformer。

混合专家模型(MoE)

With MoE architecture, only partial parameters are utilized at decoding time, saving inference cost. Expert capacity is adjusted by a capacity factor C. V-MoE adds MoE into Vision Transformer, matching SoTA with half the inference compute. Its Batch Priority Routing (BPR) assigns experts to high-priority-score tokens first, working much better than vanilla routing when capacity is low. Task MoE routes at the task level (not token level)—since routing is static for a fixed task, the server only needs to preload k experts, achieving 2.6× higher peak throughput.

MoE 架构在解码时只利用部分参数,节省推理成本。专家容量由容量因子 C 调整。V-MoE 把 MoE 加入 Vision Transformer,用一半推理算力匹配 SoTA。其批优先路由(BPR)先把专家分配给高优先级 token,在低容量时远胜普通路由。Task MoE 在任务级(而非 token 级)路由——由于固定任务的路由是静态的,服务器只需预加载 k 个专家,峰值吞吐量高 2.6 倍。

💡 AI 解读

MoE 是当代大模型扩展的核心范式(Mixtral、DeepSeek、GPT-4 据传都用 MoE)。核心 trade-off:用更多的总参数换取每个 token 更少的激活参数——容量上去了,推理算力却基本不变。但这带来新的工程难题:①负载均衡(不能让少数专家过载),②路由开销,③显存仍要装下所有专家。Task MoE 的洞察很实用:静态路由(任务级)比动态路由(token 级)部署友好得多。如今 DeepSeek 的共享专家 + 路由专家设计,正是对这些问题的演进式回答。可以说,谁掌握了高效 MoE,谁就掌握了低成本大模型。

架构优化:高效 Transformer 全景

Since self-attention has quadratic time/memory complexity, all efficient transformer models apply some form of sparsity to the dense attention layer (Tay et al. 2020 survey). Categories:

由于自注意力是二次时间/内存复杂度,所有高效 Transformer 模型都对稠密注意力层施加某种形式的稀疏(Tay 等 2020 综述)。分类:

💡 AI 解读

这张"高效注意力全家福"是理解长上下文技术演进的地图。核心矛盾:注意力是二次的,但长序列必须线性或近线性。三条主要路线:稀疏化(只算一部分注意力,如 Longformer/BigBird 的局部+全局)、低秩近似(Linformer 把序列维度投影到 k)、核方法(Performer 用随机特征把 softmax 变成可分解形式)。历史教训:很多花哨方法在理论复杂度上漂亮,但被 FlashAttention 这种"稠密但极致工程优化"的方案打败了。如今主流长上下文靠的是稠密注意力 + 工程优化(分块、重计算)而非花哨的稀疏模式——这是一堂关于"工程 > 算法"的生动课。