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

Function test_elemwise

imperative/python/test/unit/quantization/test_op.py:27–60  ·  view source on GitHub ↗
(kind)

Source from the content-addressed store, hash-verified

25
26@pytest.mark.parametrize("kind", ["abs", "sin", "sub", "mul", "fuse_add_tanh"])
27def test_elemwise(kind):
28 x1 = mge.tensor(np.random.normal(size=(3, 3)).astype("float32"))
29 x1_scale = np.float32(np.random.rand() + 1)
30 x1 = fake_quant(x1, x1_scale)
31 x1.qparams.update(create_qparams(QuantMode.SYMMERTIC, "qint8", x1_scale))
32 x1_int8 = quant(x1, x1_scale)
33
34 x2 = mge.tensor(np.random.normal(size=(3, 3)).astype("float32"))
35 x2_scale = np.float32(np.random.rand() + 1)
36 x2 = fake_quant(x2, x2_scale)
37 x2.qparams.update(create_qparams(QuantMode.SYMMERTIC, "qint8", x2_scale))
38 x2_int8 = quant(x2, x2_scale)
39
40 output_scale = np.float32(np.random.rand() + 1)
41 output_dtype = dtype.qint8(output_scale)
42
43 quantized_kind = "q" + kind
44 if kind in ("abs", "sin"):
45 desired_out = fake_quant(_elwise(x1, mode=kind), output_scale)
46 actual_out = (
47 _elemwise_multi_type(
48 x1_int8, mode=quantized_kind, dtype=output_dtype
49 ).numpy()
50 * output_scale
51 )
52 else:
53 desired_out = fake_quant(_elwise(x1, x2, mode=kind), output_scale)
54 actual_out = (
55 _elemwise_multi_type(
56 x1_int8, x2_int8, mode=quantized_kind, dtype=output_dtype
57 ).numpy()
58 * output_scale
59 )
60 np.testing.assert_allclose(actual_out, desired_out.numpy())
61
62
63@pytest.mark.skipif(

Callers

nothing calls this directly

Calls 10

create_qparamsFunction · 0.90
_elemwise_multi_typeFunction · 0.90
tensorMethod · 0.80
normalMethod · 0.80
fake_quantFunction · 0.70
quantFunction · 0.70
_elwiseFunction · 0.50
astypeMethod · 0.45
updateMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected