Skip to content

Commit

Permalink
Cleaning up more unnecessary print (facebookresearch#3455)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookresearch#3455

Code quality control by reducing the number of prints

Reviewed By: junjieqi

Differential Revision: D57502194

fbshipit-source-id: a6cd65ed4cc49590ce73d2978d41b640b5259c17
  • Loading branch information
Xiao Fu authored and facebook-github-bot committed May 17, 2024
1 parent e822a8c commit 5e452ed
Show file tree
Hide file tree
Showing 14 changed files with 0 additions and 105 deletions.
10 changes: 0 additions & 10 deletions tests/test_binary_hashindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def test_hash(self):

Lref, Dref, Iref = index_ref.range_search(xq, radius)

print("nb res: ", Lref[-1])

index = faiss.IndexBinaryHash(d, 10)
index.add(xb)
# index.display()
Expand All @@ -80,8 +78,6 @@ def test_hash(self):
self.assertTrue(snew <= set(ref))
nfound.append(Lnew[-1])
ndis.append(stats.ndis)
print('nfound=', nfound)
print('ndis=', ndis)
nfound = np.array(nfound)
self.assertTrue(nfound[-1] == Lref[-1])
self.assertTrue(np.all(nfound[1:] >= nfound[:-1]))
Expand All @@ -100,8 +96,6 @@ def test_multihash(self):

Lref, Dref, Iref = index_ref.range_search(xq, radius)

print("nb res: ", Lref[-1])

nfound = []
ndis = []

Expand All @@ -123,8 +117,6 @@ def test_multihash(self):
self.assertTrue(snew <= set(ref))
nfound.append(Lnew[-1])
ndis.append(stats.ndis)
print('nfound=', nfound)
print('ndis=', ndis)
nfound = np.array(nfound)
# self.assertTrue(nfound[-1] == Lref[-1])
self.assertTrue(np.all(nfound[1:] >= nfound[:-1]))
Expand Down Expand Up @@ -163,7 +155,6 @@ def test_hash_and_multihash(self):
# no duplicates
self.assertTrue(len(new) == len(snew))
nf += len(set(ref) & snew)
print('nfound', nh, nbit, nf)
nfound[(nh, nbit)] = nf
self.assertGreater(nfound[(nh, 4)], nfound[(nh, 7)])

Expand All @@ -175,7 +166,6 @@ def test_hash_and_multihash(self):
np.testing.assert_array_equal(Inew, I2)
np.testing.assert_array_equal(Dnew, D2)

print('nfound=', nfound)
self.assertGreater(3, abs(nfound[(0, 7)] - nfound[(1, 7)]))
self.assertGreater(nfound[(3, 7)], nfound[(1, 7)])
self.assertGreater(nfound[(5, 7)], nfound[(3, 7)])
Expand Down
15 changes: 0 additions & 15 deletions tests/test_build_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def test_l2(self):
for d in 1, 2, 4, 8, 12, 16:
x = rs.rand(d).astype('float32')
for ny in 128, 129, 130:
print("d=%d ny=%d" % (d, ny))
y = rs.rand(ny, d).astype('float32')
ref = ((x - y) ** 2).sum(1)
new = np.zeros(ny, dtype='float32')
Expand All @@ -204,7 +203,6 @@ def test_IP(self):
for d in 1, 2, 4, 8, 12, 16:
x = rs.rand(d).astype('float32')
for ny in 128, 129, 130:
print("d=%d ny=%d" % (d, ny))
y = rs.rand(ny, d).astype('float32')
ref = (x * y).sum(1)
new = np.zeros(ny, dtype='float32')
Expand All @@ -220,7 +218,6 @@ def test_0s(self):
m = rs.rand(40, 20).astype('float32')
m[5:10] = 0
comments = faiss.MatrixStats(m).comments
print(comments)
assert 'has 5 copies' in comments
assert '5 null vectors' in comments

Expand All @@ -229,31 +226,27 @@ def test_copies(self):
m = rs.rand(40, 20).astype('float32')
m[::2] = m[1::2]
comments = faiss.MatrixStats(m).comments
print(comments)
assert '20 vectors are distinct' in comments

def test_dead_dims(self):
rs = np.random.RandomState(123)
m = rs.rand(40, 20).astype('float32')
m[:, 5:10] = 0
comments = faiss.MatrixStats(m).comments
print(comments)
assert '5 dimensions are constant' in comments

def test_rogue_means(self):
rs = np.random.RandomState(123)
m = rs.rand(40, 20).astype('float32')
m[:, 5:10] += 12345
comments = faiss.MatrixStats(m).comments
print(comments)
assert '5 dimensions are too large wrt. their variance' in comments

def test_normalized(self):
rs = np.random.RandomState(123)
m = rs.rand(40, 20).astype('float32')
faiss.normalize_L2(m)
comments = faiss.MatrixStats(m).comments
print(comments)
assert 'vectors are normalized' in comments

def test_hash(self):
Expand Down Expand Up @@ -300,7 +293,6 @@ def test_8bit_equiv(self):
D, I = index.search(x[3:], 1)

# assert D[0, 0] == Dref[0, 0]
# print(D[0, 0], ((x[3] - x[2]) ** 2).sum())
assert D[0, 0] == ((x[3] - x[2]) ** 2).sum()

def test_6bit_equiv(self):
Expand All @@ -314,8 +306,6 @@ def test_6bit_equiv(self):
d, faiss.ScalarQuantizer.QT_6bit)
index.train(trainset)

print('cs=', index.code_size)

x = rs.randint(64, size=(100, d)).astype('float32')

# verify encoder / decoder
Expand All @@ -330,7 +320,6 @@ def test_6bit_equiv(self):
for i in range(20):
for j in range(10):
dis = ((y[i] - x2[I[i, j]]) ** 2).sum()
# print(dis, D[i, j])
assert abs(D[i, j] - dis) / dis < 1e-5

def test_reconstruct(self):
Expand Down Expand Up @@ -371,7 +360,6 @@ def test_randint(self):
x = faiss.randint(20000, vmax=100)
assert np.all(x >= 0) and np.all(x < 100)
c = np.bincount(x, minlength=100)
print(c)
assert c.max() - c.min() < 50 * 2

def test_rand_vector(self):
Expand Down Expand Up @@ -473,7 +461,6 @@ def do_test_array_type(self, dtype):
""" tests swig_ptr and rev_swig_ptr for this type of array """
a = np.arange(12).astype(dtype)
ptr = faiss.swig_ptr(a)
print(ptr)
a2 = faiss.rev_swig_ptr(ptr, 12)
np.testing.assert_array_equal(a, a2)

Expand Down Expand Up @@ -547,7 +534,6 @@ def subtest(self, d, K, metric):
recalls += 1
break
recall = 1.0 * recalls / (nb * K)
print('Metric: {}, knng accuracy: {}'.format(metric_names[metric], recall))
assert recall > 0.99

def test_small_nndescent(self):
Expand Down Expand Up @@ -656,7 +642,6 @@ def do_test_bucket_sort_inplace(
rows, _ = np.where(tab == b)
rows.sort()
tab2[lims[b]:lims[b + 1]].sort()
# print(rows, tab2[lims[b] : lims[b + 1]])
rows = set(rows)
self.assertEqual(rows, set(tab2[lims[b]:lims[b + 1]]))

Expand Down
7 changes: 0 additions & 7 deletions tests/test_graph_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ def subtest_add(self, build_type, thresh, metric=faiss.METRIC_L2):
Dnsg, Insg = index.search(self.xq, 1)

recalls = (Iref == Insg).sum()
print('metric: {}, nb equal: {}'.format(metrics[metric], recalls))
self.assertGreaterEqual(recalls, thresh)
self.subtest_connectivity(index, self.xb.shape[0])
self.subtest_io_and_clone(index, Dnsg, Insg)
Expand All @@ -230,7 +229,6 @@ def subtest_build(self, knn_graph, thresh, metric=faiss.METRIC_L2):
Dnsg, Insg = index.search(self.xq, 1)

recalls = (Iref == Insg).sum()
print('metric: {}, nb equal: {}'.format(metrics[metric], recalls))
self.assertGreaterEqual(recalls, thresh)
self.subtest_connectivity(index, self.xb.shape[0])

Expand Down Expand Up @@ -286,15 +284,13 @@ def test_reset(self):
index.add(self.xb)
Dnsg, Insg = index.search(self.xq, 1)
recalls = (Iref == Insg).sum()
print('metric: {}, nb equal: {}'.format(metrics[metric], recalls))
self.assertGreaterEqual(recalls, 475)
self.subtest_connectivity(index, self.xb.shape[0])

index.reset()
index.add(self.xb)
Dnsg, Insg = index.search(self.xq, 1)
recalls = (Iref == Insg).sum()
print('metric: {}, nb equal: {}'.format(metrics[metric], recalls))
self.assertGreaterEqual(recalls, 475)
self.subtest_connectivity(index, self.xb.shape[0])

Expand Down Expand Up @@ -335,7 +331,6 @@ def test_nsg_pq(self):

# test accuracy
recalls = (Iref == I).sum()
print("IndexNSGPQ", recalls)
self.assertGreaterEqual(recalls, 190) # 193

# test I/O
Expand All @@ -361,7 +356,6 @@ def test_nsg_sq(self):

# test accuracy
recalls = (Iref == I).sum()
print("IndexNSGSQ", recalls)
self.assertGreaterEqual(recalls, 405) # 411

# test I/O
Expand Down Expand Up @@ -395,7 +389,6 @@ def test_nndescentflat(self):

# test accuracy
recalls = (Iref == I).sum()
print("IndexNNDescentFlat", recalls)
self.assertGreaterEqual(recalls, 450) # 462

# do some IO tests
Expand Down
5 changes: 0 additions & 5 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ def test_4variants_ivf(self):
D, I = index.search(xq, 10)

nok[qname] = (I[:, 0] == I_ref[:, 0]).sum()
print(nok, nq)

self.assertGreaterEqual(nok['flat'], nq * 0.6)
# The tests below are a bit fragile, it happens that the
Expand Down Expand Up @@ -373,8 +372,6 @@ def test_4variants(self):
D, I = index.search(xq, 10)
nok[qname] = (I[:, 0] == I_ref[:, 0]).sum()

print(nok, nq)

self.assertGreaterEqual(nok['QT_8bit'], nq * 0.9)
self.assertGreaterEqual(nok['QT_8bit'], nok['QT_4bit'])
self.assertGreaterEqual(nok['QT_8bit'], nok['QT_8bit_uniform'])
Expand Down Expand Up @@ -442,7 +439,6 @@ def norm1(x):

recons_err = np.mean(norm1(R_flat - xb[I_flat]))

print('Reconstruction error = %.3f' % recons_err)
if eps is not None:
self.assertLessEqual(recons_err, eps)

Expand Down Expand Up @@ -638,7 +634,6 @@ def test_reconstuct_after_add(self):

# should not raise an exception
index.reconstruct(5)
print(index.ntotal)
index.reconstruct(150)


Expand Down
24 changes: 0 additions & 24 deletions tests/test_index_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def test_ivf_kmeans(self):
Dref, Iref = ivfk.search(ev.xq, 100)
ivfk.parallel_mode = 1
Dnew, Inew = ivfk.search(ev.xq, 100)
print((Iref != Inew).sum(), Iref.size)
assert (Iref != Inew).sum() < Iref.size / 5000.0
assert np.all(Dref == Dnew)

Expand Down Expand Up @@ -136,8 +135,6 @@ def test_polysemous(self):

res = ev.launch("Polysemous ht=%d" % index.polysemous_ht, index)
e_polysemous = ev.evalres(res)
print(e_baseline, e_polysemous, index.polysemous_ht)
print(stats.n_hamming_pass, stats.ncode)
# The randu dataset is difficult, so we are not too picky on
# the results. Here we assert that we have < 10 % loss when
# computing full PQ on fewer than 20% of the data.
Expand Down Expand Up @@ -248,7 +245,6 @@ def subtest(self, mt):
index.nprobe = 4 # hopefully more robust than 1
D, I = index.search(xq, 10)
ninter = faiss.eval_intersection(I, gt_I)
print("(%d, %s): %d, " % (mt, repr(qname), ninter))
assert abs(ninter - self.ref_results[(mt, qname)]) <= 10

if qname == "6bit":
Expand All @@ -264,7 +260,6 @@ def subtest(self, mt):
radius = float(D[:, -1].max())
else:
radius = float(D[:, -1].min())
# print("radius", radius)

lims, D3, I3 = index.range_search(xq, radius)
ntot = ndiff = 0
Expand All @@ -278,14 +273,11 @@ def subtest(self, mt):
Iref = set(I2[i, mask])
ndiff += len(Inew ^ Iref)
ntot += len(Iref)
# print("ndiff %d / %d" % (ndiff, ntot))
assert ndiff < ntot * 0.01

for pm in 1, 2:
# print("parallel_mode=%d" % pm)
index.parallel_mode = pm
lims4, D4, I4 = index.range_search(xq, radius)
# print("sizes", lims4[1:] - lims4[:-1])
for qno in range(len(lims) - 1):
Iref = I3[lims[qno]: lims[qno + 1]]
Inew = I4[lims4[qno]: lims4[qno + 1]]
Expand Down Expand Up @@ -485,7 +477,6 @@ def subtest(self, mt):
D, I = index.search(xq, 10)

ninter = faiss.eval_intersection(I, gt_I)
print("(%d, %s): %d, " % (mt, by_residual, ninter))

assert abs(ninter - self.ref_results[mt, by_residual]) <= 3

Expand All @@ -499,10 +490,6 @@ def subtest(self, mt):
index.polysemous_ht = 20
D, I = index.search(xq, 10)
ninter = faiss.eval_intersection(I, gt_I)
print(
"(%d, %s, %d): %d, "
% (mt, by_residual, index.polysemous_ht, ninter)
)

# polysemous behaves bizarrely on ARM
assert (
Expand All @@ -516,7 +503,6 @@ def subtest(self, mt):
radius = float(D[:, -1].max())
else:
radius = float(D[:, -1].min())
print("radius", radius)

lims, D3, I3 = index.range_search(xq, radius)
ntot = ndiff = 0
Expand All @@ -530,7 +516,6 @@ def subtest(self, mt):
Iref = set(I2[i, mask])
ndiff += len(Inew ^ Iref)
ntot += len(Iref)
print("ndiff %d / %d" % (ndiff, ntot))
assert ndiff < ntot * 0.02

def test_IVFPQ_non8bit(self):
Expand All @@ -555,7 +540,6 @@ def test_IVFPQ_non8bit(self):

D, I = index.search(xq, 10)
ninter[v] = faiss.eval_intersection(I, gt_I)
print("ninter=", ninter)
# this should be the case but we don't observe
# that... Probavly too few test points
# assert ninter['2x8'] > ninter['8x2']
Expand Down Expand Up @@ -623,9 +607,6 @@ def test_OPQ(self):
res = ev.launch("OPQ", index)
e_opq = ev.evalres(res)

print("e_pq=%s" % e_pq)
print("e_opq=%s" % e_opq)

# verify that OPQ better than PQ
for r in 1, 10, 100:
assert e_opq[r] > e_pq[r]
Expand Down Expand Up @@ -656,7 +637,6 @@ def test_OIVFPQ(self):

# verify same on OIVFPQ
for r in 1, 10, 100:
print(e_oivfpq[r], e_ivfpq[r])
assert e_oivfpq[r] >= e_ivfpq[r]


Expand Down Expand Up @@ -758,9 +738,6 @@ def test_sh(self):
ninter = faiss.eval_intersection(I, gt_I)
key = (nbit, tt, period)

print("(%d, %s, %g): %d, " % (nbit, repr(tt), period,
ninter))
print(abs(ninter - self.ref_results[key]))
assert abs(ninter - self.ref_results[key]) <= 14


Expand Down Expand Up @@ -799,7 +776,6 @@ def do_test(self, metric):
# check that with refinement, the recall@10 is the same as
# the original recall@100
recall2 = (I2 == Iref[:, :1]).sum()
# print("recalls", recall1, recall2)
self.assertEqual(recall1, recall2)

def test_IP(self):
Expand Down
1 change: 0 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def test_buf_read(self):
reader = faiss.BufferedIOReader(reader, bsz)

y = np.zeros_like(x)
print('nbytes=', y.nbytes)
reader(faiss.swig_ptr(y), y.nbytes, 1)

np.testing.assert_array_equal(x, y)
Expand Down
1 change: 0 additions & 1 deletion tests/test_ivflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def test_range_search_with_parameters(self):

Dpre, _ = index.search(xq, 15)
radius = float(np.median(Dpre[:, -1]))
print("Radius=", radius)
stats = faiss.cvar.indexIVF_stats
stats.reset()
Lref, Dref, Iref = index.range_search(xq, radius)
Expand Down
Loading

0 comments on commit 5e452ed

Please sign in to comment.