forked from facebookresearch/spreadingvectors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_Zn_decoder.py
More file actions
73 lines (54 loc) · 1.49 KB
/
Copy pathbench_Zn_decoder.py
File metadata and controls
73 lines (54 loc) · 1.49 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
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the CC-by-NC license found in the
# LICENSE file in the root directory of this source tree.
#
import numpy as np
from lattices import Zn_lattice
import time
import faiss
import sys
## single-thread benchmark
faiss.omp_set_num_threads(1)
## all data is random, we are not looking at correctness here
if True:
dim = 32
r2 = 36
else:
dim = 24
r2 = 79
codec = Zn_lattice.ZnCodec(dim, r2)
# set number of queries
nb = 10**6
nq = 1000
k = 1
print("nb=%d nq=%d" % (nb, nq))
# sample queries
rs = np.random.RandomState(123)
xq = rs.randn(nq, dim).astype('float32')
print("init dim=%d r2=%d" % (dim, r2))
codes = rs.randint(1<<31, size=nb).astype('uint64')
print("code_size=%d nv=%d %.2f bits" % (
codec.code_size, codec.nv, np.log2(codec.nv)))
assert codec.code_size == 8
dis = np.empty((nq, k), dtype='float32')
labels = np.empty((nq, k), dtype='int64')
t0 = time.time()
codec.find_nn(codes, xq)
t1 = time.time()
print ("time for code_size=%d nq=%d nb=%d: %.3f s (%.3f ms/query)" % (
codec.code_size, nq, nb, t1 - t0,
(t1 - t0) * 1000 / nq))
index = faiss.IndexPQ(dim, 8, 8)
xb = rs.randn(nb, dim).astype('float32')
print("train")
index.train(xb)
print("add")
index.add(xb)
t0 = time.time()
index.search(xq, 1)
t1 = time.time()
print ("time for IndexPQ code_size=%d nq=%d nb=%d: %.3f s (%.3f ms/query)" % (
index.pq.code_size, nq, nb, t1 - t0,
(t1 - t0) * 1000 / nq))