TensorFlow Lite 8bit量化Spec
田海立@CSDN 2021-01-03
TensorFlow Lite量化spec随时间会微调,这里记录一下目前量化采用的数据类型、范围,以及对历史模型兼容等情况。
上面的TFLite int8量化归纳起来要点如下:
- 由量化值计算浮点值的公式:real_value = (q - zero_point) x scale;
- per-channel或per-tensor的权重用int8表示。范围[-127, 127],zeor-point为0;
- activation/input用int8表示。范围[-128, 127],zeor-point为0。
而历史上,非对称per-tensor量化是用uint8(范围[0, 256])表示的。
新的相关支撑工具以及kernel实现(包括TFLite里的reference与optimized kernel)都是基于上述的spc定义。
所以,
- 用新的工具转换模型到TFLite量化模型,如果指定input/output为uint8会看到会引入一个quantize算子的,用来re-quantize;
- 历史上,有些(而且是大量的)量化模型内部也是用uint8数据表示的。
参考
- TensorFlow Lite 8bit量化spec https://tensorflow.google.cn/lite/performance/quantization_spec
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference https://arxiv.org/pdf/1712.05877.pdf