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

Move make offset map to load stage #134

Merged
merged 1 commit into from
Oct 9, 2023
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
17 changes: 13 additions & 4 deletions src/index/ivf/ivf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ IvfIndexNode<T>::Train(const DataSet& dataset, const Config& cfg) {
qzr = new (std::nothrow) typename QuantizerT<T>::type(dim, metric.value());
index = std::make_unique<faiss::IndexIVFFlat>(qzr, dim, nlist, metric.value(), is_cosine);
index->train(rows, (const float*)data);
index->make_direct_map(true);
}
if constexpr (std::is_same<faiss::IndexIVFFlatCC, T>::value) {
const IvfFlatCcConfig& ivf_flat_cc_cfg = static_cast<const IvfFlatCcConfig&>(cfg);
Expand All @@ -287,6 +286,7 @@ IvfIndexNode<T>::Train(const DataSet& dataset, const Config& cfg) {
index = std::make_unique<faiss::IndexIVFFlatCC>(qzr, dim, nlist, ivf_flat_cc_cfg.ssize.value(),
metric.value(), is_cosine);
index->train(rows, (const float*)data);
// ivfflat_cc has no serialize stage, make map at build stage
index->make_direct_map(true, faiss::DirectMap::ConcurrentArray);
}
if constexpr (std::is_same<faiss::IndexIVFPQ, T>::value) {
Expand All @@ -296,7 +296,6 @@ IvfIndexNode<T>::Train(const DataSet& dataset, const Config& cfg) {
qzr = new (std::nothrow) typename QuantizerT<T>::type(dim, metric.value());
index = std::make_unique<faiss::IndexIVFPQ>(qzr, dim, nlist, ivf_pq_cfg.m.value(), nbits, metric.value());
index->train(rows, (const float*)data);
index->make_direct_map(true);
}
if constexpr (std::is_same<faiss::IndexScaNN, T>::value) {
const ScannConfig& scann_cfg = static_cast<const ScannConfig&>(cfg);
Expand All @@ -320,15 +319,13 @@ IvfIndexNode<T>::Train(const DataSet& dataset, const Config& cfg) {
index = std::make_unique<faiss::IndexIVFScalarQuantizer>(qzr, dim, nlist, faiss::QuantizerType::QT_8bit,
metric.value());
index->train(rows, (const float*)data);
index->make_direct_map(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this for SQ and PQ

Copy link
Collaborator Author

@foxspy foxspy Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limited the making operation by having raw data or not

}
if constexpr (std::is_same<faiss::IndexBinaryIVF, T>::value) {
const IvfBinConfig& ivf_bin_cfg = static_cast<const IvfBinConfig&>(cfg);
auto nlist = MatchNlist(rows, ivf_bin_cfg.nlist.value());
qzr = new (std::nothrow) typename QuantizerT<T>::type(dim, metric.value());
index = std::make_unique<faiss::IndexBinaryIVF>(qzr, dim, nlist, metric.value());
index->train(rows, (const uint8_t*)data);
index->make_direct_map(true);
}
index->own_fields = true;
} catch (std::exception& e) {
Expand Down Expand Up @@ -751,6 +748,12 @@ IvfIndexNode<T>::Deserialize(const BinarySet& binset, const Config& config) {
} else {
index_.reset(static_cast<T*>(faiss::read_index(&reader)));
}
if constexpr (!std::is_same_v<T, faiss::IndexScaNN>) {
const BaseConfig& base_cfg = static_cast<const BaseConfig&>(config);
if (HasRawData(base_cfg.metric_type.value())) {
index_->make_direct_map(true);
}
}
} catch (const std::exception& e) {
LOG_KNOWHERE_WARNING_ << "faiss inner error: " << e.what();
return Status::faiss_inner_error;
Expand All @@ -773,6 +776,12 @@ IvfIndexNode<T>::DeserializeFromFile(const std::string& filename, const Config&
} else {
index_.reset(static_cast<T*>(faiss::read_index(filename.data(), io_flags)));
}
if constexpr (!std::is_same_v<T, faiss::IndexScaNN>) {
const BaseConfig& base_cfg = static_cast<const BaseConfig&>(config);
if (HasRawData(base_cfg.metric_type.value())) {
index_->make_direct_map(true);
}
}
} catch (const std::exception& e) {
LOG_KNOWHERE_WARNING_ << "faiss inner error: " << e.what();
return Status::faiss_inner_error;
Expand Down
Loading