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

Function test_conv

imperative/python/test/unit/quantization/test_module.py:232–282  ·  view source on GitHub ↗
(module, padding_mode)

Source from the content-addressed store, hash-verified

230@pytest.mark.parametrize("module", ["Conv2d", "ConvBn2d", "ConvBnRelu2d"])
231@pytest.mark.parametrize("padding_mode", ["zeros", "reflect", "replicate"])
232def test_conv(module, padding_mode):
233 normal_net = getattr(Float, module)(
234 3, 3, 3, 1, 1, 1, bias=True, padding_mode=padding_mode
235 )
236 normal_net.eval()
237
238 qat_net = getattr(QAT, module)(
239 3, 3, 3, 1, 1, 1, bias=True, padding_mode=padding_mode
240 )
241 qat_net.eval()
242 disable_observer(qat_net)
243
244 propagate_qconfig(qat_net, min_max_fakequant_qconfig)
245 init_qat_net(qat_net)
246
247 x = mge.tensor(np.random.normal(size=(1, 3, 3, 3)).astype("float32"))
248 inp_scale = gen_inp_scale()
249 x = fake_quant_act(x, inp_scale)
250 x.qparams.update(create_qparams(QuantMode.SYMMERTIC, "qint8", inp_scale))
251
252 x_int8 = quant(x, inp_scale)
253
254 weight = np.random.normal(size=(3, 3, 3, 3)).astype("float32")
255 bias = np.random.normal(size=(1, 3, 1, 1)).astype("float32")
256 if module in ("ConvBn2d", "ConvBnRelu2d"):
257 normal_net.conv.weight[...] = fake_quant_weight(weight, weight_scale)
258 normal_net.conv.bias[...] = fake_quant_bias(bias, inp_scale * weight_scale)
259 qat_net.conv.weight[...] = Parameter(weight)
260 qat_net.conv.bias[...] = Parameter(bias)
261 else:
262 normal_net.weight[...] = fake_quant_weight(weight, weight_scale)
263 normal_net.bias[...] = fake_quant_bias(bias, inp_scale * weight_scale)
264 qat_net.weight[...] = Parameter(weight)
265 qat_net.bias[...] = Parameter(bias)
266
267 qat_from_float = getattr(QAT, module).from_float_module(normal_net)
268 qat_from_float.eval()
269 disable_observer(qat_from_float)
270 disable_fake_quant(qat_from_float)
271
272 q_net = getattr(Q, module).from_qat_module(qat_net)
273 q_net.eval()
274
275 normal = normal_net(x)
276 qat_without_fakequant = qat_from_float(x)
277 fake_quant_normal = fake_quant_act(normal_net(x), act_scale)
278 qat = qat_net(x)
279 q = q_net(x_int8).numpy() * act_scale
280 np.testing.assert_allclose(qat_without_fakequant, normal, atol=1e-5)
281 np.testing.assert_allclose(qat, fake_quant_normal, atol=act_scale)
282 np.testing.assert_allclose(q, fake_quant_normal.numpy(), atol=act_scale)
283
284
285def test_concat():

Callers

nothing calls this directly

Calls 15

disable_observerFunction · 0.90
propagate_qconfigFunction · 0.90
create_qparamsFunction · 0.90
ParameterClass · 0.90
disable_fake_quantFunction · 0.90
gen_inp_scaleFunction · 0.85
fake_quant_biasFunction · 0.85
tensorMethod · 0.80
normalMethod · 0.80
init_qat_netFunction · 0.70
quantFunction · 0.70
evalMethod · 0.45

Tested by

no test coverage detected