ExecutionEngineImpl.cpp 35.7 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
J
jinhai 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "db/engine/ExecutionEngineImpl.h"
T
Tinkerrr 已提交
13

14
#include <faiss/utils/ConcurrentBitset.h>
S
shengjh 已提交
15
#include <fiu-local.h>
16

T
Tinkerrr 已提交
17 18 19 20
#include <stdexcept>
#include <utility>
#include <vector>

S
starlord 已提交
21
#include "cache/CpuCacheMgr.h"
S
starlord 已提交
22
#include "cache/GpuCacheMgr.h"
23
#include "db/Utils.h"
X
xiaojun.lin 已提交
24
#include "knowhere/common/Config.h"
S
starlord 已提交
25
#include "metrics/Metrics.h"
X
xiaojun.lin 已提交
26 27
#include "scheduler/Utils.h"
#include "server/Config.h"
J
jinhai 已提交
28
#include "utils/CommonUtil.h"
S
starlord 已提交
29
#include "utils/Exception.h"
S
starlord 已提交
30
#include "utils/Log.h"
31
#include "utils/TimeRecorder.h"
G
groot 已提交
32 33
#include "utils/ValidationUtil.h"
#include "wrapper/BinVecImpl.h"
S
starlord 已提交
34 35
#include "wrapper/ConfAdapter.h"
#include "wrapper/ConfAdapterMgr.h"
S
starlord 已提交
36 37
#include "wrapper/VecImpl.h"
#include "wrapper/VecIndex.h"
X
xj.lin 已提交
38

J
JinHai-CN 已提交
39
//#define ON_SEARCH
S
starlord 已提交
40 41 42
namespace milvus {
namespace engine {

G
groot 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
namespace {

Status
MappingMetricType(MetricType metric_type, knowhere::METRICTYPE& kw_type) {
    switch (metric_type) {
        case MetricType::IP:
            kw_type = knowhere::METRICTYPE::IP;
            break;
        case MetricType::L2:
            kw_type = knowhere::METRICTYPE::L2;
            break;
        case MetricType::HAMMING:
            kw_type = knowhere::METRICTYPE::HAMMING;
            break;
        case MetricType::JACCARD:
            kw_type = knowhere::METRICTYPE::JACCARD;
            break;
        case MetricType::TANIMOTO:
            kw_type = knowhere::METRICTYPE::TANIMOTO;
            break;
        default:
            return Status(DB_ERROR, "Unsupported metric type");
    }

    return Status::OK();
}

bool
IsBinaryIndexType(IndexType type) {
    return type == IndexType::FAISS_BIN_IDMAP || type == IndexType::FAISS_BIN_IVFLAT_CPU;
}

}  // namespace

W
wxyu 已提交
77 78
class CachedQuantizer : public cache::DataObj {
 public:
W
wxyu 已提交
79 80
    explicit CachedQuantizer(knowhere::QuantizerPtr data) : data_(std::move(data)) {
    }
W
wxyu 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    knowhere::QuantizerPtr
    Data() {
        return data_;
    }

    int64_t
    Size() override {
        return data_->size;
    }

 private:
    knowhere::QuantizerPtr data_;
};

S
starlord 已提交
96 97 98
ExecutionEngineImpl::ExecutionEngineImpl(uint16_t dimension, const std::string& location, EngineType index_type,
                                         MetricType metric_type, int32_t nlist)
    : location_(location), dim_(dimension), index_type_(index_type), metric_type_(metric_type), nlist_(nlist) {
G
groot 已提交
99 100 101 102
    EngineType tmp_index_type = server::ValidationUtil::IsBinaryMetricType((int32_t)metric_type)
                                    ? EngineType::FAISS_BIN_IDMAP
                                    : EngineType::FAISS_IDMAP;
    index_ = CreatetVecIndex(tmp_index_type);
103
    if (!index_) {
104
        throw Exception(DB_ERROR, "Unsupported index type");
105
    }
X
xj.lin 已提交
106

X
xiaojun.lin 已提交
107 108 109
    TempMetaConf temp_conf;
    temp_conf.gpu_id = gpu_num_;
    temp_conf.dim = dimension;
G
groot 已提交
110 111 112 113 114
    auto status = MappingMetricType(metric_type, temp_conf.metric_type);
    if (!status.ok()) {
        throw Exception(DB_ERROR, status.message());
    }

X
xiaojun.lin 已提交
115 116 117
    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->Match(temp_conf);

G
groot 已提交
118 119 120 121 122 123
    ErrorCode ec = KNOWHERE_UNEXPECTED_ERROR;
    if (auto bf_index = std::dynamic_pointer_cast<BFIndex>(index_)) {
        ec = bf_index->Build(conf);
    } else if (auto bf_bin_index = std::dynamic_pointer_cast<BinBFIndex>(index_)) {
        ec = bf_bin_index->Build(conf);
    }
124 125 126
    if (ec != KNOWHERE_SUCCESS) {
        throw Exception(DB_ERROR, "Build index error");
    }
S
starlord 已提交
127 128
}

S
starlord 已提交
129 130 131
ExecutionEngineImpl::ExecutionEngineImpl(VecIndexPtr index, const std::string& location, EngineType index_type,
                                         MetricType metric_type, int32_t nlist)
    : index_(std::move(index)), location_(location), index_type_(index_type), metric_type_(metric_type), nlist_(nlist) {
X
xj.lin 已提交
132
}
S
starlord 已提交
133

S
starlord 已提交
134 135
VecIndexPtr
ExecutionEngineImpl::CreatetVecIndex(EngineType type) {
Y
yudong.cai 已提交
136
#ifdef MILVUS_GPU_VERSION
137 138 139
    server::Config& config = server::Config::GetInstance();
    bool gpu_resource_enable = true;
    config.GetGpuResourceConfigEnable(gpu_resource_enable);
S
shengjh 已提交
140
    fiu_do_on("ExecutionEngineImpl.CreatetVecIndex.gpu_res_disabled", gpu_resource_enable = false);
Y
yudong.cai 已提交
141
#endif
S
shengjh 已提交
142 143

    fiu_do_on("ExecutionEngineImpl.CreatetVecIndex.invalid_type", type = EngineType::INVALID);
X
xj.lin 已提交
144 145 146 147
    std::shared_ptr<VecIndex> index;
    switch (type) {
        case EngineType::FAISS_IDMAP: {
            index = GetVecIndexFactory(IndexType::FAISS_IDMAP);
S
starlord 已提交
148 149
            break;
        }
J
jinhai 已提交
150
        case EngineType::FAISS_IVFFLAT: {
Y
yudong.cai 已提交
151
#ifdef MILVUS_GPU_VERSION
152 153 154
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFFLAT_MIX);
            else
Y
youny626 已提交
155
#endif
156
                index = GetVecIndexFactory(IndexType::FAISS_IVFFLAT_CPU);
S
starlord 已提交
157 158
            break;
        }
J
jinhai 已提交
159
        case EngineType::FAISS_IVFSQ8: {
Y
yudong.cai 已提交
160
#ifdef MILVUS_GPU_VERSION
161 162 163
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_MIX);
            else
Y
youny626 已提交
164
#endif
165
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_CPU);
S
starlord 已提交
166 167
            break;
        }
X
xj.lin 已提交
168 169 170 171
        case EngineType::NSG_MIX: {
            index = GetVecIndexFactory(IndexType::NSG_MIX);
            break;
        }
Y
Yukikaze-CZR 已提交
172
#ifdef CUSTOMIZATION
173
#ifdef MILVUS_GPU_VERSION
W
wxyu 已提交
174
        case EngineType::FAISS_IVFSQ8H: {
175 176 177 178 179
            if (gpu_resource_enable) {
                index = GetVecIndexFactory(IndexType::FAISS_IVFSQ8_HYBRID);
            } else {
                throw Exception(DB_ERROR, "No GPU resources for IVFSQ8H");
            }
W
wxyu 已提交
180 181
            break;
        }
182
#endif
Y
Yukikaze-CZR 已提交
183
#endif
Z
zirui.chen 已提交
184
        case EngineType::FAISS_PQ: {
Y
yudong.cai 已提交
185
#ifdef MILVUS_GPU_VERSION
186 187 188
            if (gpu_resource_enable)
                index = GetVecIndexFactory(IndexType::FAISS_IVFPQ_MIX);
            else
Z
zirui.chen 已提交
189
#endif
190
                index = GetVecIndexFactory(IndexType::FAISS_IVFPQ_CPU);
Z
zirui.chen 已提交
191 192
            break;
        }
193 194 195 196 197 198 199 200
        case EngineType::SPTAG_KDT: {
            index = GetVecIndexFactory(IndexType::SPTAG_KDT_RNT_CPU);
            break;
        }
        case EngineType::SPTAG_BKT: {
            index = GetVecIndexFactory(IndexType::SPTAG_BKT_RNT_CPU);
            break;
        }
T
Tinkerrr 已提交
201 202 203 204
        case EngineType::HNSW: {
            index = GetVecIndexFactory(IndexType::HNSW);
            break;
        }
G
groot 已提交
205 206 207 208 209 210 211 212
        case EngineType::FAISS_BIN_IDMAP: {
            index = GetVecIndexFactory(IndexType::FAISS_BIN_IDMAP);
            break;
        }
        case EngineType::FAISS_BIN_IVFFLAT: {
            index = GetVecIndexFactory(IndexType::FAISS_BIN_IVFLAT_CPU);
            break;
        }
X
xj.lin 已提交
213
        default: {
214
            ENGINE_LOG_ERROR << "Unsupported index type";
S
starlord 已提交
215 216 217
            return nullptr;
        }
    }
X
xj.lin 已提交
218
    return index;
S
starlord 已提交
219 220
}

221
void
W
wxyu 已提交
222 223 224 225 226
ExecutionEngineImpl::HybridLoad() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }

W
update  
wxyu 已提交
227 228 229 230 231
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        ENGINE_LOG_WARNING << "HybridLoad with type FAISS_IDMAP, ignore";
        return;
    }

G
groot 已提交
232
#ifdef MILVUS_GPU_VERSION
W
wxyu 已提交
233
    const std::string key = location_ + ".quantizer";
234 235

    server::Config& config = server::Config::GetInstance();
Y
yudong.cai 已提交
236
    std::vector<int64_t> gpus;
237 238 239 240 241
    Status s = config.GetGpuResourceConfigSearchResources(gpus);
    if (!s.ok()) {
        ENGINE_LOG_ERROR << s.message();
        return;
    }
W
wxyu 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    // cache hit
    {
        const int64_t NOT_FOUND = -1;
        int64_t device_id = NOT_FOUND;
        knowhere::QuantizerPtr quantizer = nullptr;

        for (auto& gpu : gpus) {
            auto cache = cache::GpuCacheMgr::GetInstance(gpu);
            if (auto cached_quantizer = cache->GetIndex(key)) {
                device_id = gpu;
                quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
            }
        }

        if (device_id != NOT_FOUND) {
            index_->SetQuantizer(quantizer);
            return;
        }
    }

    // cache miss
    {
        std::vector<int64_t> all_free_mem;
        for (auto& gpu : gpus) {
            auto cache = cache::GpuCacheMgr::GetInstance(gpu);
            auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
            all_free_mem.push_back(free_mem);
        }

        auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
        auto best_index = std::distance(all_free_mem.begin(), max_e);
        auto best_device_id = gpus[best_index];

        auto quantizer_conf = std::make_shared<knowhere::QuantizerCfg>();
        quantizer_conf->mode = 1;
        quantizer_conf->gpu_id = best_device_id;
        auto quantizer = index_->LoadQuantizer(quantizer_conf);
W
add log  
wxyu 已提交
280 281 282
        if (quantizer == nullptr) {
            ENGINE_LOG_ERROR << "quantizer is nullptr";
        }
W
wxyu 已提交
283 284 285 286
        index_->SetQuantizer(quantizer);
        auto cache_quantizer = std::make_shared<CachedQuantizer>(quantizer);
        cache::GpuCacheMgr::GetInstance(best_device_id)->InsertItem(key, cache_quantizer);
    }
G
groot 已提交
287
#endif
W
wxyu 已提交
288 289 290 291 292 293 294
}

void
ExecutionEngineImpl::HybridUnset() const {
    if (index_type_ != EngineType::FAISS_IVFSQ8H) {
        return;
    }
W
update  
wxyu 已提交
295 296 297
    if (index_->GetType() == IndexType::FAISS_IDMAP) {
        return;
    }
W
wxyu 已提交
298
    index_->UnsetQuantizer();
299 300
}

S
starlord 已提交
301
Status
S
starlord 已提交
302
ExecutionEngineImpl::AddWithIds(int64_t n, const float* xdata, const int64_t* xids) {
303 304
    auto status = index_->Add(n, xdata, xids);
    return status;
S
starlord 已提交
305 306
}

G
groot 已提交
307 308 309 310 311 312
Status
ExecutionEngineImpl::AddWithIds(int64_t n, const uint8_t* xdata, const int64_t* xids) {
    auto status = index_->Add(n, xdata, xids);
    return status;
}

S
starlord 已提交
313 314 315
size_t
ExecutionEngineImpl::Count() const {
    if (index_ == nullptr) {
S
starlord 已提交
316
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return count 0";
S
starlord 已提交
317 318
        return 0;
    }
X
xj.lin 已提交
319
    return index_->Count();
S
starlord 已提交
320 321
}

S
starlord 已提交
322 323
size_t
ExecutionEngineImpl::Size() const {
G
groot 已提交
324 325 326 327 328
    if (IsBinaryIndexType(index_->GetType())) {
        return (size_t)(Count() * Dimension() / 8);
    } else {
        return (size_t)(Count() * Dimension()) * sizeof(float);
    }
S
starlord 已提交
329 330
}

S
starlord 已提交
331 332 333
size_t
ExecutionEngineImpl::Dimension() const {
    if (index_ == nullptr) {
S
starlord 已提交
334
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, return dimension " << dim_;
S
starlord 已提交
335 336
        return dim_;
    }
X
xj.lin 已提交
337
    return index_->Dimension();
S
starlord 已提交
338 339
}

S
starlord 已提交
340 341
size_t
ExecutionEngineImpl::PhysicalSize() const {
J
jinhai 已提交
342
    return server::CommonUtil::GetFileSize(location_);
S
starlord 已提交
343 344
}

S
starlord 已提交
345 346
Status
ExecutionEngineImpl::Serialize() {
347
    auto status = write_index(index_, location_);
348 349 350 351

    // here we reset index size by file size,
    // since some index type(such as SQ8) data size become smaller after serialized
    index_->set_size(PhysicalSize());
G
add log  
groot 已提交
352
    ENGINE_LOG_DEBUG << "Finish serialize index file: " << location_ << " size: " << index_->Size();
353

G
groot 已提交
354 355 356 357 358
    if (index_->Size() == 0) {
        std::string msg = "Failed to serialize file: " + location_ + " reason: out of disk space or memory";
        status = Status(DB_ERROR, msg);
    }

359
    return status;
S
starlord 已提交
360 361
}

362
/*
S
starlord 已提交
363 364
Status
ExecutionEngineImpl::Load(bool to_cache) {
365
    index_ = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
J
jinhai 已提交
366
    bool already_in_cache = (index_ != nullptr);
S
starlord 已提交
367
    if (!already_in_cache) {
X
xj.lin 已提交
368
        try {
Y
Yu Kun 已提交
369 370
            double physical_size = PhysicalSize();
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
371
            index_ = read_index(location_);
S
starlord 已提交
372
            if (index_ == nullptr) {
S
starlord 已提交
373 374
                std::string msg = "Failed to load index from " + location_;
                ENGINE_LOG_ERROR << msg;
S
starlord 已提交
375
                return Status(DB_ERROR, msg);
S
starlord 已提交
376 377 378
            } else {
                ENGINE_LOG_DEBUG << "Disk io from: " << location_;
            }
S
starlord 已提交
379
        } catch (std::exception& e) {
S
starlord 已提交
380
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
381
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
382
        }
X
xj.lin 已提交
383 384
    }

J
jinhai 已提交
385
    if (!already_in_cache && to_cache) {
X
xj.lin 已提交
386 387 388
        Cache();
    }
    return Status::OK();
X
xj.lin 已提交
389
}
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
*/

Status
ExecutionEngineImpl::Load(bool to_cache) {
    // TODO(zhiru): refactor

    index_ = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
    bool already_in_cache = (index_ != nullptr);
    if (!already_in_cache) {
        std::string segment_dir;
        utils::GetParentPath(location_, segment_dir);
        auto segment_reader_ptr = std::make_shared<segment::SegmentReader>(segment_dir);

        if (index_type_ == EngineType::FAISS_IDMAP || index_type_ == EngineType::FAISS_BIN_IDMAP) {
            index_ = index_type_ == EngineType::FAISS_IDMAP ? GetVecIndexFactory(IndexType::FAISS_IDMAP)
                                                            : GetVecIndexFactory(IndexType::FAISS_BIN_IDMAP);

            TempMetaConf temp_conf;
            temp_conf.gpu_id = gpu_num_;
            temp_conf.dim = dim_;
            auto status = MappingMetricType(metric_type_, temp_conf.metric_type);
            if (!status.ok()) {
                return status;
            }

            auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
            auto conf = adapter->Match(temp_conf);

            status = segment_reader_ptr->Load();
            if (!status.ok()) {
                std::string msg = "Failed to load segment from " + location_;
                ENGINE_LOG_ERROR << msg;
                return Status(DB_ERROR, msg);
            }

            segment::SegmentPtr segment_ptr;
            segment_reader_ptr->GetSegment(segment_ptr);
            auto& vectors = segment_ptr->vectors_ptr_;
            auto& deleted_docs = segment_ptr->deleted_docs_ptr_->GetDeletedDocs();

            auto vectors_uids = vectors->GetUids();
            index_->SetUids(vectors_uids);

            auto vectors_data = vectors->GetData();

            faiss::ConcurrentBitsetPtr concurrent_bitset_ptr =
                std::make_shared<faiss::ConcurrentBitset>(vectors->GetCount());
            for (auto& offset : deleted_docs) {
                if (!concurrent_bitset_ptr->test(offset)) {
                    concurrent_bitset_ptr->set(offset);
                }
            }

            ErrorCode ec = KNOWHERE_UNEXPECTED_ERROR;
            if (index_type_ == EngineType::FAISS_IDMAP) {
                std::vector<float> float_vectors;
                float_vectors.resize(vectors_data.size() / sizeof(float));
                memcpy(float_vectors.data(), vectors_data.data(), vectors_data.size());
                ec = std::static_pointer_cast<BFIndex>(index_)->Build(conf);
                if (ec != KNOWHERE_SUCCESS) {
                    return status;
                }
                status = std::static_pointer_cast<BFIndex>(index_)->AddWithoutIds(vectors->GetCount(),
                                                                                  float_vectors.data(), Config());
                status = std::static_pointer_cast<BFIndex>(index_)->SetBlacklist(concurrent_bitset_ptr);
            } else if (index_type_ == EngineType::FAISS_BIN_IDMAP) {
                ec = std::static_pointer_cast<BinBFIndex>(index_)->Build(conf);
                if (ec != KNOWHERE_SUCCESS) {
                    return status;
                }
                status = std::static_pointer_cast<BinBFIndex>(index_)->AddWithoutIds(vectors->GetCount(),
                                                                                     vectors_data.data(), Config());
                status = std::static_pointer_cast<BinBFIndex>(index_)->SetBlacklist(concurrent_bitset_ptr);
            }
            if (!status.ok()) {
                return status;
            }

            ENGINE_LOG_DEBUG << "Finished loading raw data from segment " << segment_dir;

        } else {
            try {
                double physical_size = PhysicalSize();
                server::CollectExecutionEngineMetrics metrics(physical_size);
                index_ = read_index(location_);

                if (index_ == nullptr) {
                    std::string msg = "Failed to load index from " + location_;
                    ENGINE_LOG_ERROR << msg;
                    return Status(DB_ERROR, msg);
                } else {
                    segment::DeletedDocsPtr deleted_docs_ptr;
                    auto status = segment_reader_ptr->LoadDeletedDocs(deleted_docs_ptr);
                    if (!status.ok()) {
                        std::string msg = "Failed to load deleted docs from " + location_;
                        ENGINE_LOG_ERROR << msg;
                        return Status(DB_ERROR, msg);
                    }
                    auto& deleted_docs = deleted_docs_ptr->GetDeletedDocs();

                    faiss::ConcurrentBitsetPtr concurrent_bitset_ptr =
                        std::make_shared<faiss::ConcurrentBitset>(index_->Count());
                    for (auto& offset : deleted_docs) {
                        if (!concurrent_bitset_ptr->test(offset)) {
                            concurrent_bitset_ptr->set(offset);
                        }
                    }

                    index_->SetBlacklist(concurrent_bitset_ptr);

                    std::vector<segment::doc_id_t> uids;
                    segment_reader_ptr->LoadUids(uids);
                    index_->SetUids(uids);

                    ENGINE_LOG_DEBUG << "Finished loading index file from segment " << segment_dir;
                }
            } catch (std::exception& e) {
                ENGINE_LOG_ERROR << e.what();
                return Status(DB_ERROR, e.what());
            }
        }
    }

    if (!already_in_cache && to_cache) {
        Cache();
    }
    return Status::OK();
}  // namespace engine
X
xj.lin 已提交
518

S
starlord 已提交
519
Status
W
wxyu 已提交
520
ExecutionEngineImpl::CopyToGpu(uint64_t device_id, bool hybrid) {
X
xiaojun.lin 已提交
521
#if 0
W
wxyu 已提交
522
    if (hybrid) {
X
xiaojun.lin 已提交
523
        const std::string key = location_ + ".quantizer";
W
wxyu 已提交
524
        std::vector<uint64_t> gpus{device_id};
X
xiaojun.lin 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

        const int64_t NOT_FOUND = -1;
        int64_t device_id = NOT_FOUND;

        // cache hit
        {
            knowhere::QuantizerPtr quantizer = nullptr;

            for (auto& gpu : gpus) {
                auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                if (auto cached_quantizer = cache->GetIndex(key)) {
                    device_id = gpu;
                    quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
                }
            }

            if (device_id != NOT_FOUND) {
                // cache hit
                auto config = std::make_shared<knowhere::QuantizerCfg>();
                config->gpu_id = device_id;
                config->mode = 2;
                auto new_index = index_->LoadData(quantizer, config);
                index_ = new_index;
            }
        }

        if (device_id == NOT_FOUND) {
            // cache miss
            std::vector<int64_t> all_free_mem;
            for (auto& gpu : gpus) {
                auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
                all_free_mem.push_back(free_mem);
            }

            auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
            auto best_index = std::distance(all_free_mem.begin(), max_e);
            device_id = gpus[best_index];

            auto pair = index_->CopyToGpuWithQuantizer(device_id);
            index_ = pair.first;

            // cache
            auto cached_quantizer = std::make_shared<CachedQuantizer>(pair.second);
            cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(key, cached_quantizer);
        }
W
wxyu 已提交
571 572
        return Status::OK();
    }
X
xiaojun.lin 已提交
573
#endif
Y
youny626 已提交
574

G
groot 已提交
575
#ifdef MILVUS_GPU_VERSION
Y
youny626 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    auto index = std::static_pointer_cast<VecIndex>(cache::GpuCacheMgr::GetInstance(device_id)->GetIndex(location_));
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
        if (index_ == nullptr) {
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to gpu";
            return Status(DB_ERROR, "index is null");
        }

        try {
            index_ = index_->CopyToGpu(device_id);
            ENGINE_LOG_DEBUG << "CPU to GPU" << device_id;
        } catch (std::exception& e) {
            ENGINE_LOG_ERROR << e.what();
            return Status(DB_ERROR, e.what());
        }
593
    }
Y
youny626 已提交
594 595 596 597

    if (!already_in_cache) {
        GpuCache(device_id);
    }
G
groot 已提交
598
#endif
Y
youny626 已提交
599

600 601 602
    return Status::OK();
}

Y
Yu Kun 已提交
603 604
Status
ExecutionEngineImpl::CopyToIndexFileToGpu(uint64_t device_id) {
G
groot 已提交
605
#ifdef MILVUS_GPU_VERSION
G
groot 已提交
606
    // the ToIndexData is only a placeholder, cpu-copy-to-gpu action is performed in
F
fishpenguin 已提交
607
    gpu_num_ = device_id;
Y
Yu Kun 已提交
608 609
    auto to_index_data = std::make_shared<ToIndexData>(PhysicalSize());
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(to_index_data);
G
groot 已提交
610
    milvus::cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(location_ + "_placeholder", obj);
G
groot 已提交
611
#endif
Y
Yu Kun 已提交
612 613 614
    return Status::OK();
}

S
starlord 已提交
615 616
Status
ExecutionEngineImpl::CopyToCpu() {
617
    auto index = std::static_pointer_cast<VecIndex>(cache::CpuCacheMgr::GetInstance()->GetIndex(location_));
W
wxyu 已提交
618 619 620 621
    bool already_in_cache = (index != nullptr);
    if (already_in_cache) {
        index_ = index;
    } else {
S
starlord 已提交
622
        if (index_ == nullptr) {
S
starlord 已提交
623
            ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to copy to cpu";
S
starlord 已提交
624
            return Status(DB_ERROR, "index is null");
S
starlord 已提交
625 626
        }

Y
Yu Kun 已提交
627 628 629
        try {
            index_ = index_->CopyToCpu();
            ENGINE_LOG_DEBUG << "GPU to CPU";
S
starlord 已提交
630
        } catch (std::exception& e) {
S
starlord 已提交
631
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
632
            return Status(DB_ERROR, e.what());
Y
Yu Kun 已提交
633 634 635
        }
    }

W
wxyu 已提交
636
    if (!already_in_cache) {
Y
Yu Kun 已提交
637
        Cache();
638 639 640 641
    }
    return Status::OK();
}

642 643 644 645 646 647 648 649 650 651 652 653
// ExecutionEnginePtr
// ExecutionEngineImpl::Clone() {
//    if (index_ == nullptr) {
//        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to clone";
//        return nullptr;
//    }
//
//    auto ret = std::make_shared<ExecutionEngineImpl>(dim_, location_, index_type_, metric_type_, nlist_);
//    ret->Init();
//    ret->index_ = index_->Clone();
//    return ret;
//}
W
wxyu 已提交
654

655
/*
S
starlord 已提交
656
Status
S
starlord 已提交
657
ExecutionEngineImpl::Merge(const std::string& location) {
X
xj.lin 已提交
658
    if (location == location_) {
S
starlord 已提交
659
        return Status(DB_ERROR, "Cannot Merge Self");
X
xj.lin 已提交
660 661
    }
    ENGINE_LOG_DEBUG << "Merge index file: " << location << " to: " << location_;
S
starlord 已提交
662

S
starlord 已提交
663
    auto to_merge = cache::CpuCacheMgr::GetInstance()->GetIndex(location);
X
xj.lin 已提交
664
    if (!to_merge) {
X
xj.lin 已提交
665
        try {
Y
Yu Kun 已提交
666 667
            double physical_size = server::CommonUtil::GetFileSize(location);
            server::CollectExecutionEngineMetrics metrics(physical_size);
X
xj.lin 已提交
668
            to_merge = read_index(location);
S
starlord 已提交
669
        } catch (std::exception& e) {
S
starlord 已提交
670
            ENGINE_LOG_ERROR << e.what();
S
starlord 已提交
671
            return Status(DB_ERROR, e.what());
X
xj.lin 已提交
672
        }
X
xj.lin 已提交
673 674
    }

S
starlord 已提交
675
    if (index_ == nullptr) {
S
starlord 已提交
676
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to merge";
S
starlord 已提交
677
        return Status(DB_ERROR, "index is null");
S
starlord 已提交
678 679
    }

X
xj.lin 已提交
680
    if (auto file_index = std::dynamic_pointer_cast<BFIndex>(to_merge)) {
681 682
        auto status = index_->Add(file_index->Count(), file_index->GetRawVectors(), file_index->GetRawIds());
        if (!status.ok()) {
G
groot 已提交
683
            ENGINE_LOG_ERROR << "Failed to merge: " << location << " to: " << location_;
G
groot 已提交
684 685
        } else {
            ENGINE_LOG_DEBUG << "Finish merge index file: " << location;
X
xj.lin 已提交
686
        }
687
        return status;
G
groot 已提交
688 689 690 691 692 693 694 695
    } else if (auto bin_index = std::dynamic_pointer_cast<BinBFIndex>(to_merge)) {
        auto status = index_->Add(bin_index->Count(), bin_index->GetRawVectors(), bin_index->GetRawIds());
        if (!status.ok()) {
            ENGINE_LOG_ERROR << "Failed to merge: " << location << " to: " << location_;
        } else {
            ENGINE_LOG_DEBUG << "Finish merge index file: " << location;
        }
        return status;
X
xj.lin 已提交
696
    } else {
S
starlord 已提交
697
        return Status(DB_ERROR, "file index type is not idmap");
X
xj.lin 已提交
698
    }
S
starlord 已提交
699
}
700
*/
S
starlord 已提交
701 702

ExecutionEnginePtr
S
starlord 已提交
703
ExecutionEngineImpl::BuildIndex(const std::string& location, EngineType engine_type) {
X
xj.lin 已提交
704 705 706
    ENGINE_LOG_DEBUG << "Build index file: " << location << " from: " << location_;

    auto from_index = std::dynamic_pointer_cast<BFIndex>(index_);
G
groot 已提交
707 708
    auto bin_from_index = std::dynamic_pointer_cast<BinBFIndex>(index_);
    if (from_index == nullptr && bin_from_index == nullptr) {
S
starlord 已提交
709 710 711 712
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: from_index is null, failed to build index";
        return nullptr;
    }

S
starlord 已提交
713
    auto to_index = CreatetVecIndex(engine_type);
X
xj.lin 已提交
714
    if (!to_index) {
715
        throw Exception(DB_ERROR, "Unsupported index type");
X
xj.lin 已提交
716 717
    }

X
xiaojun.lin 已提交
718 719 720 721 722
    TempMetaConf temp_conf;
    temp_conf.gpu_id = gpu_num_;
    temp_conf.dim = Dimension();
    temp_conf.nlist = nlist_;
    temp_conf.size = Count();
G
groot 已提交
723 724 725 726
    auto status = MappingMetricType(metric_type_, temp_conf.metric_type);
    if (!status.ok()) {
        throw Exception(DB_ERROR, status.message());
    }
X
xiaojun.lin 已提交
727 728 729

    auto adapter = AdapterMgr::GetInstance().GetAdapter(to_index->GetType());
    auto conf = adapter->Match(temp_conf);
X
xj.lin 已提交
730

G
groot 已提交
731 732 733 734 735
    if (from_index) {
        status = to_index->BuildAll(Count(), from_index->GetRawVectors(), from_index->GetRawIds(), conf);
    } else if (bin_from_index) {
        status = to_index->BuildAll(Count(), bin_from_index->GetRawVectors(), bin_from_index->GetRawIds(), conf);
    }
S
starlord 已提交
736 737 738
    if (!status.ok()) {
        throw Exception(DB_ERROR, status.message());
    }
X
xj.lin 已提交
739

G
add log  
groot 已提交
740
    ENGINE_LOG_DEBUG << "Finish build index file: " << location << " size: " << to_index->Size();
S
starlord 已提交
741
    return std::make_shared<ExecutionEngineImpl>(to_index, location, engine_type, metric_type_, nlist_);
S
starlord 已提交
742 743
}

S
starlord 已提交
744
Status
W
wxyu 已提交
745
ExecutionEngineImpl::Search(int64_t n, const float* data, int64_t k, int64_t nprobe, float* distances, int64_t* labels,
J
JinHai-CN 已提交
746
                            bool hybrid) {
747
#if 0
J
JinHai-CN 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
    if (index_type_ == EngineType::FAISS_IVFSQ8H) {
        if (!hybrid) {
            const std::string key = location_ + ".quantizer";
            std::vector<uint64_t> gpus = scheduler::get_gpu_pool();

            const int64_t NOT_FOUND = -1;
            int64_t device_id = NOT_FOUND;

            // cache hit
            {
                knowhere::QuantizerPtr quantizer = nullptr;

                for (auto& gpu : gpus) {
                    auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                    if (auto cached_quantizer = cache->GetIndex(key)) {
                        device_id = gpu;
                        quantizer = std::static_pointer_cast<CachedQuantizer>(cached_quantizer)->Data();
                    }
                }

                if (device_id != NOT_FOUND) {
                    // cache hit
                    auto config = std::make_shared<knowhere::QuantizerCfg>();
                    config->gpu_id = device_id;
                    config->mode = 2;
                    auto new_index = index_->LoadData(quantizer, config);
                    index_ = new_index;
                }
            }

            if (device_id == NOT_FOUND) {
                // cache miss
                std::vector<int64_t> all_free_mem;
                for (auto& gpu : gpus) {
                    auto cache = cache::GpuCacheMgr::GetInstance(gpu);
                    auto free_mem = cache->CacheCapacity() - cache->CacheUsage();
                    all_free_mem.push_back(free_mem);
                }

                auto max_e = std::max_element(all_free_mem.begin(), all_free_mem.end());
                auto best_index = std::distance(all_free_mem.begin(), max_e);
                device_id = gpus[best_index];

                auto pair = index_->CopyToGpuWithQuantizer(device_id);
                index_ = pair.first;

                // cache
                auto cached_quantizer = std::make_shared<CachedQuantizer>(pair.second);
                cache::GpuCacheMgr::GetInstance(device_id)->InsertItem(key, cached_quantizer);
            }
        }
    }
800
#endif
801
    TimeRecorder rc("ExecutionEngineImpl::Search");
J
JinHai-CN 已提交
802

S
starlord 已提交
803
    if (index_ == nullptr) {
S
starlord 已提交
804
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
S
starlord 已提交
805
        return Status(DB_ERROR, "index is null");
S
starlord 已提交
806 807
    }

Y
Yu Kun 已提交
808
    ENGINE_LOG_DEBUG << "Search Params: [k]  " << k << " [nprobe] " << nprobe;
X
xiaojun.lin 已提交
809 810 811 812 813 814 815 816 817

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;
    temp_conf.k = k;
    temp_conf.nprobe = nprobe;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

W
wxyu 已提交
818 819 820
    if (hybrid) {
        HybridLoad();
    }
W
wxyu 已提交
821

822
    rc.RecordSection("search prepare");
X
xiaojun.lin 已提交
823
    auto status = index_->Search(n, data, distances, labels, conf);
824 825 826 827 828 829 830 831 832 833 834 835
    rc.RecordSection("search done");

    // map offsets to ids
    const std::vector<segment::doc_id_t>& uids = index_->GetUids();
    for (int64_t i = 0; i < n * k; i++) {
        int64_t offset = labels[i];
        if (offset != -1) {
            labels[i] = uids[offset];
        }
    }

    rc.RecordSection("map uids");
W
wxyu 已提交
836

W
wxyu 已提交
837 838 839
    if (hybrid) {
        HybridUnset();
    }
W
wxyu 已提交
840

841
    if (!status.ok()) {
G
groot 已提交
842
        ENGINE_LOG_ERROR << "Search error:" << status.message();
X
xj.lin 已提交
843
    }
844
    return status;
S
starlord 已提交
845 846
}

G
groot 已提交
847 848 849
Status
ExecutionEngineImpl::Search(int64_t n, const uint8_t* data, int64_t k, int64_t nprobe, float* distances,
                            int64_t* labels, bool hybrid) {
850 851
    TimeRecorder rc("ExecutionEngineImpl::Search");

G
groot 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    ENGINE_LOG_DEBUG << "Search Params: [k]  " << k << " [nprobe] " << nprobe;

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;
    temp_conf.k = k;
    temp_conf.nprobe = nprobe;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

    if (hybrid) {
        HybridLoad();
    }

871
    rc.RecordSection("search prepare");
G
groot 已提交
872
    auto status = index_->Search(n, data, distances, labels, conf);
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
    rc.RecordSection("search done");

    // map offsets to ids
    const std::vector<segment::doc_id_t>& uids = index_->GetUids();
    for (int64_t i = 0; i < n * k; i++) {
        int64_t offset = labels[i];
        if (offset != -1) {
            labels[i] = uids[offset];
        }
    }

    rc.RecordSection("map uids");

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
ExecutionEngineImpl::Search(int64_t n, const std::vector<int64_t>& ids, int64_t k, int64_t nprobe, float* distances,
                            int64_t* labels, bool hybrid) {
    TimeRecorder rc("ExecutionEngineImpl::Search");

    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    ENGINE_LOG_DEBUG << "Search by ids Params: [k]  " << k << " [nprobe] " << nprobe;

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;
    temp_conf.k = k;
    temp_conf.nprobe = nprobe;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

    if (hybrid) {
        HybridLoad();
    }

    rc.RecordSection("search prepare");

    // std::string segment_dir;
    // utils::GetParentPath(location_, segment_dir);
    // segment::SegmentReader segment_reader(segment_dir);
    //    segment::IdBloomFilterPtr id_bloom_filter_ptr;
    //    segment_reader.LoadBloomFilter(id_bloom_filter_ptr);

    // Check if the id is present. If so, find its offset
    const std::vector<segment::doc_id_t>& uids = index_->GetUids();

    std::vector<int64_t> offsets;
    /*
    std::vector<segment::doc_id_t> uids;
    auto status = segment_reader.LoadUids(uids);
    if (!status.ok()) {
        return status;
    }
     */

    // There is only one id in ids
    for (auto& id : ids) {
        //        if (id_bloom_filter_ptr->Check(id)) {
        //            if (uids.empty()) {
        //                segment_reader.LoadUids(uids);
        //            }
        //            auto found = std::find(uids.begin(), uids.end(), id);
        //            if (found != uids.end()) {
        //                auto offset = std::distance(uids.begin(), found);
        //                offsets.emplace_back(offset);
        //            }
        //        }
        auto found = std::find(uids.begin(), uids.end(), id);
        if (found != uids.end()) {
            auto offset = std::distance(uids.begin(), found);
            offsets.emplace_back(offset);
        }
    }

    rc.RecordSection("get offset");

    auto status = Status::OK();
    if (!offsets.empty()) {
        status = index_->SearchById(offsets.size(), offsets.data(), distances, labels, conf);
        rc.RecordSection("search by id done");

        // map offsets to ids
        for (int64_t i = 0; i < offsets.size() * k; i++) {
            int64_t offset = labels[i];
            if (offset != -1) {
                labels[i] = uids[offset];
            }
        }
        rc.RecordSection("map uids");
    }

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
ExecutionEngineImpl::GetVectorByID(const int64_t& id, float* vector, bool hybrid) {
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

    if (hybrid) {
        HybridLoad();
    }

    // Only one id for now
    std::vector<int64_t> ids{id};
    auto status = index_->GetVectorById(1, ids.data(), vector, conf);

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

Status
ExecutionEngineImpl::GetVectorByID(const int64_t& id, uint8_t* vector, bool hybrid) {
    if (index_ == nullptr) {
        ENGINE_LOG_ERROR << "ExecutionEngineImpl: index is null, failed to search";
        return Status(DB_ERROR, "index is null");
    }

    ENGINE_LOG_DEBUG << "Get binary vector by id:  " << id;

    // TODO(linxj): remove here. Get conf from function
    TempMetaConf temp_conf;

    auto adapter = AdapterMgr::GetInstance().GetAdapter(index_->GetType());
    auto conf = adapter->MatchSearch(temp_conf, index_->GetType());

    if (hybrid) {
        HybridLoad();
    }

    // Only one id for now
    std::vector<int64_t> ids{id};
    auto status = index_->GetVectorById(1, ids.data(), vector, conf);
G
groot 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

    if (hybrid) {
        HybridUnset();
    }

    if (!status.ok()) {
        ENGINE_LOG_ERROR << "Search error:" << status.message();
    }
    return status;
}

S
starlord 已提交
1050 1051
Status
ExecutionEngineImpl::Cache() {
1052
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
S
starlord 已提交
1053
    milvus::cache::CpuCacheMgr::GetInstance()->InsertItem(location_, obj);
S
starlord 已提交
1054 1055 1056 1057

    return Status::OK();
}

S
starlord 已提交
1058 1059
Status
ExecutionEngineImpl::GpuCache(uint64_t gpu_id) {
G
groot 已提交
1060
#ifdef MILVUS_GPU_VERSION
1061
    cache::DataObjPtr obj = std::static_pointer_cast<cache::DataObj>(index_);
S
starlord 已提交
1062
    milvus::cache::GpuCacheMgr::GetInstance(gpu_id)->InsertItem(location_, obj);
G
groot 已提交
1063
#endif
1064
    return Status::OK();
Y
Yu Kun 已提交
1065 1066
}

X
xj.lin 已提交
1067
// TODO(linxj): remove.
S
starlord 已提交
1068 1069
Status
ExecutionEngineImpl::Init() {
G
groot 已提交
1070
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
1071
    server::Config& config = server::Config::GetInstance();
Y
yudong.cai 已提交
1072
    std::vector<int64_t> gpu_ids;
1073
    Status s = config.GetGpuResourceConfigBuildIndexResources(gpu_ids);
F
fishpenguin 已提交
1074 1075
    if (!s.ok()) {
        gpu_num_ = knowhere::INVALID_VALUE;
1076
        return s;
F
fishpenguin 已提交
1077
    }
1078 1079 1080 1081
    for (auto id : gpu_ids) {
        if (gpu_num_ == id) {
            return Status::OK();
        }
S
starlord 已提交
1082
    }
S
starlord 已提交
1083

1084 1085
    std::string msg = "Invalid gpu_num";
    return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
1086 1087 1088
#else
    return Status::OK();
#endif
S
starlord 已提交
1089 1090
}

S
starlord 已提交
1091 1092
}  // namespace engine
}  // namespace milvus