用于根据运行时的最小值和最大值计算量化参数的观察者模块。
此观察者使用张量的最小值/最大值统计数据来计算量化参数。该模块记录传入张量的运行时的最小值和最大值,并使用此统计数据来计算量化参数。
- 参数
dtype – dtype argument to the quantize node needed to implement the reference model spec.
qscheme – Quantization scheme to be used
reduce_range – Reduces the range of the quantized data type by 1 bit
quant_min – Minimum quantization value. If unspecified, it will follow the 8-bit setup.
quant_max – Maximum quantization value. If unspecified, it will follow the 8-bit setup.
eps (Tensor) – Epsilon value for float32, Defaults to 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。
注意
如果运行时的最小值等于运行时的最大值,则缩放因子和零点分别设置为 1.0 和 0。
-
calculate_qparams()[source]
计算量化参数。
-
forward(x_orig)[source]
记录 x 的运行时的最小值和最大值。
-
reset_min_max_vals()[source]
重置最小值/最大值。