-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_fake_quantize.py
175 lines (149 loc) · 6.38 KB
/
test_fake_quantize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import platform
import numpy as np
import pytest
import torch
from pytorch_layer_test_class import PytorchLayerTest
class TestFakeQuantizePerTensorAffine(PytorchLayerTest):
rng = np.random.default_rng(seed=123)
def _prepare_input(self):
return (self.rng.standard_normal([3, 2, 2], dtype=np.float32),)
def create_model(self, scale, zero_point, quant_min, quant_max):
class fake_quantize_per_tensor_affine(torch.nn.Module):
def __init__(self, scale, zero_point, quant_min, quant_max):
super(fake_quantize_per_tensor_affine, self).__init__()
self.scale = scale
self.zero_point = zero_point
self.quant_min = quant_min
self.quant_max = quant_max
def forward(self, x):
return torch.fake_quantize_per_tensor_affine(
x, self.scale, self.zero_point, self.quant_min, self.quant_max
)
ref_net = None
return (
fake_quantize_per_tensor_affine(scale, zero_point, quant_min, quant_max),
ref_net,
"aten::fake_quantize_per_tensor_affine",
)
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize(
"scale, zero_point, quant_min, quant_max",
[
(1.0, 1, 0, 255),
(0.01, 0, 0, 255),
(-0.01, 0, 0, 255),
(0.5, 0, -128, 127),
(0.5, -1, -128, 127),
(1.0, 0, 0, 127),
],
)
@pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',
reason='Ticket - 122715')
def test_fake_quantize_per_tensor_affine(
self, ie_device, precision, ir_version, scale, zero_point, quant_min, quant_max
):
self._test(
*self.create_model(scale, zero_point, quant_min, quant_max),
ie_device,
precision,
ir_version,
freeze_model=False
)
class TestFakeQuantizePerTensorAffineCacheMaskTensorQParams(PytorchLayerTest):
rng = np.random.default_rng(seed=123)
def _prepare_input(self):
return (self.rng.standard_normal([3, 2, 2], dtype=np.float32),)
def create_model(self, scale, zero_point, quant_min, quant_max):
class _fake_quantize_per_tensor_affine_cachemask_tensor_qparams(torch.nn.Module):
def __init__(self, scale, zero_point, quant_min, quant_max):
super(_fake_quantize_per_tensor_affine_cachemask_tensor_qparams, self).__init__()
self.scale = torch.tensor(scale)
self.zero_point = torch.tensor(zero_point)
self.fake_quant_enabled = torch.tensor(1)
self.quant_min = quant_min
self.quant_max = quant_max
def forward(self, x):
return torch._fake_quantize_per_tensor_affine_cachemask_tensor_qparams(
x, self.scale, self.zero_point, self.fake_quant_enabled, self.quant_min, self.quant_max
)
ref_net = None
return (
_fake_quantize_per_tensor_affine_cachemask_tensor_qparams(scale, zero_point, quant_min, quant_max),
ref_net,
"aten::_fake_quantize_per_tensor_affine_cachemask_tensor_qparams",
)
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize(
"scale, zero_point, quant_min, quant_max",
[
(1.0, 1, 0, 255),
(0.01, 0, 0, 255),
(-0.01, 0, 0, 255),
(0.5, 0, -128, 127),
(0.5, -1, -128, 127),
(1.0, 0, 0, 127),
],
)
@pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',
reason='Ticket - 122715')
def test__fake_quantize_per_tensor_affine_cachemask_tensor_qparams(
self, ie_device, precision, ir_version, scale, zero_point, quant_min, quant_max
):
self._test(
*self.create_model(scale, zero_point, quant_min, quant_max),
ie_device,
precision,
ir_version,
freeze_model=False
)
class TestFakeQuantizePerChannelAffine(PytorchLayerTest):
rng = np.random.default_rng(seed=123)
def _prepare_input(self):
return (self.rng.standard_normal([3, 2, 2], dtype=np.float32),)
def create_model(self, scale, zero_point, axis, quant_min, quant_max):
class fake_quantize_per_channel_affine(torch.nn.Module):
def __init__(self, scale, zero_point, axis, quant_min, quant_max):
super(fake_quantize_per_channel_affine, self).__init__()
self.scale = scale
self.zero_point = zero_point
self.axis = axis
self.quant_min = quant_min
self.quant_max = quant_max
def forward(self, x):
return torch.fake_quantize_per_channel_affine(
x, self.scale, self.zero_point, self.axis, self.quant_min, self.quant_max
)
ref_net = None
return (
fake_quantize_per_channel_affine(scale, zero_point, axis, quant_min, quant_max),
ref_net,
"aten::fake_quantize_per_channel_affine",
)
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize(
"scale, zero_point, axis, quant_min, quant_max",
[
(torch.tensor([0.005, 0.7]), torch.zeros(2), 1, 0, 255),
(torch.tensor([1.5, -0.7, -0.1]), torch.tensor([1, 0, -1], dtype=torch.int32), 0, -128, 127),
(torch.tensor([-0.005, 0.7]), torch.tensor([0, 1], dtype=torch.int32), 1, 0, 127),
(torch.tensor([-0.005, -0.7, 0.1]), torch.tensor([1, 0, 1], dtype=torch.int32), 0, 0, 255),
],
)
@pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',
reason='Ticket - 122715')
def test_fake_quantize_per_channel_affine(
self, ie_device, precision, ir_version, scale, zero_point, axis, quant_min, quant_max
):
self._test(
*self.create_model(scale, zero_point, axis, quant_min, quant_max),
ie_device,
precision,
ir_version,
freeze_model=False
)