test_wrapper.cpp 7.1 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.

W
wxyu 已提交
18
#include "easyloggingpp/easylogging++.h"
S
starlord 已提交
19
#include "wrapper/VecIndex.h"
Y
youny626 已提交
20 21

#ifdef MILVUS_GPU_VERSION
X
xiaojun.lin 已提交
22
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
Y
youny626 已提交
23 24
#endif

X
xiaojun.lin 已提交
25
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
S
starlord 已提交
26
#include "wrapper/utils.h"
X
MS-154  
xj.lin 已提交
27

28 29
#include <gtest/gtest.h>

X
xj.lin 已提交
30
INITIALIZE_EASYLOGGINGPP
X
MS-154  
xj.lin 已提交
31 32 33

using ::testing::TestWithParam;
using ::testing::Values;
Z
zirui.chen 已提交
34
using ::testing::Combine;
X
MS-154  
xj.lin 已提交
35 36

class KnowhereWrapperTest
Z
zirui.chen 已提交
37 38
        : public DataGenBase,
          public TestWithParam<::std::tuple<milvus::engine::IndexType, std::string, int, int, int, int>> {
Y
youny626 已提交
39
 protected:
Z
zirui.chen 已提交
40
    void SetUp() override {
Y
youny626 已提交
41
#ifdef MILVUS_GPU_VERSION
X
xiaojun.lin 已提交
42
        knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(DEVICEID, PINMEM, TEMPMEM, RESNUM);
Y
youny626 已提交
43
#endif
X
MS-154  
xj.lin 已提交
44
        std::string generator_type;
X
xiaojun.lin 已提交
45
        std::tie(index_type, generator_type, dim, nb, nq, k) = GetParam();
X
xiaojun.lin 已提交
46
        GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
X
MS-154  
xj.lin 已提交
47

Z
zirui.chen 已提交
48 49 50 51 52 53 54
        milvus::engine::TempMetaConf tempconf;
        tempconf.metric_type = knowhere::METRICTYPE::L2;
        tempconf.gpu_id = DEVICEID;
        tempconf.size = nb;
        tempconf.dim = dim;
        tempconf.k = k;

X
MS-154  
xj.lin 已提交
55
        index_ = GetVecIndexFactory(index_type);
Z
zirui.chen 已提交
56 57 58 59 60 61
        conf = ParamGenerator::GetInstance().GenBuild(index_type, tempconf);
        searchconf = ParamGenerator::GetInstance().GenSearchConf(index_type, tempconf);

//        conf->k = k;
//        conf->d = dim;
//        conf->gpu_id = DEVICEID;
X
MS-154  
xj.lin 已提交
62
    }
S
starlord 已提交
63

Z
zirui.chen 已提交
64
    void TearDown() override {
Y
youny626 已提交
65
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
66
        knowhere::FaissGpuResourceMgr::GetInstance().Free();
Y
youny626 已提交
67
#endif
68
    }
X
MS-154  
xj.lin 已提交
69

Y
youny626 已提交
70
 protected:
S
starlord 已提交
71 72
    milvus::engine::IndexType index_type;
    milvus::engine::VecIndexPtr index_ = nullptr;
X
xiaojun.lin 已提交
73
    knowhere::Config conf;
Z
zirui.chen 已提交
74
    knowhere::Config searchconf;
X
MS-154  
xj.lin 已提交
75 76
};

Z
zirui.chen 已提交
77 78 79
INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
                        //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
Z
zirui.chen 已提交
80

Y
youny626 已提交
81 82
#ifdef MILVUS_GPU_VERSION
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_GPU, "Default", DIM, NB, 10, 10),
Z
zirui.chen 已提交
83
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_MIX, "Default", 64, 1000, 10, 10),
Y
youny626 已提交
84 85
        //                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB,
        //                            10, 10),
Y
youny626 已提交
86 87
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB, 10, 10),
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_MIX, "Default", DIM, NB, 10, 10),
Z
zirui.chen 已提交
88
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFPQ_MIX, "Default", 64, 1000, 10, 10),
Z
zirui.chen 已提交
89

X
xiaojun.lin 已提交
90
//                            std::make_tuple(IndexType::NSG_MIX, "Default", 128, 250000, 10, 10),
Y
youny626 已提交
91
#endif
Y
youny626 已提交
92
        //                            std::make_tuple(IndexType::SPTAG_KDT_RNT_CPU, "Default", 128, 250000, 10, 10),
Z
zirui.chen 已提交
93 94
        std::make_tuple(milvus::engine::IndexType::FAISS_IDMAP, "Default", 64, 1000, 10, 10),
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_CPU, "Default", 64, 1000, 10, 10),
Y
youny626 已提交
95
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_CPU, "Default", DIM, NB, 10, 10)));
X
MS-154  
xj.lin 已提交
96

S
starlord 已提交
97
TEST_P(KnowhereWrapperTest, BASE_TEST) {
X
xj.lin 已提交
98 99 100 101 102
    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 已提交
103

X
xiaojun.lin 已提交
104
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
Z
zirui.chen 已提交
105
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
106
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
107 108
}

Y
youny626 已提交
109
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
110
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
W
wxyu 已提交
111 112 113 114 115 116
    EXPECT_EQ(index_->GetType(), index_type);

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

X
xiaojun.lin 已提交
117
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
Z
zirui.chen 已提交
118
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
W
wxyu 已提交
119
    AssertResult(res_ids, res_dis);
X
xj.lin 已提交
120

W
wxyu 已提交
121
    {
X
xiaojun.lin 已提交
122
        auto dev_idx = index_->CopyToGpu(DEVICEID);
X
xj.lin 已提交
123
        for (int i = 0; i < 10; ++i) {
Z
zirui.chen 已提交
124
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
125 126
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
127 128
    }

X
xj.lin 已提交
129
    {
S
starlord 已提交
130
        std::string file_location = "/tmp/knowhere_gpu_file";
X
xj.lin 已提交
131
        write_index(index_, file_location);
S
starlord 已提交
132
        auto new_index = milvus::engine::read_index(file_location);
W
wxyu 已提交
133

X
xiaojun.lin 已提交
134
        auto dev_idx = new_index->CopyToGpu(DEVICEID);
X
xj.lin 已提交
135
        for (int i = 0; i < 10; ++i) {
Z
zirui.chen 已提交
136
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
137 138
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
139
    }
X
xj.lin 已提交
140
}
Y
youny626 已提交
141
#endif
X
xj.lin 已提交
142

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

X
xj.lin 已提交
146 147 148
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
xiaojun.lin 已提交
149
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
Z
zirui.chen 已提交
150
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
151
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
152 153

    {
X
xj.lin 已提交
154 155 156 157 158 159 160 161 162
        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);
Z
zirui.chen 已提交
163
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
164
        AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
165 166
    }

X
xj.lin 已提交
167
    {
S
starlord 已提交
168
        std::string file_location = "/tmp/knowhere";
X
xj.lin 已提交
169
        write_index(index_, file_location);
S
starlord 已提交
170
        auto new_index = milvus::engine::read_index(file_location);
X
xj.lin 已提交
171
        EXPECT_EQ(new_index->GetType(), ConvertToCpuIndexType(index_type));
X
xj.lin 已提交
172 173 174 175 176
        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);
Z
zirui.chen 已提交
177
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
X
xj.lin 已提交
178 179
        AssertResult(res_ids, res_dis);
    }
X
MS-154  
xj.lin 已提交
180
}
Y
Yu Kun 已提交
181 182

#include "wrapper/ConfAdapter.h"
Y
youny626 已提交
183

Y
Yu Kun 已提交
184 185 186 187 188 189 190 191 192
TEST(whatever, test_config) {
    milvus::engine::TempMetaConf conf;
    auto nsg_conf = std::make_shared<milvus::engine::NSGConfAdapter>();
    nsg_conf->Match(conf);
    nsg_conf->MatchSearch(conf, milvus::engine::IndexType::FAISS_IVFPQ_GPU);

    auto pq_conf = std::make_shared<milvus::engine::IVFPQConfAdapter>();
    pq_conf->Match(conf);
}