3

I am trying to understand quantization in tensorflow and I am following this tutorial.

https://heartbeat.fritz.ai/8-bit-quantization-and-tensorflow-lite-speeding-up-mobile-inference-with-low-precision-a882dfcafbbd

In the tutorial it says that, the quantization equation is:

enter image description here

  • r is the real value (usually float32)
  • q is its quantized representation as a B-bit integer (uint8, uint32, etc.)
  • S (float32) and z (uint) are the factors by which we scale and shift the number line. z is the quantized ‘zero-point’ which will always map back exactly to 0.f.

I am struggling to understand the meaning of the zero point and was hoping somebody could explain it with an example?

black sheep
  • 369
  • 4
  • 13

1 Answers1

11

If your original data has negative values, then a zero point can offset the range, allowing you to store them in an unsigned integer. So if your zero point was 128, then unscaled negative values -127 to -1 could be represented by 1 to 127, and positive values 0 to 127 could be represented by 128 to 255. Note the quantized value of 0 (or input -128 here) is intentionally unused to keep range symmetry on both ends, hence a total range of 256-1 values instead of 256.

e.g. Given an input tensor with data ranging from -1000 to +1000 (inclusive both ends), and an element in it with value 39.215686275, that element's quantized value would be 133 when using 128 as the zero point:

quantizedValue = round(realValue / scale + zeroPoint)
quantizedValue = round(39.215686275 / 7.843137255 + 128) = 133
quantizedValue = round(0            / 7.843137255 + 128) = 128
quantizedValue = round(1000         / 7.843137255 + 128) = 255
quantizedValue = round(-1000        / 7.843137255 + 128) = 1

Where:

zeroPoint = 128  ## Note 256/2 is symmetric, and q=0 isn't really used.
                 ## Use same zero point for entire tensor.
scale = (realRangeMaxValue - realRangeMinValue) / quantizedRange
scale = (1000 - -1000) / 255 = 7.843137255
## Alternately could use inverse scale and multiply above, inverseScale = 0.1275

realRangeMinValue = -1000
realRangeMaxValue = 1000
quantizedRange = 2^8 - 1 = 255
quantizedRangeMinValue = integerRangeMinValue - zeroPoint = -128
quantizedRangeMaxValue = integerRangeMaxValue - zeroPoint = 127

Conversely:

realValue = (quantizedValue - zeroPoint) * scale
realValue = (133 - 128) * 7.843137255 = 39.215686275
realValue = (128 - 128) * 7.843137255 = 0
realValue = (255 - 128) * 7.843137255 = 1000
realValue = (1   - 128) * 7.843137255 = -1000
Błażej Czapp
  • 2,478
  • 2
  • 24
  • 18
Dwayne Robinson
  • 2,034
  • 1
  • 24
  • 39