评价此页

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)[source]#

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

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

参数
  • averaging_constant – min/max 的平均常数。

  • dtypequantize 节点所需的 dtype 参数,用于实现参考模型规范。

  • qscheme – 要使用的量化方案

  • reduce_range – 将量化数据类型的范围减少 1 位

  • quant_min – 最小量化值。如果未指定,则遵循 8 位设置。

  • quant_max – 最大量化值。如果未指定,则遵循 8 位设置。

  • eps (Tensor) – float32 的 epsilon 值,默认为torch.finfo(torch.float32).eps

移动平均 min/max 计算如下:

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。