(lhs_shape, rhs_shape, lhs_transpose, rhs_transpose, dtype=None)
| 26 | @pytest.mark.skipif(not is_cuda_available(), reason="only support cuda now") |
| 27 | def test_matmul(): |
| 28 | def tester(lhs_shape, rhs_shape, lhs_transpose, rhs_transpose, dtype=None): |
| 29 | lhs = tensor(0.1 * np.random.randn(*lhs_shape), dtype=dtype) |
| 30 | rhs = tensor(0.1 * np.random.randn(*rhs_shape), dtype=dtype) |
| 31 | out = F.matmul(lhs, rhs, lhs_transpose, rhs_transpose) |
| 32 | dout = tensor(0.1 * np.random.randn(*out.shape), dtype=dtype) |
| 33 | atol = 5e-4 if dtype == np.float16 else 1e-5 |
| 34 | |
| 35 | gm = GradManager() |
| 36 | |
| 37 | @jit.xla_trace(without_host=True) |
| 38 | def func(lhs, rhs, dout): |
| 39 | gm.attach([lhs, rhs]) |
| 40 | with gm: |
| 41 | out = F.matmul(lhs, rhs, lhs_transpose, rhs_transpose) |
| 42 | gm.backward(out, dout) |
| 43 | return out, lhs.grad, rhs.grad |
| 44 | |
| 45 | mge_rsts = func(lhs, rhs, dout) |
| 46 | xla_rsts = func(lhs, rhs, dout) |
| 47 | |
| 48 | for mge_rst, xla_rst in zip(mge_rsts, xla_rsts): |
| 49 | np.testing.assert_allclose(mge_rst.numpy(), xla_rst.numpy(), atol=atol) |
| 50 | |
| 51 | for dtype in [np.float16, np.float32]: |
| 52 | tester((5,), (5,), False, False) |
no test coverage detected