-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_math_utils.py
More file actions
134 lines (114 loc) · 5.01 KB
/
test_math_utils.py
File metadata and controls
134 lines (114 loc) · 5.01 KB
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
import numpy as np
import scipy
import scipy.sparse
from TELF.factorization.decompositions.utilities import math_utils
import time
import pytest
def test_prune_numpy():
for dtype in [np.float32, np.float64]:
A0 = np.array([[1, 0, 0, 3], [0, 0, 0, 0], [1, 0, 2, 4]], dtype=dtype)
for typ in [np.array, scipy.sparse.csr_matrix]:
A = typ(A0)
B, rows, cols = math_utils.prune(A)
assert np.all(np.equal(cols, np.array([True, False, True, True])))
assert np.all(np.equal(rows, np.array([True, False, True])))
def test_unprune_numpy():
for dtype in [np.float32, np.float64]:
A = np.array([[1, 0, 0, 3], [0, 0, 0, 0], [1, 0, 2, 4]], dtype=dtype)
B, rows, cols = math_utils.prune(A)
Arecon = math_utils.unprune(math_utils.unprune(B, rows, 0), cols, 1)
assert np.allclose(A, Arecon)
assert A.dtype == Arecon.dtype
assert type(B) == type(A)
def test_fro_norm_numpy():
for dtype in [np.float32, np.float64]:
A0 = np.array([[1, 2], [3, 4]], dtype=dtype)
norm0 = math_utils.fro_norm(A0)
for typ in [np.array, scipy.sparse.csr_matrix]:
A = typ(A0)
norm = math_utils.fro_norm(A)
assert np.dtype(type(norm)) == np.dtype(dtype)
def test_kl_divergence_numpy():
for dtype in [np.float32, np.float64]:
X0 = np.array([[1, 2], [3, 4]], dtype=dtype)
Y = np.array([[2, 3], [4, 5]], dtype=dtype)
div0 = math_utils.kl_divergence(X0, Y)
for typ in [np.array, scipy.sparse.csr_matrix]:
X = typ(X0)
div = math_utils.kl_divergence(X, Y)
assert np.dtype(type(div)) == np.dtype(dtype)
def test_sparse_divide_product_numpy():
np.random.seed(0)
m, k, n = 10, 3, 9
for dtype in [np.float32, np.float64]:
W0 = np.random.rand(m, k).astype(dtype)
H0 = np.random.rand(k, n).astype(dtype)
X0 = W0@H0
for typ in [scipy.sparse.csr_matrix]:
X = typ(X0)
ans = math_utils.sparse_divide_product(X, W0, H0)
assert type(ans) == type(X)
if scipy.sparse.issparse(ans):
assert np.allclose(np.array(ans.todense()),
np.ones((m, n), dtype=dtype))
else:
assert np.allclose(ans, np.ones((m, n), dtype=dtype))
def test_prune_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
for dtype in [np.float32, np.float64]:
A0 = cp.array([[1, 0, 0, 3], [0, 0, 0, 0], [1, 0, 2, 4]], dtype=dtype)
for typ in [cp.array, cupyx.scipy.sparse.csr_matrix]:
A = typ(A0)
B, rows, cols = math_utils.prune(A, use_gpu=True)
assert cp.all(cp.equal(cols, cp.array([True, False, True, True])))
assert cp.all(cp.equal(rows, cp.array([True, False, True])))
def test_unprune_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
for dtype in [np.float32, np.float64]:
A = cp.array([[1, 0, 0, 3], [0, 0, 0, 0], [1, 0, 2, 4]], dtype=dtype)
B, rows, cols = math_utils.prune(A, use_gpu=True)
Arecon = math_utils.unprune(math_utils.unprune(B, rows, 0, use_gpu=True), cols, 1, use_gpu=True)
assert np.allclose(A, Arecon)
assert A.dtype == Arecon.dtype
assert type(B) == type(A)
def test_fro_norm_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
for dtype in [np.float32, np.float64]:
A0 = cp.array([[1, 2], [3, 4]], dtype=dtype)
norm0 = math_utils.fro_norm(A0)
for typ in [cp.array, cupyx.scipy.sparse.csr_matrix]:
A = typ(A0)
norm = math_utils.fro_norm(A, use_gpu=True)
assert cp.dtype(norm.dtype) == cp.dtype(dtype)
def test_kl_divergence_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
for dtype in [np.float32, np.float64]:
X0 = np.array([[1, 2], [3, 4]], dtype=dtype)
Y = np.array([[2, 3], [4, 5]], dtype=dtype)
div0 = math_utils.kl_divergence(X0, Y)
for typ in [np.array, scipy.sparse.csr_matrix]:
X = typ(X0)
div = math_utils.kl_divergence(X, Y)
assert np.dtype(type(div)) == np.dtype(dtype)
def test_sparse_divide_product_cupy():
cp = pytest.importorskip("cupy")
cupyx = pytest.importorskip("cupyx")
np.random.seed(0)
m, k, n = 10, 3, 9
for dtype in [np.float32, np.float64]:
W0 = cp.random.rand(m, k).astype(dtype)
H0 = cp.random.rand(k, n).astype(dtype)
X0 = W0@H0
for typ in [cupyx.scipy.sparse.csr_matrix]:
X = typ(X0)
ans = math_utils.sparse_divide_product(X, W0, H0, use_gpu=True)
assert type(ans) == type(X)
if cupyx.scipy.sparse.issparse(ans):
assert cp.allclose(cp.array(ans.todense()),
np.ones((m, n), dtype=dtype))
else:
assert cp.allclose(ans, cp.ones((m, n), dtype=dtype))