PyTorch torch.quantize_per_channel Function
Pytorch torch Reference Manual
torch.quantize_per_channelIt is a function in PyTorch used to create a quantized tensor with per-channel quantization.
Function Definition
torch.quantize_per_channel(input, scales, zero_points, axis, dtype)
Usage Example
Example
import torch
# Create input tensor (2D)
input = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
# Quantize per channel
# scales: scaling factor for each channel
# zero_points: zero point for each channel
# axis: specify which dimension to quantize along
scales = torch.tensor([0.1, 0.2])
zero_points = torch.tensor([10, 10])
axis = 0
x = torch.quantize_per_channel(input, scales, zero_points, axis, dtype=torch.quint8)
print("Quantized tensor:")
print(x)
print("Dequantized original values:")
print(x.dequantize())
# Create input tensor (2D)
input = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
# Quantize per channel
# scales: scaling factor for each channel
# zero_points: zero point for each channel
# axis: specify which dimension to quantize along
scales = torch.tensor([0.1, 0.2])
zero_points = torch.tensor([10, 10])
axis = 0
x = torch.quantize_per_channel(input, scales, zero_points, axis, dtype=torch.quint8)
print("Quantized tensor:")
print(x)
print("Dequantized original values:")
print(x.dequantize())
Other Extensions