Shortcuts

torch.quantize_per_channel

torch.quantize_per_channel(input, scales, zero_points, axis, dtype) → Tensor

Converts a float tensor to a per-channel quantized tensor with given scales and zero points.

Parameters
  • input (Tensor) – float tensor to quantize

  • scales (Tensor) – float 1D tensor of scales to use, size should match input.size(axis)

  • zero_points (int) – integer 1D tensor of offset to use, size should match input.size(axis)

  • axis (int) – dimension on which apply per-channel quantization

  • 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

Return type

Tensor

Example:

>>> x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
>>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)
tensor([[-1.,  0.],
        [ 1.,  2.]], size=(2, 2), dtype=torch.quint8,
       quantization_scheme=torch.per_channel_affine,
       scale=tensor([0.1000, 0.0100], dtype=torch.float64),
       zero_point=tensor([10,  0]), axis=0)
>>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8).int_repr()
tensor([[  0,  10],
        [100, 200]], dtype=torch.uint8)

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