From 93f2ce45f421ffa08fd0faf19cceca38931908d8 Mon Sep 17 00:00:00 2001 From: "yudong.cai" Date: Fri, 29 Nov 2019 14:13:56 +0800 Subject: [PATCH 1/4] #331 add exception handle when search fail --- .../knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp | 4 +++- core/src/scheduler/task/SearchTask.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp index 923ca0db56..d496d8085c 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp @@ -133,9 +133,11 @@ GPUIVF::search_impl(int64_t n, const float* data, int64_t k, float* distances, i device_index->nprobe = search_cfg->nprobe; // assert(device_index->getNumProbes() == search_cfg->nprobe); - { + try { ResScope rs(res_, gpu_id_); device_index->search(n, (float*)data, k, distances, labels); + } catch (faiss::FaissException& e) { + KNOWHERE_THROW_MSG(e.what()); } } else { KNOWHERE_THROW_MSG("Not a GpuIndexIVF type."); diff --git a/core/src/scheduler/task/SearchTask.cpp b/core/src/scheduler/task/SearchTask.cpp index 08bc6525aa..34c8fc4b4b 100644 --- a/core/src/scheduler/task/SearchTask.cpp +++ b/core/src/scheduler/task/SearchTask.cpp @@ -212,7 +212,13 @@ XSearchTask::Execute() { ResMgrInst::GetInstance()->GetResource(path().Last())->type() == ResourceType::CPU) { hybrid = true; } - index_engine_->Search(nq, vectors, topk, nprobe, output_distance.data(), output_ids.data(), hybrid); + Status s = + index_engine_->Search(nq, vectors, topk, nprobe, output_distance.data(), output_ids.data(), hybrid); + if (!s.ok()) { + search_job->GetStatus() = s; + search_job->SearchDone(index_id_); + return; + } double span = rc.RecordSection(hdr + ", do search"); // search_job->AccumSearchCost(span); From 9a467fb1a0b8cbad15d0dcc46afd823c96c59106 Mon Sep 17 00:00:00 2001 From: "yudong.cai" Date: Fri, 29 Nov 2019 14:16:03 +0800 Subject: [PATCH 2/4] #331 update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eca7440ac3..1fa66be4c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Please mark all change in change log and use the ticket from JIRA. - \#248 - Reside src/external in thirdparty - \#316 - Some files not merged after vectors added - \#327 - Search does not use GPU when index type is FLAT +- \#331 - Add exception handle when search fail - \#340 - Test cases run failed on 0.6.0 - \#353 - Rename config.h.in to version.h.in - \#374 - sdk_simple return empty result From 3a0cc24d57cd2d05d8b70de2c5a3b9b3f674873e Mon Sep 17 00:00:00 2001 From: "yudong.cai" Date: Fri, 29 Nov 2019 14:31:53 +0800 Subject: [PATCH 3/4] #331 update exception handle when search fail --- .../index/vector_index/IndexGPUIVF.cpp | 11 +-- .../knowhere/index/vector_index/IndexIVF.cpp | 71 ++++++++++--------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp index d496d8085c..872f05aa67 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp @@ -131,14 +131,9 @@ GPUIVF::search_impl(int64_t n, const float* data, int64_t k, float* distances, i if (auto device_index = std::dynamic_pointer_cast(index_)) { auto search_cfg = std::dynamic_pointer_cast(cfg); device_index->nprobe = search_cfg->nprobe; - // assert(device_index->getNumProbes() == search_cfg->nprobe); - - try { - ResScope rs(res_, gpu_id_); - device_index->search(n, (float*)data, k, distances, labels); - } catch (faiss::FaissException& e) { - KNOWHERE_THROW_MSG(e.what()); - } + //assert(device_index->getNumProbes() == search_cfg->nprobe); + ResScope rs(res_, gpu_id_); + device_index->search(n, (float*)data, k, distances, labels); } else { KNOWHERE_THROW_MSG("Not a GpuIndexIVF type."); } diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp index f99c6e3972..884aa8c31d 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp @@ -119,42 +119,49 @@ IVF::Search(const DatasetPtr& dataset, const Config& config) { GETTENSOR(dataset) - auto elems = rows * search_cfg->k; - auto res_ids = (int64_t*)malloc(sizeof(int64_t) * elems); - auto res_dis = (float*)malloc(sizeof(float) * elems); + try { + auto elems = rows * search_cfg->k; + auto res_ids = (int64_t *) malloc(sizeof(int64_t) * elems); + auto res_dis = (float *) malloc(sizeof(float) * elems); - search_impl(rows, (float*)p_data, search_cfg->k, res_dis, res_ids, config); + search_impl(rows, (float *) p_data, search_cfg->k, res_dis, res_ids, config); - // std::stringstream ss_res_id, ss_res_dist; - // for (int i = 0; i < 10; ++i) { - // printf("%llu", res_ids[i]); - // printf("\n"); - // printf("%.6f", res_dis[i]); - // printf("\n"); - // ss_res_id << res_ids[i] << " "; - // ss_res_dist << res_dis[i] << " "; - // } - // std::cout << std::endl << "after search: " << std::endl; - // std::cout << ss_res_id.str() << std::endl; - // std::cout << ss_res_dist.str() << std::endl << std::endl; + // std::stringstream ss_res_id, ss_res_dist; + // for (int i = 0; i < 10; ++i) { + // printf("%llu", res_ids[i]); + // printf("\n"); + // printf("%.6f", res_dis[i]); + // printf("\n"); + // ss_res_id << res_ids[i] << " "; + // ss_res_dist << res_dis[i] << " "; + // } + // std::cout << std::endl << "after search: " << std::endl; + // std::cout << ss_res_id.str() << std::endl; + // std::cout << ss_res_dist.str() << std::endl << std::endl; - // auto id_buf = MakeMutableBufferSmart((uint8_t*)res_ids, sizeof(int64_t) * elems); - // auto dist_buf = MakeMutableBufferSmart((uint8_t*)res_dis, sizeof(float) * elems); - // - // std::vector id_bufs{nullptr, id_buf}; - // std::vector dist_bufs{nullptr, dist_buf}; - // - // auto int64_type = std::make_shared(); - // auto float_type = std::make_shared(); - // - // auto id_array_data = arrow::ArrayData::Make(int64_type, elems, id_bufs); - // auto dist_array_data = arrow::ArrayData::Make(float_type, elems, dist_bufs); - // - // auto ids = std::make_shared>(id_array_data); - // auto dists = std::make_shared>(dist_array_data); - // std::vector array{ids, dists}; + // auto id_buf = MakeMutableBufferSmart((uint8_t*)res_ids, sizeof(int64_t) * elems); + // auto dist_buf = MakeMutableBufferSmart((uint8_t*)res_dis, sizeof(float) * elems); + // + // std::vector id_bufs{nullptr, id_buf}; + // std::vector dist_bufs{nullptr, dist_buf}; + // + // auto int64_type = std::make_shared(); + // auto float_type = std::make_shared(); + // + // auto id_array_data = arrow::ArrayData::Make(int64_type, elems, id_bufs); + // auto dist_array_data = arrow::ArrayData::Make(float_type, elems, dist_bufs); + // + // auto ids = std::make_shared>(id_array_data); + // auto dists = std::make_shared>(dist_array_data); + // std::vector array{ids, dists}; - return std::make_shared((void*)res_ids, (void*)res_dis); + return std::make_shared((void *) res_ids, (void *) res_dis); + + } catch (faiss::FaissException& e) { + KNOWHERE_THROW_MSG(e.what()); + } catch (std::exception& e) { + KNOWHERE_THROW_MSG(e.what()); + } } void From 3af4da6d526799cf04ea1bf81c8ce9a27b6be1bf Mon Sep 17 00:00:00 2001 From: "yudong.cai" Date: Fri, 29 Nov 2019 14:40:42 +0800 Subject: [PATCH 4/4] #331 update clang-format --- .../knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp | 2 +- .../knowhere/knowhere/index/vector_index/IndexIVF.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp index 872f05aa67..2001dd0511 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexGPUIVF.cpp @@ -131,7 +131,7 @@ GPUIVF::search_impl(int64_t n, const float* data, int64_t k, float* distances, i if (auto device_index = std::dynamic_pointer_cast(index_)) { auto search_cfg = std::dynamic_pointer_cast(cfg); device_index->nprobe = search_cfg->nprobe; - //assert(device_index->getNumProbes() == search_cfg->nprobe); + // assert(device_index->getNumProbes() == search_cfg->nprobe); ResScope rs(res_, gpu_id_); device_index->search(n, (float*)data, k, distances, labels); } else { diff --git a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp index 884aa8c31d..021819c83e 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/IndexIVF.cpp @@ -121,10 +121,10 @@ IVF::Search(const DatasetPtr& dataset, const Config& config) { try { auto elems = rows * search_cfg->k; - auto res_ids = (int64_t *) malloc(sizeof(int64_t) * elems); - auto res_dis = (float *) malloc(sizeof(float) * elems); + auto res_ids = (int64_t*)malloc(sizeof(int64_t) * elems); + auto res_dis = (float*)malloc(sizeof(float) * elems); - search_impl(rows, (float *) p_data, search_cfg->k, res_dis, res_ids, config); + search_impl(rows, (float*)p_data, search_cfg->k, res_dis, res_ids, config); // std::stringstream ss_res_id, ss_res_dist; // for (int i = 0; i < 10; ++i) { @@ -155,8 +155,7 @@ IVF::Search(const DatasetPtr& dataset, const Config& config) { // auto dists = std::make_shared>(dist_array_data); // std::vector array{ids, dists}; - return std::make_shared((void *) res_ids, (void *) res_dis); - + return std::make_shared((void*)res_ids, (void*)res_dis); } catch (faiss::FaissException& e) { KNOWHERE_THROW_MSG(e.what()); } catch (std::exception& e) {