knowhere_test.cpp 10.0 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

#include <gtest/gtest.h>
20
#include "utils/easylogging++.h"
X
MS-154  
xj.lin 已提交
21

X
xiaojun.lin 已提交
22
#include "src/wrapper/vec_index.h"
W
wxyu 已提交
23
#include "knowhere/index/vector_index/gpu_ivf.h"
X
MS-154  
xj.lin 已提交
24 25 26

#include "utils.h"

X
xj.lin 已提交
27
INITIALIZE_EASYLOGGINGPP
X
MS-154  
xj.lin 已提交
28

X
xj.lin 已提交
29
using namespace zilliz::milvus::engine;
W
wxyu 已提交
30
//using namespace zilliz::knowhere;
X
MS-154  
xj.lin 已提交
31 32 33 34 35

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

X
xj.lin 已提交
36 37 38
constexpr int64_t DIM = 128;
constexpr int64_t NB = 100000;
constexpr int64_t DEVICE_ID = 0;
X
MS-154  
xj.lin 已提交
39 40

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

X
MS-154  
xj.lin 已提交
46 47 48 49
        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 已提交
50
        generator->GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
X
MS-154  
xj.lin 已提交
51 52 53

        index_ = GetVecIndexFactory(index_type);
    }
54 55 56
    void TearDown() override {
        zilliz::knowhere::FaissGpuResourceMgr::GetInstance().Free();
    }
X
MS-154  
xj.lin 已提交
57

X
xj.lin 已提交
58 59 60 61 62 63
    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 已提交
64
            //EXPECT_EQ(dis[i * k], gt_dis[i * k]);
X
xj.lin 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        }

        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 已提交
84
 protected:
X
xj.lin 已提交
85
    IndexType index_type;
X
MS-154  
xj.lin 已提交
86 87 88
    Config train_cfg;
    Config search_cfg;

S
starlord 已提交
89 90
    int dim = DIM;
    int nb = NB;
X
MS-154  
xj.lin 已提交
91 92 93 94 95 96 97 98 99 100
    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 已提交
101
    std::vector<float> gt_dis;
X
MS-154  
xj.lin 已提交
102 103 104 105
};

INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
X
xj.lin 已提交
106
                            //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
X
xj.lin 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                            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 已提交
138
                            std::make_tuple(IndexType::FAISS_IVFSQ8_MIX, "Default",
S
starlord 已提交
139
                                            DIM, NB, 10, 10,
X
xj.lin 已提交
140
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
S
starlord 已提交
141
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
X
MS-154  
xj.lin 已提交
142
                            )
W
wxyu 已提交
143 144 145 146 147 148
//                            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 已提交
149 150 151 152 153
                            //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 已提交
154 155 156
                        )
);

S
starlord 已提交
157
TEST_P(KnowhereWrapperTest, BASE_TEST) {
X
xj.lin 已提交
158 159 160 161 162
    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 已提交
163 164

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

S
starlord 已提交
169
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
W
wxyu 已提交
170 171 172 173 174 175 176 177 178
    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 已提交
179

W
wxyu 已提交
180
    {
X
xj.lin 已提交
181 182 183 184 185
        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 已提交
186 187
    }

X
xj.lin 已提交
188
    {
S
starlord 已提交
189
        std::string file_location = "/tmp/knowhere_gpu_file";
X
xj.lin 已提交
190 191
        write_index(index_, file_location);
        auto new_index = read_index(file_location);
W
wxyu 已提交
192

X
xj.lin 已提交
193 194 195 196 197
        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 已提交
198
    }
X
xj.lin 已提交
199 200
}

S
starlord 已提交
201
TEST_P(KnowhereWrapperTest, TO_CPU_TEST) {
X
xj.lin 已提交
202
    // dev
W
wxyu 已提交
203 204
}

S
starlord 已提交
205
TEST_P(KnowhereWrapperTest, SERIALIZE_TEST) {
X
xj.lin 已提交
206
    EXPECT_EQ(index_->GetType(), index_type);
X
MS-154  
xj.lin 已提交
207

X
xj.lin 已提交
208 209 210
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
MS-154  
xj.lin 已提交
211
    index_->BuildAll(nb, xb.data(), ids.data(), train_cfg);
X
xj.lin 已提交
212 213
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), search_cfg);
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
214 215

    {
X
xj.lin 已提交
216 217 218 219 220 221 222 223 224 225 226
        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 已提交
227 228
    }

X
xj.lin 已提交
229
    {
S
starlord 已提交
230
        std::string file_location = "/tmp/knowhere";
X
xj.lin 已提交
231 232
        write_index(index_, file_location);
        auto new_index = read_index(file_location);
X
xj.lin 已提交
233
        EXPECT_EQ(new_index->GetType(), ConvertToCpuIndexType(index_type));
X
xj.lin 已提交
234 235 236 237 238 239 240 241
        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 已提交
242
}
X
xj.lin 已提交
243

X
xj.lin 已提交
244 245 246 247
// TODO(linxj): add exception test
//TEST_P(KnowhereWrapperTest, exception_test) {
//}