Skip to content

Instantly share code, notes, and snippets.

@vkuzo
Created December 23, 2020 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkuzo/2456e16d0f40366d830bcfe176fafe5c to your computer and use it in GitHub Desktop.
Save vkuzo/2456e16d0f40366d830bcfe176fafe5c to your computer and use it in GitHub Desktop.
# script
import torch
import torch.nn as nn
import torch.nn.quantized as nnq
m = nn.Sequential(nn.Conv2d(1, 1, 1))
m.qconfig = torch.quantization.get_default_qat_qconfig('qnnpack')
mp = torch.quantization.prepare_qat(m)
print('prepared model', mp)
mp(torch.randn(4, 1, 4, 4))
mq = torch.quantization.convert(mp)
print('quantized model', mq)
print('prepared parameters')
print(list(mp.named_parameters()))
print('quantized packed parameters')
for name, mod in mq.named_modules():
if isinstance(mod, nnq.Conv2d):
weight, bias = mod._weight_bias()
print(name, 'weight', weight, 'bias', bias)
# output
prepared model Sequential(
(0): Conv2d(
1, 1, kernel_size=(1, 1), stride=(1, 1)
(weight_fake_quant): FakeQuantize(
fake_quant_enabled=tensor([1], dtype=torch.uint8), observer_enabled=tensor([1], dtype=torch.uint8), quant_m
in=-128, quant_max=127, dtype=torch.qint8, qscheme=torch.per_tensor_symmetric, ch_axis=-1, scale=tensor([1.]), ze
ro_point=tensor([0])
(activation_post_process): MovingAverageMinMaxObserver(min_val=inf, max_val=-inf)
)
(activation_post_process): FakeQuantize(
fake_quant_enabled=tensor([1], dtype=torch.uint8), observer_enabled=tensor([1], dtype=torch.uint8), quant_m
in=0, quant_max=255, dtype=torch.quint8, qscheme=torch.per_tensor_affine, ch_axis=-1, scale=tensor([1.]), zero_po
int=tensor([0])
(activation_post_process): MovingAverageMinMaxObserver(min_val=inf, max_val=-inf)
)
)
)
quantized model Sequential(
(0): QuantizedConv2d(1, 1, kernel_size=(1, 1), stride=(1, 1), scale=0.006431101355701685, zero_point=109)
)
prepared parameters
[('0.weight', Parameter containing:
tensor([[[[0.4274]]]], requires_grad=True)), ('0.bias', Parameter containing:
tensor([0.1753], requires_grad=True))]
quantized packed parameters
0 weight tensor([[[[0.4257]]]], size=(1, 1, 1, 1), dtype=torch.qint8,
quantization_scheme=torch.per_tensor_affine, scale=0.0033521773293614388,
zero_point=0) bias tensor([0.1753], requires_grad=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment