# Quantization Operation coverage¶

Quantized Tensors support a limited subset of data manipulation methods of the regular full-precision tensor. For NN operators included in PyTorch, we restrict support to:

1. 8 bit weights (data_type = qint8)

2. 8 bit activations (data_type = quint8)

Note that operator implementations currently only support per channel quantization for weights of the conv and linear operators. Furthermore the minimum and the maximum of the input data is mapped linearly to the minimum and the maximum of the quantized data type such that zero is represented with no quantization error.

Additional data types and quantization schemes can be implemented through the custom operator mechanism.

Many operations for quantized tensors are available under the same API as full float version in torch or torch.nn. Quantized version of NN modules that perform re-quantization are available in torch.nn.quantized. Those operations explicitly take output quantization parameters (scale and zero_point) in the operation signature.

In addition, we also support fused versions corresponding to common fusion patterns that impact quantization at: torch.nn.intrinsic.quantized.

For quantization aware training, we support modules prepared for quantization aware training at torch.nn.qat and torch.nn.intrinsic.qat

The following operation list is sufficient to cover typical CNN and RNN models

## Quantized torch.Tensor operations¶

Operations that are available from the torch namespace or as methods on Tensor for quantized tensors:

## torch.nn.functional¶

Basic activations are supported.

## torch.nn.intrinsic¶

Fused modules are provided for common patterns in CNNs. Combining several operations together (like convolution and relu) allows for better quantization accuracy

## torch.nn.qat¶

Layers for the quantization-aware training

## torch.quantization¶

• Functions for eager mode quantization:

• Functions for graph mode quantization:

• quantize_jit() - Function for graph mode post training static quantization

• quantize_dynamic_jit() - Function for graph mode post training dynamic quantization

• Quantization configurations
• QConfig — Quantization configuration class

• default_qconfig — Same as QConfig(activation=default_observer, weight=default_weight_observer) (See QConfig)

• default_qat_qconfig — Same as QConfig(activation=default_fake_quant, weight=default_weight_fake_quant) (See QConfig)

• default_dynamic_qconfig — Same as QConfigDynamic(weight=default_weight_observer) (See QConfigDynamic)

• float16_dynamic_qconfig — Same as QConfigDynamic(weight=NoopObserver.with_args(dtype=torch.float16)) (See QConfigDynamic)

• Stubs
• Observers for computing the quantization parameters

• Default Observers. The rest of observers are available from torch.quantization.observer:

• default_observer — Same as MinMaxObserver.with_args(reduce_range=True)

• default_weight_observer — Same as MinMaxObserver.with_args(dtype=torch.qint8, qscheme=torch.per_tensor_symmetric)

• Observer — Abstract base class for observers

• MinMaxObserver — Derives the quantization parameters from the running minimum and maximum of the observed tensor inputs (per tensor variant)

• MovingAverageMinMaxObserver — Derives the quantization parameters from the running averages of the minimums and maximums of the observed tensor inputs (per tensor variant)

• PerChannelMinMaxObserver — Derives the quantization parameters from the running minimum and maximum of the observed tensor inputs (per channel variant)

• MovingAveragePerChannelMinMaxObserver — Derives the quantization parameters from the running averages of the minimums and maximums of the observed tensor inputs (per channel variant)

• HistogramObserver — Derives the quantization parameters by creating a histogram of running minimums and maximums.

• Observers that do not compute the quantization parameters:
• FakeQuantize module

## torch.nn.quantized¶

Quantized version of standard NN layers.

## torch.nn.quantized.dynamic¶

Layers used in dynamically quantized models (i.e. quantized only on weights)

## torch.nn.quantized.functional¶

Functional versions of quantized NN layers (many of them accept explicit quantization output parameters)

## Quantized dtypes and quantization schemes¶

• torch.qscheme — Type to describe the quantization scheme of a tensor. Supported types:

• torch.per_tensor_affine — per tensor, asymmetric

• torch.per_channel_affine — per channel, asymmetric

• torch.per_tensor_symmetric — per tensor, symmetric

• torch.per_channel_symmetric — per tensor, symmetric

• torch.dtype — Type to describe the data. Supported types:

• torch.quint8 — 8-bit unsigned integer

• torch.qint8 — 8-bit signed integer

• torch.qint32 — 32-bit signed integer