utils.h 5.4 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.

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

S
starlord 已提交
20
#include <gtest/gtest.h>
X
MS-154  
xj.lin 已提交
21
#include <cstdio>
Y
youny626 已提交
22
#include <cstdlib>
X
MS-154  
xj.lin 已提交
23
#include <fstream>
Y
youny626 已提交
24 25
#include <memory>
#include <vector>
X
MS-154  
xj.lin 已提交
26

Y
youny626 已提交
27
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
X
xiaojun.lin 已提交
28 29 30
#include "wrapper/VecIndex.h"
#include "wrapper/utils.h"

X
MS-154  
xj.lin 已提交
31 32 33 34
class DataGenBase;

using DataGenPtr = std::shared_ptr<DataGenBase>;

X
xiaojun.lin 已提交
35 36 37 38 39 40 41 42
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;

Y
youny626 已提交
43 44
static const char* CONFIG_PATH = "/tmp/milvus_test";
static const char* CONFIG_FILE = "/server_config.yaml";
S
starlord 已提交
45 46 47

class KnowhereTest : public ::testing::Test {
 protected:
Y
youny626 已提交
48 49 50 51
    void
    SetUp() override;
    void
    TearDown() override;
S
starlord 已提交
52 53
};

X
MS-154  
xj.lin 已提交
54 55
class DataGenBase {
 public:
Y
youny626 已提交
56 57 58
    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 已提交
59

Y
youny626 已提交
60 61 62
    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 已提交
63

Y
youny626 已提交
64 65
    void
    AssertResult(const std::vector<int64_t>& ids, const std::vector<float>& dis);
X
xiaojun.lin 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    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:
Y
youny626 已提交
82 83
    static ParamGenerator&
    GetInstance() {
X
xiaojun.lin 已提交
84 85 86 87
        static ParamGenerator instance;
        return instance;
    }

Y
youny626 已提交
88 89
    knowhere::Config
    Gen(const milvus::engine::IndexType& type) {
X
xiaojun.lin 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        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;
            }
Y
youny626 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                //            case milvus::engine::IndexType::FAISS_IVFPQ_CPU:
                //            case milvus::engine::IndexType::FAISS_IVFPQ_GPU: {
                //                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 已提交
138 139
        }
    }
X
MS-154  
xj.lin 已提交
140 141
};

Y
youny626 已提交
142
// class SanityCheck : public DataGenBase {
X
xj.lin 已提交
143 144 145 146
// 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;
//};