~magnusmorton/delayrepay

ref: 1348e55c1ead7322cfc589004bdd54c12b5ed48d delayrepay/tests/test.py -rw-r--r-- 4.7 KiB
1348e55c — Magnus Morton cleaned up 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''Test simple cl examples'''
import unittest
from delayrepay import ones, full, NPArray, array, sum
import numpy as np
import numpy.testing as npt

SIZE = 64

def assertEqF(one, two):
    return abs(one-two) < 0.001

class TestElwise(unittest.TestCase):
    # pylint: disable=C

    def setUp(self):
        self.arr = ones(SIZE)
        self.np_arr = np.ones(SIZE)

    def test_scalar_add(self):
        res = self.arr + 1
        npt.assert_array_almost_equal(res.get(), self.np_arr + 1)

    def test_scalar_mul(self):
        res = self.arr * 3
        npt.assert_array_almost_equal(res.get(), self.np_arr * 3)

    def test_var_add(self):
        a = 7
        res = a * self.arr
        npt.assert_array_almost_equal(res.get(), self.np_arr * 7)

    def test_axpy(self):
        def axpy(a, x, y):
            return a*x + y
        res = axpy(8, self.arr, 9)
        npt.assert_array_almost_equal(res.get(), axpy(8, self.np_arr, 9))

    def test_regression(self):
        def fun(mat):
            return mat + mat * 3 + 9

        res = fun(self.arr)
        npt.assert_array_almost_equal(res.get(), fun(self.np_arr))

    def test_ir(self):
        res = self.arr + 3
        assert(res)

    def test_cos(self):
        res = np.cos(self.arr)
        npt.assert_array_almost_equal(res.get(), np.cos(self.np_arr))

    def test_exp(self):
        res = self.arr ** 2
        npt.assert_array_almost_equal(res.get(), self.np_arr ** 2)

    def test_exp32(self):
        arr = self.arr.astype(np.float32)
        res = arr ** 2
        npt.assert_array_almost_equal(res.get(), self.np_arr.astype(np.float32) ** 2)

    def test_fuse_bench(self):
        res = np.sin(self.arr) ** 2 + np.cos(self.arr) ** 2
        resn = np.sin(self.np_arr) ** 2 + np.cos(self.np_arr) ** 2
        npt.assert_array_almost_equal(res.get(), resn)


class TestVector(unittest.TestCase):
    # pylint: disable=C

    def setUp(self):
        self.arr = full((SIZE,), 7).astype(np.float32)
        self.arr2 = full((SIZE,), 3).astype(np.float32)
        self.np_arr = np.full((SIZE,), 7).astype(np.float32)
        self.np_arr2 = np.full((SIZE,), 3).astype(np.float32)

    def test_vecadd(self):
        res = self.arr + self.arr2
        npt.assert_array_almost_equal(res.get(), self.np_arr + self.np_arr2)

    def test_vecmul(self):
        res = self.arr * self.arr2
        npt.assert_array_almost_equal(res.get(), self.np_arr * self.np_arr2)

    def test_dot_method(self):
        res = self.arr.dot(self.arr2)
        test = self.np_arr.dot(self.np_arr2)
        assertEqF(res, test)

    def test_dot_func(self):
        res = np.dot(self.arr, self.arr2)
        test = self.np_arr.dot(self.np_arr2)
        assertEqF(res, test)

    def test_matmul(self):
        res = self.arr @ self.arr2
        test = self.np_arr @ self.np_arr2
        assertEqF(res, test)

    def test_sum(self):
        res = sum(self.arr)
        test = np.sum(self.np_arr)
        print(res)
        self.assertEqual(res, test)

    def test_atan2(self):
        res = np.arctan2(self.arr, self.arr2)
        test = np.arctan2(self.np_arr, self.np_arr2)
        npt.assert_array_almost_equal(res.get(), test)


class TestMatrix(unittest.TestCase):

    def setUp(self):
        self.mat = full((SIZE, SIZE), 7).astype(np.float32)
        self.vec = full((SIZE,), 3).astype(np.float32)
        self.np_mat = np.full((SIZE, SIZE), 7).astype(np.float32)
        self.np_vec = np.full((SIZE,), 3).astype(np.float32)

    def test_scalar_mul(self):
        res = self.mat * 3
        npt.assert_array_almost_equal(res.get(), self.np_mat * 3)

    def test_matvec(self):
        res = self.mat @ self.vec
        test = self.np_mat @ self.np_vec
        npt.assert_array_almost_equal(res.get(), test)

    def test_kuba(self):
        a = full((64, 64), 10.0, dtype=np.float32)
        b = full((64,), 2.0, dtype=np.float32)
        an = np.full((64, 64), 10.0, dtype=np.float32)
        bn = np.full((64,), 2.0, dtype=np.float32)
        npt.assert_array_almost_equal((a @ b).get(), an @ bn)

    def test_gemm(self):
        res = self.mat @ self.mat
        npt.assert_array_almost_equal(res.get(), self.np_mat @ self.np_mat)


class TestMeta(unittest.TestCase):

    def test_memoise(self):
        arr = np.array([1, 2, 3])
        ar1 = NPArray(arr)
        ar2 = NPArray(arr)
        self.assertIs(ar1, ar2)

    def test_no_memoize(self):
        arr = full((3,), 5).astype(np.float32)
        arr2 = full((3,), 3).astype(np.float32)
        self.assertIsNot(arr, arr2)

    def test_memo_ex(self):
       arr = array([1, 2, 3])
       ex1 = np.sin(arr)
       print(id(ex1))
       ex2 = np.sin(arr)
       print(id(ex2))
       self.assertIs(ex1, ex2)

    
if __name__ == '__main__':
    unittest.main()