knowhere_test.cpp 9.6 KB
Newer Older
X
MS-154  
xj.lin 已提交
1 2 3 4 5 6 7
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////

#include <gtest/gtest.h>
X
xj.lin 已提交
8
#include <easylogging++.h>
X
MS-154  
xj.lin 已提交
9 10

#include <wrapper/knowhere/vec_index.h>
W
wxyu 已提交
11
#include "knowhere/index/vector_index/gpu_ivf.h"
X
MS-154  
xj.lin 已提交
12 13 14

#include "utils.h"

X
xj.lin 已提交
15
INITIALIZE_EASYLOGGINGPP
X
MS-154  
xj.lin 已提交
16

X
xj.lin 已提交
17
using namespace zilliz::milvus::engine;
W
wxyu 已提交
18
//using namespace zilliz::knowhere;
X
MS-154  
xj.lin 已提交
19 20 21 22 23

using ::testing::TestWithParam;
using ::testing::Values;
using ::testing::Combine;

X
xj.lin 已提交
24 25 26
constexpr int64_t DIM = 128;
constexpr int64_t NB = 100000;
constexpr int64_t DEVICE_ID = 0;
X
MS-154  
xj.lin 已提交
27 28

class KnowhereWrapperTest
X
xj.lin 已提交
29
    : public TestWithParam<::std::tuple<IndexType, std::string, int, int, int, int, Config, Config>> {
X
MS-154  
xj.lin 已提交
30 31
 protected:
    void SetUp() override {
32
        zilliz::knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(DEVICE_ID, 1024*1024*200, 1024*1024*300, 2);
S
starlord 已提交
33

X
MS-154  
xj.lin 已提交
34 35 36 37 38
        std::string generator_type;
        std::tie(index_type, generator_type, dim, nb, nq, k, train_cfg, search_cfg) = GetParam();

        //auto generator = GetGenerateFactory(generator_type);
        auto generator = std::make_shared<DataGenBase>();
X
xj.lin 已提交
39
        generator->GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
X
MS-154  
xj.lin 已提交
40 41 42

        index_ = GetVecIndexFactory(index_type);
    }
43 44 45
    void TearDown() override {
        zilliz::knowhere::FaissGpuResourceMgr::GetInstance().Free();
    }
X
MS-154  
xj.lin 已提交
46

X
xj.lin 已提交
47 48 49 50 51 52
    void AssertResult(const std::vector<long> &ids, const std::vector<float> &dis) {
        EXPECT_EQ(ids.size(), nq * k);
        EXPECT_EQ(dis.size(), nq * k);

        for (auto i = 0; i < nq; i++) {
            EXPECT_EQ(ids[i * k], gt_ids[i * k]);
X
xj.lin 已提交
53
            //EXPECT_EQ(dis[i * k], gt_dis[i * k]);
X
xj.lin 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        }

        int match = 0;
        for (int i = 0; i < nq; ++i) {
            for (int j = 0; j < k; ++j) {
                for (int l = 0; l < k; ++l) {
                    if (ids[i * nq + j] == gt_ids[i * nq + l]) match++;
                }
            }
        }

        auto precision = float(match) / (nq * k);
        EXPECT_GT(precision, 0.5);
        std::cout << std::endl << "Precision: " << precision
                  << ", match: " << match
                  << ", total: " << nq * k
                  << std::endl;
    }

X
MS-154  
xj.lin 已提交
73
 protected:
X
xj.lin 已提交
74
    IndexType index_type;
X
MS-154  
xj.lin 已提交
75 76 77
    Config train_cfg;
    Config search_cfg;

S
starlord 已提交
78 79
    int dim = DIM;
    int nb = NB;
X
MS-154  
xj.lin 已提交
80 81 82 83 84 85 86 87 88 89
    int nq = 10;
    int k = 10;
    std::vector<float> xb;
    std::vector<float> xq;
    std::vector<long> ids;

    VecIndexPtr index_ = nullptr;

    // Ground Truth
    std::vector<long> gt_ids;
X
xj.lin 已提交
90
    std::vector<float> gt_dis;
X
MS-154  
xj.lin 已提交
91 92 93 94
};

INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
X
xj.lin 已提交
95
                            //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
X
xj.lin 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                            std::make_tuple(IndexType::FAISS_IVFFLAT_CPU, "Default",
                                            64, 100000, 10, 10,
                                            Config::object{{"nlist", 100}, {"dim", 64}, {"metric_type", "L2"}},
                                            Config::object{{"dim", 64}, {"k", 10}, {"nprobe", 10}}
                            ),
                            // to_gpu_test Failed
                            std::make_tuple(IndexType::FAISS_IVFFLAT_GPU, "Default",
                                            DIM, NB, 10, 10,
                                            Config::object{{"nlist", 100}, {"dim", DIM}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 40}}
                            ),
                            std::make_tuple(IndexType::FAISS_IVFFLAT_MIX, "Default",
                                            64, 100000, 10, 10,
                                            Config::object{{"nlist", 1000}, {"dim", 64}, {"metric_type", "L2"}},
                                            Config::object{{"dim", 64}, {"k", 10}, {"nprobe", 5}}
                            ),
                            std::make_tuple(IndexType::FAISS_IDMAP, "Default",
                                            64, 100000, 10, 10,
                                            Config::object{{"dim", 64}, {"metric_type", "L2"}},
                                            Config::object{{"dim", 64}, {"k", 10}}
                            ),
                            std::make_tuple(IndexType::FAISS_IVFSQ8_CPU, "Default",
                                            DIM, NB, 10, 10,
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
                            ),
                            std::make_tuple(IndexType::FAISS_IVFSQ8_GPU, "Default",
                                            DIM, NB, 10, 10,
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
                            ),
X
xj.lin 已提交
127
                            std::make_tuple(IndexType::FAISS_IVFSQ8_MIX, "Default",
S
starlord 已提交
128
                                            DIM, NB, 10, 10,
X
xj.lin 已提交
129
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
S
starlord 已提交
130
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
X
MS-154  
xj.lin 已提交
131
                            )
W
wxyu 已提交
132 133 134 135 136 137
//                            std::make_tuple(IndexType::NSG_MIX, "Default",
//                                            128, 250000, 10, 10,
//                                            Config::object{{"dim", 128}, {"nlist", 8192}, {"nprobe", 16}, {"metric_type", "L2"},
//                                                           {"knng", 200}, {"search_length", 40}, {"out_degree", 60}, {"candidate_pool_size", 200}},
//                                            Config::object{{"k", 10}, {"search_length", 20}}
//                            )
X
xj.lin 已提交
138 139 140 141 142
                            //std::make_tuple(IndexType::SPTAG_KDT_RNT_CPU, "Default",
                            //                64, 10000, 10, 10,
                            //                Config::object{{"TPTNumber", 1}, {"dim", 64}},
                            //                Config::object{{"dim", 64}, {"k", 10}}
                            //)
X
MS-154  
xj.lin 已提交
143 144 145 146
                        )
);

TEST_P(KnowhereWrapperTest, base_test) {
X
xj.lin 已提交
147 148 149 150 151
    EXPECT_EQ(index_->GetType(), index_type);

    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
MS-154  
xj.lin 已提交
152 153

    index_->BuildAll(nb, xb.data(), ids.data(), train_cfg);
X
xj.lin 已提交
154 155
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
156 157
}

W
wxyu 已提交
158 159 160 161 162 163 164 165 166 167
TEST_P(KnowhereWrapperTest, to_gpu_test) {
    EXPECT_EQ(index_->GetType(), index_type);

    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);

    index_->BuildAll(nb, xb.data(), ids.data(), train_cfg);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
    AssertResult(res_ids, res_dis);
X
xj.lin 已提交
168

W
wxyu 已提交
169
    {
X
xj.lin 已提交
170 171 172 173 174
        auto dev_idx = index_->CopyToGpu(DEVICE_ID);
        for (int i = 0; i < 10; ++i) {
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
175 176
    }

X
xj.lin 已提交
177 178 179 180
    {
        std::string file_location = "/tmp/test_gpu_file";
        write_index(index_, file_location);
        auto new_index = read_index(file_location);
W
wxyu 已提交
181

X
xj.lin 已提交
182 183 184 185 186
        auto dev_idx = new_index->CopyToGpu(DEVICE_ID);
        for (int i = 0; i < 10; ++i) {
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
187
    }
X
xj.lin 已提交
188 189 190 191
}

TEST_P(KnowhereWrapperTest, to_cpu_test) {
    // dev
W
wxyu 已提交
192 193
}

X
xj.lin 已提交
194 195
TEST_P(KnowhereWrapperTest, serialize) {
    EXPECT_EQ(index_->GetType(), index_type);
X
MS-154  
xj.lin 已提交
196

X
xj.lin 已提交
197 198 199
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
MS-154  
xj.lin 已提交
200
    index_->BuildAll(nb, xb.data(), ids.data(), train_cfg);
X
xj.lin 已提交
201 202
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
203 204

    {
X
xj.lin 已提交
205 206 207 208 209 210 211 212 213 214 215
        auto binary = index_->Serialize();
        auto type = index_->GetType();
        auto new_index = GetVecIndexFactory(type);
        new_index->Load(binary);
        EXPECT_EQ(new_index->Dimension(), index_->Dimension());
        EXPECT_EQ(new_index->Count(), index_->Count());

        std::vector<int64_t> res_ids(elems);
        std::vector<float> res_dis(elems);
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
        AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
216 217
    }

X
xj.lin 已提交
218 219 220 221
    {
        std::string file_location = "/tmp/whatever";
        write_index(index_, file_location);
        auto new_index = read_index(file_location);
X
xj.lin 已提交
222
        EXPECT_EQ(new_index->GetType(), ConvertToCpuIndexType(index_type));
X
xj.lin 已提交
223 224 225 226 227 228 229 230
        EXPECT_EQ(new_index->Dimension(), index_->Dimension());
        EXPECT_EQ(new_index->Count(), index_->Count());

        std::vector<int64_t> res_ids(elems);
        std::vector<float> res_dis(elems);
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
        AssertResult(res_ids, res_dis);
    }
X
MS-154  
xj.lin 已提交
231
}
X
xj.lin 已提交
232

X
xj.lin 已提交
233 234 235 236
// TODO(linxj): add exception test
//TEST_P(KnowhereWrapperTest, exception_test) {
//}