fix knowhere error (#4769)

* fix knowhere error
(1) fix hnsw uninitialized variable access
(2) fix memory leak when creating IVFSQ8 on GPU
(3) fix memory leak when querying by IVFSQ8H
(4) fix minor memory leak when building NSG
(5) fix minor memory leak when querying by BinaryFlat
(6) fix memory leak in UT
(7) fix CheckGPUPQParams
(8) fix memory leak in QueryByDistance
(9) fix result merge in QueryByDistance
(10) fix RHNSW::Reset
(11) fix RHNSW max_level
(11) fix memory leak if exception occur
(12) fix mismatch malloc/free new[]/delete[]
(13) fix IVFHNSW load

Signed-off-by: shengjun.li <shengjun.li@zilliz.com>

* fix docker

Signed-off-by: shengjun.li <shengjun.li@zilliz.com>
pull/4796/head
shengjun.li 2021-03-05 11:03:08 +08:00 committed by GitHub
parent 7211d5bae4
commit 2f15969d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 278 additions and 115 deletions

View File

@ -10,6 +10,7 @@
// or implied. See the License for the specific language governing permissions and limitations under the License
#include <cstdio>
#include <cstring>
#include <utility>
#include "Log.h"
@ -22,19 +23,13 @@ KnowhereException::KnowhereException(std::string msg) : msg_(std::move(msg)) {
}
KnowhereException::KnowhereException(const std::string& m, const char* funcName, const char* file, int line) {
std::string filename;
try {
size_t pos;
std::string file_path(file);
pos = file_path.find_last_of('/');
filename = file_path.substr(pos + 1);
} catch (std::exception& e) {
LOG_KNOWHERE_ERROR_ << e.what();
const char* filename = funcName;
while (auto tmp = strchr(filename, '/')) {
filename = tmp + 1;
}
int size = snprintf(nullptr, 0, "Error in %s at %s:%d: %s", funcName, filename.c_str(), line, m.c_str());
int size = snprintf(nullptr, 0, "Error in %s at %s:%d: %s", funcName, filename, line, m.c_str());
msg_.resize(size + 1);
snprintf(&msg_[0], msg_.size(), "Error in %s at %s:%d: %s", funcName, filename.c_str(), line, m.c_str());
snprintf(msg_.data(), m.size(), "Error in %s at %s:%d: %s", funcName, filename, line, m.c_str());
}
const char*

View File

@ -37,10 +37,9 @@ Slice(const std::string& prefix, const BinaryPtr& data_src, const int64_t& slice
for (int64_t i = 0; i < data_src->size; ++slice_num) {
int64_t ri = std::min(i + slice_len, data_src->size);
auto size = static_cast<size_t>(ri - i);
auto slice_i = reinterpret_cast<uint8_t*>(malloc(size));
memcpy(slice_i, data_src->data.get() + i, size);
std::shared_ptr<uint8_t[]> slice_i_sp(slice_i, std::default_delete<uint8_t[]>());
binarySet.Append(prefix + "_" + std::to_string(slice_num), slice_i_sp, ri - i);
auto slice_i = std::shared_ptr<uint8_t[]>(new uint8_t[size]);
memcpy(slice_i.get(), data_src->data.get() + i, size);
binarySet.Append(prefix + "_" + std::to_string(slice_num), slice_i, ri - i);
i = ri;
}
ret[NAME] = prefix;
@ -62,15 +61,14 @@ Assemble(BinarySet& binarySet) {
std::string prefix = item[NAME];
int slice_num = item[SLICE_NUM];
auto total_len = static_cast<size_t>(item[TOTAL_LEN]);
auto p_data = reinterpret_cast<uint8_t*>(malloc(total_len));
auto p_data = std::shared_ptr<uint8_t[]>(new uint8_t[total_len]);
int64_t pos = 0;
for (auto i = 0; i < slice_num; ++i) {
auto slice_i_sp = binarySet.Erase(prefix + "_" + std::to_string(i));
memcpy(p_data + pos, slice_i_sp->data.get(), static_cast<size_t>(slice_i_sp->size));
memcpy(p_data.get() + pos, slice_i_sp->data.get(), static_cast<size_t>(slice_i_sp->size));
pos += slice_i_sp->size;
}
std::shared_ptr<uint8_t[]> integral_data(p_data, std::default_delete<uint8_t[]>());
binarySet.Append(prefix, integral_data, total_len);
binarySet.Append(prefix, p_data, total_len);
}
}

View File

@ -194,6 +194,10 @@ IVFPQConfAdapter::CheckGPUPQParams(int64_t dimension, int64_t m, int64_t nbits)
static const std::vector<int64_t> support_dim_per_subquantizer{32, 28, 24, 20, 16, 12, 10, 8, 6, 4, 3, 2, 1};
static const std::vector<int64_t> support_subquantizer{96, 64, 56, 48, 40, 32, 28, 24, 20, 16, 12, 8, 4, 3, 2, 1};
if (!CheckCPUPQParams(dimension, m)) {
return false;
}
int64_t sub_dim = dimension / m;
return (std::find(std::begin(support_subquantizer), std::end(support_subquantizer), m) !=
support_subquantizer.end()) &&

View File

@ -58,14 +58,25 @@ BinaryIVF::Query(const DatasetPtr& dataset_ptr, const Config& config, const fais
GET_TENSOR_DATA(dataset_ptr)
int64_t* p_id = nullptr;
float* p_dist = nullptr;
auto release_when_exception = [&]() {
if (p_id != nullptr) {
free(p_id);
}
if (p_dist != nullptr) {
free(p_dist);
}
};
try {
auto k = config[meta::TOPK].get<int64_t>();
auto elems = rows * k;
size_t p_id_size = sizeof(int64_t) * elems;
size_t p_dist_size = sizeof(float) * elems;
auto p_id = static_cast<int64_t*>(malloc(p_id_size));
auto p_dist = static_cast<float*>(malloc(p_dist_size));
p_id = static_cast<int64_t*>(malloc(p_id_size));
p_dist = static_cast<float*>(malloc(p_dist_size));
QueryImpl(rows, reinterpret_cast<const uint8_t*>(p_data), k, p_dist, p_id, config, bitset);
MapOffsetToUid(p_id, static_cast<size_t>(elems));
@ -77,8 +88,10 @@ BinaryIVF::Query(const DatasetPtr& dataset_ptr, const Config& config, const fais
return ret_ds;
} catch (faiss::FaissException& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
} catch (std::exception& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
}
}

View File

@ -101,6 +101,17 @@ IVF::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::Bit
GET_TENSOR_DATA(dataset_ptr)
int64_t* p_id = nullptr;
float* p_dist = nullptr;
auto release_when_exception = [&]() {
if (p_id != nullptr) {
free(p_id);
}
if (p_dist != nullptr) {
free(p_dist);
}
};
try {
fiu_do_on("IVF.Search.throw_std_exception", throw std::exception());
fiu_do_on("IVF.Search.throw_faiss_exception", throw faiss::FaissException(""));
@ -109,8 +120,8 @@ IVF::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::Bit
size_t p_id_size = sizeof(int64_t) * elems;
size_t p_dist_size = sizeof(float) * elems;
auto p_id = static_cast<int64_t*>(malloc(p_id_size));
auto p_dist = static_cast<float*>(malloc(p_dist_size));
p_id = static_cast<int64_t*>(malloc(p_id_size));
p_dist = static_cast<float*>(malloc(p_dist_size));
QueryImpl(rows, reinterpret_cast<const float*>(p_data), k, p_dist, p_id, config, bitset);
MapOffsetToUid(p_id, static_cast<size_t>(elems));
@ -120,8 +131,10 @@ IVF::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::Bit
ret_ds->Set(meta::DISTANCE, p_dist);
return ret_ds;
} catch (faiss::FaissException& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
} catch (std::exception& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
}
}

View File

@ -46,7 +46,7 @@ IVFHNSW::Serialize(const Config& config) {
MemoryIOWriter writer;
faiss::write_index(real_idx->storage, &writer);
std::shared_ptr<uint8_t[]> data(writer.data_);
res_set.Append("HNSW_META", data, writer.rp);
res_set.Append("HNSW_STORAGE", data, writer.rp);
if (config.contains(INDEX_FILE_SLICE_SIZE_IN_MEGABYTE)) {
Disassemble(config[INDEX_FILE_SLICE_SIZE_IN_MEGABYTE].get<int64_t>() * 1024 * 1024, res_set);
@ -66,15 +66,12 @@ IVFHNSW::Load(const BinarySet& binary_set) {
auto index = dynamic_cast<faiss::IndexIVFFlat*>(index_.get());
MemoryIOReader reader;
auto binary = binary_set.GetByName("HNSW_META");
auto binary = binary_set.GetByName("HNSW_STORAGE");
reader.total = static_cast<size_t>(binary->size);
reader.data_ = binary->data.get();
auto idx = faiss::read_index(&reader);
auto real_idx = dynamic_cast<faiss::IndexRHNSWFlat*>(index->quantizer);
real_idx->storage = new faiss::IndexFlat(idx->d, idx->metric_type);
real_idx->storage->add(idx->ntotal,
reinterpret_cast<const float*>(dynamic_cast<faiss::IndexFlat*>(idx)->xb.data()));
real_idx->storage = faiss::read_index(&reader);
real_idx->init_hnsw();
} catch (std::exception& e) {
KNOWHERE_THROW_MSG(e.what());

View File

@ -87,9 +87,10 @@ IndexRHNSWFlat::Train(const DatasetPtr& dataset_ptr, const Config& config) {
try {
GET_TENSOR_DATA_DIM(dataset_ptr)
faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get<std::string>());
int32_t efConstruction = config[IndexParams::efConstruction];
auto idx = new faiss::IndexRHNSWFlat(int(dim), config[IndexParams::M], metric_type);
idx->hnsw.efConstruction = config[IndexParams::efConstruction];
idx->hnsw.efConstruction = efConstruction;
index_ = std::shared_ptr<faiss::Index>(idx);
index_->train(rows, reinterpret_cast<const float*>(p_data));
} catch (std::exception& e) {

View File

@ -84,9 +84,10 @@ void
IndexRHNSWPQ::Train(const DatasetPtr& dataset_ptr, const Config& config) {
try {
GET_TENSOR_DATA_DIM(dataset_ptr)
int32_t efConstruction = config[IndexParams::efConstruction];
auto idx = new faiss::IndexRHNSWPQ(int(dim), config[IndexParams::PQM], config[IndexParams::M]);
idx->hnsw.efConstruction = config[IndexParams::efConstruction];
idx->hnsw.efConstruction = efConstruction;
index_ = std::shared_ptr<faiss::Index>(idx);
index_->train(rows, reinterpret_cast<const float*>(p_data));
} catch (std::exception& e) {

View File

@ -88,10 +88,11 @@ IndexRHNSWSQ::Train(const DatasetPtr& dataset_ptr, const Config& config) {
try {
GET_TENSOR_DATA_DIM(dataset_ptr)
faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get<std::string>());
int32_t efConstruction = config[IndexParams::efConstruction];
auto idx =
new faiss::IndexRHNSWSQ(int(dim), faiss::QuantizerType::QT_8bit, config[IndexParams::M], metric_type);
idx->hnsw.efConstruction = config[IndexParams::efConstruction];
idx->hnsw.efConstruction = efConstruction;
index_ = std::shared_ptr<faiss::Index>(idx);
index_->train(rows, static_cast<const float*>(p_data));
} catch (std::exception& e) {

View File

@ -28,16 +28,13 @@ void
GPUIVFPQ::Train(const DatasetPtr& dataset_ptr, const Config& config) {
GET_TENSOR_DATA_DIM(dataset_ptr)
gpu_id_ = config[knowhere::meta::DEVICEID];
auto gpu_res = FaissGpuResourceMgr::GetInstance().GetRes(gpu_id_);
if (gpu_res != nullptr) {
ResScope rs(gpu_res, gpu_id_, true);
auto device_index =
new faiss::gpu::GpuIndexIVFPQ(gpu_res->faiss_res.get(), dim, config[IndexParams::nlist].get<int64_t>(),
config[IndexParams::m], config[IndexParams::nbits],
GetMetricType(config[Metric::TYPE].get<std::string>())); // IP not support
auto device_index = new faiss::gpu::GpuIndexIVFPQ(
gpu_res->faiss_res.get(), dim, config[IndexParams::nlist].get<int64_t>(), config[IndexParams::m],
config[IndexParams::nbits], GetMetricType(config[Metric::TYPE].get<std::string>()));
device_index->train(rows, reinterpret_cast<const float*>(p_data));
index_.reset(device_index);
res_ = gpu_res;
} else {

View File

@ -12,6 +12,7 @@
#include <faiss/IndexFlat.h>
#include <faiss/IndexScalarQuantizer.h>
#include <faiss/gpu/GpuCloner.h>
#include <faiss/gpu/GpuIndexIVFScalarQuantizer.h>
#include <memory>
#include <string>
@ -29,24 +30,13 @@ void
GPUIVFSQ::Train(const DatasetPtr& dataset_ptr, const Config& config) {
GET_TENSOR_DATA_DIM(dataset_ptr)
gpu_id_ = config[knowhere::meta::DEVICEID];
// std::stringstream index_type;
// index_type << "IVF" << config[IndexParams::nlist] << ","
// << "SQ" << config[IndexParams::nbits];
// faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get<std::string>());
// auto build_index = faiss::index_factory(dim, index_type.str().c_str(), metric_type);
faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get<std::string>());
faiss::Index* coarse_quantizer = new faiss::IndexFlat(dim, metric_type);
auto build_index = new faiss::IndexIVFScalarQuantizer(
coarse_quantizer, dim, config[IndexParams::nlist].get<int64_t>(), faiss::QuantizerType::QT_8bit, metric_type);
auto gpu_res = FaissGpuResourceMgr::GetInstance().GetRes(gpu_id_);
if (gpu_res != nullptr) {
ResScope rs(gpu_res, gpu_id_, true);
auto device_index = faiss::gpu::index_cpu_to_gpu(gpu_res->faiss_res.get(), gpu_id_, build_index);
auto device_index = new faiss::gpu::GpuIndexIVFScalarQuantizer(
gpu_res->faiss_res.get(), dim, config[IndexParams::nlist].get<int64_t>(), faiss::QuantizerType::QT_8bit,
GetMetricType(config[Metric::TYPE].get<std::string>()));
device_index->train(rows, reinterpret_cast<const float*>(p_data));
index_.reset(device_index);
res_ = gpu_res;
} else {

View File

@ -50,6 +50,7 @@ IVFSQHybrid::Train(const DatasetPtr& dataset_ptr, const Config& config) {
res_ = gpu_res;
gpu_mode_ = 2;
} else {
delete build_index;
KNOWHERE_THROW_MSG("Build IVFSQHybrid can't get gpu resource");
}
@ -131,16 +132,15 @@ IVFSQHybrid::LoadData(const FaissIVFQuantizerPtr& quantizer_ptr, const Config& c
option.allInGpu = true;
auto ivf_quantizer = std::dynamic_pointer_cast<FaissIVFQuantizer>(quantizer_ptr);
if (ivf_quantizer == nullptr) {
if (ivf_quantizer == nullptr)
KNOWHERE_THROW_MSG("quantizer type not faissivfquantizer");
}
auto index_composition = new faiss::IndexComposition;
index_composition->index = index_.get();
index_composition->quantizer = ivf_quantizer->quantizer;
index_composition->mode = 2; // only 2
faiss::IndexComposition index_composition;
index_composition.index = index_.get();
index_composition.quantizer = ivf_quantizer->quantizer;
index_composition.mode = 2; // only copy data
auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, index_composition, &option);
auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, &index_composition, &option);
std::shared_ptr<faiss::Index> new_idx;
new_idx.reset(gpu_index);
auto sq_idx = std::make_shared<IVFSQHybrid>(new_idx, gpu_id, res);
@ -159,17 +159,17 @@ IVFSQHybrid::LoadQuantizer(const Config& config) {
faiss::gpu::GpuClonerOptions option;
option.allInGpu = true;
auto index_composition = new faiss::IndexComposition;
index_composition->index = index_.get();
index_composition->quantizer = nullptr;
index_composition->mode = 1; // only 1
faiss::IndexComposition index_composition;
index_composition.index = index_.get();
index_composition.quantizer = nullptr;
index_composition.mode = 1; // only copy quantizer
auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, index_composition, &option);
auto gpu_index = faiss::gpu::index_cpu_to_gpu(res->faiss_res.get(), gpu_id, &index_composition, &option);
delete gpu_index;
auto q = std::make_shared<FaissIVFQuantizer>();
auto& q_ptr = index_composition->quantizer;
auto q_ptr = index_composition.quantizer;
q->size = q_ptr->d * q_ptr->getNumVecs() * sizeof(float);
q->quantizer = q_ptr;
q->gpu_id = gpu_id;

View File

@ -25,7 +25,7 @@ namespace knowhere {
***********************************************************************/
void
DynamicResultSet::do_alloction() {
DynamicResultSet::AlloctionImpl() {
if (count <= 0) {
KNOWHERE_THROW_MSG("DynamicResultSet::do_alloction failed because of count <= 0");
}
@ -36,7 +36,7 @@ DynamicResultSet::do_alloction() {
}
void
DynamicResultSet::do_sort(ResultSetPostProcessType postProcessType) {
DynamicResultSet::SortImpl(ResultSetPostProcessType postProcessType) {
if (postProcessType == ResultSetPostProcessType::SortAsc) {
quick_sort<true>(0, count);
} else if (postProcessType == ResultSetPostProcessType::SortDesc) {
@ -128,7 +128,7 @@ DynamicResultCollector::Merge(size_t limit, ResultSetPostProcessType postProcess
// boundaries[i] += boundaries[i - 1];
}
ret.count = boundaries[seg_num] <= limit ? boundaries[seg_num] : limit;
ret.do_alloction();
ret.AlloctionImpl();
// abandon redundancy answers randomly
// abandon strategy: keep the top limit sequentially
@ -142,7 +142,7 @@ DynamicResultCollector::Merge(size_t limit, ResultSetPostProcessType postProcess
pos--; // last segment id
// full copy
#pragma omp parallel for
for (auto i = 0; i < pos - 1; ++i) {
for (auto i = 0; i < pos; ++i) {
for (auto& pseg : seg_results[i]) {
auto len = pseg->buffers.size() * pseg->buffer_size - pseg->buffer_size + pseg->wp;
pseg->copy_range(0, len, ret.labels.get() + boundaries[i], ret.distances.get() + boundaries[i]);
@ -163,20 +163,20 @@ DynamicResultCollector::Merge(size_t limit, ResultSetPostProcessType postProcess
}
if (postProcessType != ResultSetPostProcessType::None) {
ret.do_sort(postProcessType);
ret.SortImpl(postProcessType);
}
return ret;
}
void
DynamicResultCollector::Append(milvus::knowhere::DynamicResultSegment&& seg_result) {
seg_results.push_back(std::move(seg_result));
seg_results.emplace_back(std::move(seg_result));
}
void
ExchangeDataset(DynamicResultSegment& milvus_dataset, std::vector<faiss::RangeSearchPartialResult*>& faiss_dataset) {
for (auto& prspr : faiss_dataset) {
auto mrspr = new DynamicResultFragment(prspr->res->buffer_size);
auto mrspr = std::make_shared<DynamicResultFragment>(prspr->res->buffer_size);
mrspr->wp = prspr->wp;
mrspr->buffers.resize(prspr->buffers.size());
for (auto i = 0; i < prspr->buffers.size(); ++i) {
@ -186,6 +186,7 @@ ExchangeDataset(DynamicResultSegment& milvus_dataset, std::vector<faiss::RangeSe
prspr->buffers[i].dis = nullptr;
}
delete prspr->res;
delete prspr;
milvus_dataset.push_back(mrspr);
}
}

View File

@ -21,16 +21,21 @@ namespace milvus {
namespace knowhere {
enum class ResultSetPostProcessType { None = 0, SortDesc, SortAsc };
using idx_t = int64_t;
/*
* Class: Dynamic result set (merged results)
*/
struct DynamicResultSet {
std::shared_ptr<idx_t[]> labels; /// result for query i is labels[lims[i]:lims[i + 1]]
std::shared_ptr<float[]> distances; /// corresponding distances, not sorted
size_t count; /// size of the result buffer's size, when reaches this size, auto start a new buffer
void
do_alloction();
AlloctionImpl();
void
do_sort(ResultSetPostProcessType postProcessType = ResultSetPostProcessType::SortAsc);
SortImpl(ResultSetPostProcessType postProcessType = ResultSetPostProcessType::SortAsc);
private:
template <bool asc>
@ -38,14 +43,34 @@ struct DynamicResultSet {
quick_sort(size_t lp, size_t rp);
};
// BufferPool (inner classes)
typedef faiss::BufferList DynamicResultFragment;
typedef DynamicResultFragment* DynamicResultFragmentPtr;
typedef std::shared_ptr<DynamicResultFragment> DynamicResultFragmentPtr;
typedef std::vector<DynamicResultFragmentPtr> DynamicResultSegment;
/*
* Class: Dynamic result collector
* Example:
DynamicResultCollector collector;
for (auto &seg: segments) {
auto seg_rst = seg.QueryByDistance(xxx);
collector.append(seg_rst);
}
auto rst = collector.merge();
*/
struct DynamicResultCollector {
public:
/*
* Merge the results of segments
* Notes: Now, we apply limit before sort.
* It can be updated if you don't expect the policy.
*/
DynamicResultSet
Merge(size_t limit = 10000, ResultSetPostProcessType postProcessType = ResultSetPostProcessType::None);
/*
* Collect the results of segments
*/
void
Append(DynamicResultSegment&& seg_result);

View File

@ -127,6 +127,7 @@ NsgIndex::InitNavigationPoint(float* data) {
//
// float r1 = distance_->Compare(center, ori_data_ + navigation_point * dimension, dimension);
// assert(r1 == resset[0].distance);
delete[] center;
}
// Specify Link

View File

@ -144,6 +144,17 @@ IVF_NM::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::
GET_TENSOR_DATA(dataset_ptr)
int64_t* p_id = nullptr;
float* p_dist = nullptr;
auto release_when_exception = [&]() {
if (p_id != nullptr) {
free(p_id);
}
if (p_dist != nullptr) {
free(p_dist);
}
};
try {
fiu_do_on("IVF_NM.Search.throw_std_exception", throw std::exception());
fiu_do_on("IVF_NM.Search.throw_faiss_exception", throw faiss::FaissException(""));
@ -163,8 +174,10 @@ IVF_NM::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::
ret_ds->Set(meta::DISTANCE, p_dist);
return ret_ds;
} catch (faiss::FaissException& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
} catch (std::exception& e) {
release_when_exception();
KNOWHERE_THROW_MSG(e.what());
}
}

View File

@ -56,13 +56,13 @@ RHNSW::~RHNSW() {
void RHNSW::reset() {
max_level = -1;
entry_point = -1;
levels.clear();
free(level0_links);
for (auto i = 0; i < levels.size(); ++ i) {
if (levels[i])
free(linkLists[i]);
}
free(linkLists);
levels.clear();
level0_links = nullptr;
linkLists = nullptr;
level_constant = 1 / log(1.0 * M);
@ -104,7 +104,6 @@ int RHNSW::prepare_level_tab(size_t n, bool preset_levels)
}
linkLists = linkLists_new;
int max_level = 0;
int debug_space = 0;
for (int i = 0; i < n; i++) {
int pt_level = levels[i + n0];

View File

@ -375,6 +375,7 @@ void binary_distance_knn_hc (
memcpy(ha->val, value, thread_heap_size * sizeof(T));
memcpy(ha->ids, labels, thread_heap_size * sizeof(int64_t));
delete[] hc;
delete[] value;
delete[] labels;

View File

@ -500,20 +500,14 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
// Reallocate base layer
char * data_level0_memory_new = (char *) malloc(new_max_elements * size_data_per_element_);
if (data_level0_memory_new == nullptr)
data_level0_memory_ = (char *) realloc(data_level0_memory_, new_max_elements * size_data_per_element_);
if (data_level0_memory_ == nullptr)
throw std::runtime_error("Not enough memory: resizeIndex failed to allocate base layer");
memcpy(data_level0_memory_new, data_level0_memory_,cur_element_count * size_data_per_element_);
free(data_level0_memory_);
data_level0_memory_=data_level0_memory_new;
// Reallocate all other layers
char ** linkLists_new = (char **) malloc(sizeof(void *) * new_max_elements);
if (linkLists_new == nullptr)
linkLists_ = (char **) realloc(linkLists_, sizeof(void *) * new_max_elements);
if (linkLists_ == nullptr)
throw std::runtime_error("Not enough memory: resizeIndex failed to allocate other layers");
memcpy(linkLists_new, linkLists_,cur_element_count * sizeof(void *));
free(linkLists_);
linkLists_=linkLists_new;
max_elements_=new_max_elements;
@ -989,8 +983,10 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {
ret += element_levels_.size() * sizeof(int);
ret += max_elements_ * size_data_per_element_;
ret += max_elements_ * sizeof(void*);
for (size_t i = 0; i < max_elements_; ++ i) {
ret += linkLists_[i] ? size_links_per_element_ * element_levels_[i] : 0;
for (auto i = 0; i < max_elements_; ++ i) {
if (element_levels_[i] > 0) {
ret += size_links_per_element_ * element_levels_[i];
}
}
return ret;
}

View File

@ -67,6 +67,7 @@ TEST_P(AnnoyTest, annoy_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
/*
* output result to check by eyes
@ -106,9 +107,11 @@ TEST_P(AnnoyTest, annoy_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
/*
* delete result checked by eyes
@ -214,6 +217,7 @@ TEST_P(AnnoyTest, annoy_slice) {
ASSERT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -66,6 +66,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
auto binaryset = index_->Serialize(conf);
auto new_index = std::make_shared<milvus::knowhere::BinaryIDMAP>();
@ -73,6 +74,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_basic) {
auto result2 = new_index->Query(query_dataset, conf, nullptr);
AssertAnns(result2, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(result2);
faiss::ConcurrentBitsetPtr concurrent_bitset_ptr = std::make_shared<faiss::ConcurrentBitset>(nb);
for (int64_t i = 0; i < nq; ++i) {
@ -81,6 +83,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_basic) {
auto result_bs_1 = index_->Query(query_dataset, conf, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result_bs_1);
// auto result4 = index_->SearchById(id_dataset, conf);
// AssertAneq(result4, nq, k);
@ -109,6 +112,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_serialize) {
auto re_result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(re_result, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(re_result);
EXPECT_EQ(index_->Count(), nb);
EXPECT_EQ(index_->Dim(), dim);
auto binaryset = index_->Serialize(conf);
@ -128,6 +132,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_serialize) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
@ -147,6 +152,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_slice) {
auto re_result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(re_result, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(re_result);
EXPECT_EQ(index_->Count(), nb);
EXPECT_EQ(index_->Dim(), dim);
auto binaryset = index_->Serialize(conf);
@ -157,6 +163,7 @@ TEST_P(BinaryIDMAPTest, binaryidmap_slice) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}

View File

@ -74,6 +74,7 @@ TEST_P(BinaryIVFTest, binaryivf_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
faiss::ConcurrentBitsetPtr concurrent_bitset_ptr = std::make_shared<faiss::ConcurrentBitset>(nb);
for (int64_t i = 0; i < nq; ++i) {
@ -82,6 +83,7 @@ TEST_P(BinaryIVFTest, binaryivf_basic) {
auto result2 = index_->Query(query_dataset, conf, concurrent_bitset_ptr);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
#if 0
auto result3 = index_->QueryById(id_dataset, conf, nullptr);
@ -146,6 +148,7 @@ TEST_P(BinaryIVFTest, binaryivf_serialize) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
@ -161,5 +164,6 @@ TEST_P(BinaryIVFTest, binaryivf_slice) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}

View File

@ -70,6 +70,7 @@ TEST_F(SingleIndexTest, IVFSQHybrid) {
auto result = gpu_idx->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
}
@ -86,6 +87,7 @@ TEST_F(SingleIndexTest, IVFSQHybrid) {
auto result = gpu_idx->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
milvus::json quantizer_conf{{milvus::knowhere::meta::DEVICEID, DEVICEID}, {"mode", 2}};
for (int i = 0; i < 2; ++i) {
@ -96,6 +98,7 @@ TEST_F(SingleIndexTest, IVFSQHybrid) {
auto result = new_idx->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
@ -116,6 +119,7 @@ TEST_F(SingleIndexTest, IVFSQHybrid) {
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
// PrintResult(result, nq, k);
hybrid_idx->UnsetQuantizer();
ReleaseQueryResult(result);
}
}
}

View File

@ -42,14 +42,14 @@ class GPURESTEST : public DataGen, public TestGpuIndexBase {
k = K;
elems = nq * k;
ids = (int64_t*)malloc(sizeof(int64_t) * elems);
dis = (float*)malloc(sizeof(float) * elems);
ids = new int64_t[elems];
dis = new float[elems];
}
void
TearDown() override {
delete ids;
delete dis;
delete[] ids;
delete[] dis;
TestGpuIndexBase::TearDown();
}
@ -76,6 +76,7 @@ TEST_F(GPURESTEST, copyandsearch) {
index_->AddWithoutIds(base_dataset, conf);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
index_->SetIndexSize(nb * dim * sizeof(float));
auto cpu_idx = milvus::knowhere::cloner::CopyGpuToCpu(index_, milvus::knowhere::Config());
@ -88,7 +89,8 @@ TEST_F(GPURESTEST, copyandsearch) {
auto search_func = [&] {
// TimeRecorder tc("search&load");
for (int i = 0; i < search_count; ++i) {
search_idx->Query(query_dataset, conf, nullptr);
auto result = search_idx->Query(query_dataset, conf, nullptr);
ReleaseQueryResult(result);
// if (i > search_count - 6 || i == 0)
// tc.RecordSection("search once");
}
@ -107,7 +109,8 @@ TEST_F(GPURESTEST, copyandsearch) {
milvus::knowhere::TimeRecorder tc("Basic");
milvus::knowhere::cloner::CopyCpuToGpu(cpu_idx, DEVICEID, milvus::knowhere::Config());
tc.RecordSection("Copy to gpu once");
search_idx->Query(query_dataset, conf, nullptr);
auto result2 = search_idx->Query(query_dataset, conf, nullptr);
ReleaseQueryResult(result2);
tc.RecordSection("Search once");
search_func();
tc.RecordSection("Search total cost");
@ -147,6 +150,7 @@ TEST_F(GPURESTEST, trainandsearch) {
for (int i = 0; i < search_count; ++i) {
auto result = search_idx->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
};

View File

@ -79,6 +79,7 @@ TEST_P(HNSWTest, HNSW_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
TEST_P(HNSWTest, HNSW_delete) {
@ -109,9 +110,11 @@ TEST_P(HNSWTest, HNSW_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
/*
* delete result checked by eyes
@ -166,6 +169,7 @@ TEST_P(HNSWTest, HNSW_serialize) {
EXPECT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}*/

View File

@ -86,6 +86,7 @@ TEST_P(IDMAPTest, idmap_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
if (index_mode_ == milvus::knowhere::IndexMode::MODE_GPU) {
#ifdef MILVUS_GPU_VERSION
@ -100,6 +101,7 @@ TEST_P(IDMAPTest, idmap_basic) {
auto result2 = new_index->Query(query_dataset, conf, nullptr);
AssertAnns(result2, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(result2);
#if 0
auto result3 = new_index->QueryById(id_dataset, conf);
@ -116,6 +118,7 @@ TEST_P(IDMAPTest, idmap_basic) {
auto result_bs_1 = index_->Query(query_dataset, conf, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result_bs_1);
#if 0
auto result_bs_2 = index_->QueryById(id_dataset, conf);
@ -154,6 +157,7 @@ TEST_P(IDMAPTest, idmap_serialize) {
auto re_result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(re_result, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(re_result);
EXPECT_EQ(index_->Count(), nb);
EXPECT_EQ(index_->Dim(), dim);
auto binaryset = index_->Serialize(conf);
@ -173,6 +177,7 @@ TEST_P(IDMAPTest, idmap_serialize) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
@ -197,6 +202,7 @@ TEST_P(IDMAPTest, idmap_slice) {
auto re_result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(re_result, nq, k);
// PrintResult(re_result, nq, k);
ReleaseQueryResult(re_result);
EXPECT_EQ(index_->Count(), nb);
EXPECT_EQ(index_->Dim(), dim);
auto binaryset = index_->Serialize(conf);
@ -207,6 +213,7 @@ TEST_P(IDMAPTest, idmap_slice) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}
@ -460,6 +467,7 @@ TEST_P(IDMAPTest, idmap_copy) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
{
// clone
@ -474,6 +482,7 @@ TEST_P(IDMAPTest, idmap_copy) {
auto clone_index = milvus::knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, conf);
auto clone_result = clone_index->Query(query_dataset, conf,nullptr);
AssertAnns(clone_result, nq, k);
ReleaseQueryResult(clone_result);
ASSERT_THROW({ std::static_pointer_cast<milvus::knowhere::GPUIDMAP>(clone_index)->GetRawVectors(); },
milvus::knowhere::KnowhereException);
@ -486,6 +495,7 @@ TEST_P(IDMAPTest, idmap_copy) {
clone_index->Load(binary);
auto new_result = clone_index->Query(query_dataset, conf, nullptr);
AssertAnns(new_result, nq, k);
ReleaseQueryResult(new_result);
// auto clone_gpu_idx = clone_index->Clone();
// auto clone_gpu_res = clone_gpu_idx->Search(query_dataset, conf);
@ -495,6 +505,7 @@ TEST_P(IDMAPTest, idmap_copy) {
auto host_index = milvus::knowhere::cloner::CopyGpuToCpu(clone_index, conf);
auto host_result = host_index->Query(query_dataset, conf, nullptr);
AssertAnns(host_result, nq, k);
ReleaseQueryResult(host_result);
ASSERT_TRUE(std::static_pointer_cast<milvus::knowhere::IDMAP>(host_index)->GetRawVectors() != nullptr);
// gpu to gpu
@ -503,6 +514,7 @@ TEST_P(IDMAPTest, idmap_copy) {
std::static_pointer_cast<milvus::knowhere::GPUIDMAP>(device_index)->CopyGpuToGpu(DEVICEID, conf);
auto device_result = new_device_index->Query(query_dataset, conf, nullptr);
AssertAnns(device_result, nq, k);
ReleaseQueryResult(device_result);
}
}
#endif

View File

@ -106,6 +106,7 @@ TEST_P(IVFTest, ivf_basic_cpu) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
if (index_type_ != milvus::knowhere::IndexEnum::INDEX_FAISS_IVFPQ) {
#if 0
@ -130,6 +131,7 @@ TEST_P(IVFTest, ivf_basic_cpu) {
auto result_bs_1 = index_->Query(query_dataset, conf_, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
// PrintResult(result, nq, k);
ReleaseQueryResult(result_bs_1);
#if 0
auto result_bs_2 = index_->QueryById(id_dataset, conf_);
@ -163,6 +165,7 @@ TEST_P(IVFTest, ivf_basic_gpu) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
// PrintResult(result, nq, k);
ReleaseQueryResult(result);
faiss::ConcurrentBitsetPtr concurrent_bitset_ptr = std::make_shared<faiss::ConcurrentBitset>(nb);
for (int64_t i = 0; i < nq; ++i) {
@ -172,6 +175,7 @@ TEST_P(IVFTest, ivf_basic_gpu) {
auto result_bs_1 = index_->Query(query_dataset, conf_, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
// PrintResult(result, nq, k);
ReleaseQueryResult(result_bs_1);
#ifdef MILVUS_GPU_VERSION
milvus::knowhere::FaissGpuResourceMgr::GetInstance().Dump();
@ -208,6 +212,7 @@ TEST_P(IVFTest, ivf_serialize) {
EXPECT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, conf_[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -224,6 +229,7 @@ TEST_P(IVFTest, ivf_slice) {
EXPECT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, conf_[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -262,6 +268,7 @@ TEST_P(IVFTest, clone_test) {
auto clone_index = milvus::knowhere::cloner::CopyGpuToCpu(index_, milvus::knowhere::Config());
auto clone_result = clone_index->Query(query_dataset, conf_, nullptr);
AssertEqual(result, clone_result);
ReleaseQueryResult(clone_result);
std::cout << "clone G <=> C [" << index_type_ << "] success" << std::endl;
});
} else {
@ -281,11 +288,14 @@ TEST_P(IVFTest, clone_test) {
auto clone_index = milvus::knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, milvus::knowhere::Config());
auto clone_result = clone_index->Query(query_dataset, conf_, nullptr);
AssertEqual(result, clone_result);
ReleaseQueryResult(clone_result);
std::cout << "clone C <=> G [" << index_type_ << "] success" << std::endl;
});
EXPECT_ANY_THROW(milvus::knowhere::cloner::CopyCpuToGpu(index_, -1, milvus::knowhere::Config()));
}
}
ReleaseQueryResult(result);
}
#endif
@ -309,6 +319,7 @@ TEST_P(IVFTest, gpu_seal_test) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, conf_[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
fiu_init(0);
fiu_enable("IVF.Search.throw_std_exception", 1, nullptr, 0);

View File

@ -109,6 +109,7 @@ TEST_P(IVFNMCPUTest, ivf_basic_cpu) {
auto clone_index = milvus::knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, conf_);
auto clone_result = clone_index->Query(query_dataset, conf_, nullptr);
AssertAnns(clone_result, nq, k);
ReleaseQueryResult(clone_result);
std::cout << "clone C <=> G [" << index_type_ << "] success" << std::endl;
});
EXPECT_ANY_THROW(milvus::knowhere::cloner::CopyCpuToGpu(index_, -1, milvus::knowhere::Config()));
@ -122,10 +123,13 @@ TEST_P(IVFNMCPUTest, ivf_basic_cpu) {
auto result_bs_1 = index_->Query(query_dataset, conf_, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result_bs_1);
#ifdef MILVUS_GPU_VERSION
milvus::knowhere::FaissGpuResourceMgr::GetInstance().Dump();
#endif
ReleaseQueryResult(result);
}
TEST_P(IVFNMCPUTest, ivf_slice) {
@ -158,4 +162,5 @@ TEST_P(IVFNMCPUTest, ivf_slice) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}

View File

@ -119,6 +119,7 @@ TEST_F(IVFNMGPUTest, ivf_basic_gpu) {
SERIALIZE_AND_LOAD(clone_index);
auto clone_result = clone_index->Query(query_dataset, conf_, nullptr);
AssertEqual(result, clone_result);
ReleaseQueryResult(clone_result);
std::cout << "clone G <=> C [" << index_type_ << "] success" << std::endl;
});
}
@ -130,7 +131,10 @@ TEST_F(IVFNMGPUTest, ivf_basic_gpu) {
auto result_bs_1 = index_->Query(query_dataset, conf_, concurrent_bitset_ptr);
AssertAnns(result_bs_1, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result_bs_1);
milvus::knowhere::FaissGpuResourceMgr::GetInstance().Dump();
ReleaseQueryResult(result);
}
#endif

View File

@ -77,6 +77,7 @@ TEST_P(IVFHNSWTest, ivfhnsw_basic_cpu) {
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
TEST_P(IVFHNSWTest, ivfhnsw_slice) {
@ -92,5 +93,6 @@ TEST_P(IVFHNSWTest, ivfhnsw_slice) {
EXPECT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
}

View File

@ -70,6 +70,7 @@ TEST_P(NGTONNGTest, ngtonng_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
TEST_P(NGTONNGTest, ngtonng_delete) {
@ -86,9 +87,11 @@ TEST_P(NGTONNGTest, ngtonng_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
}
TEST_P(NGTONNGTest, ngtonng_serialize) {
@ -146,6 +149,7 @@ TEST_P(NGTONNGTest, ngtonng_serialize) {
ASSERT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -161,5 +165,6 @@ TEST_P(NGTONNGTest, ngtonng_slice) {
ASSERT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -70,6 +70,7 @@ TEST_P(NGTPANNGTest, ngtpanng_basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
}
TEST_P(NGTPANNGTest, ngtpanng_delete) {
@ -86,9 +87,11 @@ TEST_P(NGTPANNGTest, ngtpanng_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
}
TEST_P(NGTPANNGTest, ngtpanng_serialize) {
@ -146,6 +149,7 @@ TEST_P(NGTPANNGTest, ngtpanng_serialize) {
ASSERT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -161,5 +165,6 @@ TEST_P(NGTPANNGTest, ngtpanng_slice) {
ASSERT_EQ(index_->Dim(), dim);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -101,16 +101,17 @@ TEST_F(NSGInterfaceTest, basic_test) {
index_->Load(bs);
auto result = index_->Query(query_dataset, search_conf, nullptr);
AssertAnns(result, nq, k);
auto result_1 = index_->Query(query_dataset, search_conf, nullptr);
AssertAnns(result_1, nq, k);
ReleaseQueryResult(result_1);
/* test NSG GPU train */
auto new_index_1 = std::make_shared<milvus::knowhere::NSG_NM>(DEVICE_GPU0);
auto new_index = std::make_shared<milvus::knowhere::NSG_NM>(DEVICE_GPU0);
train_conf[milvus::knowhere::meta::DEVICEID] = DEVICE_GPU0;
new_index_1->BuildAll(base_dataset, train_conf);
new_index->BuildAll(base_dataset, train_conf);
// Serialize and Load before Query
bs = new_index_1->Serialize(search_conf);
bs = new_index->Serialize(search_conf);
dim = base_dataset->Get<int64_t>(milvus::knowhere::meta::DIM);
rows = base_dataset->Get<int64_t>(milvus::knowhere::meta::ROWS);
@ -120,10 +121,11 @@ TEST_F(NSGInterfaceTest, basic_test) {
bptr->size = dim * rows * sizeof(float);
bs.Append(RAW_DATA, bptr);
new_index_1->Load(bs);
new_index->Load(bs);
auto new_result_1 = new_index_1->Query(query_dataset, search_conf, nullptr);
AssertAnns(new_result_1, nq, k);
auto result_2 = new_index->Query(query_dataset, search_conf, nullptr);
AssertAnns(result_2, nq, k);
ReleaseQueryResult(result_2);
ASSERT_EQ(index_->Count(), nb);
ASSERT_EQ(index_->Dim(), dim);
@ -165,17 +167,11 @@ TEST_F(NSGInterfaceTest, delete_test) {
auto result = index_->Query(query_dataset, search_conf, nullptr);
AssertAnns(result, nq, k);
auto I_before = result->Get<int64_t*>(milvus::knowhere::meta::IDS);
ASSERT_EQ(index_->Count(), nb);
ASSERT_EQ(index_->Dim(), dim);
faiss::ConcurrentBitsetPtr bitset = std::make_shared<faiss::ConcurrentBitset>(nb);
for (int i = 0; i < nq; i++) {
bitset->set(i);
}
auto I_before = result->Get<int64_t*>(milvus::knowhere::meta::IDS);
// Serialize and Load before Query
bs = index_->Serialize(search_conf);
@ -188,7 +184,14 @@ TEST_F(NSGInterfaceTest, delete_test) {
bs.Append(RAW_DATA, bptr);
index_->Load(bs);
// search xq with delete
faiss::ConcurrentBitsetPtr bitset = std::make_shared<faiss::ConcurrentBitset>(nb);
for (int i = 0; i < nq; i++) {
bitset->set(i);
}
auto result_after = index_->Query(query_dataset, search_conf, bitset);
AssertAnns(result_after, nq, k, CheckMode::CHECK_NOT_EQUAL);
auto I_after = result_after->Get<int64_t*>(milvus::knowhere::meta::IDS);
@ -196,6 +199,9 @@ TEST_F(NSGInterfaceTest, delete_test) {
for (int i = 0; i < nq; i++) {
ASSERT_NE(I_before[i * k], I_after[i * k]);
}
ReleaseQueryResult(result);
ReleaseQueryResult(result_after);
}
TEST_F(NSGInterfaceTest, slice_test) {
@ -226,6 +232,7 @@ TEST_F(NSGInterfaceTest, slice_test) {
auto result = index_->Query(query_dataset, search_conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
/* test NSG GPU train */
auto new_index_1 = std::make_shared<milvus::knowhere::NSG_NM>(DEVICE_GPU0);
@ -247,6 +254,7 @@ TEST_F(NSGInterfaceTest, slice_test) {
auto new_result_1 = new_index_1->Query(query_dataset, search_conf, nullptr);
AssertAnns(new_result_1, nq, k);
ReleaseQueryResult(new_result_1);
ASSERT_EQ(index_->Count(), nb);
ASSERT_EQ(index_->Dim(), dim);

View File

@ -55,6 +55,7 @@ TEST_P(RHNSWFlatTest, HNSW_basic) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
// Serialize and Load before Query
milvus::knowhere::BinarySet bs = index_->Serialize(conf);
@ -72,6 +73,7 @@ TEST_P(RHNSWFlatTest, HNSW_basic) {
auto result2 = tmp_index->Query(query_dataset, conf, nullptr);
// AssertAnns(result2, nq, k);
ReleaseQueryResult(result2);
}
TEST_P(RHNSWFlatTest, HNSW_delete) {
@ -89,9 +91,11 @@ TEST_P(RHNSWFlatTest, HNSW_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
// AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
/*
* delete result checked by eyes
@ -168,6 +172,7 @@ TEST_P(RHNSWFlatTest, HNSW_serialize) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -190,5 +195,6 @@ TEST_P(RHNSWFlatTest, HNSW_slice) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -57,6 +57,7 @@ TEST_P(RHNSWPQTest, HNSW_basic) {
milvus::knowhere::BinarySet bs = index_->Serialize(conf);
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto tmp_index = std::make_shared<milvus::knowhere::IndexRHNSWPQ>();
@ -64,6 +65,7 @@ TEST_P(RHNSWPQTest, HNSW_basic) {
auto result2 = tmp_index->Query(query_dataset, conf, nullptr);
// AssertAnns(result2, nq, k);
ReleaseQueryResult(result2);
}
TEST_P(RHNSWPQTest, HNSW_delete) {
@ -81,9 +83,11 @@ TEST_P(RHNSWPQTest, HNSW_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
// AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
/*
* delete result checked by eyes
@ -144,6 +148,7 @@ TEST_P(RHNSWPQTest, HNSW_serialize) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -159,5 +164,6 @@ TEST_P(RHNSWPQTest, HNSW_slice) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -58,6 +58,7 @@ TEST_P(RHNSWSQ8Test, HNSW_basic) {
milvus::knowhere::BinarySet bs = index_->Serialize(conf);
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto tmp_index = std::make_shared<milvus::knowhere::IndexRHNSWSQ>();
@ -65,6 +66,7 @@ TEST_P(RHNSWSQ8Test, HNSW_basic) {
auto result2 = tmp_index->Query(query_dataset, conf, nullptr);
// AssertAnns(result2, nq, k);
ReleaseQueryResult(result2);
}
TEST_P(RHNSWSQ8Test, HNSW_delete) {
@ -82,9 +84,11 @@ TEST_P(RHNSWSQ8Test, HNSW_delete) {
auto result1 = index_->Query(query_dataset, conf, nullptr);
// AssertAnns(result1, nq, k);
ReleaseQueryResult(result1);
auto result2 = index_->Query(query_dataset, conf, bitset);
// AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
ReleaseQueryResult(result2);
/*
* delete result checked by eyes
@ -145,6 +149,7 @@ TEST_P(RHNSWSQ8Test, HNSW_serialize) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}
@ -160,5 +165,6 @@ TEST_P(RHNSWSQ8Test, HNSW_slice) {
EXPECT_EQ(new_idx->Dim(), dim);
auto result = new_idx->Query(query_dataset, conf, nullptr);
// AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
}
}

View File

@ -69,6 +69,7 @@ TEST_P(SPTAGTest, sptag_basic) {
// index_->Add(base_dataset, conf);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
ReleaseQueryResult(result);
{
auto ids = result->Get<int64_t*>(milvus::knowhere::meta::IDS);
@ -102,6 +103,7 @@ TEST_P(SPTAGTest, sptag_serialize) {
auto result = new_index->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
PrintResult(result, nq, k);
ReleaseQueryResult(result);
ASSERT_EQ(new_index->Count(), nb);
ASSERT_EQ(new_index->Dim(), dim);
// ASSERT_THROW({ new_index->Clone(); }, milvus::knowhere::KnowhereException);
@ -186,5 +188,6 @@ TEST_P(SPTAGTest, sptag_slice) {
auto result = new_index->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, k);
PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
}

View File

@ -85,6 +85,7 @@ TEST_P(VecIndexTest, basic) {
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
PrintResult(result, nq, k);
ReleaseQueryResult(result);
}
TEST_P(VecIndexTest, serialize) {
@ -95,6 +96,7 @@ TEST_P(VecIndexTest, serialize) {
EXPECT_EQ(index_->index_mode(), index_mode_);
auto result = index_->Query(query_dataset, conf, nullptr);
AssertAnns(result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(result);
auto binaryset = index_->Serialize();
auto new_index = milvus::knowhere::VecIndexFactory::GetInstance().CreateVecIndex(index_type_, index_mode_);
@ -105,6 +107,7 @@ TEST_P(VecIndexTest, serialize) {
EXPECT_EQ(index_->index_mode(), new_index->index_mode());
auto new_result = new_index_->Query(query_dataset, conf, nullptr);
AssertAnns(new_result, nq, conf[milvus::knowhere::meta::TOPK]);
ReleaseQueryResult(new_result);
}
// todo

View File

@ -230,6 +230,15 @@ PrintResult(const milvus::knowhere::DatasetPtr& result, const int& nq, const int
std::cout << "dist\n" << ss_dist.str() << std::endl;
}
void
ReleaseQueryResult(const milvus::knowhere::DatasetPtr& result) {
float* res_dist = result->Get<float*>(milvus::knowhere::meta::DISTANCE);
free(res_dist);
int64_t* res_ids = result->Get<int64_t*>(milvus::knowhere::meta::IDS);
free(res_ids);
}
// not used
#if 0
void

View File

@ -93,6 +93,9 @@ AssertBinVec(const milvus::knowhere::DatasetPtr& result, const milvus::knowhere:
void
PrintResult(const milvus::knowhere::DatasetPtr& result, const int& nq, const int& k);
void
ReleaseQueryResult(const milvus::knowhere::DatasetPtr& result);
struct FileIOWriter {
std::fstream fs;
std::string name;

View File

@ -11,6 +11,8 @@
FROM milvusdb/milvus-dev:amd64-centos-7-core AS openblas
FROM hectormolinero/tini:v18 AS tini
FROM centos:centos7
RUN yum install -y wget && \
@ -25,8 +27,7 @@ COPY --from=openblas /usr/lib/libopenblas-r0.3.9.so /var/lib/milvus/lib/
RUN ln -s /var/lib/milvus/lib/libopenblas-r0.3.9.so /var/lib/milvus/lib/libopenblas.so.0 && \
ln -s /var/lib/milvus/lib/libopenblas.so.0 /var/lib/milvus/lib/libopenblas.so
RUN wget -O /tini https://github.com/krallin/tini/releases/download/v0.19.0/tini && \
chmod +x /tini
COPY --from=tini /usr/bin/tini /tini
ENTRYPOINT ["/tini", "--"]

View File

@ -11,6 +11,8 @@
FROM milvusdb/milvus-dev:amd64-centos-7-core AS openblas
FROM hectormolinero/tini:v18 AS tini
FROM nvidia/cuda:10.1-devel-centos7
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
@ -27,8 +29,7 @@ COPY --from=openblas /usr/lib/libopenblas-r0.3.9.so /var/lib/milvus/lib/
RUN ln -s /var/lib/milvus/lib/libopenblas-r0.3.9.so /var/lib/milvus/lib/libopenblas.so.0 && \
ln -s /var/lib/milvus/lib/libopenblas.so.0 /var/lib/milvus/lib/libopenblas.so
RUN wget -O /tini https://github.com/krallin/tini/releases/download/v0.19.0/tini && \
chmod +x /tini
COPY --from=tini /usr/bin/tini /tini
ENTRYPOINT ["/tini", "--"]