Table of Contents
Shortcuts

# 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)

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

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

  Parameters:

    - 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.

  Given running min/max as x min x_\text{min}x min​ and x max x_\text{max}x max​, scale s s s and zero point z z z are computed as:

  The running minimum/maximum x min/max x_\text{min/max}x min/max​ is computed as:

  x min={min⁡(X)if x min=None min⁡(x min,min⁡(X))otherwise x max={max⁡(X)if x max=None max⁡(x max,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}x min​x max​​={min(X)min(x min​,min(X))​if x min​=None otherwise​={max(X)max(x max​,max(X))​if x max​=None otherwise​​

  where X X X is the observed tensor.

  The scale s s s and zero point z z z are then computed as:

  if Symmetric:s=2 max⁡(∣x min∣,x max)/(Q max−Q min)z={0 if dtype is qint8 128 otherwise Otherwise:s=(x max−x min)/(Q max−Q min)z=Q min−round(x min/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}if Symmetric:Otherwise:​s=2 max(∣x min​∣,x max​)/(Q max​−Q min​)z={0 128​if dtype is qint8 otherwise​s=(x max​−x min​)/(Q max​−Q min​)z=Q min​−round(x min​/s)​

  where Q min Q_\text{min}Q min​ and Q max Q_\text{max}Q max​ are the minimum and maximum of the quantized data type.

  Warning

  `dtype` can only take `torch.qint8` or `torch.quint8`.

  Note

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

  calculate_qparams()

    Calculates the quantization parameters.

  forward(x_orig)

    Records the running minimum and maximum of `x`.

  reset_min_max_vals()

    Resets the min/max values.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.