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

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
using ::testing::Combine;
X
MS-154  
xj.lin 已提交
33 34 35 36
using ::testing::TestWithParam;
using ::testing::Values;

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

Z
zirui.chen 已提交
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;
Z
zirui.chen 已提交
55
        tempconf.nprobe = 16;
Z
zirui.chen 已提交
56

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

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

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

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

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

Y
youny626 已提交
85 86
#ifdef MILVUS_GPU_VERSION
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_GPU, "Default", DIM, NB, 10, 10),
Z
zirui.chen 已提交
87
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_MIX, "Default", 64, 1000, 10, 10),
Y
youny626 已提交
88 89
        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 已提交
90
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFPQ_MIX, "Default", 64, 1000, 10, 10),
91
        // std::make_tuple(milvus::engine::IndexType::NSG_MIX, "Default", 128, 250000, 10, 10),
Y
youny626 已提交
92
#endif
93 94
        // std::make_tuple(milvus::engine::IndexType::SPTAG_KDT_RNT_CPU, "Default", 128, 100, 10, 10),
        // std::make_tuple(milvus::engine::IndexType::SPTAG_BKT_RNT_CPU, "Default", 128, 100, 10, 10),
Z
zirui.chen 已提交
95 96
        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 已提交
97
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_CPU, "Default", DIM, NB, 10, 10)));
X
MS-154  
xj.lin 已提交
98

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

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

Y
youny626 已提交
111
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
112
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
W
wxyu 已提交
113 114 115 116 117 118
    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 已提交
119
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
Z
zirui.chen 已提交
120
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
W
wxyu 已提交
121
    AssertResult(res_ids, res_dis);
X
xj.lin 已提交
122

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

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

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

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

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

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

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

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

Y
Yu Kun 已提交
186 187 188 189 190 191 192 193 194
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);
}