knowhere_test.cpp 9.5 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
        std::string generator_type;
        std::tie(index_type, generator_type, dim, nb, nq, k, train_cfg, search_cfg) = GetParam();

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

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

X
xj.lin 已提交
46 47 48 49 50 51
    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 已提交
52
            //EXPECT_EQ(dis[i * k], gt_dis[i * k]);
X
xj.lin 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        }

        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 已提交
72
 protected:
X
xj.lin 已提交
73
    IndexType index_type;
X
MS-154  
xj.lin 已提交
74 75 76
    Config train_cfg;
    Config search_cfg;

S
starlord 已提交
77 78
    int dim = DIM;
    int nb = NB;
X
MS-154  
xj.lin 已提交
79 80 81 82 83 84 85 86 87 88
    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 已提交
89
    std::vector<float> gt_dis;
X
MS-154  
xj.lin 已提交
90 91 92 93
};

INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
X
xj.lin 已提交
94
                            //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
X
xj.lin 已提交
95 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
                            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 已提交
126
                            std::make_tuple(IndexType::FAISS_IVFSQ8_MIX, "Default",
S
starlord 已提交
127
                                            DIM, NB, 10, 10,
X
xj.lin 已提交
128
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
S
starlord 已提交
129
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
X
MS-154  
xj.lin 已提交
130
                            )
W
wxyu 已提交
131 132 133 134 135 136
//                            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 已提交
137 138 139 140 141
                            //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 已提交
142 143 144 145
                        )
);

TEST_P(KnowhereWrapperTest, base_test) {
X
xj.lin 已提交
146 147 148 149 150
    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 已提交
151 152

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

W
wxyu 已提交
157 158 159 160 161 162 163 164 165 166
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 已提交
167

W
wxyu 已提交
168
    {
X
xj.lin 已提交
169 170 171 172 173
        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 已提交
174 175
    }

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

X
xj.lin 已提交
181 182 183 184 185
        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 已提交
186
    }
X
xj.lin 已提交
187 188 189 190
}

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

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

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

    {
X
xj.lin 已提交
204 205 206 207 208 209 210 211 212 213 214
        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 已提交
215 216
    }

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

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