forked from intel/ScalableVectorSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_vamana.py
More file actions
258 lines (218 loc) · 7.29 KB
/
example_vamana.py
File metadata and controls
258 lines (218 loc) · 7.29 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Import `unittest` to allow for automated testing.
import unittest
# [imports]
import os
import svs
# [imports]
DEBUG_MODE = False
def assert_equal(lhs, rhs, message: str = "", epsilon = 0.05):
if DEBUG_MODE:
print(f"{message}: {lhs} == {rhs}")
else:
assert lhs < rhs + epsilon, message
assert lhs > rhs - epsilon, message
def run_test_float(index, queries, groundtruth):
expected = {
10: 0.5664,
20: 0.7397,
30: 0.8288,
40: 0.8837,
}
for window_size in range(10, 50, 10):
index.search_window_size = window_size
I, D = index.search(queries, 10)
recall = svs.k_recall_at(groundtruth, I, 10, 10)
assert_equal(
recall, expected[window_size], f"Standard Search Check ({window_size})"
)
def run_test_two_level4_8(index, queries, groundtruth):
expected = {
10: 0.5482,
20: 0.7294,
30: 0.8223,
40: 0.8756,
}
for window_size in range(10, 50, 10):
index.search_window_size = window_size
I, D = index.search(queries, 10)
recall = svs.k_recall_at(groundtruth, I, 10, 10)
assert_equal(
recall, expected[window_size], f"Compressed Search Check ({window_size})"
)
def run_test_build_two_level4_8(index, queries, groundtruth):
expected = {
10: 0.5484,
20: 0.7295,
30: 0.8221,
40: 0.8758,
}
for window_size in range(10, 50, 10):
index.search_window_size = window_size
I, D = index.search(queries, 10)
recall = svs.k_recall_at(groundtruth, I, 10, 10)
assert_equal(
recall, expected[window_size], f"Compressed Search Check ({window_size})"
)
# Shadow this as a global to make it available to the test-case clean-up.
test_data_dir = None
def run():
# ###
# Generating test data
# ###
# [generate-dataset]
# Create a test dataset.
# This will create a directory "example_data_vamana" and populate it with three
# entries:
# - data.fvecs: The test dataset.
# - queries.fvecs: The test queries.
# - groundtruth.ivecs: The groundtruth.
test_data_dir = "./example_data_vamana"
svs.generate_test_dataset(
10000, # Create 10000 vectors in the dataset.
1000, # Generate 1000 query vectors.
128, # Set the vector dimensionality to 128.
test_data_dir, # The directory where results will be generated.
data_seed = 1234, # Random number seed for reproducibility.
query_seed = 5678, # Random number seed for reproducibility.
num_threads = 4, # Number of threads to use.
distance = svs.DistanceType.L2, # The distance type to use.
)
# [generate-dataset]
# ###
# Building the index
# ###
# [build-parameters]
# Now, we can build a graph index over the data set.
parameters = svs.VamanaBuildParameters(
graph_max_degree = 64,
window_size = 128,
)
# [build-parameters]
# [build-index]
# Build the index.
index = svs.Vamana.build(
parameters,
svs.VectorDataLoader(
os.path.join(test_data_dir, "data.fvecs"), svs.DataType.float32
),
svs.DistanceType.L2,
num_threads = 4,
)
# [build-index]
# [build-index-fromNumpyArray]
# Build the index.
data = svs.read_vecs(os.path.join(test_data_dir, "data.fvecs"))
index = svs.Vamana.build(
parameters,
data,
svs.DistanceType.L2,
num_threads = 4,
)
# [build-index-fromNumpyArray]
# ###
# Searching the index
# ###
# [load-aux]
# Load the queries and ground truth.
queries = svs.read_vecs(os.path.join(test_data_dir, "queries.fvecs"))
groundtruth = svs.read_vecs(os.path.join(test_data_dir, "groundtruth.ivecs"))
# [load-aux]
# [perform-queries]
# Set the search window size of the index and perform queries.
index.search_window_size = 30
I, D = index.search(queries, 10)
# Compare with the groundtruth.
recall = svs.k_recall_at(groundtruth, I, 10, 10)
print(f"Recall = {recall}")
assert_equal(recall, 0.8288)
# [perform-queries]
# [search-window-size]
# We can vary the search window size to demonstrate the trade off in accuracy.
for window_size in range(10, 50, 10):
index.search_window_size = window_size
I, D = index.search(queries, 10)
recall = svs.k_recall_at(groundtruth, I, 10, 10)
print(f"Window size = {window_size}, Recall = {recall}")
# [search-window-size]
##### Begin Test
run_test_float(index, queries, groundtruth)
##### End Test
# ###
# Saving the index
# ###
# [saving-results]
# Finally, we can save the results.
index.save(
os.path.join(test_data_dir, "example_config"),
os.path.join(test_data_dir, "example_graph"),
os.path.join(test_data_dir, "example_data"),
)
# [saving-results]
# ###
# Reloading a saved index
# ###
# [loading]
# We can reload an index from a previously saved set of files.
index = svs.Vamana(
os.path.join(test_data_dir, "example_config"),
svs.GraphLoader(os.path.join(test_data_dir, "example_graph")),
svs.VectorDataLoader(
os.path.join(test_data_dir, "example_data"), svs.DataType.float32
),
svs.DistanceType.L2,
num_threads = 4,
)
# We can rerun the queries to ensure everything works properly.
index.search_window_size = 30
I, D = index.search(queries, 10)
# Compare with the groundtruth.
recall = svs.k_recall_at(groundtruth, I, 10, 10)
print(f"Recall = {recall}")
assert_equal(recall, 0.8288)
# [loading]
##### Begin Test
run_test_float(index, queries, groundtruth)
##### End Test
# [only-loading]
# We can reload an index from a previously saved set of files.
index = svs.Vamana(
os.path.join(test_data_dir, "example_config"),
svs.GraphLoader(os.path.join(test_data_dir, "example_graph")),
svs.VectorDataLoader(
os.path.join(test_data_dir, "example_data"), svs.DataType.float32
),
svs.DistanceType.L2,
num_threads = 4,
)
# [only-loading]
# [runtime-nthreads]
index.num_threads = 4
# [runtime-nthreads]
#####
##### Main Executable
#####
if __name__ == "__main__":
run()
#####
##### As a unit test.
#####
class VamanaExampleTestCase(unittest.TestCase):
def tearDown(self):
if test_data_dir is not None:
print(f"Removing temporary directory {test_data_dir}")
os.rmdir(test_data_dir)
def test_all(self):
run()