From 0fb88f733a188897e6928a1b72bfb0c0077a620f Mon Sep 17 00:00:00 2001 From: "shengjun.li" <49774184+shengjun1985@users.noreply.github.com> Date: Wed, 24 Jun 2020 09:46:48 +0800 Subject: [PATCH] fix too many copies (#2660) Signed-off-by: shengjun.li --- .../knowhere/index/vector_index/gpu/IndexGPUIVF.cpp | 11 ++++------- .../knowhere/index/vector_index/gpu/IndexGPUIVFPQ.cpp | 5 +---- .../index/vector_index/gpu/IndexIVFSQHybrid.cpp | 9 ++------- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVF.cpp b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVF.cpp index 5735f5f84b..3552a19045 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVF.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVF.cpp @@ -40,14 +40,11 @@ GPUIVF::Train(const DatasetPtr& dataset_ptr, const Config& config) { idx_config.device = gpu_id_; int32_t nlist = config[IndexParams::nlist]; faiss::MetricType metric_type = GetMetricType(config[Metric::TYPE].get()); - faiss::gpu::GpuIndexIVFFlat device_index(gpu_res->faiss_res.get(), dim, nlist, metric_type, idx_config); - device_index.train(rows, (float*)p_data); + auto device_index = + new faiss::gpu::GpuIndexIVFFlat(gpu_res->faiss_res.get(), dim, nlist, metric_type, idx_config); + device_index->train(rows, (float*)p_data); - std::shared_ptr host_index = nullptr; - host_index.reset(faiss::gpu::index_gpu_to_cpu(&device_index)); - - auto device_index1 = faiss::gpu::index_cpu_to_gpu(gpu_res->faiss_res.get(), gpu_id_, host_index.get()); - index_.reset(device_index1); + index_.reset(device_index); res_ = gpu_res; } else { KNOWHERE_THROW_MSG("Build IVF can't get gpu resource"); diff --git a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVFPQ.cpp b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVFPQ.cpp index 6706c173a1..d9465761d9 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVFPQ.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexGPUIVFPQ.cpp @@ -38,11 +38,8 @@ GPUIVFPQ::Train(const DatasetPtr& dataset_ptr, const Config& config) { config[IndexParams::m], config[IndexParams::nbits], GetMetricType(config[Metric::TYPE].get())); // IP not support device_index->train(rows, (float*)p_data); - std::shared_ptr host_index = nullptr; - host_index.reset(faiss::gpu::index_gpu_to_cpu(device_index)); - auto device_index1 = faiss::gpu::index_cpu_to_gpu(gpu_res->faiss_res.get(), gpu_id_, host_index.get()); - index_.reset(device_index1); + index_.reset(device_index); res_ = gpu_res; } else { KNOWHERE_THROW_MSG("Build IVFPQ can't get gpu resource"); diff --git a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp index 10db0c327f..dde0fcd2e8 100644 --- a/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp +++ b/core/src/index/knowhere/knowhere/index/vector_index/gpu/IndexIVFSQHybrid.cpp @@ -45,19 +45,14 @@ IVFSQHybrid::Train(const DatasetPtr& dataset_ptr, const Config& config) { auto device_index = faiss::gpu::index_cpu_to_gpu(gpu_res->faiss_res.get(), gpu_id_, build_index); device_index->train(rows, (float*)p_data); - std::shared_ptr host_index = nullptr; - host_index.reset(faiss::gpu::index_gpu_to_cpu(device_index)); - - delete device_index; - delete build_index; - - device_index = faiss::gpu::index_cpu_to_gpu(gpu_res->faiss_res.get(), gpu_id_, host_index.get()); index_.reset(device_index); res_ = gpu_res; gpu_mode_ = 2; } else { KNOWHERE_THROW_MSG("Build IVFSQHybrid can't get gpu resource"); } + + delete build_index; } VecIndexPtr