用於根據執行中的最小值和最大值計算量化引數的觀察器模組。
此觀察器使用張量的最小值/最大值統計資訊來計算量化引數。該模組記錄輸入張量的執行中的最小值和最大值,並使用此統計資訊來計算量化引數。
- 引數
dtype – quantize 節點所需的 dtype 引數,用於實現參考模型規範。
qscheme – 要使用的量化方案
reduce_range – 將量化資料型別的範圍減少 1 位元
quant_min – 最小量化值。如果未指定,將遵循 8 位元設定。
quant_max – 最大量化值。如果未指定,將遵循 8 位元設定。
eps (張量) – float32 的 Epsilon 值,預設為 torch.finfo(torch.float32).eps。
給定執行中的最小/最大值為 xmin 和 xmax,標度 s 和零點 z 計算如下:
執行中的最小/最大值 xmin/max 計算如下:
xminxmax={min(X)min(xmin,min(X))if xmin=Noneotherwise={max(X)max(xmax,max(X))if xmax=Noneotherwise 其中 X 是觀察到的張量。
隨後,標度 s 和零點 z 計算如下:
if Symmetric:Otherwise:s=2max(∣xmin∣,xmax)/(Qmax−Qmin)z={0128if dtype is qint8otherwises=(xmax−xmin)/(Qmax−Qmin)z=Qmin−round(xmin/s) 其中 Qmin 和 Qmax 分別是量化資料型別的最小值和最大值。
警告
dtype 只能是 torch.qint8 或 torch.quint8。
注意
如果執行最小值等於執行最大值,則 scale 和 zero_point 會被設定為 1.0 和 0。
-
calculate_qparams()[source][source]
計算量化引數。
-
forward(x_orig)[source][source]
記錄 x 的執行最小值和最大值。
-
reset_min_max_vals()[source][source]
重置最小值/最大值。