Skip to content

Commit f395126

Browse files
mdouzefacebook-github-bot
authored andcommitted
group SWIG tests into one file
Summary: This groups tests related to SWIG into a single file removes a dep on platform.Version that does not exist on some envs Differential Revision: D61959750
1 parent 145e93d commit f395126

5 files changed

+111
-99
lines changed

faiss/python/loader.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
55

6-
from packaging.version import Version
76
import platform
87
import subprocess
98
import logging
109
import os
1110

1211

12+
def Version(v):
13+
return [int(x) for x in v.split('.')]
14+
1315
def supported_instruction_sets():
1416
"""
1517
Returns the set of supported CPU features, see

faiss/python/swigfaiss.swig

+6-5
Original file line numberDiff line numberDiff line change
@@ -1245,10 +1245,11 @@ void * cast_integer_to_void_ptr (int64_t x) {
12451245
%}
12461246

12471247
%inline %{
1248-
void wait() {
1249-
// in gdb, use return to get out of this function
1250-
for(int i = 0; i == 0; i += 0);
1251-
}
1252-
%}
1248+
1249+
uint64_t swig_version() {
1250+
return SWIG_VERSION;
1251+
}
1252+
1253+
%}
12531254

12541255
// End of file...

tests/test_build_blocks.py

-76
Original file line numberDiff line numberDiff line change
@@ -418,82 +418,6 @@ def test_IP(self):
418418
dis[i], np.dot(x[ix[i]], y[iy[i]]))
419419

420420

421-
class TestSWIGWrap(unittest.TestCase):
422-
""" various regressions with the SWIG wrapper """
423-
424-
def test_size_t_ptr(self):
425-
# issue 1064
426-
index = faiss.IndexHNSWFlat(10, 32)
427-
428-
hnsw = index.hnsw
429-
index.add(np.random.rand(100, 10).astype('float32'))
430-
be = np.empty(2, 'uint64')
431-
hnsw.neighbor_range(23, 0, faiss.swig_ptr(be), faiss.swig_ptr(be[1:]))
432-
433-
def test_id_map_at(self):
434-
# issue 1020
435-
n_features = 100
436-
feature_dims = 10
437-
438-
features = np.random.random((n_features, feature_dims)).astype(np.float32)
439-
idx = np.arange(n_features).astype(np.int64)
440-
441-
index = faiss.IndexFlatL2(feature_dims)
442-
index = faiss.IndexIDMap2(index)
443-
index.add_with_ids(features, idx)
444-
445-
[index.id_map.at(int(i)) for i in range(index.ntotal)]
446-
447-
def test_downcast_Refine(self):
448-
449-
index = faiss.IndexRefineFlat(
450-
faiss.IndexScalarQuantizer(10, faiss.ScalarQuantizer.QT_8bit)
451-
)
452-
453-
# serialize and deserialize
454-
index2 = faiss.deserialize_index(
455-
faiss.serialize_index(index)
456-
)
457-
458-
assert isinstance(index2, faiss.IndexRefineFlat)
459-
460-
def do_test_array_type(self, dtype):
461-
""" tests swig_ptr and rev_swig_ptr for this type of array """
462-
a = np.arange(12).astype(dtype)
463-
ptr = faiss.swig_ptr(a)
464-
a2 = faiss.rev_swig_ptr(ptr, 12)
465-
np.testing.assert_array_equal(a, a2)
466-
467-
def test_all_array_types(self):
468-
self.do_test_array_type('float32')
469-
self.do_test_array_type('float64')
470-
self.do_test_array_type('int8')
471-
self.do_test_array_type('uint8')
472-
self.do_test_array_type('int16')
473-
self.do_test_array_type('uint16')
474-
self.do_test_array_type('int32')
475-
self.do_test_array_type('uint32')
476-
self.do_test_array_type('int64')
477-
self.do_test_array_type('uint64')
478-
479-
def test_int64(self):
480-
# see https://github.com/facebookresearch/faiss/issues/1529
481-
v = faiss.Int64Vector()
482-
483-
for i in range(10):
484-
v.push_back(i)
485-
a = faiss.vector_to_array(v)
486-
assert a.dtype == 'int64'
487-
np.testing.assert_array_equal(a, np.arange(10, dtype='int64'))
488-
489-
# check if it works in an IDMap
490-
idx = faiss.IndexIDMap(faiss.IndexFlatL2(32))
491-
idx.add_with_ids(
492-
np.random.rand(10, 32).astype('float32'),
493-
np.random.randint(1000, size=10, dtype='int64')
494-
)
495-
faiss.vector_to_array(idx.id_map)
496-
497421

498422
class TestNNDescentKNNG(unittest.TestCase):
499423

tests/test_doxygen_documentation.py

-17
This file was deleted.

tests/test_swig_wrapper.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
# a few tests of the swig wrapper
7+
8+
import unittest
9+
import faiss
10+
import numpy as np
11+
12+
13+
class TestSWIGWrap(unittest.TestCase):
14+
""" various regressions with the SWIG wrapper """
15+
16+
def test_size_t_ptr(self):
17+
# issue 1064
18+
index = faiss.IndexHNSWFlat(10, 32)
19+
20+
hnsw = index.hnsw
21+
index.add(np.random.rand(100, 10).astype('float32'))
22+
be = np.empty(2, 'uint64')
23+
hnsw.neighbor_range(23, 0, faiss.swig_ptr(be), faiss.swig_ptr(be[1:]))
24+
25+
def test_id_map_at(self):
26+
# issue 1020
27+
n_features = 100
28+
feature_dims = 10
29+
30+
features = np.random.random((n_features, feature_dims)).astype(np.float32)
31+
idx = np.arange(n_features).astype(np.int64)
32+
33+
index = faiss.IndexFlatL2(feature_dims)
34+
index = faiss.IndexIDMap2(index)
35+
index.add_with_ids(features, idx)
36+
37+
[index.id_map.at(int(i)) for i in range(index.ntotal)]
38+
39+
def test_downcast_Refine(self):
40+
41+
index = faiss.IndexRefineFlat(
42+
faiss.IndexScalarQuantizer(10, faiss.ScalarQuantizer.QT_8bit)
43+
)
44+
45+
# serialize and deserialize
46+
index2 = faiss.deserialize_index(
47+
faiss.serialize_index(index)
48+
)
49+
50+
assert isinstance(index2, faiss.IndexRefineFlat)
51+
52+
def do_test_array_type(self, dtype):
53+
""" tests swig_ptr and rev_swig_ptr for this type of array """
54+
a = np.arange(12).astype(dtype)
55+
ptr = faiss.swig_ptr(a)
56+
a2 = faiss.rev_swig_ptr(ptr, 12)
57+
np.testing.assert_array_equal(a, a2)
58+
59+
def test_all_array_types(self):
60+
self.do_test_array_type('float32')
61+
self.do_test_array_type('float64')
62+
self.do_test_array_type('int8')
63+
self.do_test_array_type('uint8')
64+
self.do_test_array_type('int16')
65+
self.do_test_array_type('uint16')
66+
self.do_test_array_type('int32')
67+
self.do_test_array_type('uint32')
68+
self.do_test_array_type('int64')
69+
self.do_test_array_type('uint64')
70+
71+
def test_int64(self):
72+
# see https://github.com/facebookresearch/faiss/issues/1529
73+
v = faiss.Int64Vector()
74+
75+
for i in range(10):
76+
v.push_back(i)
77+
a = faiss.vector_to_array(v)
78+
assert a.dtype == 'int64'
79+
np.testing.assert_array_equal(a, np.arange(10, dtype='int64'))
80+
81+
# check if it works in an IDMap
82+
idx = faiss.IndexIDMap(faiss.IndexFlatL2(32))
83+
idx.add_with_ids(
84+
np.random.rand(10, 32).astype('float32'),
85+
np.random.randint(1000, size=10, dtype='int64')
86+
)
87+
faiss.vector_to_array(idx.id_map)
88+
89+
def test_asan(self):
90+
# this test should fail with ASAN
91+
index = faiss.IndexFlatL2(32)
92+
index.this.own(False) # this is a mem leak, should be catched by ASAN
93+
94+
95+
@unittest.skipIf(faiss.swig_version() < 0x040000, "swig < 4 does not support Doxygen comments")
96+
class TestDocumentation(unittest.TestCase):
97+
98+
def test_doxygen_comments(self):
99+
maxheap_array = faiss.float_maxheap_array_t()
100+
101+
self.assertTrue("a template structure for a set of [min|max]-heaps"
102+
in maxheap_array.__doc__)

0 commit comments

Comments
 (0)