|
1 | 1 | import unittest |
2 | 2 |
|
3 | | -import numpy |
4 | | - |
5 | | -import dpnp as inp |
| 3 | +from tests.third_party.cupy import testing |
6 | 4 |
|
7 | 5 |
|
8 | 6 | class TestMatMul(unittest.TestCase): |
9 | | - def test_matmul(self): |
10 | | - array_data = [1.0, 2.0, 3.0, 4.0] |
11 | | - size = 2 |
12 | | - |
13 | | - # DPNP |
14 | | - array1 = inp.reshape( |
15 | | - inp.array(array_data, dtype=inp.float64), (size, size) |
16 | | - ) |
17 | | - array2 = inp.reshape( |
18 | | - inp.array(array_data, dtype=inp.float64), (size, size) |
19 | | - ) |
20 | | - result = inp.matmul(array1, array2) |
21 | | - # print(result) |
22 | | - |
23 | | - # original |
24 | | - array_1 = numpy.array(array_data, dtype=numpy.float64).reshape( |
25 | | - (size, size) |
26 | | - ) |
27 | | - array_2 = numpy.array(array_data, dtype=numpy.float64).reshape( |
28 | | - (size, size) |
29 | | - ) |
30 | | - expected = numpy.matmul(array_1, array_2) |
31 | | - # print(expected) |
| 7 | + @testing.for_float_dtypes() |
| 8 | + @testing.numpy_cupy_allclose() |
| 9 | + def test_matmul(self, xp, dtype): |
| 10 | + data = [1.0, 2.0, 3.0, 4.0] |
| 11 | + shape = (2, 2) |
32 | 12 |
|
33 | | - # passed |
34 | | - numpy.testing.assert_array_equal(expected, result) |
35 | | - # still failed |
36 | | - # self.assertEqual(expected, result) |
| 13 | + a = xp.array(data, dtype=dtype).reshape(shape) |
| 14 | + b = xp.array(data, dtype=dtype).reshape(shape) |
37 | 15 |
|
38 | | - def test_matmul2(self): |
39 | | - array_data1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] |
40 | | - array_data2 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] |
| 16 | + return xp.matmul(a, b) |
41 | 17 |
|
42 | | - # DPNP |
43 | | - array1 = inp.reshape(inp.array(array_data1, dtype=inp.float64), (3, 2)) |
44 | | - array2 = inp.reshape(inp.array(array_data2, dtype=inp.float64), (2, 4)) |
45 | | - result = inp.matmul(array1, array2) |
46 | | - # print(result) |
| 18 | + @testing.for_float_dtypes() |
| 19 | + @testing.numpy_cupy_allclose() |
| 20 | + def test_matmul2(self, xp, dtype): |
| 21 | + data1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] |
| 22 | + data2 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] |
47 | 23 |
|
48 | | - # original |
49 | | - array_1 = numpy.array(array_data1, dtype=numpy.float64).reshape((3, 2)) |
50 | | - array_2 = numpy.array(array_data2, dtype=numpy.float64).reshape((2, 4)) |
51 | | - expected = numpy.matmul(array_1, array_2) |
52 | | - # print(expected) |
| 24 | + a = xp.array(data1, dtype=dtype).reshape(3, 2) |
| 25 | + b = xp.array(data2, dtype=dtype).reshape(2, 4) |
53 | 26 |
|
54 | | - numpy.testing.assert_array_equal(expected, result) |
| 27 | + return xp.matmul(a, b) |
55 | 28 |
|
56 | | - def test_matmul3(self): |
57 | | - array_data1 = numpy.full((513, 513), 5) |
58 | | - array_data2 = numpy.full((513, 513), 2) |
59 | | - out = numpy.empty((513, 513), dtype=numpy.float64) |
| 29 | + @testing.for_float_dtypes() |
| 30 | + @testing.numpy_cupy_allclose() |
| 31 | + def test_matmul3(self, xp, dtype): |
| 32 | + data1 = xp.full((513, 513), 5) |
| 33 | + data2 = xp.full((513, 513), 2) |
| 34 | + out = xp.empty((513, 513), dtype=dtype) |
60 | 35 |
|
61 | | - # DPNP |
62 | | - array1 = inp.array(array_data1, dtype=inp.float64) |
63 | | - array2 = inp.array(array_data2, dtype=inp.float64) |
64 | | - out1 = inp.array(out, dtype=inp.float64) |
65 | | - result = inp.matmul(array1, array2, out=out1) |
| 36 | + a = xp.array(data1, dtype=dtype) |
| 37 | + b = xp.array(data2, dtype=dtype) |
66 | 38 |
|
67 | | - # original |
68 | | - array_1 = numpy.array(array_data1, dtype=numpy.float64) |
69 | | - array_2 = numpy.array(array_data2, dtype=numpy.float64) |
70 | | - expected = numpy.matmul(array_1, array_2, out=out) |
| 39 | + xp.matmul(a, b, out=out) |
71 | 40 |
|
72 | | - numpy.testing.assert_array_equal(expected, result) |
| 41 | + return out |
73 | 42 |
|
74 | 43 |
|
75 | 44 | if __name__ == "__main__": |
|
0 commit comments