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.

18
#include "utils/easylogging++.h"
19
#include "src/wrapper/VecIndex.h"
X
xiaojun.lin 已提交
20
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
X
MS-154  
xj.lin 已提交
21 22
#include "utils.h"

23 24
#include <gtest/gtest.h>

X
xj.lin 已提交
25
INITIALIZE_EASYLOGGINGPP
X
MS-154  
xj.lin 已提交
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

G
groot 已提交
199
TEST_P(KnowhereWrapperTest, TO_CPU_TEST) {
X
xj.lin 已提交
200
    // dev
W
wxyu 已提交
201 202
}

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

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

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

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

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