Rate this Page

MinMaxObserver#

classtorch.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]#

Observer module for computing the quantization parameters based on therunning min and max values.

This observer uses the tensor min/max statistics to compute the quantizationparameters. The module records the running minimum and maximum of incomingtensors, and uses this statistic to compute the quantization parameters.

Parameters
  • dtype – dtype argument to thequantize node needed to implement thereference 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 totorch.finfo(torch.float32).eps.

Given running min/max asxminx_\text{min} andxmaxx_\text{max},scaless and zero pointzz are computed as:

The running minimum/maximumxmin/maxx_\text{min/max} is computed as:

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}

whereXX is the observed tensor.

The scaless and zero pointzz are then computed as:

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}

whereQminQ_\text{min} andQmaxQ_\text{max} are the minimum andmaximum of the quantized data type.

Warning

dtype can only taketorch.qint8 ortorch.quint8.

Note

If the running minimum equals to the running maximum, the scaleand zero_point are set to 1.0 and 0.

calculate_qparams()[source]#

Calculates the quantization parameters.

forward(x_orig)[source]#

Records the running minimum and maximum ofx.

reset_min_max_vals()[source]#

Resets the min/max values.