# 量化感知训练

**量化感知训练**（QAT）是一种在训练循环内部模拟低精度量化效应来训练或微调神经网络的技术，使模型学到的权重在其权重和激活值随后被转换为低比特整数形式用于部署时仍能保持准确。它是[神经网络量化](https://www.edgepedia.org/zh/neural-network-quantization)的两大主要范式之一，另一种是训练后量化（PTQ），后者对已训练好的浮点模型直接进行转换，无需任何重新训练。<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11965593/)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 训练过程中通过伪量化权重和激活值来模拟量化，而参数更新使用更高精度的算术运算，使模型学会在低精度推理下保持鲁棒<sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC13568402/)</sup> |
| 与PTQ的对比 | PTQ不经过重新训练直接施加量化，使用小型校准集计算截断范围和缩放因子；QAT则需要用训练数据进行重新训练或微调<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11965593/)</sup><sup> • </sup><sup>[3](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2022/EECS-2022-231.pdf)</sup> |
| 精度 | 与PTQ相比，QAT在相同量化精度下通常能获得更优的模型性能<sup>[4](https://www.mdpi.com/2079-9292/14/7/1345)</sup> |
| 部署收益 | 在TensorFlow API默认设置下，模型大小缩小至原来的四分之一，测试的CPU后端显示1.5至4倍的延迟改善<sup>[5](https://www.tensorflow.org/model_optimization/guide/quantization/training)</sup> |
| 奠基性论文 | 雅各布等，《面向高效纯整数算术推理的神经网络量化与训练》，CVPR 2018，第2704-2713页<sup>[6](https://openaccess.thecvf.com/content_cvpr_2018/html/Jacob_Quantization_and_Training_CVPR_2018_paper.html)</sup> |
| 主要代价 | 基于反向传播的QAT对大型语言模型产生难以承受的内存开销，限制了其在十亿参数级部署中的实用性<sup>[7](https://arxiv.org/abs/2509.00031)</sup><sup> • </sup><sup>[8](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab6eba9a853087993addff937c8cec87-Paper-Conference.pdf)</sup> |

## 工作原理

在QAT中，伪量化操作被插入网络的前向传播中。权重和激活值在用于每一层的计算之前被舍入到目标精度（例如8位整数），但参数本身在训练期间以更高精度存储和更新。<sup>[9](https://nodique.com/comparisons/ptq-vs-qat)</sup><sup> • </sup><sup>[2](https://pmc.ncbi.nlm.nih.gov/articles/PMC13568402/)</sup>因此，训练损失反映了模型在真正量化后所具有的精度，梯度下降调整权重以补偿舍入误差——而此时量化尚未在模拟之外被实际应用。<sup>[9](https://nodique.com/comparisons/ptq-vs-qat)</sup>

舍入操作是不连续的，这构成了核心的优化难题：阶跃函数对反向传播没有可用的梯度。标准的解决方案是直通估计器（STE），它在前向传播中执行量化感知的计算，但在反向传播中将舍入视为恒等函数来计算梯度。这种不匹配会导致误差失控和不稳定，在超低精度情形下尤为突出。<sup>[10](https://proceedings.iclr.cc/paper_files/paper/2026/hash/eee6efe709623f36483e3fbb0bb513dd-Abstract-Conference.html)</sup>在使用直通估计训练的向量量化网络中，研究者将不稳定性追溯到模型嵌入与码向量分布之间的差异，包括码本梯度稀疏性和承诺损失的非对称性。<sup>[11](https://proceedings.mlr.press/v202/huh23a.html)</sup>

## 雅各布等人的纯整数推理方案

最具影响力的QAT设计是本努瓦·雅各布、斯克尔曼塔斯·克利吉斯、陈波、朱梦龙、马修·唐、安德鲁·霍华德、哈特维希·亚当和德米特里·卡列尼琴科（当时均在谷歌）于2017年12月在arXiv上发表、并在CVPR 2018上展示的方案。<sup>[6](https://openaccess.thecvf.com/content_cvpr_2018/html/Jacob_Quantization_and_Training_CVPR_2018_paper.html)</sup><sup> • </sup><sup>[12](https://arxiv.org/abs/1712.05877)</sup>它将量化方案与协同设计的训练流程配对，使得推理可以完全以整数算术执行，同时保持端到端模型精度接近浮点推理。<sup>[6](https://openaccess.thecvf.com/content_cvpr_2018/html/Jacob_Quantization_and_Training_CVPR_2018_paper.html)</sup>

该方案将激活值和权重都量化为8位整数，相比32位浮点表示，内存占用可降至接近原来的四分之一。纯整数推理在典型的ARM CPU上表现优于浮点算术，并可在仅支持整数算术的硬件上运行，例如高通Hexagon等移动加速器。在MobileNet这一已以运行效率著称的模型家族上，该方法在主流ARM CPU上改善了ImageNet分类和COCO检测的延迟-精度权衡。<sup>[6](https://openaccess.thecvf.com/content_cvpr_2018/html/Jacob_Quantization_and_Training_CVPR_2018_paper.html)</sup>推理时该方案使用纯整数算术，权重和输入为uint8，偏置和累加器为uint32，输出为uint8；训练时使用浮点算术，在权重之前和激活值之后插入模拟量化节点，两种实现之间保持高度对应。<sup>[13](https://openaccess.thecvf.com/content_cvpr_2018/papers/Jacob_Quantization_and_Training_CVPR_2018_paper.pdf)</sup>

## QAT与训练后量化

两种范式的区别在于量化进入流水线的时机以及各自所需的数据。PTQ取一个已完成的浮点模型，事后对其权重进行舍入，是一次性转换，无需重新训练，通常使用大约100到500个样本的小型无标签校准集来计算量化参数。QAT将量化整合到训练或微调循环中，需要原始训练数据集或其代表性标注子集。<sup>[9](https://nodique.com/comparisons/ptq-vs-qat)</sup><sup> • </sup><sup>[14](https://inferensys.com/glossary/inference-optimization-and-latency-reduction/mixed-precision-inference/quantization-aware-fine-tuning)</sup>一份伯克利技术报告将两种流程并列描述：QAT依次运行预训练模型、量化、然后用训练数据进行重新训练或微调；而PTQ在小数据子集上运行校准以计算截断范围和缩放因子，然后再进行量化。<sup>[3](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2022/EECS-2022-231.pdf)</sup>

QAT的精度优势随着精度降低而增大。PTQ在8位和实现良好的4位权重量化下精度损失较小，而低于4位时退化不再温和——恰恰是在这一范围内QAT的优势最大，因为权重在训练中已适应了激进的舍入。<sup>[9](https://nodique.com/comparisons/ptq-vs-qat)</sup>

代价则是成本。

## 实测结果

[TensorFlow](https://www.edgepedia.org/zh/tensorflow)的模型优化包通过在训练期间模拟推理时量化来实现QAT，然后将模型交给下游工具生成真正量化的模型。在ImageNet上，其报告的8位结果接近浮点基线：MobilenetV1 224的top-1准确率从71.03%变为71.06%，ResNet v1 50从76.3%变为76.1%，MobilenetV2 224从70.77%变为70.01%。<sup>[5](https://www.tensorflow.org/model_optimization/guide/quantization/training)</sup>TensorFlow文档指出，其工具基于雅各布等人论文中的概念构建，并增加了卷积层的逐轴量化等扩展。<sup>[5](https://www.tensorflow.org/model_optimization/guide/quantization/training)</sup>

部署工具链可直接消费QAT输出。[ONNX Runtime](https://www.edgepedia.org/zh/onnx-runtime)可以在其QDQ（量化/反量化）张量格式中直接运行从TensorFlow转换或从PyTorch导出的QAT模型，无需进一步量化，因为这些模型已经携带了量化参数。<sup>[16](https://onnxruntime.ai/docs/performance/model-optimizations/quantization.html)</sup>

## 降低大模型QAT的成本

由于通过大型模型的反向传播主导了QAT的内存开销，近期工作直接针对这一瓶颈展开。2025年发表的ZeroQAT是一个基于零阶优化的QAT框架，同时支持权重和激活量化，使用仅前向的梯度估计来消除反向传播，在保持端到端优化的同时大幅降低计算和内存开销。其实验表明，它以显著更少的内存超越了代表性的PTQ和QAT基线：在单块8GB GPU上以2至4比特微调13B模型，在一加12手机上微调6.7B模型。<sup>[7](https://arxiv.org/abs/2509.00031)</sup>

## References

1. [A survey of model compression techniques: past, present, and future, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC11965593/)
2. [Fully Quantized Training vs. Post-Training Quantization for a Small Hyperspectral Transformer Model, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC13568402/)
3. [Introduction and Related Work of Quantization, EECS-2022-231, UC Berkeley](https://www2.eecs.berkeley.edu/Pubs/TechRpts/2022/EECS-2022-231.pdf)
4. [Optimization Methods, Challenges, and Opportunities for Edge Inference: A Comprehensive Survey, MDPI Electronics](https://www.mdpi.com/2079-9292/14/7/1345)
5. [Quantization aware training, TensorFlow Model Optimization](https://www.tensorflow.org/model_optimization/guide/quantization/training)
6. [Jacob et al., Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference, CVPR 2018](https://openaccess.thecvf.com/content_cvpr_2018/html/Jacob_Quantization_and_Training_CVPR_2018_paper.html)
7. [Tan et al., End-to-End On-Device Quantization-Aware Training for LLMs at Inference Cost, arXiv:2509.00031](https://arxiv.org/abs/2509.00031)
8. [Towards Next-Level Post-Training Quantization, NeurIPS 2024](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab6eba9a853087993addff937c8cec87-Paper-Conference.pdf)
9. [Post-Training Quantization vs Quantization-Aware Training, Nodique](https://nodique.com/comparisons/ptq-vs-qat)
10. [Robust Training of Neural Networks at Arbitrary Precision and Sparsity, ICLR](https://proceedings.iclr.cc/paper_files/paper/2026/hash/eee6efe709623f36483e3fbb0bb513dd-Abstract-Conference.html)
11. [Huh et al., Straightening Out the Straight-Through Estimator, ICML 2023, PMLR 202:14096-14113](https://proceedings.mlr.press/v202/huh23a.html)
12. [Jacob et al., arXiv:1712.05877](https://arxiv.org/abs/1712.05877)
13. [Jacob et al., CVPR 2018 paper PDF](https://openaccess.thecvf.com/content_cvpr_2018/papers/Jacob_Quantization_and_Training_CVPR_2018_paper.pdf)
14. [Quantization-Aware Fine-Tuning (QAFT) Explained, Inference Systems](https://inferensys.com/glossary/inference-optimization-and-latency-reduction/mixed-precision-inference/quantization-aware-fine-tuning)
15. [Post-training Quantization or Quantization-aware Training? That is the Question, IEEE](https://ieeexplore.ieee.org/document/10219214/)
16. [Quantize ONNX models, ONNX Runtime documentation](https://onnxruntime.ai/docs/performance/model-optimizations/quantization.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Neural network architectures › Binary and quantized networks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/quantization-aware-training

English original: https://www.edgepedia.org/quantization-aware-training
