评价此页

MovingAverageMinMaxObserver#

class torch.ao.quantization.observer.MovingAverageMinMaxObserver(averaging_constant=0.01, dtype=torch.quint8, qscheme=torch.per_tensor_affine, reduce_range=False, quant_min=None, quant_max=None, eps=1.1920928955078125e-07, is_dynamic=False, **kwargs)[源代码]#

用于根据最小和最大值的移动平均值计算量化参数的观察者模块。

此观察者根据传入张量的最小值的移动平均值和最大值的移动平均值来计算量化参数。该模块记录传入张量的平均最小值和最大值,并使用此统计信息来计算量化参数。

参数
  • averaging_constant – 最小/最大值的平均常数。

  • 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={min(X)if xmin=None(1c)xmin+cmin(X)otherwisexmax={max(X)if xmax=None(1c)xmax+cmax(X)otherwise\begin{array}{ll} x_\text{min} = \begin{cases} \min(X) & \text{if~}x_\text{min} = \text{None} \\ (1 - c) x_\text{min} + c \min(X) & \text{otherwise} \end{cases}\\ x_\text{max} = \begin{cases} \max(X) & \text{if~}x_\text{max} = \text{None} \\ (1 - c) x_\text{max} + c \max(X) & \text{otherwise} \end{cases}\\ \end{array}

其中 xmin/maxx_\text{min/max} 是运行平均最小值/最大值,XX 是传入张量,ccaveraging_constant

然后,缩放因子 (scale) 和零点 (zero point) 的计算方法与 MinMaxObserver 相同。

注意

仅适用于 torch.per_tensor_affine 量化方案。

注意

如果运行最小值等于运行最大值,则缩放因子和零点将分别设置为 1.0 和 0。