PyTorch torch.quantized_batch_norm function
PyTorch torch reference manual
torch.quantized_batch_norm is a function in PyTorch used to perform batch normalization on quantized tensors. This function is very useful during inference with quantized models, allowing batch normalization while preserving the benefits of quantization.
Function definition
torch.quantized_batch_norm(input, weight, bias, mean, var, eps, output_scale, output_zero_point)
Parameter Description
input: Input quantized tensorweight: Scaling parameter for batch normalizationbias: Bias parameter for batch normalizationmean: Mean for batch normalizationvar: Variance for batch normalizationeps: Small constant to prevent division by zerooutput_scale: Quantization scale of the output tensoroutput_zero_point: Quantization zero point of the output tensor
Usage examples
Example
import
torch
Create a quantized input tensor
input
=
torch.
quantize_per_tensor
(
torch.
randn
(
1
,
3
,
4
,
4
)
,
scale
=
0.1
,
zero_point
=
0
,
dtype
=
torch.
quint8
)
Batch normalization parameters
weight
=
torch.
ones
(
3
)
bias
=
torch.
zeros
(
3
)
mean
=
torch.
ones
(
3
)
0.5
var
=
torch.
ones
(
3
)
0.2
Perform quantized batch normalization
output
=
torch.
quantized_batch_norm
(
input
,
weight
,
bias
,
mean
,
var
,
eps
=
1e-5
,
output_scale
=
0.1
,
output_zero_point
=
0
)
(
"Output shape:"
,
output.
shape
)
(
"Output type:"
,
output.
dtype
)
The output is:
输出形状: torch.Size([1, 3, 4, 4])
输出类型: torch.quint8