mirror of https://github.com/milvus-io/milvus.git
Merge branch 'branch-0.4.0' into 'branch-0.4.0'
refine code See merge request megasearch/milvus!402 Former-commit-id: 1b896db5f16336e00d848fe600709b4a20bf0179pull/191/head
commit
59ff9b57f3
|
@ -45,5 +45,3 @@ engine_config:
|
|||
use_blas_threshold: 20
|
||||
metric_type: L2 # compare vectors by euclidean distance(L2) or inner product(IP), optional: L2 or IP
|
||||
omp_thread_num: 0 # how many compute threads be used by engine, 0 means use all cpu core to compute
|
||||
use_hybrid_index: false # use GPU/CPU hybrid index
|
||||
hybrid_index_gpu: 0 # hybrid index gpu device id
|
||||
|
|
|
@ -46,7 +46,7 @@ DataObjPtr CacheMgr::GetItem(const std::string& key) {
|
|||
return cache_->get(key);
|
||||
}
|
||||
|
||||
engine::Index_ptr CacheMgr::GetIndex(const std::string& key) {
|
||||
engine::VecIndexPtr CacheMgr::GetIndex(const std::string& key) {
|
||||
DataObjPtr obj = GetItem(key);
|
||||
if(obj != nullptr) {
|
||||
return obj->data();
|
||||
|
@ -65,7 +65,7 @@ void CacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) {
|
|||
server::Metrics::GetInstance().CacheAccessTotalIncrement();
|
||||
}
|
||||
|
||||
void CacheMgr::InsertItem(const std::string& key, const engine::Index_ptr& index) {
|
||||
void CacheMgr::InsertItem(const std::string& key, const engine::VecIndexPtr& index) {
|
||||
if(cache_ == nullptr) {
|
||||
SERVER_LOG_ERROR << "Cache doesn't exist";
|
||||
return;
|
||||
|
|
|
@ -19,10 +19,10 @@ public:
|
|||
virtual bool ItemExists(const std::string& key);
|
||||
|
||||
virtual DataObjPtr GetItem(const std::string& key);
|
||||
virtual engine::Index_ptr GetIndex(const std::string& key);
|
||||
virtual engine::VecIndexPtr GetIndex(const std::string& key);
|
||||
|
||||
virtual void InsertItem(const std::string& key, const DataObjPtr& data);
|
||||
virtual void InsertItem(const std::string& key, const engine::Index_ptr& index);
|
||||
virtual void InsertItem(const std::string& key, const engine::VecIndexPtr& index);
|
||||
|
||||
virtual void EraseItem(const std::string& key);
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "wrapper/Index.h"
|
||||
#include "wrapper/knowhere/vec_index.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
@ -16,17 +16,17 @@ namespace cache {
|
|||
|
||||
class DataObj {
|
||||
public:
|
||||
DataObj(const engine::Index_ptr& index)
|
||||
DataObj(const engine::VecIndexPtr& index)
|
||||
: index_(index)
|
||||
{}
|
||||
|
||||
DataObj(const engine::Index_ptr& index, int64_t size)
|
||||
DataObj(const engine::VecIndexPtr& index, int64_t size)
|
||||
: index_(index),
|
||||
size_(size)
|
||||
{}
|
||||
|
||||
engine::Index_ptr data() { return index_; }
|
||||
const engine::Index_ptr& data() const { return index_; }
|
||||
engine::VecIndexPtr data() { return index_; }
|
||||
const engine::VecIndexPtr& data() const { return index_; }
|
||||
|
||||
int64_t size() const {
|
||||
if(index_ == nullptr) {
|
||||
|
@ -41,7 +41,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
engine::Index_ptr index_ = nullptr;
|
||||
engine::VecIndexPtr index_ = nullptr;
|
||||
int64_t size_ = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "FaissGpuResources.h"
|
||||
#include "map"
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
FaissGpuResources::Ptr& FaissGpuResources::GetGpuResources(int device_id) {
|
||||
static std::map<int, FaissGpuResources::Ptr> gpu_resources_map;
|
||||
auto search = gpu_resources_map.find(device_id);
|
||||
if (search != gpu_resources_map.end()) {
|
||||
return gpu_resources_map[device_id];
|
||||
} else {
|
||||
gpu_resources_map[device_id] = std::make_shared<faiss::gpu::StandardGpuResources>();
|
||||
return gpu_resources_map[device_id];
|
||||
}
|
||||
}
|
||||
|
||||
void FaissGpuResources::SelectGpu() {
|
||||
using namespace zilliz::milvus::server;
|
||||
ServerConfig &config = ServerConfig::GetInstance();
|
||||
ConfigNode server_config = config.GetConfig(CONFIG_SERVER);
|
||||
gpu_num_ = server_config.GetInt32Value(server::CONFIG_GPU_INDEX, 0);
|
||||
}
|
||||
|
||||
int32_t FaissGpuResources::GetGpu() {
|
||||
return gpu_num_;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
* Proprietary and confidential.
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#include "faiss/gpu/GpuResources.h"
|
||||
#include "faiss/gpu/StandardGpuResources.h"
|
||||
|
||||
#include "server/ServerConfig.h"
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
class FaissGpuResources {
|
||||
|
||||
public:
|
||||
using Ptr = std::shared_ptr<faiss::gpu::GpuResources>;
|
||||
|
||||
static FaissGpuResources::Ptr& GetGpuResources(int device_id);
|
||||
|
||||
void SelectGpu();
|
||||
|
||||
int32_t GetGpu();
|
||||
|
||||
FaissGpuResources() : gpu_num_(0) { SelectGpu(); }
|
||||
|
||||
private:
|
||||
int32_t gpu_num_;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
// TODO: maybe support static search
|
||||
#ifdef GPU_VERSION
|
||||
|
||||
#include "faiss/gpu/GpuAutoTune.h"
|
||||
#include "faiss/gpu/StandardGpuResources.h"
|
||||
#include "faiss/gpu/utils/DeviceUtils.h"
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#include "Index.h"
|
||||
#include "faiss/index_io.h"
|
||||
#include "faiss/IndexIVF.h"
|
||||
#include "faiss/IVFlib.h"
|
||||
#include "faiss/IndexScalarQuantizer.h"
|
||||
#include "server/ServerConfig.h"
|
||||
#include "src/wrapper/FaissGpuResources.h"
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
using std::string;
|
||||
using std::unordered_map;
|
||||
using std::vector;
|
||||
|
||||
Index::Index(const std::shared_ptr<faiss::Index> &raw_index) {
|
||||
index_ = raw_index;
|
||||
dim = index_->d;
|
||||
ntotal = index_->ntotal;
|
||||
store_on_gpu = false;
|
||||
}
|
||||
|
||||
bool Index::reset() {
|
||||
try {
|
||||
index_->reset();
|
||||
ntotal = index_->ntotal;
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
// LOG(ERROR) << e.what();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Index::add_with_ids(idx_t n, const float *xdata, const long *xids) {
|
||||
try {
|
||||
index_->add_with_ids(n, xdata, xids);
|
||||
ntotal += n;
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
// LOG(ERROR) << e.what();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Index::search(idx_t n, const float *data, idx_t k, float *distances, long *labels) const {
|
||||
try {
|
||||
index_->search(n, data, k, distances, labels);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
// LOG(ERROR) << e.what();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void write_index(const Index_ptr &index, const std::string &file_name) {
|
||||
write_index(index->index_.get(), file_name.c_str());
|
||||
}
|
||||
|
||||
Index_ptr read_index(const std::string &file_name) {
|
||||
std::shared_ptr<faiss::Index> raw_index = nullptr;
|
||||
faiss::Index *cpu_index = faiss::read_index(file_name.c_str());
|
||||
|
||||
server::ServerConfig &config = server::ServerConfig::GetInstance();
|
||||
server::ConfigNode engine_config = config.GetConfig(server::CONFIG_ENGINE);
|
||||
bool use_hybrid_index_ = engine_config.GetBoolValue(server::CONFIG_USE_HYBRID_INDEX, false);
|
||||
|
||||
if (dynamic_cast<faiss::IndexIVFScalarQuantizer *>(cpu_index) != nullptr && use_hybrid_index_) {
|
||||
|
||||
int device_id = engine_config.GetInt32Value(server::CONFIG_HYBRID_INDEX_GPU, 0);
|
||||
auto gpu_resources = engine::FaissGpuResources::GetGpuResources(device_id);
|
||||
faiss::gpu::GpuClonerOptions clone_option;
|
||||
clone_option.storeInCpu = true;
|
||||
faiss::Index *gpu_index = faiss::gpu::index_cpu_to_gpu(gpu_resources.get(), device_id, cpu_index, &clone_option);
|
||||
|
||||
delete cpu_index;
|
||||
raw_index.reset(gpu_index);
|
||||
return std::make_shared<Index>(raw_index);
|
||||
} else {
|
||||
raw_index.reset(cpu_index);
|
||||
return std::make_shared<Index>(raw_index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,88 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
//#include <vector>
|
||||
//#include <string>
|
||||
//#include <unordered_map>
|
||||
//#include <memory>
|
||||
//#include <fstream>
|
||||
//
|
||||
//#include "faiss/AutoTune.h"
|
||||
//#include "faiss/index_io.h"
|
||||
//
|
||||
//#include "Operand.h"
|
||||
|
||||
#include "knowhere/vec_index.h"
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
using Index_ptr = VecIndexPtr;
|
||||
|
||||
#if 0
|
||||
//class Index;
|
||||
//using Index_ptr = std::shared_ptr<Index>;
|
||||
|
||||
class Index {
|
||||
typedef long idx_t;
|
||||
|
||||
public:
|
||||
int dim; ///< std::vector dimension
|
||||
idx_t ntotal; ///< total nb of indexed std::vectors
|
||||
bool store_on_gpu;
|
||||
|
||||
explicit Index(const std::shared_ptr<faiss::Index> &raw_index);
|
||||
|
||||
virtual bool reset();
|
||||
|
||||
/**
|
||||
* @brief Same as add, but stores xids instead of sequential ids.
|
||||
*
|
||||
* @param data input matrix, size n * d
|
||||
* @param if ids is not empty ids for the std::vectors
|
||||
*/
|
||||
virtual bool add_with_ids(idx_t n, const float *xdata, const long *xids);
|
||||
|
||||
/**
|
||||
* @brief for each query std::vector, find its k nearest neighbors in the database
|
||||
*
|
||||
* @param n queries size
|
||||
* @param data query std::vectors
|
||||
* @param k top k nearest neighbors
|
||||
* @param distances top k nearest distances
|
||||
* @param labels neighbors of the queries
|
||||
*/
|
||||
virtual bool search(idx_t n, const float *data, idx_t k, float *distances, long *labels) const;
|
||||
|
||||
//virtual bool search(idx_t n, const std::vector<float> &data, idx_t k,
|
||||
// std::vector<float> &distances, std::vector<float> &labels) const;
|
||||
|
||||
//virtual bool remove_ids(const faiss::IDSelector &sel, long &nremove, long &location);
|
||||
//virtual bool remove_ids_range(const faiss::IDSelector &sel, long &nremove);
|
||||
//virtual bool index_display();
|
||||
|
||||
virtual std::shared_ptr<faiss::Index> data() { return index_; }
|
||||
|
||||
virtual const std::shared_ptr<faiss::Index>& data() const { return index_; }
|
||||
|
||||
private:
|
||||
friend void write_index(const Index_ptr &index, const std::string &file_name);
|
||||
std::shared_ptr<faiss::Index> index_ = nullptr;
|
||||
};
|
||||
|
||||
|
||||
void write_index(const Index_ptr &index, const std::string &file_name);
|
||||
|
||||
extern Index_ptr read_index(const std::string &file_name);
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,163 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
#include "mutex"
|
||||
|
||||
|
||||
#ifdef GPU_VERSION
|
||||
#include <faiss/gpu/StandardGpuResources.h>
|
||||
#include <faiss/gpu/GpuIndexIVFFlat.h>
|
||||
#include <faiss/gpu/GpuAutoTune.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include <faiss/IndexFlat.h>
|
||||
#include <easylogging++.h>
|
||||
#include "faiss/IndexScalarQuantizer.h"
|
||||
|
||||
#include "server/ServerConfig.h"
|
||||
#include "IndexBuilder.h"
|
||||
#include "FaissGpuResources.h"
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
using std::vector;
|
||||
|
||||
static std::mutex gpu_resource;
|
||||
static std::mutex cpu_resource;
|
||||
|
||||
IndexBuilder::IndexBuilder(const Operand_ptr &opd) {
|
||||
opd_ = opd;
|
||||
|
||||
using namespace zilliz::milvus::server;
|
||||
ServerConfig &config = ServerConfig::GetInstance();
|
||||
ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
|
||||
use_hybrid_index_ = engine_config.GetBoolValue(CONFIG_USE_HYBRID_INDEX, false);
|
||||
hybrid_index_device_id_ = engine_config.GetInt32Value(server::CONFIG_HYBRID_INDEX_GPU, 0);
|
||||
}
|
||||
|
||||
// Default: build use gpu
|
||||
Index_ptr IndexBuilder::build_all(const long &nb,
|
||||
const float *xb,
|
||||
const long *ids,
|
||||
const long &nt,
|
||||
const float *xt) {
|
||||
std::shared_ptr<faiss::Index> host_index = nullptr;
|
||||
#ifdef GPU_VERSION
|
||||
{
|
||||
LOG(DEBUG) << "Build index by GPU";
|
||||
// TODO: list support index-type.
|
||||
faiss::MetricType metric_type = opd_->metric_type == "L2" ? faiss::METRIC_L2 : faiss::METRIC_INNER_PRODUCT;
|
||||
faiss::Index *ori_index = faiss::index_factory(opd_->d, opd_->get_index_type(nb).c_str(), metric_type);
|
||||
|
||||
std::lock_guard<std::mutex> lk(gpu_resource);
|
||||
|
||||
#ifdef UNITTEST_ONLY
|
||||
faiss::gpu::StandardGpuResources res;
|
||||
int device_id = 0;
|
||||
faiss::gpu::GpuClonerOptions clone_option;
|
||||
clone_option.storeInCpu = use_hybrid_index_;
|
||||
auto device_index = faiss::gpu::index_cpu_to_gpu(&res, device_id, ori_index, &clone_option);
|
||||
#else
|
||||
engine::FaissGpuResources res;
|
||||
int device_id = res.GetGpu();
|
||||
auto gpu_resources = engine::FaissGpuResources::GetGpuResources(device_id);
|
||||
faiss::gpu::GpuClonerOptions clone_option;
|
||||
clone_option.storeInCpu = use_hybrid_index_;
|
||||
auto device_index = faiss::gpu::index_cpu_to_gpu(gpu_resources.get(), device_id, ori_index, &clone_option);
|
||||
#endif
|
||||
|
||||
if (!device_index->is_trained) {
|
||||
nt == 0 || xt == nullptr ? device_index->train(nb, xb)
|
||||
: device_index->train(nt, xt);
|
||||
}
|
||||
device_index->add_with_ids(nb, xb, ids); // TODO: support with add_with_IDMAP
|
||||
|
||||
if (dynamic_cast<faiss::IndexIVFScalarQuantizer*>(ori_index) != nullptr
|
||||
&& use_hybrid_index_) {
|
||||
std::shared_ptr<faiss::Index> device_hybrid_index = nullptr;
|
||||
if (hybrid_index_device_id_ != device_id) {
|
||||
auto host_hybrid_index = faiss::gpu::index_gpu_to_cpu(device_index);
|
||||
auto hybrid_gpu_resources = engine::FaissGpuResources::GetGpuResources(hybrid_index_device_id_);
|
||||
auto another_device_index = faiss::gpu::index_cpu_to_gpu(hybrid_gpu_resources.get(),
|
||||
hybrid_index_device_id_,
|
||||
host_hybrid_index,
|
||||
&clone_option);
|
||||
device_hybrid_index.reset(another_device_index);
|
||||
delete device_index;
|
||||
delete host_hybrid_index;
|
||||
} else {
|
||||
device_hybrid_index.reset(device_index);
|
||||
}
|
||||
delete ori_index;
|
||||
return std::make_shared<Index>(device_hybrid_index);
|
||||
}
|
||||
|
||||
host_index.reset(faiss::gpu::index_gpu_to_cpu(device_index));
|
||||
|
||||
delete device_index;
|
||||
delete ori_index;
|
||||
}
|
||||
#else
|
||||
{
|
||||
LOG(DEBUG) << "Build index by CPU";
|
||||
faiss::MetricType metric_type = opd_->metric_type == "L2" ? faiss::METRIC_L2 : faiss::METRIC_INNER_PRODUCT;
|
||||
faiss::Index *index = faiss::index_factory(opd_->d, opd_->get_index_type(nb).c_str(), metric_type);
|
||||
if (!index->is_trained) {
|
||||
nt == 0 || xt == nullptr ? index->train(nb, xb)
|
||||
: index->train(nt, xt);
|
||||
}
|
||||
index->add_with_ids(nb, xb, ids);
|
||||
host_index.reset(index);
|
||||
}
|
||||
#endif
|
||||
|
||||
return std::make_shared<Index>(host_index);
|
||||
}
|
||||
|
||||
Index_ptr IndexBuilder::build_all(const long &nb, const vector<float> &xb,
|
||||
const vector<long> &ids,
|
||||
const long &nt, const vector<float> &xt) {
|
||||
return build_all(nb, xb.data(), ids.data(), nt, xt.data());
|
||||
}
|
||||
|
||||
BgCpuBuilder::BgCpuBuilder(const zilliz::milvus::engine::Operand_ptr &opd) : IndexBuilder(opd) {};
|
||||
|
||||
Index_ptr BgCpuBuilder::build_all(const long &nb, const float *xb, const long *ids, const long &nt, const float *xt) {
|
||||
std::shared_ptr<faiss::Index> index = nullptr;
|
||||
faiss::MetricType metric_type = opd_->metric_type == "L2" ? faiss::METRIC_L2 : faiss::METRIC_INNER_PRODUCT;
|
||||
index.reset(faiss::index_factory(opd_->d, opd_->get_index_type(nb).c_str(), metric_type));
|
||||
|
||||
LOG(DEBUG) << "Build index by CPU";
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(cpu_resource);
|
||||
if (!index->is_trained) {
|
||||
nt == 0 || xt == nullptr ? index->train(nb, xb)
|
||||
: index->train(nt, xt);
|
||||
}
|
||||
index->add_with_ids(nb, xb, ids);
|
||||
}
|
||||
|
||||
return std::make_shared<Index>(index);
|
||||
}
|
||||
|
||||
IndexBuilderPtr GetIndexBuilder(const Operand_ptr &opd) {
|
||||
if (opd->index_type == "IDMap") {
|
||||
IndexBuilderPtr index = nullptr;
|
||||
return std::make_shared<BgCpuBuilder>(opd);
|
||||
}
|
||||
|
||||
return std::make_shared<IndexBuilder>(opd);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,70 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
#pragma once
|
||||
|
||||
#include "faiss/Index.h"
|
||||
|
||||
#include "Operand.h"
|
||||
#include "Index.h"
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
class IndexBuilder {
|
||||
public:
|
||||
explicit IndexBuilder(const Operand_ptr &opd);
|
||||
|
||||
virtual Index_ptr build_all(const long &nb,
|
||||
const float *xb,
|
||||
const long *ids,
|
||||
const long &nt = 0,
|
||||
const float *xt = nullptr);
|
||||
|
||||
virtual Index_ptr build_all(const long &nb,
|
||||
const std::vector<float> &xb,
|
||||
const std::vector<long> &ids,
|
||||
const long &nt = 0,
|
||||
const std::vector<float> &xt = std::vector<float>());
|
||||
|
||||
//void train(const long &nt,
|
||||
// const std::vector<float> &xt);
|
||||
//
|
||||
//Index_ptr add(const long &nb,
|
||||
// const std::vector<float> &xb,
|
||||
// const std::vector<long> &ids);
|
||||
//
|
||||
//void set_build_option(const Operand_ptr &opd);
|
||||
|
||||
|
||||
protected:
|
||||
Operand_ptr opd_ = nullptr;
|
||||
bool use_hybrid_index_;
|
||||
int hybrid_index_device_id_;
|
||||
};
|
||||
|
||||
class BgCpuBuilder : public IndexBuilder {
|
||||
public:
|
||||
BgCpuBuilder(const Operand_ptr &opd);
|
||||
|
||||
virtual Index_ptr build_all(const long &nb,
|
||||
const float *xb,
|
||||
const long *ids,
|
||||
const long &nt = 0,
|
||||
const float *xt = nullptr) override;
|
||||
};
|
||||
|
||||
using IndexBuilderPtr = std::shared_ptr<IndexBuilder>;
|
||||
|
||||
extern IndexBuilderPtr GetIndexBuilder(const Operand_ptr &opd);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,123 +0,0 @@
|
|||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
#include "Operand.h"
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
using std::string;
|
||||
|
||||
enum IndexType {
|
||||
Invalid_Option = 0,
|
||||
IVF = 1,
|
||||
IDMAP = 2,
|
||||
IVFSQ8 = 3,
|
||||
};
|
||||
|
||||
IndexType resolveIndexType(const string &index_type) {
|
||||
if (index_type == "IVF") { return IndexType::IVF; }
|
||||
if (index_type == "IDMap") { return IndexType::IDMAP; }
|
||||
if (index_type == "IVFSQ8") { return IndexType::IVFSQ8; }
|
||||
return IndexType::Invalid_Option;
|
||||
}
|
||||
|
||||
int CalcBacketCount(int nb, size_t nlist) {
|
||||
int backet_count = int(nb / 1000000.0 * nlist);
|
||||
if(backet_count == 0) {
|
||||
backet_count = 1; //avoid faiss rash
|
||||
}
|
||||
|
||||
return backet_count;
|
||||
}
|
||||
|
||||
// nb at least 100
|
||||
string Operand::get_index_type(const int &nb) {
|
||||
if (!index_str.empty()) { return index_str; }
|
||||
|
||||
switch (resolveIndexType(index_type)) {
|
||||
case Invalid_Option: {
|
||||
// TODO: add exception
|
||||
break;
|
||||
}
|
||||
case IVF: {
|
||||
|
||||
using namespace zilliz::milvus::server;
|
||||
ServerConfig &config = ServerConfig::GetInstance();
|
||||
ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
|
||||
size_t nlist = engine_config.GetInt32Value(CONFIG_NLIST, 16384);
|
||||
|
||||
index_str += (ncent != 0 ? index_type + std::to_string(ncent) :
|
||||
index_type + std::to_string(CalcBacketCount(nb, nlist)));
|
||||
// std::cout<<"nlist = "<<nlist<<std::endl;
|
||||
if (!postproc.empty()) { index_str += ("," + postproc); }
|
||||
break;
|
||||
}
|
||||
case IVFSQ8: {
|
||||
|
||||
using namespace zilliz::milvus::server;
|
||||
ServerConfig &config = ServerConfig::GetInstance();
|
||||
ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
|
||||
size_t nlist = engine_config.GetInt32Value(CONFIG_NLIST, 16384);
|
||||
|
||||
index_str += (ncent != 0 ? "IVF" + std::to_string(ncent) :
|
||||
"IVF" + std::to_string(CalcBacketCount(nb, nlist)));
|
||||
index_str += ",SQ8";
|
||||
// std::cout<<"nlist = "<<nlist<<std::endl;
|
||||
break;
|
||||
}
|
||||
case IDMAP: {
|
||||
index_str += index_type;
|
||||
if (!postproc.empty()) { index_str += ("," + postproc); }
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return index_str;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Operand &obj) {
|
||||
os << obj.d << " "
|
||||
<< obj.index_type << " "
|
||||
<< obj.metric_type << " "
|
||||
<< obj.preproc << " "
|
||||
<< obj.postproc << " "
|
||||
<< obj.ncent;
|
||||
return os;
|
||||
}
|
||||
|
||||
std::istream &operator>>(std::istream &is, Operand &obj) {
|
||||
is >> obj.d
|
||||
>> obj.index_type
|
||||
>> obj.metric_type
|
||||
>> obj.preproc
|
||||
>> obj.postproc
|
||||
>> obj.ncent;
|
||||
return is;
|
||||
}
|
||||
|
||||
std::string operand_to_str(const Operand_ptr &opd) {
|
||||
std::ostringstream ss;
|
||||
ss << *opd;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
Operand_ptr str_to_operand(const std::string &input) {
|
||||
std::istringstream is(input);
|
||||
auto opd = std::make_shared<Operand>();
|
||||
is >> *(opd.get());
|
||||
|
||||
return opd;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -1,46 +0,0 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
|
||||
// Unauthorized copying of this file, via any medium is strictly prohibited.
|
||||
// Proprietary and confidential.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace zilliz {
|
||||
namespace milvus {
|
||||
namespace engine {
|
||||
|
||||
struct Operand {
|
||||
friend std::ostream &operator<<(std::ostream &os, const Operand &obj);
|
||||
|
||||
friend std::istream &operator>>(std::istream &is, Operand &obj);
|
||||
|
||||
int d;
|
||||
std::string index_type = "IVF";
|
||||
std::string metric_type = "L2"; //> L2 / IP(Inner Product)
|
||||
std::string preproc;
|
||||
std::string postproc = "Flat";
|
||||
std::string index_str;
|
||||
int ncent = 0;
|
||||
|
||||
std::string get_index_type(const int &nb);
|
||||
};
|
||||
|
||||
using Operand_ptr = std::shared_ptr<Operand>;
|
||||
|
||||
extern std::string operand_to_str(const Operand_ptr &opd);
|
||||
|
||||
extern Operand_ptr str_to_operand(const std::string &input);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
|
@ -8,7 +8,6 @@
|
|||
#include "cache/GpuCacheMgr.h"
|
||||
|
||||
#include "utils/Error.h"
|
||||
#include "wrapper/Index.h"
|
||||
#include "wrapper/knowhere/vec_index.h"
|
||||
|
||||
using namespace zilliz::milvus;
|
||||
|
@ -112,7 +111,7 @@ TEST(CacheTest, CPU_CACHE_TEST) {
|
|||
for (int i = 0; i < 20; i++) {
|
||||
MockVecIndex* mock_index = new MockVecIndex();
|
||||
mock_index->ntotal_ = 1000000;//less 1G per index
|
||||
engine::Index_ptr index(mock_index);
|
||||
engine::VecIndexPtr index(mock_index);
|
||||
|
||||
cpu_mgr->InsertItem("index_" + std::to_string(i), index);
|
||||
}
|
||||
|
@ -137,7 +136,7 @@ TEST(CacheTest, CPU_CACHE_TEST) {
|
|||
|
||||
MockVecIndex* mock_index = new MockVecIndex();
|
||||
mock_index->ntotal_ = 6000000;//6G less
|
||||
engine::Index_ptr index(mock_index);
|
||||
engine::VecIndexPtr index(mock_index);
|
||||
|
||||
cpu_mgr->InsertItem("index_6g", index);
|
||||
ASSERT_EQ(cpu_mgr->ItemCount(), 0);//data greater than capacity can not be inserted sucessfully
|
||||
|
@ -154,7 +153,7 @@ TEST(CacheTest, GPU_CACHE_TEST) {
|
|||
for(int i = 0; i < 20; i++) {
|
||||
MockVecIndex* mock_index = new MockVecIndex();
|
||||
mock_index->ntotal_ = 1000;
|
||||
engine::Index_ptr index(mock_index);
|
||||
engine::VecIndexPtr index(mock_index);
|
||||
|
||||
cache::DataObjPtr obj = std::make_shared<cache::DataObj>(index);
|
||||
|
||||
|
@ -175,7 +174,7 @@ TEST(CacheTest, INVALID_TEST) {
|
|||
ASSERT_EQ(mgr.GetItem("test"), nullptr);
|
||||
|
||||
mgr.InsertItem("test", cache::DataObjPtr());
|
||||
mgr.InsertItem("test", engine::Index_ptr(nullptr));
|
||||
mgr.InsertItem("test", engine::VecIndexPtr(nullptr));
|
||||
mgr.EraseItem("test");
|
||||
mgr.PrintInfo();
|
||||
mgr.ClearCache();
|
||||
|
@ -189,7 +188,7 @@ TEST(CacheTest, INVALID_TEST) {
|
|||
for(int i = 0; i < 20; i++) {
|
||||
MockVecIndex* mock_index = new MockVecIndex();
|
||||
mock_index->ntotal_ = 2;
|
||||
engine::Index_ptr index(mock_index);
|
||||
engine::VecIndexPtr index(mock_index);
|
||||
|
||||
cache::DataObjPtr obj = std::make_shared<cache::DataObj>(index);
|
||||
mgr.InsertItem("index_" + std::to_string(i), obj);
|
||||
|
|
Loading…
Reference in New Issue