Inference and Serving, lesson 16
Layers that resist quantization
Outliers and sensitive layers, and how to find them.
You can quantize a row of weights by hand with a scale ("Quantizing weights").
Part 1. One big number ruins the scale
Here is a new row: 0.12, −0.08, 0.05, 0.21, −0.15, and one weight of 8.0. Five ordinary weights and one that's forty times bigger than the next largest.
Your turn
You quantize this row to INT4 (−7 to 7) with one symmetric scale. What do the five small weights come back as?
Example 1.1
The same row, with and without the outlier
- INT8, with 8.0scale 0.0630.126−0.0630.0630.189−0.1268.0
INT8 survives, barely. The small weights get one to three steps each, and 0.05 comes back as 0.063, 26% off.
- INT4, with 8.0scale 1.140.00.00.00.00.08.0
INT4 wipes them out. Only the outlier is left.
- INT4, without 8.0scale 0.030.12−0.090.060.21−0.15
Take the outlier away and the same 4 bits do fine. The bits were never the problem. The scale was.
def quantize(row, qmax):
scale = max(abs(x) for x in row) / qmax
return [round(round(x / scale) * scale, 3) for x in row]
row = [0.12, -0.08, 0.05, 0.21, -0.15, 8.0]
print(quantize(row, 127))
print(quantize(row, 7))
print(quantize(row[:5], 7))
# [0.126, -0.063, 0.063, 0.189, -0.126, 8.0]
# [0.0, 0.0, 0.0, 0.0, 0.0, 8.0]
# [0.12, -0.09, 0.06, 0.21, -0.15]Drag the outlier up and down and watch what happens to the error on every other weight.
Part 2. Outlier features
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Outlier features
- Not every layer is equal
Inference and Serving overviewSpotted a mistake? Tell us.