Skip to content

Instantly share code, notes, and snippets.

@mdouze
Created August 17, 2017 09:41
Show Gist options
  • Save mdouze/c660f9bde737a57cb11cb1c715feb412 to your computer and use it in GitHub Desktop.
Save mdouze/c660f9bde737a57cb11cb1c715feb412 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python2
import numpy as np
import faiss
import time
xd = 100
yd = 1000000
np.random.seed(1234)
faiss.omp_set_num_threads(1)
print 'xd=%d yd=%d' % (xd, yd)
for d in 3, 4, 12, 36, 64:
x = faiss.rand(xd * d).reshape(xd, d)
y = faiss.rand(yd * d).reshape(yd, d)
distances = np.empty((xd, yd), dtype='float32')
t0 = time.time()
for i in xrange(xd):
faiss.fvec_inner_products_ny(faiss.swig_ptr(distances[i]),
faiss.swig_ptr(x[i]),
faiss.swig_ptr(y),
d, yd)
t1 = time.time()
# sparse verification
ntry = 100
num, denom = 0, 0
for t in range(ntry):
xi = np.random.randint(xd)
yi = np.random.randint(yd)
num += abs(distances[xi, yi] - np.dot(x[xi], y[yi]))
denom += abs(distances[xi, yi])
print 'd=%d t=%.3f s diff=%g' % (d, t1 - t0, num / denom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment