Comments (2)
PPQ 使用 QuantizationState 控制量化逻辑,该字段是 TensorQuantizationConfig 中最重要的内容,我们目前支持 12 种量化状态:
- INITIAL = 1 # 量化参数刚刚被初始化,当前 config 不生效,数据不能被使用
- BAKED = 2 # 只针对参数量化,表示参数已经被静态量化,当前 config 不生效,数据可以直接使用
- OVERLAPPED = 3 # 只针对activation量化,表示数据流的量化由其他 config 管理,当前 config 不生效
- SLAVE = 4 # 只针对activation量化,表示当前数据流处于强制联合定点,当前 config 生效
- DEACTIVATED = 5 # 表示当前 config 不生效
- ACTIVATED = 6 # 表示当前 config 生效
- DEQUANTIZED = 7 # 表示当前 config 处于解量化状态,解量化是 PPQ 的一个系统操作
- SOI = 8 # 表示这一路输入与 Shape or index 相关,不量化
- PASSIVE = 9 # 表示这一路输入被动量化,如 bias, clip value 等
- PASSIVE_INIT = 10 # 表示这一路输入被动量化,并且刚刚初始化不能被使用
- PASSIVE_BAKED = 11 # 被动量化且静态量化,当前config不生效,数据可以直接使用
- FP32 = 12 # 表示这一路输入直接为FP32浮点数
其中最常见的量化状态是 INITIAL -> ACTIVATED -> BAKED,大部分量化都遵循这样的状态变迁。在一开始,Quantizer负责为所有 TensorQuantizationConfig 初始化状态,即全部初始化为 INITIAL。而后 Quantizer 负责将 Conv, Gemm 中的 bias 修改为 PASSIVE_INIT 状态,这表明需要 PPQ 对 bias 展开被动定点逻辑,只有bias的量化处于 PASSIVE_INIT 时它的量化参数才会被 input scale * weight scale覆盖,否则bias将执行主动定点逻辑,生成自己的量化参数。
当你修改了此处的状态初始化,如果你将其修改为 INITIAL,则 bias 执行主动定点,会通过 observer 给自己确定一个独立的scale 和 offset,你的推理框架可能会向你报告错误。如果你将其修改为 FP32,则会直接撤销 bias 的定点过程。修改为其他状态可能会造成执行错误。
在 PPQ 中量化是这样发生的:
- 由 Quantizer 负责初始化状态,写明位宽,截断值等基本信息。
- 由 QuantizeReducePass 负责关闭一些多余的量化信息,被关闭的量化信息状态为 OVERLAPPED,同时建立父子连接。
- 由 QuantizeRefinePass 负责修正量化错误,即强制要求 'Reshape', 'Slice', 'Gather', 'Resize', 'Split' 的部分输入不可量化,状态置为 SOI。
- 由 QuantizeRefinePass 负责修正量化错误,即强制要求 'Clip', 'Pad' 的部分输入状态为 PASSIVE_INIT。
- 由 RuntimeCalibrationPass 或者 ParameterQuantizePass 负责处理状态为INITIAL的量化信息,寻找合适的 scale 与 offset,并将量化状态置为ACTIVATED。
- 由 PassiveParameterQuantizePass 负责处理状态为 PASSIVE_INIT 的量化信息,并将这些它们的状态设置为 PASSIVE。
- 如果开启了PPQ的训练功能,则由 ppq/quantization/optim/training.py 中的一系列 pass 对 scale 和 offset 进行微调
- 如果开启了parameter_baking,则由 ParameterBakingPass 负责处理所有状态为 ACTIVATED 或 PASSIVE 的参数,对他们执行量化烘焙,直接将其数值替换成量化后的值,同时将状态转换为 BAKED 或者 PASSIVE_BAKED
from ppq.
原来是这样,感谢😃
from ppq.
Related Issues (20)
- LSQ算法微调后,输出的scale量化参数存在负值,请问如何修改源码,使其输出的scale为正值? HOT 1
- PPQ installtion error: `TypeError: Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.` HOT 4
- PPQ yolov5-7.0 :return torch.cat(concat_view, axis=axis) RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 28 but got size 27 for tensor number 1 in the list. HOT 1
- yolov8量化后无结果 HOT 3
- YOLOv6量化后仅能预测一类 HOT 3
- 能不能量化自己的模型啊?有教程吗 HOT 1
- 内存泄露 HOT 3
- yolov5 模型量化后的精度损失 HOT 2
- QLinearMatmul : weight zero point must be a scalar, 1D tensor of size 1, or last to second dimension is 1
- 量化时出现RandomNormalLike这个算子没有注册跟实现的问题,请问有没有简单的注册实现例子可以参考? HOT 1
- NotImplementedError: Graph op: /quantize/ArgMin(ArgMin) has no backend implementation HOT 1
- 请问model zoo的模型在哪里? HOT 1
- 调度部分算子至FP32时,生成engine失败 HOT 2
- Op Execution Error: PRelu_1 HOT 4
- 关于resize op的一个bug HOT 1
- 量化模型的时候出现TypeError HOT 1
- yolov5_sample报错 HOT 1
- 关于导出平台的报错信息 HOT 2
- 加减类算子类型匹配问题 HOT 3
- 请问 是否可以通过量化后产生的onnx 推理结果可以直观看出量化的情况? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ppq.