Shortcuts

torch.quantize_per_tensor

torch.quantize_per_tensor(input, scale, zero_point, dtype) Tensor

Converts a float tensor to a quantized tensor with given scale and zero point.

Parameters:
  • input (Tensor) – float tensor or list of tensors to quantize

  • scale (float or Tensor) – scale to apply in quantization formula

  • zero_point (int or Tensor) – offset in integer value that maps to float zero

  • dtype (torch.dtype) – the desired data type of returned tensor. Has to be one of the quantized dtypes: torch.quint8, torch.qint8, torch.qint32

Returns:

A newly quantized tensor or list of quantized tensors.

Return type:

Tensor

Example:

>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=torch.quint8,
       quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()
tensor([ 0, 10, 20, 30], dtype=torch.uint8)
>>> torch.quantize_per_tensor([torch.tensor([-1.0, 0.0]), torch.tensor([-2.0, 2.0])],
>>> torch.tensor([0.1, 0.2]), torch.tensor([10, 20]), torch.quint8)
(tensor([-1.,  0.], size=(2,), dtype=torch.quint8,
    quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10),
    tensor([-2.,  2.], size=(2,), dtype=torch.quint8,
    quantization_scheme=torch.per_tensor_affine, scale=0.2, zero_point=20))
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), torch.tensor(0.1), torch.tensor(10), torch.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=torch.quint8,
   quantization_scheme=torch.per_tensor_affine, scale=0.10, zero_point=10)

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