|
| 1 | +/** |
| 2 | +* Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | +* |
| 4 | +* This source code is licensed under the MIT license found in the |
| 5 | +* LICENSE file in the root directory of this source tree. |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | + |
| 10 | +#include <random> |
| 11 | + |
| 12 | +#include "faiss/Index.h" |
| 13 | +#include "faiss/IndexHNSW.h" |
| 14 | +#include "faiss/index_factory.h" |
| 15 | +#include "faiss/index_io.h" |
| 16 | +#include "test_util.h" |
| 17 | + |
| 18 | +pthread_mutex_t temp_file_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 19 | + |
| 20 | +TEST(IO, TestReadHNSWPQ_whenReadOnly_thenDisableSDCTable) { |
| 21 | + Tempfilename index_filename(&temp_file_mutex, "/tmp/faiss_TestReadHNSWPQ"); |
| 22 | + int d = 32, n = 500; |
| 23 | + std::default_random_engine rng(123); |
| 24 | + std::uniform_real_distribution<float> u(0, 100); |
| 25 | + std::vector<float> vectors(n*d); |
| 26 | + for (size_t i = 0; i < n * d; i++) { |
| 27 | + vectors[i] = u(rng); |
| 28 | + } |
| 29 | + |
| 30 | + // Build the index and write it to the temp file |
| 31 | + { |
| 32 | + std::unique_ptr<faiss::Index> index_writer( |
| 33 | + faiss::index_factory(d, "HNSW32,PQ8", faiss::METRIC_L2) |
| 34 | + ); |
| 35 | + index_writer->train(n, vectors.data()); |
| 36 | + index_writer->add(n, vectors.data()); |
| 37 | + |
| 38 | + faiss::write_index(index_writer.get(), index_filename.c_str()); |
| 39 | + } |
| 40 | + |
| 41 | + // Load index from disk. Confirm that the sdc table is equal to 0 when read |
| 42 | + // only is set |
| 43 | + { |
| 44 | + std::unique_ptr<faiss::IndexHNSWPQ> index_reader_read_write ( |
| 45 | + dynamic_cast<faiss::IndexHNSWPQ *> ( |
| 46 | + faiss::read_index(index_filename.c_str()) |
| 47 | + ) |
| 48 | + ); |
| 49 | + std::unique_ptr<faiss::IndexHNSWPQ> index_reader_read_only( |
| 50 | + dynamic_cast<faiss::IndexHNSWPQ *> ( |
| 51 | + faiss::read_index( |
| 52 | + index_filename.c_str(), |
| 53 | + faiss::IO_FLAG_READ_ONLY |
| 54 | + ) |
| 55 | + ) |
| 56 | + ); |
| 57 | + |
| 58 | + ASSERT_NE(dynamic_cast<faiss::IndexPQ *> |
| 59 | + (index_reader_read_write->storage)->pq.sdc_table.size(), 0); |
| 60 | + ASSERT_EQ(dynamic_cast<faiss::IndexPQ *> |
| 61 | + (index_reader_read_only->storage)->pq.sdc_table.size(), 0); |
| 62 | + } |
| 63 | +} |
0 commit comments