• 文档 >
  • 量化 >
  • 量化 API 参考 >
  • MinMaxObserver
快捷键

MinMaxObserver

class torch.ao.quantization.observer.MinMaxObserver(dtype=torch.quint8, qscheme=torch.per_tensor_affine, reduce_range=False, quant_min=None, quant_max=None, factory_kwargs=None, eps=1.1920928955078125e-07, is_dynamic=False, **kwargs)[source][source]

基于运行中的最小值和最大值计算量化参数的观察器模块。

此观察者使用张量最小/最大统计信息来计算量化参数。该模块记录传入张量的运行最小值和最大值,并使用此统计信息来计算量化参数。

参数:
  • dtype – 量化节点需要的 dtype 参数,以实现参考模型规范。

  • qscheme – 要使用的量化方案

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

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

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

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

给定运行中的最小/最大值分别为 xminx_\text{min}xmaxx_\text{max} ,则缩放 ss 和零点 zz 的计算如下:

运行最小/最大值 xmin/maxx_\text{min/max} 的计算方法如下:

xmin={min(X)if xmin=Nonemin(xmin,min(X))otherwisexmax={max(X)if xmax=Nonemax(xmax,max(X))otherwise\begin{array}{ll} x_\text{min} &= \begin{cases} \min(X) & \text{if~}x_\text{min} = \text{None} \\ \min\left(x_\text{min}, \min(X)\right) & \text{otherwise} \end{cases}\\ x_\text{max} &= \begin{cases} \max(X) & \text{if~}x_\text{max} = \text{None} \\ \max\left(x_\text{max}, \max(X)\right) & \text{otherwise} \end{cases}\\ \end{array}

其中 XX 表示观测到的张量。

然后计算比例因子 ss 和零点 zz

if Symmetric:s=2max(xmin,xmax)/(QmaxQmin)z={0if dtype is qint8128otherwiseOtherwise:s=(xmaxxmin)/(QmaxQmin)z=Qminround(xmin/s)\begin{aligned} \text{if Symmetric:}&\\ &s = 2 \max(|x_\text{min}|, x_\text{max}) / \left( Q_\text{max} - Q_\text{min} \right) \\ &z = \begin{cases} 0 & \text{if dtype is qint8} \\ 128 & \text{otherwise} \end{cases}\\ \text{Otherwise:}&\\ &s = \left( x_\text{max} - x_\text{min} \right ) / \left( Q_\text{max} - Q_\text{min} \right ) \\ &z = Q_\text{min} - \text{round}(x_\text{min} / s) \end{aligned}

其中 QminQ_\text{min}QmaxQ_\text{max} 分别是量化数据类型的最大值和最小值。

警告

只能取 torch.qint8torch.quint8

注意

如果运行最小值等于运行最大值,则比例尺和零点设置为 1.0 和 0。

calculate_qparams()[source][source]

计算量化参数。

forward(x_orig)[source][source]

记录运行中的最小值和最大值 x

reset_min_max_vals()[source][source]

重置最小/最大值。


© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,主题由 Read the Docs 提供。

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源