Skip to content
Navigation Menu
{{ message }}
forked from ashvardanian/NumKong
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
230 lines (174 loc) · 9.07 KB
/
Copy pathtest.py
File metadata and controls
230 lines (174 loc) · 9.07 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import pytest
import numpy as np
import simsimd as simd
import scipy.spatial.distance as spd
# For normalized distances we use the absolute tolerance, because the result is close to zero.
# For unnormalized ones (like squared Euclidean or Jaccard), we use the relative.
SIMSIMD_RTOL = 0.2
SIMSIMD_ATOL = 0.15
def test_pointers_availability():
"""Tests the availability of pre-compiled functions for compatibility with USearch."""
assert simd.pointer_to_sqeuclidean("f64") != 0
assert simd.pointer_to_cosine("f64") != 0
assert simd.pointer_to_inner("f64") != 0
assert simd.pointer_to_sqeuclidean("f32") != 0
assert simd.pointer_to_cosine("f32") != 0
assert simd.pointer_to_inner("f32") != 0
assert simd.pointer_to_sqeuclidean("f16") != 0
assert simd.pointer_to_cosine("f16") != 0
assert simd.pointer_to_inner("f16") != 0
assert simd.pointer_to_sqeuclidean("i8") != 0
assert simd.pointer_to_cosine("i8") != 0
assert simd.pointer_to_inner("i8") != 0
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float64, np.float32, np.float16])
def test_dot(ndim, dtype):
"""Compares the simd.dot() function with numpy.dot(), measuring the accuracy error for f16, and f32 types."""
np.random.seed()
a = np.random.randn(ndim).astype(dtype)
b = np.random.randn(ndim).astype(dtype)
a /= np.linalg.norm(a)
b /= np.linalg.norm(b)
expected = 1 - np.inner(a.astype(np.float32), b.astype(np.float32))
result = simd.inner(a, b)
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float64, np.float32, np.float16])
def test_sqeuclidean(ndim, dtype):
"""Compares the simd.sqeuclidean() function with scipy.spatial.distance.sqeuclidean(), measuring the accuracy error for f16, and f32 types."""
np.random.seed()
a = np.random.randn(ndim).astype(dtype)
b = np.random.randn(ndim).astype(dtype)
expected = spd.sqeuclidean(a.astype(np.float32), b.astype(np.float32))
result = simd.sqeuclidean(a, b)
np.testing.assert_allclose(expected, result, atol=0, rtol=SIMSIMD_RTOL)
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float64, np.float32, np.float16])
def test_cosine(ndim, dtype):
"""Compares the simd.cosine() function with scipy.spatial.distance.cosine(), measuring the accuracy error for f16, and f32 types."""
np.random.seed()
a = np.random.randn(ndim).astype(dtype)
b = np.random.randn(ndim).astype(dtype)
expected = spd.cosine(a.astype(np.float32), b.astype(np.float32))
result = simd.cosine(a, b)
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
@pytest.mark.skip
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float32, np.float16])
def test_jensen_shannon(ndim, dtype):
"""Compares the simd.jensenshannon() function with scipy.spatial.distance.jensenshannon(), measuring the accuracy error for f16, and f32 types."""
np.random.seed()
a = np.abs(np.random.randn(ndim)).astype(dtype)
b = np.abs(np.random.randn(ndim)).astype(dtype)
a /= np.sum(a)
b /= np.sum(b)
expected = spd.jensenshannon(a, b) ** 2
result = simd.jensenshannon(a, b)
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
def test_cosine_i8(ndim):
"""Compares the simd.cosine() function with scipy.spatial.distance.cosine(), measuring the accuracy error for 8-bit int types."""
np.random.seed()
a = np.random.randint(0, 100, size=ndim, dtype=np.int8)
b = np.random.randint(0, 100, size=ndim, dtype=np.int8)
expected = spd.cosine(a.astype(np.float32), b.astype(np.float32))
result = simd.cosine(a, b)
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
def test_sqeuclidean_i8(ndim):
"""Compares the simd.sqeuclidean() function with scipy.spatial.distance.sqeuclidean(), measuring the accuracy error for 8-bit int types."""
np.random.seed()
a = np.random.randint(0, 100, size=ndim, dtype=np.int8)
b = np.random.randint(0, 100, size=ndim, dtype=np.int8)
expected = spd.sqeuclidean(a.astype(np.float32), b.astype(np.float32))
result = simd.sqeuclidean(a, b)
np.testing.assert_allclose(expected, result, atol=0, rtol=SIMSIMD_RTOL)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float32, np.float16])
def test_cosine_zero_vector(ndim, dtype):
"""Tests the simd.cosine() function with zero vectors, to catch division by zero errors."""
np.random.seed()
a = np.zeros(ndim, dtype=dtype)
b = np.random.randn(ndim).astype(dtype)
# SciPy raises: "RuntimeWarning: invalid value encountered in scalar divide"
with pytest.raises(RuntimeWarning):
expected = spd.cosine(a, b)
expected = 1
result = simd.cosine(a, b)
assert result == expected, f"Expected {expected}, but got {result}"
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
def test_hamming(ndim):
"""Compares the simd.hamming() function with scipy.spatial.distance.hamming."""
np.random.seed()
a = np.random.randint(2, size=ndim).astype(np.uint8)
b = np.random.randint(2, size=ndim).astype(np.uint8)
expected = spd.hamming(a, b) * ndim
result = simd.hamming(np.packbits(a), np.packbits(b))
np.testing.assert_allclose(expected, result, atol=0, rtol=SIMSIMD_RTOL)
@pytest.mark.repeat(50)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
def test_jaccard(ndim):
"""Compares the simd.jaccard() function with scipy.spatial.distance.jaccard."""
np.random.seed()
a = np.random.randint(2, size=ndim).astype(np.uint8)
b = np.random.randint(2, size=ndim).astype(np.uint8)
expected = spd.jaccard(a, b)
result = simd.jaccard(np.packbits(a), np.packbits(b))
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float64, np.float32, np.float16])
def test_batch(ndim, dtype):
"""Compares the simd.simd.sqeuclidean() function with scipy.spatial.distance.sqeuclidean() for a batch of vectors, measuring the accuracy error for f16, and f32 types."""
np.random.seed()
# Distance between matrixes A (N x D scalars) and B (N x D scalars) is an array with N floats.
A = np.random.randn(10, ndim).astype(dtype)
B = np.random.randn(10, ndim).astype(dtype)
result_np = [spd.sqeuclidean(A[i], B[i]) for i in range(10)]
result_simd = simd.sqeuclidean(A, B)
assert np.allclose(result_simd, result_np, atol=0, rtol=SIMSIMD_RTOL)
# Distance between matrixes A (N x D scalars) and B (1 x D scalars) is an array with N floats.
B = np.random.randn(1, ndim).astype(dtype)
result_np = [spd.sqeuclidean(A[i], B[0]) for i in range(10)]
result_simd = simd.sqeuclidean(A, B)
assert np.allclose(result_simd, result_np, atol=0, rtol=SIMSIMD_RTOL)
# Distance between matrixes A (1 x D scalars) and B (N x D scalars) is an array with N floats.
A = np.random.randn(1, ndim).astype(dtype)
B = np.random.randn(10, ndim).astype(dtype)
result_np = [spd.sqeuclidean(A[0], B[i]) for i in range(10)]
result_simd = simd.sqeuclidean(A, B)
assert np.allclose(result_simd, result_np, atol=0, rtol=SIMSIMD_RTOL)
# Distance between matrix A (N x D scalars) and array B (D scalars) is an array with N floats.
A = np.random.randn(10, ndim).astype(dtype)
B = np.random.randn(ndim).astype(dtype)
result_np = [spd.sqeuclidean(A[i], B) for i in range(10)]
result_simd = simd.sqeuclidean(A, B)
assert np.allclose(result_simd, result_np, atol=0, rtol=SIMSIMD_RTOL)
# Distance between matrix B (N x D scalars) and array A (D scalars) is an array with N floats.
B = np.random.randn(10, ndim).astype(dtype)
A = np.random.randn(ndim).astype(dtype)
result_np = [spd.sqeuclidean(B[i], A) for i in range(10)]
result_simd = simd.sqeuclidean(B, A)
assert np.allclose(result_simd, result_np, atol=0, rtol=SIMSIMD_RTOL)
@pytest.mark.parametrize("ndim", [3, 97, 1536])
@pytest.mark.parametrize("dtype", [np.float32, np.float16])
@pytest.mark.parametrize("metric", ["cosine"])
def test_cdist(ndim, dtype, metric):
"""Compares the simd.cdist() function with scipy.spatial.distance.cdist(), measuring the accuracy error for f16, and f32 types using sqeuclidean and cosine metrics."""
np.random.seed()
# Create random matrices A (M x D) and B (N x D).
M, N = 10, 15 # or any other sizes you deem appropriate
A = np.random.randn(M, ndim).astype(dtype)
B = np.random.randn(N, ndim).astype(dtype)
# Compute cdist using scipy.
expected = spd.cdist(A, B, metric)
# Compute cdist using simd.
result = simd.cdist(A, B, metric=metric)
# Assert they're close.
np.testing.assert_allclose(expected, result, atol=SIMSIMD_ATOL, rtol=0)
You can’t perform that action at this time.
