Skip to content

Commit

Permalink
group SWIG tests into one file (#3807)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #3807

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
  • Loading branch information
mdouze authored and facebook-github-bot committed Aug 30, 2024
1 parent 95e0a66 commit d669c16
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 100 deletions.
4 changes: 3 additions & 1 deletion faiss/python/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from packaging.version import Version
import platform
import subprocess
import logging
import os


def Version(v):
return [int(x) for x in v.split('.')]

def supported_instruction_sets():
"""
Returns the set of supported CPU features, see
Expand Down
13 changes: 8 additions & 5 deletions faiss/python/swigfaiss.swig
Original file line number Diff line number Diff line change
Expand Up @@ -1245,10 +1245,13 @@ void * cast_integer_to_void_ptr (int64_t x) {
%}

%inline %{
void wait() {
// in gdb, use return to get out of this function
for(int i = 0; i == 0; i += 0);
}
%}

// the SWIG version is a 6-digit hex string, eg. version 3.2.1 is encoded as
// 0x030201
uint64_t swig_version() {
return SWIG_VERSION;
}

%}

// End of file...
76 changes: 0 additions & 76 deletions tests/test_build_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,82 +418,6 @@ def test_IP(self):
dis[i], np.dot(x[ix[i]], y[iy[i]]))


class TestSWIGWrap(unittest.TestCase):
""" various regressions with the SWIG wrapper """

def test_size_t_ptr(self):
# issue 1064
index = faiss.IndexHNSWFlat(10, 32)

hnsw = index.hnsw
index.add(np.random.rand(100, 10).astype('float32'))
be = np.empty(2, 'uint64')
hnsw.neighbor_range(23, 0, faiss.swig_ptr(be), faiss.swig_ptr(be[1:]))

def test_id_map_at(self):
# issue 1020
n_features = 100
feature_dims = 10

features = np.random.random((n_features, feature_dims)).astype(np.float32)
idx = np.arange(n_features).astype(np.int64)

index = faiss.IndexFlatL2(feature_dims)
index = faiss.IndexIDMap2(index)
index.add_with_ids(features, idx)

[index.id_map.at(int(i)) for i in range(index.ntotal)]

def test_downcast_Refine(self):

index = faiss.IndexRefineFlat(
faiss.IndexScalarQuantizer(10, faiss.ScalarQuantizer.QT_8bit)
)

# serialize and deserialize
index2 = faiss.deserialize_index(
faiss.serialize_index(index)
)

assert isinstance(index2, faiss.IndexRefineFlat)

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)
a2 = faiss.rev_swig_ptr(ptr, 12)
np.testing.assert_array_equal(a, a2)

def test_all_array_types(self):
self.do_test_array_type('float32')
self.do_test_array_type('float64')
self.do_test_array_type('int8')
self.do_test_array_type('uint8')
self.do_test_array_type('int16')
self.do_test_array_type('uint16')
self.do_test_array_type('int32')
self.do_test_array_type('uint32')
self.do_test_array_type('int64')
self.do_test_array_type('uint64')

def test_int64(self):
# see https://github.com/facebookresearch/faiss/issues/1529
v = faiss.Int64Vector()

for i in range(10):
v.push_back(i)
a = faiss.vector_to_array(v)
assert a.dtype == 'int64'
np.testing.assert_array_equal(a, np.arange(10, dtype='int64'))

# check if it works in an IDMap
idx = faiss.IndexIDMap(faiss.IndexFlatL2(32))
idx.add_with_ids(
np.random.rand(10, 32).astype('float32'),
np.random.randint(1000, size=10, dtype='int64')
)
faiss.vector_to_array(idx.id_map)


class TestNNDescentKNNG(unittest.TestCase):

Expand Down
1 change: 0 additions & 1 deletion tests/test_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# LICENSE file in the root directory of this source tree.

import unittest

import faiss


Expand Down
17 changes: 0 additions & 17 deletions tests/test_doxygen_documentation.py

This file was deleted.

105 changes: 105 additions & 0 deletions tests/test_swig_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# a few tests of the swig wrapper

import unittest
import faiss
import numpy as np


class TestSWIGWrap(unittest.TestCase):
""" various regressions with the SWIG wrapper """

def test_size_t_ptr(self):
# issue 1064
index = faiss.IndexHNSWFlat(10, 32)

hnsw = index.hnsw
index.add(np.random.rand(100, 10).astype('float32'))
be = np.empty(2, 'uint64')
hnsw.neighbor_range(23, 0, faiss.swig_ptr(be), faiss.swig_ptr(be[1:]))

def test_id_map_at(self):
# issue 1020
n_features = 100
feature_dims = 10

features = np.random.random((n_features, feature_dims)).astype(np.float32)
idx = np.arange(n_features).astype(np.int64)

index = faiss.IndexFlatL2(feature_dims)
index = faiss.IndexIDMap2(index)
index.add_with_ids(features, idx)

[index.id_map.at(int(i)) for i in range(index.ntotal)]

def test_downcast_Refine(self):

index = faiss.IndexRefineFlat(
faiss.IndexScalarQuantizer(10, faiss.ScalarQuantizer.QT_8bit)
)

# serialize and deserialize
index2 = faiss.deserialize_index(
faiss.serialize_index(index)
)

assert isinstance(index2, faiss.IndexRefineFlat)

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)
a2 = faiss.rev_swig_ptr(ptr, 12)
np.testing.assert_array_equal(a, a2)

def test_all_array_types(self):
self.do_test_array_type('float32')
self.do_test_array_type('float64')
self.do_test_array_type('int8')
self.do_test_array_type('uint8')
self.do_test_array_type('int16')
self.do_test_array_type('uint16')
self.do_test_array_type('int32')
self.do_test_array_type('uint32')
self.do_test_array_type('int64')
self.do_test_array_type('uint64')

def test_int64(self):
# see https://github.com/facebookresearch/faiss/issues/1529
v = faiss.Int64Vector()

for i in range(10):
v.push_back(i)
a = faiss.vector_to_array(v)
assert a.dtype == 'int64'
np.testing.assert_array_equal(a, np.arange(10, dtype='int64'))

# check if it works in an IDMap
idx = faiss.IndexIDMap(faiss.IndexFlatL2(32))
idx.add_with_ids(
np.random.rand(10, 32).astype('float32'),
np.random.randint(1000, size=10, dtype='int64')
)
faiss.vector_to_array(idx.id_map)

def test_asan(self):
# this test should fail with ASAN
index = faiss.IndexFlatL2(32)
index.this.own(False) # this is a mem leak, should be catched by ASAN

def test_SWIG_version(self):
self.assertLess(faiss.swig_version(), 0x050000)


@unittest.skipIf(faiss.swig_version() < 0x040000, "swig < 4 does not support Doxygen comments")
class TestDoxygen(unittest.TestCase):

def test_doxygen_comments(self):
maxheap_array = faiss.float_maxheap_array_t()

self.assertTrue("a template structure for a set of [min|max]-heaps"
in maxheap_array.__doc__)

0 comments on commit d669c16

Please sign in to comment.