-
Notifications
You must be signed in to change notification settings - Fork 123
/
datasets.py
649 lines (541 loc) · 21.7 KB
/
datasets.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import math
import numpy
import os
import random
import sys
import struct
import time
import numpy as np
from urllib.request import urlopen
from urllib.request import urlretrieve
BASEDIR = "data/"
def download(src, dst=None, max_size=None):
""" download an URL, possibly cropped """
if os.path.exists(dst):
return
print('downloading %s -> %s...' % (src, dst))
if max_size is not None:
print(" stopping at %d bytes" % max_size)
t0 = time.time()
outf = open(dst, "wb")
inf = urlopen(src)
info = dict(inf.info())
content_size = int(info['Content-Length'])
bs = 1 << 20
totsz = 0
while True:
block = inf.read(bs)
elapsed = time.time() - t0
print(
" [%.2f s] downloaded %.2f MiB / %.2f MiB at %.2f MiB/s " % (
elapsed,
totsz / 2**20, content_size / 2**20,
totsz / 2**20 / elapsed),
flush=True, end="\r"
)
if not block:
break
if max_size is not None and totsz + len(block) >= max_size:
block = block[:max_size - totsz]
outf.write(block)
totsz += len(block)
break
outf.write(block)
totsz += len(block)
print()
print("download finished in %.2f s, total size %d bytes" % (
time.time() - t0, totsz
))
def download_accelerated(src, dst, quiet=False, sas_string=""):
""" dowload using an accelerator. Make sure the executable is in the path """
print('downloading %s -> %s...' % (src, dst))
if "windows.net" in src:
if sas_string == "":
cmd = f"azcopy copy {src} {dst}"
else:
cmd = f"azcopy copy '{src}?{sas_string}' '{dst}'"
else:
cmd = f"axel --alternate -n 10 {src} -o {dst}"
if quiet:
cmd += " -q"
print("running", cmd)
ret = os.system(cmd)
assert ret == 0
def upload_accelerated(local_dir, blob_prefix, component, sas_string, quiet=False):
""" Upload index component to Azure blob using SAS string"""
src = os.path.join(local_dir, component)
dst = blob_prefix + '/' + component + '?' + sas_string
print('Uploading %s -> %s...' % (src, dst))
cmd = f"azcopy copy '{src}' '{dst}'"
print("running", cmd)
ret = os.system(cmd)
assert ret == 0
def bvecs_mmap(fname):
x = numpy.memmap(fname, dtype='uint8', mode='r')
d = x[:4].view('int32')[0]
return x.reshape(-1, d + 4)[:, 4:]
def ivecs_read(fname):
a = numpy.fromfile(fname, dtype='int32')
d = a[0]
return a.reshape(-1, d + 1)[:, 1:].copy()
def xbin_mmap(fname, dtype, maxn=-1):
""" mmap the competition file format for a given type of items """
n, d = map(int, np.fromfile(fname, dtype="uint32", count=2))
assert os.stat(fname).st_size == 8 + n * d * np.dtype(dtype).itemsize
if maxn > 0:
n = min(n, maxn)
return np.memmap(fname, dtype=dtype, mode="r", offset=8, shape=(n, d))
def range_result_read(fname):
""" read the range search result file format """
f = open(fname, "rb")
nq, total_res = np.fromfile(f, count=2, dtype="int32")
nres = np.fromfile(f, count=nq, dtype="int32")
assert nres.sum() == total_res
I = np.fromfile(f, count=total_res, dtype="int32")
D = np.fromfile(f, count=total_res, dtype="float32")
return nres, I, D
def knn_result_read(fname):
n, d = map(int, np.fromfile(fname, dtype="uint32", count=2))
assert os.stat(fname).st_size == 8 + n * d * (4 + 4)
f = open(fname, "rb")
f.seek(4+4)
I = np.fromfile(f, dtype="int32", count=n * d).reshape(n, d)
D = np.fromfile(f, dtype="float32", count=n * d).reshape(n, d)
return I, D
def read_fbin(filename, start_idx=0, chunk_size=None):
""" Read *.fbin file that contains float32 vectors
Args:
:param filename (str): path to *.fbin file
:param start_idx (int): start reading vectors from this index
:param chunk_size (int): number of vectors to read.
If None, read all vectors
Returns:
Array of float32 vectors (numpy.ndarray)
"""
with open(filename, "rb") as f:
nvecs, dim = np.fromfile(f, count=2, dtype=np.int32)
nvecs = (nvecs - start_idx) if chunk_size is None else chunk_size
arr = np.fromfile(f, count=nvecs * dim, dtype=np.float32,
offset=start_idx * 4 * dim)
return arr.reshape(nvecs, dim)
def read_ibin(filename, start_idx=0, chunk_size=None):
""" Read *.ibin file that contains int32 vectors
Args:
:param filename (str): path to *.ibin file
:param start_idx (int): start reading vectors from this index
:param chunk_size (int): number of vectors to read.
If None, read all vectors
Returns:
Array of int32 vectors (numpy.ndarray)
"""
with open(filename, "rb") as f:
nvecs, dim = np.fromfile(f, count=2, dtype=np.int32)
nvecs = (nvecs - start_idx) if chunk_size is None else chunk_size
arr = np.fromfile(f, count=nvecs * dim, dtype=np.int32,
offset=start_idx * 4 * dim)
return arr.reshape(nvecs, dim)
def sanitize(x):
return numpy.ascontiguousarray(x, dtype='float32')
class Dataset():
def prepare(self):
"""
Download and prepare dataset, queries, groundtruth.
"""
pass
def get_dataset_fn(self):
"""
Return filename of dataset file.
"""
pass
def get_dataset(self):
"""
Return memmapped version of the dataset.
"""
pass
def get_dataset_iterator(self, bs=512, split=(1, 0)):
"""
Return iterator over blocks of dataset of size at most 512.
The split argument takes a pair of integers (n, p) where p = 0..n-1
The dataset is split in n shards, and the iterator returns only shard #p
This makes it possible to process the dataset independently from several
processes / threads.
"""
pass
def get_queries(self):
"""
Return (nq, d) array containing the nq queries.
"""
pass
def get_groundtruth(self, k=None):
"""
Return (nq, k) array containing groundtruth indices
for each query."""
pass
def search_type(self):
"""
"knn" or "range"
"""
pass
def distance(self):
"""
"euclidean" or "ip" or "angular"
"""
pass
def default_count(self):
return 10
def short_name(self):
return f"{self.__class__.__name__}-{self.nb}"
def __str__(self):
return (
f"Dataset {self.__class__.__name__} in dimension {self.d}, with distance {self.distance()}, "
f"search_type {self.search_type()}, size: Q {self.nq} B {self.nb}")
#############################################################################
# Datasets for the competition
##############################################################################
class DatasetCompetitionFormat(Dataset):
"""
Dataset in the native competition format, that is able to read the
files in the https://big-ann-benchmarks.com/ page.
The constructor should set all fields. The functions below are generic.
For the 10M versions of the dataset, the database files are downloaded in
part and stored with a specific suffix. This is to avoid having to maintain
two versions of the file.
"""
def prepare(self, skip_data=False):
if not os.path.exists(self.basedir):
os.makedirs(self.basedir)
# start with the small ones...
for fn in [self.qs_fn, self.gt_fn]:
if fn is None:
continue
if fn.startswith("https://"):
sourceurl = fn
outfile = os.path.join(self.basedir, fn.split("/")[-1])
else:
sourceurl = os.path.join(self.base_url, fn)
outfile = os.path.join(self.basedir, fn)
if os.path.exists(outfile):
print("file %s already exists" % outfile)
continue
download(sourceurl, outfile)
if skip_data:
return
fn = self.ds_fn
sourceurl = os.path.join(self.base_url, fn)
outfile = os.path.join(self.basedir, fn)
if os.path.exists(outfile):
print("file %s already exists" % outfile)
return
if self.nb == 10**9:
download_accelerated(sourceurl, outfile)
else:
# download cropped version of file
file_size = 8 + self.d * self.nb * np.dtype(self.dtype).itemsize
outfile = outfile + '.crop_nb_%d' % self.nb
if os.path.exists(outfile):
print("file %s already exists" % outfile)
return
download(sourceurl, outfile, max_size=file_size)
# then overwrite the header...
header = np.memmap(outfile, shape=2, dtype='uint32', mode="r+")
assert header[0] == 10**9
assert header[1] == self.d
header[0] = self.nb
def get_dataset_fn(self):
fn = os.path.join(self.basedir, self.ds_fn)
if os.path.exists(fn):
return fn
if self.nb != 10**9:
fn += '.crop_nb_%d' % self.nb
return fn
else:
raise RuntimeError("file not found")
def get_dataset_iterator(self, bs=512, split=(1,0)):
nsplit, rank = split
i0, i1 = self.nb * rank // nsplit, self.nb * (rank + 1) // nsplit
filename = self.get_dataset_fn()
x = xbin_mmap(filename, dtype=self.dtype, maxn=self.nb)
assert x.shape == (self.nb, self.d)
for j0 in range(i0, i1, bs):
j1 = min(j0 + bs, i1)
yield sanitize(x[j0:j1])
def search_type(self):
return "knn"
def get_groundtruth(self, k=None):
assert self.gt_fn is not None
fn = self.gt_fn.split("/")[-1] # in case it's a URL
assert self.search_type() == "knn"
I, D = knn_result_read(os.path.join(self.basedir, fn))
assert I.shape[0] == self.nq
if k is not None:
assert k <= 100
I = I[:, :k]
D = D[:, :k]
return I, D
def get_dataset(self):
assert self.nb <= 10**7, "dataset too large, use iterator"
return sanitize(next(self.get_dataset_iterator(bs=self.nb)))
def get_queries(self):
filename = os.path.join(self.basedir, self.qs_fn)
x = xbin_mmap(filename, dtype=self.dtype)
assert x.shape == (self.nq, self.d)
return sanitize(x)
subset_url = "https://dl.fbaipublicfiles.com/billion-scale-ann-benchmarks/"
class SSNPPDataset(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
# assert nb_M in (10, 1000)
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 256
self.nq = 100000
self.dtype = "uint8"
self.ds_fn = "FB_ssnpp_database.u8bin"
self.qs_fn = "FB_ssnpp_public_queries.u8bin"
self.gt_fn = (
"FB_ssnpp_public_queries_1B_GT.rangeres" if self.nb_M == 1000 else
subset_url + "GT_100M/ssnpp-100M" if self.nb_M == 100 else
subset_url + "GT_10M/ssnpp-10M" if self.nb_M == 10 else
None
)
self.base_url = "https://dl.fbaipublicfiles.com/billion-scale-ann-benchmarks/"
self.basedir = os.path.join(BASEDIR, "FB_ssnpp")
def search_type(self):
return "range"
def default_count(self):
return 60000
def distance(self):
return "euclidean"
def get_groundtruth(self, k=None):
""" override the ground-truth function as this is the only range search dataset """
assert self.gt_fn is not None
fn = self.gt_fn.split("/")[-1] # in case it's a URL
return range_result_read(os.path.join(self.basedir, fn))
class BigANNDataset(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 128
self.nq = 10000
self.dtype = "uint8"
self.ds_fn = "base.1B.u8bin"
self.qs_fn = "query.public.10K.u8bin"
self.gt_fn = (
"GT.public.1B.ibin" if self.nb_M == 1000 else
subset_url + "GT_100M/bigann-100M" if self.nb_M == 100 else
subset_url + "GT_10M/bigann-10M" if self.nb_M == 10 else
None
)
# self.gt_fn = "https://comp21storage.blob.core.windows.net/publiccontainer/comp21/bigann/public_query_gt100.bin" if self.nb == 10**9 else None
self.base_url = "https://dl.fbaipublicfiles.com/billion-scale-ann-benchmarks/bigann/"
self.basedir = os.path.join(BASEDIR, "bigann")
def distance(self):
return "euclidean"
class Deep1BDataset(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 96
self.nq = 10000
self.dtype = "float32"
self.ds_fn = "base.1B.fbin"
self.qs_fn = "query.public.10K.fbin"
self.gt_fn = (
"https://storage.yandexcloud.net/yandex-research/ann-datasets/deep_new_groundtruth.public.10K.bin" if self.nb_M == 1000 else
subset_url + "GT_100M/deep-100M" if self.nb_M == 100 else
subset_url + "GT_10M/deep-10M" if self.nb_M == 10 else
None
)
self.base_url = "https://storage.yandexcloud.net/yandex-research/ann-datasets/DEEP/"
self.basedir = os.path.join(BASEDIR, "deep1b")
def distance(self):
return "euclidean"
class Text2Image1B(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 200
self.nq = 100000
self.dtype = "float32"
self.ds_fn = "base.1B.fbin"
self.qs_fn = "query.public.100K.fbin"
self.gt_fn = (
"https://storage.yandexcloud.net/yandex-research/ann-datasets/t2i_new_groundtruth.public.100K.bin" if self.nb_M == 1000 else
subset_url + "GT_100M/text2image-100M" if self.nb_M == 100 else
subset_url + "GT_10M/text2image-10M" if self.nb_M == 10 else
None
)
self.base_url = "https://storage.yandexcloud.net/yandex-research/ann-datasets/T2I/"
self.basedir = os.path.join(BASEDIR, "text2image1B")
def distance(self):
return "ip"
def get_query_train(self, maxn=10**6):
xq_train = np.memmap(
BASEDIR + "/text2image1B/query.learn.50M.fbin", offset=8,
dtype='float32', shape=(maxn, 200), mode='r')
return np.array(xq_train)
class MSTuringANNS(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 100
self.nq = 100000
self.dtype = "float32"
self.ds_fn = "base1b.fbin"
self.qs_fn = "query100K.fbin"
self.gt_fn = (
"query_gt100.bin" if self.nb_M == 1000 else
subset_url + "GT_100M/msturing-100M" if self.nb_M == 100 else
subset_url + "GT_10M/msturing-10M" if self.nb_M == 10 else
None
)
self.base_url = "https://comp21storage.blob.core.windows.net/publiccontainer/comp21/MSFT-TURING-ANNS/"
self.basedir = os.path.join(BASEDIR, "MSTuringANNS")
def distance(self):
return "euclidean"
class MSSPACEV1B(DatasetCompetitionFormat):
def __init__(self, nb_M=1000):
self.nb_M = nb_M
self.nb = 10**6 * nb_M
self.d = 100
self.nq = 29316
self.dtype = "int8"
self.ds_fn = "spacev1b_base.i8bin"
self.qs_fn = "query.i8bin"
self.gt_fn = (
"public_query_gt100.bin" if self.nb_M == 1000 else
subset_url + "GT_100M/msspacev-100M" if self.nb_M == 100 else
subset_url + "GT_10M/msspacev-10M" if self.nb_M == 10 else
None
)
self.base_url = "https://comp21storage.blob.core.windows.net/publiccontainer/comp21/spacev1b/"
self.basedir = os.path.join(BASEDIR, "MSSPACEV1B")
def distance(self):
return "euclidean"
class RandomRangeDS(DatasetCompetitionFormat):
def __init__(self, nb, nq, d):
self.nb = nb
self.nq = nq
self.d = d
self.dtype = 'float32'
self.ds_fn = f"data_{self.nb}_{self.d}"
self.qs_fn = f"queries_{self.nq}_{self.d}"
self.gt_fn = f"gt_{self.nb}_{self.nq}_{self.d}"
self.basedir = os.path.join(BASEDIR, f"random{self.nb}")
if not os.path.exists(self.basedir):
os.makedirs(self.basedir)
def prepare(self, skip_data=False):
import sklearn.datasets
import sklearn.model_selection
from sklearn.neighbors import NearestNeighbors
print(f"Preparing datasets with {self.nb} random points and {self.nq} queries.")
X, _ = sklearn.datasets.make_blobs(
n_samples=self.nb + self.nq, n_features=self.d,
centers=self.nq, random_state=1)
data, queries = sklearn.model_selection.train_test_split(
X, test_size=self.nq, random_state=1)
with open(os.path.join(self.basedir, self.ds_fn), "wb") as f:
np.array([self.nb, self.d], dtype='uint32').tofile(f)
data.astype('float32').tofile(f)
with open(os.path.join(self.basedir, self.qs_fn), "wb") as f:
np.array([self.nq, self.d], dtype='uint32').tofile(f)
queries.astype('float32').tofile(f)
print("Computing groundtruth")
nbrs = NearestNeighbors(n_neighbors=100, metric="euclidean", algorithm='brute').fit(data)
D, I = nbrs.kneighbors(queries)
nres = np.count_nonzero((D < math.sqrt(self.default_count())) == True, axis=1)
DD = np.zeros(nres.sum())
II = np.zeros(nres.sum(), dtype='int32')
s = 0
for i, l in enumerate(nres):
DD[s : s + l] = D[i, 0 : l]
II[s : s + l] = I[i, 0 : l]
s += l
with open(os.path.join(self.basedir, self.gt_fn), "wb") as f:
np.array([self.nq, nres.sum()], dtype='uint32').tofile(f)
nres.astype('int32').tofile(f)
II.astype('int32').tofile(f)
DD.astype('float32').tofile(f)
def get_groundtruth(self, k=None):
""" override the ground-truth function as this is the only range search dataset """
assert self.gt_fn is not None
fn = self.gt_fn.split("/")[-1] # in case it's a URL
return range_result_read(os.path.join(self.basedir, fn))
def search_type(self):
return "range"
def default_count(self):
return 49
def distance(self):
return "euclidean"
def __str__(self):
return f"RandomRange({self.nb})"
class RandomDS(DatasetCompetitionFormat):
def __init__(self, nb, nq, d):
self.nb = nb
self.nq = nq
self.d = d
self.dtype = 'float32'
self.ds_fn = f"data_{self.nb}_{self.d}"
self.qs_fn = f"queries_{self.nq}_{self.d}"
self.gt_fn = f"gt_{self.nb}_{self.nq}_{self.d}"
self.basedir = os.path.join(BASEDIR, f"random{self.nb}")
if not os.path.exists(self.basedir):
os.makedirs(self.basedir)
def prepare(self, skip_data=False):
import sklearn.datasets
import sklearn.model_selection
from sklearn.neighbors import NearestNeighbors
print(f"Preparing datasets with {self.nb} random points and {self.nq} queries.")
X, _ = sklearn.datasets.make_blobs(
n_samples=self.nb + self.nq, n_features=self.d,
centers=self.nq, random_state=1)
data, queries = sklearn.model_selection.train_test_split(
X, test_size=self.nq, random_state=1)
with open(os.path.join(self.basedir, self.ds_fn), "wb") as f:
np.array([self.nb, self.d], dtype='uint32').tofile(f)
data.astype('float32').tofile(f)
with open(os.path.join(self.basedir, self.qs_fn), "wb") as f:
np.array([self.nq, self.d], dtype='uint32').tofile(f)
queries.astype('float32').tofile(f)
print("Computing groundtruth")
nbrs = NearestNeighbors(n_neighbors=100, metric="euclidean", algorithm='brute').fit(data)
D, I = nbrs.kneighbors(queries)
with open(os.path.join(self.basedir, self.gt_fn), "wb") as f:
np.array([self.nq, 100], dtype='uint32').tofile(f)
I.astype('uint32').tofile(f)
D.astype('float32').tofile(f)
def search_type(self):
return "knn"
def distance(self):
return "euclidean"
def __str__(self):
return f"Random({self.nb})"
def default_count(self):
return 10
DATASETS = {
'bigann-1B': lambda : BigANNDataset(1000),
'bigann-100M': lambda : BigANNDataset(100),
'bigann-10M': lambda : BigANNDataset(10),
'deep-1B': lambda : Deep1BDataset(),
'deep-100M': lambda : Deep1BDataset(100),
'deep-10M': lambda : Deep1BDataset(10),
'ssnpp-1B': lambda : SSNPPDataset(1000),
'ssnpp-10M': lambda : SSNPPDataset(10),
'ssnpp-100M': lambda : SSNPPDataset(100),
'ssnpp-1M': lambda : SSNPPDataset(1),
'text2image-1B': lambda : Text2Image1B(),
'text2image-1M': lambda : Text2Image1B(1),
'text2image-10M': lambda : Text2Image1B(10),
'text2image-100M': lambda : Text2Image1B(100),
'msturing-1B': lambda : MSTuringANNS(1000),
'msturing-1M': lambda : MSTuringANNS(1),
'msturing-10M': lambda : MSTuringANNS(10),
'msturing-100M': lambda : MSTuringANNS(100),
'msspacev-1B': lambda : MSSPACEV1B(1000),
'msspacev-10M': lambda : MSSPACEV1B(10),
'msspacev-100M': lambda : MSSPACEV1B(100),
'msspacev-1M': lambda : MSSPACEV1B(1),
'random-xs': lambda : RandomDS(10000, 1000, 20),
'random-s': lambda : RandomDS(100000, 1000, 50),
'random-range-xs': lambda : RandomRangeDS(10000, 1000, 20),
'random-range-s': lambda : RandomRangeDS(100000, 1000, 50),
}