From bf5fdc3131f3fa24bb76a09bc1d14584f97b5996 Mon Sep 17 00:00:00 2001 From: "shengjun.li" Date: Tue, 1 Dec 2020 09:36:04 +0800 Subject: [PATCH] Fix memory leak in IVF indexes (#4325) * fix memory lead in IVF indexes Signed-off-by: shengjun.li * [skip ci] modify change log Signed-off-by: shengjun.li --- CHANGELOG.md | 17 +++++++++-------- .../index/vector_index/IndexBinaryIVF.cpp | 5 +++-- .../knowhere/index/vector_index/IndexIVF.cpp | 8 +++++--- .../knowhere/index/vector_index/IndexIVFPQ.cpp | 11 ++++++----- .../knowhere/index/vector_index/IndexIVFSQ.cpp | 14 ++++++++------ 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3116826fe6..0f9211a456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,18 +4,19 @@ Please mark all change in change log and use the issue from GitHub # Milvus 0.10.4 (TBD) ## Bug -- \#3626 Fix server crash when searching with IVF_PQ on GPU. -- \#3903 The performance of IVF_SQ8H in 0.10.3 is degraded. -- \#3906 Change DeleteTask state when it is loaded to avoid server crash. -- \#4012 Milvus hangs when continually creating and dropping partitions. -- \#4075 Improve performance for create large amount of partitions -- \#4174 Search out of memory: CPU2GPU1 with index flat +- \#3626 Fix server crash when searching with IVF_PQ on GPU +- \#3903 The performance of IVF_SQ8H in 0.10.3 is degraded +- \#3906 Fix the delete task state to avoid server crash +- \#4012 Milvus hangs when continually creating and dropping partitions +- \#4174 Fix out of memory caused by too many data loaded to GPU +- \#4318 Fix memory leak in IVF indexes ## Feature -- \#3773 Support IVF_PQ to run on FPGA. +- \#3773 Support IVF_PQ to run on FPGA ## Improvement -- \#3775 Improve search performance in the case that no item deleted. +- \#3775 Improve search performance in the case that no item deleted +- \#4075 Improve performance for create large amount of partitions ## Task diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexBinaryIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexBinaryIVF.cpp index adb6332479..6ad384156d 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexBinaryIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexBinaryIVF.cpp @@ -157,8 +157,9 @@ BinaryIVF::Train(const DatasetPtr& dataset_ptr, const Config& config) { faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get()); faiss::IndexBinary* coarse_quantizer = new faiss::IndexBinaryFlat(dim, metric_type); auto index = std::make_shared(coarse_quantizer, dim, nlist, metric_type); - index->train(rows, (uint8_t*)p_data); - index->add_with_ids(rows, (uint8_t*)p_data, p_ids); + index->own_fields = true; + index->train(rows, static_cast(p_data)); + index->add_with_ids(rows, static_cast(p_data), p_ids); index_ = index; } diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp index 4beeeee458..fc78b0eafd 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp @@ -68,11 +68,13 @@ void IVF::Train(const DatasetPtr& dataset_ptr, const Config& config) { GETTENSOR(dataset_ptr) + int64_t nlist = config[IndexParams::nlist].get(); faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get()); faiss::Index* coarse_quantizer = new faiss::IndexFlat(dim, metric_type); - int64_t nlist = config[IndexParams::nlist].get(); - index_ = std::shared_ptr(new faiss::IndexIVFFlat(coarse_quantizer, dim, nlist, metric_type)); - index_->train(rows, (float*)p_data); + auto index = std::make_shared(coarse_quantizer, dim, nlist, metric_type); + index->own_fields = true; + index->train(rows, reinterpret_cast(p_data)); + index_ = index; } void diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFPQ.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFPQ.cpp index cbedac80af..cb98712e93 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFPQ.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFPQ.cpp @@ -38,11 +38,12 @@ IVFPQ::Train(const DatasetPtr& dataset_ptr, const Config& config) { faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get()); faiss::Index* coarse_quantizer = new faiss::IndexFlat(dim, metric_type); - index_ = std::shared_ptr(new faiss::IndexIVFPQ( - coarse_quantizer, dim, config[IndexParams::nlist].get(), config[IndexParams::m].get(), - config[IndexParams::nbits].get(), metric_type)); - - index_->train(rows, (float*)p_data); + auto index = std::make_shared(coarse_quantizer, dim, config[IndexParams::nlist].get(), + config[IndexParams::m].get(), + config[IndexParams::nbits].get(), metric_type); + index->own_fields = true; + index->train(rows, reinterpret_cast(p_data)); + index_ = index; } VecIndexPtr diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFSQ.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFSQ.cpp index 5ceb732c83..1484e91003 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFSQ.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVFSQ.cpp @@ -16,6 +16,7 @@ #include #include #endif +#include #include #include #include @@ -36,12 +37,13 @@ void IVFSQ::Train(const DatasetPtr& dataset_ptr, const Config& config) { GETTENSOR(dataset_ptr) - std::stringstream index_type; - index_type << "IVF" << config[IndexParams::nlist] << "," - << "SQ" << config[IndexParams::nbits]; - index_ = std::shared_ptr( - faiss::index_factory(dim, index_type.str().c_str(), GetMetricType(config[Metric::TYPE].get()))); - index_->train(rows, (float*)p_data); + faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get()); + faiss::Index* coarse_quantizer = new faiss::IndexFlat(dim, metric_type); + auto index = std::make_shared( + coarse_quantizer, dim, config[IndexParams::nlist].get(), faiss::QuantizerType::QT_8bit, metric_type); + index->own_fields = true; + index->train(rows, reinterpret_cast(p_data)); + index_ = index; } VecIndexPtr