utils.h 5.8 KB
Newer Older
J
jinhai 已提交
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.

Z
zirui.chen 已提交
18

X
MS-154  
xj.lin 已提交
19 20
#pragma once

S
starlord 已提交
21
#include <gtest/gtest.h>
Y
youny626 已提交
22 23
#include <memory>
#include <vector>
Z
zirui.chen 已提交
24 25 26
#include <cstdlib>
#include <cstdio>
#include <fstream>
27

X
MS-154  
xj.lin 已提交
28

X
xiaojun.lin 已提交
29 30
#include "wrapper/VecIndex.h"
#include "wrapper/utils.h"
Z
zirui.chen 已提交
31 32
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
#include "wrapper/ConfAdapterMgr.h"
33
#include "wrapper/ConfAdapter.h"
X
xiaojun.lin 已提交
34

X
MS-154  
xj.lin 已提交
35 36 37 38
class DataGenBase;

using DataGenPtr = std::shared_ptr<DataGenBase>;

X
xiaojun.lin 已提交
39 40 41 42 43 44 45 46
constexpr int64_t DIM = 128;
constexpr int64_t NB = 100000;
constexpr int64_t NQ = 10;
constexpr int64_t DEVICEID = 0;
constexpr int64_t PINMEM = 1024 * 1024 * 200;
constexpr int64_t TEMPMEM = 1024 * 1024 * 300;
constexpr int64_t RESNUM = 2;

Z
zirui.chen 已提交
47 48
static const char *CONFIG_PATH = "/tmp/milvus_test";
static const char *CONFIG_FILE = "/server_config.yaml";
S
starlord 已提交
49 50 51

class KnowhereTest : public ::testing::Test {
 protected:
Z
zirui.chen 已提交
52 53
    void SetUp() override;
    void TearDown() override;
S
starlord 已提交
54 55
};

X
MS-154  
xj.lin 已提交
56 57
class DataGenBase {
 public:
Z
zirui.chen 已提交
58 59
    virtual void GenData(const int& dim, const int& nb, const int& nq, float* xb, float* xq, int64_t* ids,
                         const int& k, int64_t* gt_ids, float* gt_dis);
X
xiaojun.lin 已提交
60

Z
zirui.chen 已提交
61 62 63 64 65 66 67 68 69
    virtual void GenData(const int& dim,
                         const int& nb,
                         const int& nq,
                         std::vector<float>& xb,
                         std::vector<float>& xq,
                         std::vector<int64_t>& ids,
                         const int& k,
                         std::vector<int64_t>& gt_ids,
                         std::vector<float>& gt_dis);
X
xiaojun.lin 已提交
70

Z
zirui.chen 已提交
71
    void AssertResult(const std::vector<int64_t>& ids, const std::vector<float>& dis);
X
xiaojun.lin 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    int dim = DIM;
    int nb = NB;
    int nq = NQ;
    int k = 10;
    std::vector<float> xb;
    std::vector<float> xq;
    std::vector<int64_t> ids;

    // Ground Truth
    std::vector<int64_t> gt_ids;
    std::vector<float> gt_dis;
};

class ParamGenerator {
 public:
Z
zirui.chen 已提交
88
    static ParamGenerator& GetInstance() {
X
xiaojun.lin 已提交
89 90 91 92
        static ParamGenerator instance;
        return instance;
    }

Z
zirui.chen 已提交
93 94 95 96 97 98 99 100 101 102 103
    knowhere::Config GenSearchConf(const milvus::engine::IndexType& type, const milvus::engine::TempMetaConf& conf) {
        auto adapter = milvus::engine::AdapterMgr::GetInstance().GetAdapter(type);
        return adapter->MatchSearch(conf, type);
    }

    knowhere::Config GenBuild(const milvus::engine::IndexType& type, const milvus::engine::TempMetaConf& conf) {
        auto adapter = milvus::engine::AdapterMgr::GetInstance().GetAdapter(type);
        return adapter->Match(conf);
    }

    knowhere::Config Gen(const milvus::engine::IndexType& type) {
X
xiaojun.lin 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        switch (type) {
            case milvus::engine::IndexType::FAISS_IDMAP: {
                auto tempconf = std::make_shared<knowhere::Cfg>();
                tempconf->metric_type = knowhere::METRICTYPE::L2;
                return tempconf;
            }
            case milvus::engine::IndexType::FAISS_IVFFLAT_CPU:
            case milvus::engine::IndexType::FAISS_IVFFLAT_GPU:
            case milvus::engine::IndexType::FAISS_IVFFLAT_MIX: {
                auto tempconf = std::make_shared<knowhere::IVFCfg>();
                tempconf->nlist = 100;
                tempconf->nprobe = 16;
                tempconf->metric_type = knowhere::METRICTYPE::L2;
                return tempconf;
            }
            case milvus::engine::IndexType::FAISS_IVFSQ8_HYBRID:
            case milvus::engine::IndexType::FAISS_IVFSQ8_CPU:
            case milvus::engine::IndexType::FAISS_IVFSQ8_GPU:
            case milvus::engine::IndexType::FAISS_IVFSQ8_MIX: {
                auto tempconf = std::make_shared<knowhere::IVFSQCfg>();
                tempconf->nlist = 100;
                tempconf->nprobe = 16;
                tempconf->nbits = 8;
                tempconf->metric_type = knowhere::METRICTYPE::L2;
                return tempconf;
            }
Z
zirui.chen 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            case milvus::engine::IndexType::FAISS_IVFPQ_CPU:
            case milvus::engine::IndexType::FAISS_IVFPQ_GPU:
            case milvus::engine::IndexType::FAISS_IVFPQ_MIX: {
                auto tempconf = std::make_shared<knowhere::IVFPQCfg>();
                tempconf->nlist = 100;
                tempconf->nprobe = 16;
                tempconf->nbits = 8;
                tempconf->m = 8;
                tempconf->metric_type = knowhere::METRICTYPE::L2;
                return tempconf;
            }
            case milvus::engine::IndexType::NSG_MIX: {
                auto tempconf = std::make_shared<knowhere::NSGCfg>();
                tempconf->nlist = 100;
                tempconf->nprobe = 16;
                tempconf->search_length = 8;
                tempconf->knng = 200;
                tempconf->search_length = 40; // TODO(linxj): be 20 when search
                tempconf->out_degree = 60;
                tempconf->candidate_pool_size = 200;
                tempconf->metric_type = knowhere::METRICTYPE::L2;
                return tempconf;
            }
X
xiaojun.lin 已提交
153 154
        }
    }
X
MS-154  
xj.lin 已提交
155 156
};

Z
zirui.chen 已提交
157 158

//class SanityCheck : public DataGenBase {
X
xj.lin 已提交
159 160 161 162
// public:
//    void GenData(const int &dim, const int &nb, const int &nq, float *xb, float *xq, long *ids,
//                 const int &k, long *gt_ids, float *gt_dis) override;
//};
Z
zirui.chen 已提交
163