ConfAdapter.cpp 5.2 KB
Newer Older
X
xiaojun.lin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
//
//   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.

S
starlord 已提交
18 19 20
#include "wrapper/ConfAdapter.h"
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
#include "utils/Log.h"
X
xiaojun.lin 已提交
21 22

#include <cmath>
S
starlord 已提交
23
#include <memory>
X
xiaojun.lin 已提交
24

S
starlord 已提交
25
// TODO(lxj): add conf checker
X
xiaojun.lin 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

namespace milvus {
namespace engine {

#if CUDA_VERSION > 9000
#define GPU_MAX_NRPOBE 2048
#else
#define GPU_MAX_NRPOBE 1024
#endif

void
ConfAdapter::MatchBase(knowhere::Config conf) {
    if (conf->metric_type == knowhere::DEFAULT_TYPE)
        conf->metric_type = knowhere::METRICTYPE::L2;
    if (conf->gpu_id == knowhere::INVALID_VALUE)
        conf->gpu_id = 0;
}

knowhere::Config
S
starlord 已提交
45
ConfAdapter::Match(const TempMetaConf& metaconf) {
X
xiaojun.lin 已提交
46 47 48 49 50 51 52 53 54
    auto conf = std::make_shared<knowhere::Cfg>();
    conf->d = metaconf.dim;
    conf->metric_type = metaconf.metric_type;
    conf->gpu_id = conf->gpu_id;
    MatchBase(conf);
    return conf;
}

knowhere::Config
S
starlord 已提交
55
ConfAdapter::MatchSearch(const TempMetaConf& metaconf, const IndexType& type) {
X
xiaojun.lin 已提交
56 57 58 59 60 61
    auto conf = std::make_shared<knowhere::Cfg>();
    conf->k = metaconf.k;
    return conf;
}

knowhere::Config
S
starlord 已提交
62
IVFConfAdapter::Match(const TempMetaConf& metaconf) {
X
xiaojun.lin 已提交
63 64 65 66 67 68 69 70 71 72 73 74
    auto conf = std::make_shared<knowhere::IVFCfg>();
    conf->nlist = MatchNlist(metaconf.size, metaconf.nlist);
    conf->d = metaconf.dim;
    conf->metric_type = metaconf.metric_type;
    conf->gpu_id = conf->gpu_id;
    MatchBase(conf);
    return conf;
}

static constexpr float TYPICAL_COUNT = 1000000.0;

int64_t
S
starlord 已提交
75
IVFConfAdapter::MatchNlist(const int64_t& size, const int64_t& nlist) {
X
xiaojun.lin 已提交
76 77 78 79 80 81 82
    if (size <= TYPICAL_COUNT / 16384 + 1) {
        // handle less row count, avoid nlist set to 0
        return 1;
    } else if (int(size / TYPICAL_COUNT) * nlist == 0) {
        // calculate a proper nlist if nlist not specified or size less than TYPICAL_COUNT
        return int(size / TYPICAL_COUNT * 16384);
    }
83
    return nlist;
X
xiaojun.lin 已提交
84 85 86
}

knowhere::Config
S
starlord 已提交
87
IVFConfAdapter::MatchSearch(const TempMetaConf& metaconf, const IndexType& type) {
X
xiaojun.lin 已提交
88 89 90 91 92 93 94 95 96 97
    auto conf = std::make_shared<knowhere::IVFCfg>();
    conf->k = metaconf.k;
    conf->nprobe = metaconf.nprobe;

    switch (type) {
        case IndexType::FAISS_IVFFLAT_GPU:
        case IndexType::FAISS_IVFSQ8_GPU:
        case IndexType::FAISS_IVFPQ_GPU:
            if (conf->nprobe > GPU_MAX_NRPOBE) {
                WRAPPER_LOG_WARNING << "When search with GPU, nprobe shoud be no more than " << GPU_MAX_NRPOBE
S
starlord 已提交
98 99
                                    << ", but you passed " << conf->nprobe << ". Search with " << GPU_MAX_NRPOBE
                                    << " instead";
X
xiaojun.lin 已提交
100 101 102 103 104 105 106
                conf->nprobe = GPU_MAX_NRPOBE;
            }
    }
    return conf;
}

knowhere::Config
S
starlord 已提交
107
IVFSQConfAdapter::Match(const TempMetaConf& metaconf) {
X
xiaojun.lin 已提交
108 109 110 111
    auto conf = std::make_shared<knowhere::IVFSQCfg>();
    conf->nlist = MatchNlist(metaconf.size, metaconf.nlist);
    conf->d = metaconf.dim;
    conf->metric_type = metaconf.metric_type;
Y
youny626 已提交
112
    conf->gpu_id = conf->gpu_id;
X
xiaojun.lin 已提交
113 114 115 116 117 118
    conf->nbits = 8;
    MatchBase(conf);
    return conf;
}

knowhere::Config
S
starlord 已提交
119
IVFPQConfAdapter::Match(const TempMetaConf& metaconf) {
X
xiaojun.lin 已提交
120 121 122 123 124 125 126 127 128 129 130 131
    auto conf = std::make_shared<knowhere::IVFPQCfg>();
    conf->nlist = MatchNlist(metaconf.size, metaconf.nlist);
    conf->d = metaconf.dim;
    conf->metric_type = metaconf.metric_type;
    conf->gpu_id = conf->gpu_id;
    conf->nbits = 8;
    conf->m = 8;
    MatchBase(conf);
    return conf;
}

knowhere::Config
S
starlord 已提交
132
NSGConfAdapter::Match(const TempMetaConf& metaconf) {
X
xiaojun.lin 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
    auto conf = std::make_shared<knowhere::NSGCfg>();
    conf->nlist = MatchNlist(metaconf.size, metaconf.nlist);
    conf->d = metaconf.dim;
    conf->metric_type = metaconf.metric_type;
    conf->gpu_id = conf->gpu_id;

    auto scale_factor = round(metaconf.dim / 128.0);
    scale_factor = scale_factor >= 4 ? 4 : scale_factor;
    conf->nprobe = 6 + 10 * scale_factor;
    conf->knng = 100 + 100 * scale_factor;
    conf->search_length = 40 + 5 * scale_factor;
    conf->out_degree = 50 + 5 * scale_factor;
    conf->candidate_pool_size = 200 + 100 * scale_factor;
    MatchBase(conf);
147

S
starlord 已提交
148 149 150
    //    WRAPPER_LOG_DEBUG << "nlist: " << conf->nlist
    //    << ", gpu_id: " << conf->gpu_id << ", d: " << conf->d
    //    << ", nprobe: " << conf->nprobe << ", knng: " << conf->knng;
X
xiaojun.lin 已提交
151 152 153 154
    return conf;
}

knowhere::Config
S
starlord 已提交
155
NSGConfAdapter::MatchSearch(const TempMetaConf& metaconf, const IndexType& type) {
X
xiaojun.lin 已提交
156 157 158 159 160 161
    auto conf = std::make_shared<knowhere::NSGCfg>();
    conf->k = metaconf.k;
    conf->search_length = metaconf.search_length;
    return conf;
}

S
starlord 已提交
162 163
}  // namespace engine
}  // namespace milvus