(kind)
| 25 | |
| 26 | @pytest.mark.parametrize("kind", ["abs", "sin", "sub", "mul", "fuse_add_tanh"]) |
| 27 | def 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( |
nothing calls this directly
no test coverage detected