Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ivf_iterator will copy query vectors #901

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/index/ivf/ivf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,9 @@ class IvfIndexNode : public IndexNode {
// TODO: If SCANN support Iterator, raw_distance() function should be override.
class iterator : public IndexIterator {
public:
iterator(const IndexType* index, const float* query_data, std::unique_ptr<float[]>&& copied_norm_query,
const BitsetView& bitset, size_t nprobe, bool larger_is_closer, const float refine_ratio = 0.5f)
: IndexIterator(larger_is_closer, refine_ratio),
index_(index),
copied_norm_query_(std::move(copied_norm_query)) {
if (copied_norm_query_ != nullptr) {
query_data = copied_norm_query_.get();
}

iterator(const IndexType* index, std::unique_ptr<float[]>&& copied_query, const BitsetView& bitset,
size_t nprobe, bool larger_is_closer, const float refine_ratio = 0.5f)
: IndexIterator(larger_is_closer, refine_ratio), index_(index), copied_query_(std::move(copied_query)) {
if (!bitset.empty()) {
bw_idselector_ = std::make_unique<BitsetViewIDSelector>(bitset);
ivf_search_params_.sel = bw_idselector_.get();
Expand All @@ -306,7 +300,7 @@ class IvfIndexNode : public IndexNode {
ivf_search_params_.nprobe = nprobe;
ivf_search_params_.max_codes = 0;

workspace_ = index_->getIteratorWorkspace(query_data, &ivf_search_params_);
workspace_ = index_->getIteratorWorkspace(copied_query_.get(), &ivf_search_params_);
}

protected:
Expand All @@ -320,7 +314,7 @@ class IvfIndexNode : public IndexNode {
private:
const IndexType* index_ = nullptr;
std::unique_ptr<faiss::IVFIteratorWorkspace> workspace_ = nullptr;
std::unique_ptr<float[]> copied_norm_query_ = nullptr;
std::unique_ptr<float[]> copied_query_ = nullptr;
std::unique_ptr<BitsetViewIDSelector> bw_idselector_ = nullptr;
faiss::IVFSearchParameters ivf_search_params_;
};
Expand Down Expand Up @@ -926,14 +920,18 @@ IvfIndexNode<DataType, IndexType>::AnnIterator(const DataSetPtr dataset, std::un
for (int i = 0; i < rows; ++i) {
futs.emplace_back(search_pool_->push([&, index = i] {
auto cur_query = (const float*)data + index * dim;
std::unique_ptr<float[]> copied_norm_query = nullptr;
// if cosine, need normalize
std::unique_ptr<float[]> copied_query = nullptr;
if (is_cosine) {
copied_norm_query = CopyAndNormalizeVecs(cur_query, 1, dim);
copied_query = CopyAndNormalizeVecs(cur_query, 1, dim);
} else {
copied_query = std::make_unique<float[]>(dim);
std::copy_n(cur_query, dim, copied_query.get());
}

// the iterator only own the copied_norm_query.
auto it = std::make_shared<iterator>(index_.get(), cur_query, std::move(copied_norm_query), bitset,
nprobe, larger_is_closer, iterator_refine_ratio);
// iterator only own the copied_query.
auto it = std::make_shared<iterator>(index_.get(), std::move(copied_query), bitset, nprobe,
larger_is_closer, iterator_refine_ratio);
it->initialize();
vec[index] = it;
}));
Expand Down
Loading