wrapper_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"
X
xiaojun.lin 已提交
23 24
#include "knowhere/index/vector_index/IndexGPUIVF.h"
#include "knowhere/index/vector_index/utils/FaissGpuResourceMgr.h"
X
MS-154  
xj.lin 已提交
25 26 27

#include "utils.h"

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

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

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

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

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

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

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

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

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

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

INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
X
xj.lin 已提交
107
                            //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
X
xj.lin 已提交
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 138
                            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 已提交
139
                            std::make_tuple(IndexType::FAISS_IVFSQ8_MIX, "Default",
G
groot 已提交
140
                                            DIM, NB, 10, 10,
X
xj.lin 已提交
141
                                            Config::object{{"dim", DIM}, {"nlist", 1000}, {"nbits", 8}, {"metric_type", "L2"}, {"gpu_id", DEVICE_ID}},
G
groot 已提交
142
                                            Config::object{{"dim", DIM}, {"k", 10}, {"nprobe", 5}}
X
MS-154  
xj.lin 已提交
143
                            )
W
wxyu 已提交
144 145 146 147 148 149
//                            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 已提交
150 151 152 153 154
                            //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 已提交
155 156 157
                        )
);

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

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

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

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

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

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

G
groot 已提交
202
TEST_P(KnowhereWrapperTest, TO_CPU_TEST) {
X
xj.lin 已提交
203
    // dev
W
wxyu 已提交
204 205
}

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

X
xj.lin 已提交
209 210 211
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
MS-154  
xj.lin 已提交
212
    index_->BuildAll(nb, xb.data(), ids.data(), train_cfg);
X
xj.lin 已提交
213 214
    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 221 222 223 224 225 226 227
        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 已提交
228 229
    }

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

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