-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip HNSWPQ sdc init with new io flag (#3250)
Summary: ## Description Related issue: #3246 When reading HNSWPQ from disk, if index ~read only~ new `IO_FLAG_PQ_SKIP_SDC_TABLE` flag is set, skip initializing the sdc_table. In addition, adds cpp test case verifying functionality and build test util header file to share creation of temporary files amongst tests. Pull Request resolved: #3250 Test Plan: buck test //faiss/tests/:test_disable_pq_sdc_tables Reviewed By: junjieqi Differential Revision: D53844075 Pulled By: mdouze fbshipit-source-id: e9a83c0e5243867edbca8f80e3b1242b38ef6a42
- Loading branch information
1 parent
943d08b
commit 12b92e9
Showing
6 changed files
with
113 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <random> | ||
|
||
#include "faiss/Index.h" | ||
#include "faiss/IndexHNSW.h" | ||
#include "faiss/index_factory.h" | ||
#include "faiss/index_io.h" | ||
#include "test_util.h" | ||
|
||
pthread_mutex_t temp_file_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
TEST(IO, TestReadHNSWPQ_whenSDCDisabledFlagPassed_thenDisableSDCTable) { | ||
Tempfilename index_filename(&temp_file_mutex, "/tmp/faiss_TestReadHNSWPQ"); | ||
int d = 32, n = 256; | ||
std::default_random_engine rng(123); | ||
std::uniform_real_distribution<float> u(0, 100); | ||
std::vector<float> vectors(n * d); | ||
for (size_t i = 0; i < n * d; i++) { | ||
vectors[i] = u(rng); | ||
} | ||
|
||
// Build the index and write it to the temp file | ||
{ | ||
std::unique_ptr<faiss::Index> index_writer( | ||
faiss::index_factory(d, "HNSW8,PQ4np", faiss::METRIC_L2)); | ||
index_writer->train(n, vectors.data()); | ||
index_writer->add(n, vectors.data()); | ||
|
||
faiss::write_index(index_writer.get(), index_filename.c_str()); | ||
} | ||
|
||
// Load index from disk. Confirm that the sdc table is equal to 0 when | ||
// disable sdc is set | ||
{ | ||
std::unique_ptr<faiss::IndexHNSWPQ> index_reader_read_write( | ||
dynamic_cast<faiss::IndexHNSWPQ*>( | ||
faiss::read_index(index_filename.c_str()))); | ||
std::unique_ptr<faiss::IndexHNSWPQ> index_reader_sdc_disabled( | ||
dynamic_cast<faiss::IndexHNSWPQ*>(faiss::read_index( | ||
index_filename.c_str(), | ||
faiss::IO_FLAG_PQ_SKIP_SDC_TABLE))); | ||
|
||
ASSERT_NE( | ||
dynamic_cast<faiss::IndexPQ*>(index_reader_read_write->storage) | ||
->pq.sdc_table.size(), | ||
0); | ||
ASSERT_EQ( | ||
dynamic_cast<faiss::IndexPQ*>( | ||
index_reader_sdc_disabled->storage) | ||
->pq.sdc_table.size(), | ||
0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
#ifndef FAISS_TEST_UTIL_H | ||
#define FAISS_TEST_UTIL_H | ||
|
||
#include <faiss/IndexIVFPQ.h> | ||
#include <unistd.h> | ||
#include <cstdlib> | ||
|
||
struct Tempfilename { | ||
pthread_mutex_t* mutex; | ||
std::string filename; | ||
|
||
Tempfilename(pthread_mutex_t* mutex, std::string filename) { | ||
this->mutex = mutex; | ||
this->filename = filename; | ||
pthread_mutex_lock(mutex); | ||
int fd = mkstemp(&filename[0]); | ||
close(fd); | ||
pthread_mutex_unlock(mutex); | ||
} | ||
|
||
~Tempfilename() { | ||
if (access(filename.c_str(), F_OK)) { | ||
unlink(filename.c_str()); | ||
} | ||
} | ||
|
||
const char* c_str() { | ||
return filename.c_str(); | ||
} | ||
}; | ||
|
||
#endif // FAISS_TEST_UTIL_H |