MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_qat_conv

Function test_qat_conv

imperative/python/test/unit/module/test_qat.py:143–188  ·  view source on GitHub ↗
(padding, padding_mode)

Source from the content-addressed store, hash-verified

141 ],
142)
143def test_qat_conv(padding, padding_mode):
144
145 in_channels = 32
146 out_channels = 64
147 kernel_size = 3
148
149 class TestNet(Module):
150 def __init__(self, groups, bias):
151 super().__init__()
152 self.quant = QuantStub()
153 self.dequant = DequantStub()
154 self.conv = Conv2d(
155 in_channels,
156 out_channels,
157 kernel_size,
158 groups=groups,
159 bias=bias,
160 padding=padding,
161 padding_mode=padding_mode,
162 )
163 self.conv_relu = ConvRelu2d(
164 out_channels, in_channels, kernel_size, groups=groups, bias=bias
165 )
166
167 def forward(self, inp):
168 out = self.quant(inp)
169 out = self.conv(out)
170 out = self.conv_relu(out)
171 out = self.dequant(out)
172 return out
173
174 inputs = tensor(np.random.randn(4, in_channels, 32, 32).astype(np.float32))
175 for groups, bias in product([1, 4], [True, False]):
176 net = TestNet(groups, bias)
177 net.train()
178 qat_net = quantize_qat(net, inplace=False)
179 disable_fake_quant(qat_net)
180 normal_outputs = net(inputs)
181 qat_outputs = qat_net(inputs)
182 np.testing.assert_allclose(normal_outputs.numpy(), qat_outputs.numpy())
183
184 net.eval()
185 normal_outputs = net(inputs)
186 qat_net.eval()
187 qat_outputs = qat_net(inputs)
188 np.testing.assert_allclose(normal_outputs.numpy(), qat_outputs.numpy())
189
190
191@pytest.mark.skipif(get_device_count("gpu") > 0, reason="no int8 algorithm on cuda")

Callers

nothing calls this directly

Calls 7

quantize_qatFunction · 0.90
disable_fake_quantFunction · 0.90
TestNetClass · 0.85
astypeMethod · 0.45
trainMethod · 0.45
numpyMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected