Skip to content

Commit e5c8151

Browse files
mdouzefacebook-github-bot
authored andcommitted
add skip_storage flag to HNSW
Summary: Sometimes it is not useful to serialize the storage index along with a HNSW index. This diff adds a flag that supports skipping the storage of the index. Searchign and adding to the index is not possible until a storage index is added back in. Differential Revision: D57911060
1 parent 6e7d9e0 commit e5c8151

File tree

6 files changed

+58
-20
lines changed

6 files changed

+58
-20
lines changed

faiss/IndexHNSW.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// -*- c++ -*-
9-
108
#include <faiss/IndexHNSW.h>
119

1210
#include <omp.h>
@@ -251,7 +249,8 @@ void hnsw_search(
251249
const SearchParameters* params_in) {
252250
FAISS_THROW_IF_NOT_MSG(
253251
index->storage,
254-
"Please use IndexHNSWFlat (or variants) instead of IndexHNSW directly");
252+
"No storage index, please use IndexHNSWFlat (or variants) "
253+
"instead of IndexHNSW directly");
255254
const SearchParametersHNSW* params = nullptr;
256255
const HNSW& hnsw = index->hnsw;
257256

faiss/impl/index_read.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// -*- c++ -*-
98

109
#include <faiss/index_io.h>
1110

@@ -531,7 +530,10 @@ Index* read_index(IOReader* f, int io_flags) {
531530
Index* idx = nullptr;
532531
uint32_t h;
533532
READ1(h);
534-
if (h == fourcc("IxFI") || h == fourcc("IxF2") || h == fourcc("IxFl")) {
533+
if (h == fourcc("null")) {
534+
// denotes a missing index, useful for some cases
535+
return nullptr;
536+
} else if (h == fourcc("IxFI") || h == fourcc("IxF2") || h == fourcc("IxFl")) {
535537
IndexFlat* idxf;
536538
if (h == fourcc("IxFI")) {
537539
idxf = new IndexFlatIP();
@@ -961,7 +963,7 @@ Index* read_index(IOReader* f, int io_flags) {
961963
read_index_header(idxhnsw, f);
962964
read_HNSW(&idxhnsw->hnsw, f);
963965
idxhnsw->storage = read_index(f, io_flags);
964-
idxhnsw->own_fields = true;
966+
idxhnsw->own_fields = idxhnsw->storage != nullptr;
965967
if (h == fourcc("IHNp") && !(io_flags & IO_FLAG_PQ_SKIP_SDC_TABLE)) {
966968
dynamic_cast<IndexPQ*>(idxhnsw->storage)->pq.compute_sdc_table();
967969
}

faiss/impl/index_write.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// -*- c++ -*-
9-
108
#include <faiss/index_io.h>
119

1210
#include <faiss/impl/io.h>
@@ -390,7 +388,7 @@ static void write_ivf_header(const IndexIVF* ivf, IOWriter* f) {
390388
write_direct_map(&ivf->direct_map, f);
391389
}
392390

393-
void write_index(const Index* idx, IOWriter* f) {
391+
void write_index(const Index* idx, IOWriter* f, int io_flags) {
394392
if (const IndexFlat* idxf = dynamic_cast<const IndexFlat*>(idx)) {
395393
uint32_t h =
396394
fourcc(idxf->metric_type == METRIC_INNER_PRODUCT ? "IxFI"
@@ -765,6 +763,12 @@ void write_index(const Index* idx, IOWriter* f) {
765763
WRITE1(h);
766764
write_index_header(idxhnsw, f);
767765
write_HNSW(&idxhnsw->hnsw, f);
766+
if (io_flags & IO_FLAG_SKIP_STORAGE) {
767+
uint32_t n4 = fourcc("null");
768+
WRITE1(n4);
769+
} else {
770+
write_index(idxhnsw->storage, f);
771+
}
768772
write_index(idxhnsw->storage, f);
769773
} else if (const IndexNSG* idxnsg = dynamic_cast<const IndexNSG*>(idx)) {
770774
uint32_t h = dynamic_cast<const IndexNSGFlat*>(idx) ? fourcc("INSf")
@@ -841,14 +845,14 @@ void write_index(const Index* idx, IOWriter* f) {
841845
}
842846
}
843847

844-
void write_index(const Index* idx, FILE* f) {
848+
void write_index(const Index* idx, FILE* f, int io_flags) {
845849
FileIOWriter writer(f);
846-
write_index(idx, &writer);
850+
write_index(idx, &writer, io_flags);
847851
}
848852

849-
void write_index(const Index* idx, const char* fname) {
853+
void write_index(const Index* idx, const char* fname, int io_flags) {
850854
FileIOWriter writer(fname);
851-
write_index(idx, &writer);
855+
write_index(idx, &writer, io_flags);
852856
}
853857

854858
void write_VectorTransform(const VectorTransform* vt, const char* fname) {

faiss/index_io.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// -*- c++ -*-
9-
108
// I/O code for indexes
119

1210
#ifndef FAISS_INDEX_IO_H
@@ -35,9 +33,12 @@ struct IOReader;
3533
struct IOWriter;
3634
struct InvertedLists;
3735

38-
void write_index(const Index* idx, const char* fname);
39-
void write_index(const Index* idx, FILE* f);
40-
void write_index(const Index* idx, IOWriter* writer);
36+
/// skip the storage for graph-based indexes
37+
const int IO_FLAG_SKIP_STORAGE = 1;
38+
39+
void write_index(const Index* idx, const char* fname, int io_flags = 0);
40+
void write_index(const Index* idx, FILE* f, int io_flags = 0);
41+
void write_index(const Index* idx, IOWriter* writer, int io_flags = 0);
4142

4243
void write_index_binary(const IndexBinary* idx, const char* fname);
4344
void write_index_binary(const IndexBinary* idx, FILE* f);

faiss/python/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ def range_search_with_parameters(index, x, radius, params=None, output_stats=Fal
292292
###########################################
293293

294294

295-
def serialize_index(index):
295+
def serialize_index(index, io_flags=0):
296296
""" convert an index to a numpy uint8 array """
297297
writer = VectorIOWriter()
298-
write_index(index, writer)
298+
write_index(index, writer, io_flags)
299299
return vector_to_array(writer.data)
300300

301301

tests/test_graph_based.py

+32
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,38 @@ def test_ndis_stats(self):
133133
Dhnsw, Ihnsw = index.search(self.xq, 1)
134134
self.assertGreater(stats.ndis, len(self.xq) * index.hnsw.efSearch)
135135

136+
def test_io_no_storage(self):
137+
d = self.xq.shape[1]
138+
index = faiss.IndexHNSWFlat(d, 16)
139+
index.add(self.xb)
140+
141+
Dref, Iref = index.search(self.xq, 5)
142+
143+
# test writing without storage
144+
index2 = faiss.deserialize_index(
145+
faiss.serialize_index(index, faiss.IO_FLAG_SKIP_STORAGE)
146+
)
147+
self.assertEquals(index2.storage, None)
148+
self.assertRaises(
149+
RuntimeError,
150+
index2.search, self.xb, 1)
151+
152+
# add storage afterwards
153+
index.storage = faiss.clone_index(index.storage)
154+
index.own_fields = True
155+
156+
Dnew, Inew = index.search(self.xq, 5)
157+
np.testing.assert_array_equal(Dnew, Dref)
158+
np.testing.assert_array_equal(Inew, Iref)
159+
160+
if False:
161+
# test reading without storage
162+
# not implemented because it is hard to skip over an index
163+
index3 = faiss.deserialize_index(
164+
faiss.serialize_index(index), faiss.IO_FLAG_SKIP_STORAGE
165+
)
166+
self.assertEquals(index3.storage, None)
167+
136168

137169
class TestNSG(unittest.TestCase):
138170

0 commit comments

Comments
 (0)