未验证 提交 a88f5b6c 编写于 作者: S shengjun.li 提交者: GitHub

Fix index size (#2941)

* add virtual IF UpdateIndexSize
Signed-off-by: Nshengjun.li <shengjun.li@zilliz.com>

* update ivf index size
Signed-off-by: Nsahuang <xiaohai.xu@zilliz.com>

* fix PQ logic
Signed-off-by: Nsahuang <xiaohai.xu@zilliz.com>

* fix index size of index hnsw, annoy and nsg
Signed-off-by: Ncmli <chengming.li@zilliz.com>

* add GetSize() interface 4 SPTAG
Signed-off-by: Ncmli <chengming.li@zilliz.com>

* fix binary ivf
Signed-off-by: Nshengjun.li <shengjun.li@zilliz.com>
Co-authored-by: Nsahuang <xiaohai.xu@zilliz.com>
Co-authored-by: Ncmli <chengming.li@zilliz.com>
上级 1806e7e4
...@@ -5,6 +5,7 @@ Please mark all change in change log and use the issue from GitHub ...@@ -5,6 +5,7 @@ Please mark all change in change log and use the issue from GitHub
# Milvus 0.10.2 (TBD) # Milvus 0.10.2 (TBD)
## Bug ## Bug
- \#2890 Fix the wrong index size
## Feature ## Feature
...@@ -16,9 +17,10 @@ Please mark all change in change log and use the issue from GitHub ...@@ -16,9 +17,10 @@ Please mark all change in change log and use the issue from GitHub
## Bug ## Bug
- \#2487 Enlarge timeout value for creating collection - \#2487 Enlarge timeout value for creating collection
- \#2487 HotFix release lock failed on NAS
- \#2557 Fix random crash of INSERT_DUPLICATE_ID case - \#2557 Fix random crash of INSERT_DUPLICATE_ID case
- \#2578 Result count doesn't match target vectors count - \#2578 Result count doesn't match target vectors count
- \#2585 IVF_PQ on GPU with using metric_type IP - \#2585 Support IVF_PQ IP on GPU
- \#2598 Fix Milvus docker image report illegal instruction - \#2598 Fix Milvus docker image report illegal instruction
- \#2617 Fix HNSW and RNSG index files size - \#2617 Fix HNSW and RNSG index files size
- \#2637 Suit the range of HNSW parameters - \#2637 Suit the range of HNSW parameters
...@@ -29,11 +31,10 @@ Please mark all change in change log and use the issue from GitHub ...@@ -29,11 +31,10 @@ Please mark all change in change log and use the issue from GitHub
- \#2739 Fix mishards start failed - \#2739 Fix mishards start failed
- \#2752 Milvus formats vectors data to double-precision and return to http client - \#2752 Milvus formats vectors data to double-precision and return to http client
- \#2767 Fix a bug of getting wrong nprobe limitation in knowhere on GPU version - \#2767 Fix a bug of getting wrong nprobe limitation in knowhere on GPU version
- \#2768 After building the index,the number of vectors increases - \#2768 After building the index, the number of vectors increases
- \#2774 Server down during loading data - \#2774 Server down during loading data
- \#2776 Fix too many data copies during creating IVF index - \#2776 Fix too many data copies during creating IVF index
- \#2813 To implemente RNSG IP - \#2813 To implemente RNSG IP
- \#2487 HotFix release lock failed on NAS
## Feature ## Feature
......
...@@ -92,7 +92,8 @@ DefaultVectorIndexFormat::read_internal(const storage::FSHandlerPtr& fs_ptr, con ...@@ -92,7 +92,8 @@ DefaultVectorIndexFormat::read_internal(const storage::FSHandlerPtr& fs_ptr, con
vec_index_factory.CreateVecIndex(knowhere::OldIndexTypeToStr(current_type), knowhere::IndexMode::MODE_CPU); vec_index_factory.CreateVecIndex(knowhere::OldIndexTypeToStr(current_type), knowhere::IndexMode::MODE_CPU);
if (index != nullptr) { if (index != nullptr) {
index->Load(load_data_list); index->Load(load_data_list);
index->SetIndexSize(length); index->UpdateIndexSize();
LOG_ENGINE_DEBUG_ << "index file size " << length << " index size " << index->IndexSize();
} else { } else {
LOG_ENGINE_ERROR_ << "Fail to create vector index: " << path; LOG_ENGINE_ERROR_ << "Fail to create vector index: " << path;
} }
......
...@@ -367,7 +367,7 @@ ExecutionEngineImpl::Serialize() { ...@@ -367,7 +367,7 @@ ExecutionEngineImpl::Serialize() {
// here we reset index size by file size, // here we reset index size by file size,
// since some index type(such as SQ8) data size become smaller after serialized // since some index type(such as SQ8) data size become smaller after serialized
index_->SetIndexSize(server::CommonUtil::GetFileSize(location_)); index_->UpdateIndexSize();
LOG_ENGINE_DEBUG_ << "Finish serialize index file: " << location_ << " size: " << index_->Size(); LOG_ENGINE_DEBUG_ << "Finish serialize index file: " << location_ << " size: " << index_->Size();
if (index_->Size() == 0) { if (index_->Size() == 0) {
......
...@@ -118,7 +118,6 @@ IVFConfAdapter::CheckSearch(Config& oricfg, const IndexType type, const IndexMod ...@@ -118,7 +118,6 @@ IVFConfAdapter::CheckSearch(Config& oricfg, const IndexType type, const IndexMod
} else { } else {
CheckIntByRange(knowhere::IndexParams::nprobe, MIN_NPROBE, MAX_NPROBE); CheckIntByRange(knowhere::IndexParams::nprobe, MIN_NPROBE, MAX_NPROBE);
} }
CheckIntByRange(knowhere::IndexParams::nprobe, MIN_NPROBE, MAX_NPROBE);
return ConfAdapter::CheckSearch(oricfg, type, mode); return ConfAdapter::CheckSearch(oricfg, type, mode);
} }
......
...@@ -162,5 +162,13 @@ IndexAnnoy::Dim() { ...@@ -162,5 +162,13 @@ IndexAnnoy::Dim() {
return index_->get_dim(); return index_->get_dim();
} }
void
IndexAnnoy::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
index_size_ = index_->cal_size();
}
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -62,6 +62,9 @@ class IndexAnnoy : public VecIndex { ...@@ -62,6 +62,9 @@ class IndexAnnoy : public VecIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
private: private:
MetricType metric_type_; MetricType metric_type_;
std::shared_ptr<AnnoyIndexInterface<int64_t, float>> index_ = nullptr; std::shared_ptr<AnnoyIndexInterface<int64_t, float>> index_ = nullptr;
......
...@@ -145,6 +145,20 @@ BinaryIVF::Dim() { ...@@ -145,6 +145,20 @@ BinaryIVF::Dim() {
return index_->d; return index_->d;
} }
void
BinaryIVF::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
auto bin_ivf_index = dynamic_cast<faiss::IndexBinaryIVF*>(index_.get());
auto nb = bin_ivf_index->invlists->compute_ntotal();
auto nlist = bin_ivf_index->nlist;
auto code_size = bin_ivf_index->code_size;
// binary ivf codes, ids and quantizer
index_size_ = nb * code_size + nb * sizeof(int64_t) + nlist * code_size;
}
void void
BinaryIVF::Train(const DatasetPtr& dataset_ptr, const Config& config) { BinaryIVF::Train(const DatasetPtr& dataset_ptr, const Config& config) {
GETTENSORWITHIDS(dataset_ptr) GETTENSORWITHIDS(dataset_ptr)
......
...@@ -73,6 +73,9 @@ class BinaryIVF : public VecIndex, public FaissBaseBinaryIndex { ...@@ -73,6 +73,9 @@ class BinaryIVF : public VecIndex, public FaissBaseBinaryIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
#if 0 #if 0
DatasetPtr DatasetPtr
GetVectorById(const DatasetPtr& dataset_ptr, const Config& config); GetVectorById(const DatasetPtr& dataset_ptr, const Config& config);
......
...@@ -205,5 +205,13 @@ IndexHNSW::Dim() { ...@@ -205,5 +205,13 @@ IndexHNSW::Dim() {
return (*(size_t*)index_->dist_func_param_); return (*(size_t*)index_->dist_func_param_);
} }
void
IndexHNSW::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
index_size_ = index_->cal_size();
}
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -54,6 +54,9 @@ class IndexHNSW : public VecIndex { ...@@ -54,6 +54,9 @@ class IndexHNSW : public VecIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
private: private:
bool normalize = false; bool normalize = false;
std::mutex mutex_; std::mutex mutex_;
......
...@@ -239,6 +239,19 @@ IVF::Seal() { ...@@ -239,6 +239,19 @@ IVF::Seal() {
SealImpl(); SealImpl();
} }
void
IVF::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
auto ivf_index = dynamic_cast<faiss::IndexIVFFlat*>(index_.get());
auto nb = ivf_index->invlists->compute_ntotal();
auto nlist = ivf_index->nlist;
auto code_size = ivf_index->code_size;
// ivf codes, ivf ids and quantizer
index_size_ = nb * code_size + nb * sizeof(int64_t) + nlist * code_size;
}
VecIndexPtr VecIndexPtr
IVF::CopyCpuToGpu(const int64_t device_id, const Config& config) { IVF::CopyCpuToGpu(const int64_t device_id, const Config& config) {
#ifdef MILVUS_GPU_VERSION #ifdef MILVUS_GPU_VERSION
......
...@@ -64,6 +64,9 @@ class IVF : public VecIndex, public FaissBaseIndex { ...@@ -64,6 +64,9 @@ class IVF : public VecIndex, public FaissBaseIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
#if 0 #if 0
DatasetPtr DatasetPtr
GetVectorById(const DatasetPtr& dataset, const Config& config) override; GetVectorById(const DatasetPtr& dataset, const Config& config) override;
......
...@@ -73,5 +73,28 @@ IVFPQ::GenParams(const Config& config) { ...@@ -73,5 +73,28 @@ IVFPQ::GenParams(const Config& config) {
return params; return params;
} }
void
IVFPQ::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
auto ivfpq_index = dynamic_cast<faiss::IndexIVFPQ*>(index_.get());
auto nb = ivfpq_index->invlists->compute_ntotal();
auto code_size = ivfpq_index->code_size;
auto pq = ivfpq_index->pq;
auto nlist = ivfpq_index->nlist;
auto d = ivfpq_index->d;
// ivf codes, ivf ids and quantizer
auto capacity = nb * code_size + nb * sizeof(int64_t) + nlist * d * sizeof(float);
auto centroid_table = pq.M * pq.ksub * pq.dsub * sizeof(float);
auto precomputed_table = nlist * pq.M * pq.ksub * sizeof(float);
if (precomputed_table > ivfpq_index->precomputed_table_max_bytes) {
// will not precompute table
precomputed_table = 0;
}
index_size_ = capacity + centroid_table + precomputed_table;
}
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -35,6 +35,9 @@ class IVFPQ : public IVF { ...@@ -35,6 +35,9 @@ class IVFPQ : public IVF {
VecIndexPtr VecIndexPtr
CopyCpuToGpu(const int64_t, const Config&) override; CopyCpuToGpu(const int64_t, const Config&) override;
void
UpdateIndexSize() override;
protected: protected:
std::shared_ptr<faiss::IVFSearchParameters> std::shared_ptr<faiss::IVFSearchParameters>
GenParams(const Config& config) override; GenParams(const Config& config) override;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <faiss/gpu/GpuAutoTune.h> #include <faiss/gpu/GpuAutoTune.h>
#include <faiss/gpu/GpuCloner.h> #include <faiss/gpu/GpuCloner.h>
#endif #endif
#include <faiss/IndexScalarQuantizer.h>
#include <faiss/clone_index.h> #include <faiss/clone_index.h>
#include <faiss/index_factory.h> #include <faiss/index_factory.h>
...@@ -62,5 +63,19 @@ IVFSQ::CopyCpuToGpu(const int64_t device_id, const Config& config) { ...@@ -62,5 +63,19 @@ IVFSQ::CopyCpuToGpu(const int64_t device_id, const Config& config) {
#endif #endif
} }
void
IVFSQ::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
auto ivfsq_index = dynamic_cast<faiss::IndexIVFScalarQuantizer*>(index_.get());
auto nb = ivfsq_index->invlists->compute_ntotal();
auto code_size = ivfsq_index->code_size;
auto nlist = ivfsq_index->nlist;
auto d = ivfsq_index->d;
// ivf codes, ivf ids, sq trained vectors and quantizer
index_size_ = nb * code_size + nb * sizeof(int64_t) + 2 * d * sizeof(float) + nlist * d * sizeof(float);
}
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -34,6 +34,9 @@ class IVFSQ : public IVF { ...@@ -34,6 +34,9 @@ class IVFSQ : public IVF {
VecIndexPtr VecIndexPtr
CopyCpuToGpu(const int64_t, const Config&) override; CopyCpuToGpu(const int64_t, const Config&) override;
void
UpdateIndexSize() override;
}; };
using IVFSQPtr = std::shared_ptr<IVFSQ>; using IVFSQPtr = std::shared_ptr<IVFSQ>;
......
...@@ -172,5 +172,13 @@ NSG::Dim() { ...@@ -172,5 +172,13 @@ NSG::Dim() {
return index_->dimension; return index_->dimension;
} }
void
NSG::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
index_size_ = index_->GetSize();
}
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -67,6 +67,9 @@ class NSG : public VecIndex { ...@@ -67,6 +67,9 @@ class NSG : public VecIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
private: private:
std::mutex mutex_; std::mutex mutex_;
int64_t gpu_; int64_t gpu_;
......
...@@ -214,6 +214,14 @@ CPUSPTAGRNG::Dim() { ...@@ -214,6 +214,14 @@ CPUSPTAGRNG::Dim() {
return index_ptr_->GetFeatureDim(); return index_ptr_->GetFeatureDim();
} }
void
CPUSPTAGRNG::UpdateIndexSize() {
if (!index_ptr_) {
KNOWHERE_THROW_MSG("index not initialize");
}
index_size_ = index_ptr_->GetIndexSize();
}
// void // void
// CPUSPTAGRNG::Add(const DatasetPtr& origin, const Config& add_config) { // CPUSPTAGRNG::Add(const DatasetPtr& origin, const Config& add_config) {
// SetParameters(add_config); // SetParameters(add_config);
......
...@@ -60,6 +60,9 @@ class CPUSPTAGRNG : public VecIndex { ...@@ -60,6 +60,9 @@ class CPUSPTAGRNG : public VecIndex {
int64_t int64_t
Dim() override; Dim() override;
void
UpdateIndexSize() override;
private: private:
void void
SetParameters(const Config& config); SetParameters(const Config& config);
......
...@@ -129,6 +129,10 @@ class VecIndex : public Index { ...@@ -129,6 +129,10 @@ class VecIndex : public Index {
index_size_ = size; index_size_ = size;
} }
virtual void
UpdateIndexSize() {
}
int64_t int64_t
Size() override { Size() override {
return BlacklistSize() + UidsSize() + IndexSize(); return BlacklistSize() + UidsSize() + IndexSize();
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License // or implied. See the License for the specific language governing permissions and limitations under the License
#include <faiss/IndexSQHybrid.h>
#include <faiss/gpu/GpuCloner.h> #include <faiss/gpu/GpuCloner.h>
#include <faiss/gpu/GpuIndexIVF.h> #include <faiss/gpu/GpuIndexIVF.h>
#include <faiss/index_factory.h> #include <faiss/index_factory.h>
...@@ -261,6 +262,20 @@ IVFSQHybrid::QueryImpl(int64_t n, const float* data, int64_t k, float* distances ...@@ -261,6 +262,20 @@ IVFSQHybrid::QueryImpl(int64_t n, const float* data, int64_t k, float* distances
} }
} }
void
IVFSQHybrid::UpdateIndexSize() {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}
auto ivfsqh_index = dynamic_cast<faiss::IndexIVFSQHybrid*>(index_.get());
auto nb = ivfsqh_index->invlists->compute_ntotal();
auto code_size = ivfsqh_index->code_size;
auto nlist = ivfsqh_index->nlist;
auto d = ivfsqh_index->d;
// ivf codes, ivf ids, sq trained vectors and quantizer
index_size_ = nb * code_size + nb * sizeof(int64_t) + 2 * d * sizeof(float) + nlist * d * sizeof(float);
}
FaissIVFQuantizer::~FaissIVFQuantizer() { FaissIVFQuantizer::~FaissIVFQuantizer() {
if (quantizer != nullptr) { if (quantizer != nullptr) {
delete quantizer; delete quantizer;
......
...@@ -77,6 +77,9 @@ class IVFSQHybrid : public GPUIVFSQ { ...@@ -77,6 +77,9 @@ class IVFSQHybrid : public GPUIVFSQ {
void void
UnsetQuantizer(); UnsetQuantizer();
void
UpdateIndexSize() override;
protected: protected:
BinarySet BinarySet
SerializeImpl(const IndexType&) override; SerializeImpl(const IndexType&) override;
......
...@@ -872,6 +872,22 @@ NsgIndex::SetKnnGraph(Graph& g) { ...@@ -872,6 +872,22 @@ NsgIndex::SetKnnGraph(Graph& g) {
knng = std::move(g); knng = std::move(g);
} }
int64_t
NsgIndex::GetSize() {
int64_t ret = 0;
ret += sizeof(*this);
ret += ntotal * dimension * sizeof(float);
ret += ntotal * sizeof(int64_t);
ret += sizeof(*distance_);
for (auto i = 0; i < nsg.size(); ++i) {
ret += nsg[i].size() * sizeof(node_t);
}
for (auto i = 0; i < knng.size(); ++i) {
ret += knng[i].size() * sizeof(node_t);
}
return ret;
}
} // namespace impl } // namespace impl
} // namespace knowhere } // namespace knowhere
} // namespace milvus } // namespace milvus
...@@ -86,6 +86,9 @@ class NsgIndex { ...@@ -86,6 +86,9 @@ class NsgIndex {
Search(const float* query, const unsigned& nq, const unsigned& dim, const unsigned& k, float* dist, int64_t* ids, Search(const float* query, const unsigned& nq, const unsigned& dim, const unsigned& k, float* dist, int64_t* ids,
SearchParams& params, faiss::ConcurrentBitsetPtr bitset = nullptr); SearchParams& params, faiss::ConcurrentBitsetPtr bitset = nullptr);
int64_t
GetSize();
// Not support yet. // Not support yet.
// virtual void Add() = 0; // virtual void Add() = 0;
// virtual void Add_with_ids() = 0; // virtual void Add_with_ids() = 0;
......
...@@ -79,6 +79,7 @@ namespace SPTAG ...@@ -79,6 +79,7 @@ namespace SPTAG
~Index() {} ~Index() {}
inline SizeType GetNumSamples() const { return m_pSamples.R(); } inline SizeType GetNumSamples() const { return m_pSamples.R(); }
inline SizeType GetIndexSize() const { return sizeof(*this); }
inline DimensionType GetFeatureDim() const { return m_pSamples.C(); } inline DimensionType GetFeatureDim() const { return m_pSamples.C(); }
inline int GetCurrMaxCheck() const { return m_iMaxCheck; } inline int GetCurrMaxCheck() const { return m_iMaxCheck; }
......
...@@ -79,6 +79,7 @@ namespace SPTAG ...@@ -79,6 +79,7 @@ namespace SPTAG
~Index() {} ~Index() {}
inline SizeType GetNumSamples() const { return m_pSamples.R(); } inline SizeType GetNumSamples() const { return m_pSamples.R(); }
inline SizeType GetIndexSize() const { return sizeof(*this); }
inline DimensionType GetFeatureDim() const { return m_pSamples.C(); } inline DimensionType GetFeatureDim() const { return m_pSamples.C(); }
inline int GetCurrMaxCheck() const { return m_iMaxCheck; } inline int GetCurrMaxCheck() const { return m_iMaxCheck; }
......
...@@ -37,6 +37,7 @@ public: ...@@ -37,6 +37,7 @@ public:
virtual DimensionType GetFeatureDim() const = 0; virtual DimensionType GetFeatureDim() const = 0;
virtual SizeType GetNumSamples() const = 0; virtual SizeType GetNumSamples() const = 0;
virtual SizeType GetIndexSize() const = 0;
virtual DistCalcMethod GetDistCalcMethod() const = 0; virtual DistCalcMethod GetDistCalcMethod() const = 0;
virtual IndexAlgoType GetIndexAlgoType() const = 0; virtual IndexAlgoType GetIndexAlgoType() const = 0;
......
...@@ -850,6 +850,7 @@ class AnnoyIndexInterface { ...@@ -850,6 +850,7 @@ class AnnoyIndexInterface {
virtual void get_item(S item, T* v) const = 0; virtual void get_item(S item, T* v) const = 0;
virtual void set_seed(int q) = 0; virtual void set_seed(int q) = 0;
virtual bool on_disk_build(const char* filename, char** error=nullptr) = 0; virtual bool on_disk_build(const char* filename, char** error=nullptr) = 0;
virtual int64_t cal_size() = 0;
}; };
template<typename S, typename T, typename Distance, typename Random> template<typename S, typename T, typename Distance, typename Random>
...@@ -1396,6 +1397,14 @@ protected: ...@@ -1396,6 +1397,14 @@ protected:
result->push_back(nns_dist[i].second); result->push_back(nns_dist[i].second);
} }
} }
int64_t cal_size() {
int64_t ret = 0;
ret += sizeof(*this);
ret += _roots.size() * sizeof(S);
ret += std::max(_n_nodes, _nodes_size) * _s;
return ret;
}
}; };
#endif #endif
......
...@@ -75,7 +75,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> { ...@@ -75,7 +75,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
throw std::runtime_error("Not enough memory: HierarchicalNSW failed to allocate linklists"); throw std::runtime_error("Not enough memory: HierarchicalNSW failed to allocate linklists");
size_links_per_element_ = maxM_ * sizeof(tableint) + sizeof(linklistsizeint); size_links_per_element_ = maxM_ * sizeof(tableint) + sizeof(linklistsizeint);
mult_ = 1 / log(1.0 * M_); mult_ = 1 / log(1.0 * M_);
revSize_ = 1.0 / mult_;
} }
struct CompareByFirst { struct CompareByFirst {
...@@ -113,7 +112,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> { ...@@ -113,7 +112,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
size_t maxM0_; size_t maxM0_;
size_t ef_construction_; size_t ef_construction_;
double mult_, revSize_; double mult_;
int maxlevel_; int maxlevel_;
...@@ -709,7 +708,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> { ...@@ -709,7 +708,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
if (linkLists_ == nullptr) if (linkLists_ == nullptr)
throw std::runtime_error("Not enough memory: loadIndex failed to allocate linklists"); throw std::runtime_error("Not enough memory: loadIndex failed to allocate linklists");
element_levels_ = std::vector<int>(max_elements); element_levels_ = std::vector<int>(max_elements);
revSize_ = 1.0 / mult_;
ef_ = 10; ef_ = 10;
for (size_t i = 0; i < cur_element_count; i++) { for (size_t i = 0; i < cur_element_count; i++) {
label_lookup_[getExternalLabel(i)]=i; label_lookup_[getExternalLabel(i)]=i;
...@@ -846,7 +844,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> { ...@@ -846,7 +844,6 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
if (linkLists_ == nullptr) if (linkLists_ == nullptr)
throw std::runtime_error("Not enough memory: loadIndex failed to allocate linklists"); throw std::runtime_error("Not enough memory: loadIndex failed to allocate linklists");
element_levels_ = std::vector<int>(max_elements); element_levels_ = std::vector<int>(max_elements);
revSize_ = 1.0 / mult_;
ef_ = 10; ef_ = 10;
for (size_t i = 0; i < cur_element_count; i++) { for (size_t i = 0; i < cur_element_count; i++) {
label_lookup_[getExternalLabel(i)]=i; label_lookup_[getExternalLabel(i)]=i;
...@@ -1130,6 +1127,21 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> { ...@@ -1130,6 +1127,21 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
return result; return result;
} }
int64_t cal_size() {
int64_t ret = 0;
ret += sizeof(*this);
ret += sizeof(*space);
ret += visited_list_pool_->GetSize();
ret += link_list_locks_.size() * sizeof(std::mutex);
ret += element_levels_.size() * sizeof(int);
ret += max_elements_ * size_data_per_element_;
ret += max_elements_ * sizeof(void*);
for (auto i = 0; i < max_elements_; ++ i) {
ret += linkLists_[i] ? size_links_per_element_ * element_levels_[i] : 0;
}
return ret;
}
}; };
} }
...@@ -26,6 +26,7 @@ class VisitedList { ...@@ -26,6 +26,7 @@ class VisitedList {
} }
}; };
~VisitedList() { delete[] mass; } ~VisitedList() { delete[] mass; }
}; };
...@@ -74,6 +75,12 @@ class VisitedListPool { ...@@ -74,6 +75,12 @@ class VisitedListPool {
delete rez; delete rez;
} }
}; };
int64_t GetSize() {
auto visit_list_size = sizeof(VisitedList) + numelements * sizeof(vl_type);
auto pool_size = pool.size() * (sizeof(VisitedList *) + visit_list_size);
return pool_size + sizeof(*this);
}
}; };
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册