test_wrapper.cpp 6.6 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 "external/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

Y
youny626 已提交
32
using ::testing::Combine;
X
MS-154  
xj.lin 已提交
33 34 35 36
using ::testing::TestWithParam;
using ::testing::Values;

class KnowhereWrapperTest
Y
youny626 已提交
37 38 39 40 41
    : public DataGenBase,
      public TestWithParam<::std::tuple<milvus::engine::IndexType, std::string, int, int, int, int>> {
 protected:
    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
S
starlord 已提交
45

X
MS-154  
xj.lin 已提交
46
        std::string generator_type;
X
xiaojun.lin 已提交
47
        std::tie(index_type, generator_type, dim, nb, nq, k) = GetParam();
X
xiaojun.lin 已提交
48
        GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
X
MS-154  
xj.lin 已提交
49 50

        index_ = GetVecIndexFactory(index_type);
X
xiaojun.lin 已提交
51 52 53
        conf = ParamGenerator::GetInstance().Gen(index_type);
        conf->k = k;
        conf->d = dim;
X
xiaojun.lin 已提交
54
        conf->gpu_id = DEVICEID;
X
MS-154  
xj.lin 已提交
55
    }
S
starlord 已提交
56

Y
youny626 已提交
57 58
    void
    TearDown() override {
Y
youny626 已提交
59
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
60
        knowhere::FaissGpuResourceMgr::GetInstance().Free();
Y
youny626 已提交
61
#endif
62
    }
X
MS-154  
xj.lin 已提交
63

Y
youny626 已提交
64
 protected:
S
starlord 已提交
65 66
    milvus::engine::IndexType index_type;
    milvus::engine::VecIndexPtr index_ = nullptr;
X
xiaojun.lin 已提交
67
    knowhere::Config conf;
X
MS-154  
xj.lin 已提交
68 69
};

Y
youny626 已提交
70 71 72 73
INSTANTIATE_TEST_CASE_P(
    WrapperParam, KnowhereWrapperTest,
    Values(
//["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
Y
youny626 已提交
74 75
#ifdef MILVUS_GPU_VERSION
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_GPU, "Default", DIM, NB, 10, 10),
Y
youny626 已提交
76 77 78
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_MIX, "Default", 64, 100000, 10, 10),
        //                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB,
        //                            10, 10),
Y
youny626 已提交
79 80
        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),
X
xiaojun.lin 已提交
81
//                            std::make_tuple(IndexType::NSG_MIX, "Default", 128, 250000, 10, 10),
Y
youny626 已提交
82
#endif
Y
youny626 已提交
83
        //                            std::make_tuple(IndexType::SPTAG_KDT_RNT_CPU, "Default", 128, 250000, 10, 10),
Y
youny626 已提交
84
        std::make_tuple(milvus::engine::IndexType::FAISS_IDMAP, "Default", 64, 100000, 10, 10),
Y
youny626 已提交
85 86
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_CPU, "Default", 64, 100000, 10, 10),
        std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_CPU, "Default", DIM, NB, 10, 10)));
X
MS-154  
xj.lin 已提交
87

S
starlord 已提交
88
TEST_P(KnowhereWrapperTest, BASE_TEST) {
X
xj.lin 已提交
89 90 91 92 93
    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 已提交
94

X
xiaojun.lin 已提交
95 96
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
97
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
98 99
}

Y
youny626 已提交
100 101
#ifdef MILVUS_GPU_VERSION

S
starlord 已提交
102
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
W
wxyu 已提交
103 104 105 106 107 108
    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 已提交
109 110
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
W
wxyu 已提交
111
    AssertResult(res_ids, res_dis);
X
xj.lin 已提交
112

W
wxyu 已提交
113
    {
X
xiaojun.lin 已提交
114
        auto dev_idx = index_->CopyToGpu(DEVICEID);
X
xj.lin 已提交
115
        for (int i = 0; i < 10; ++i) {
X
xiaojun.lin 已提交
116
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
117 118
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
119 120
    }

X
xj.lin 已提交
121
    {
S
starlord 已提交
122
        std::string file_location = "/tmp/knowhere_gpu_file";
X
xj.lin 已提交
123
        write_index(index_, file_location);
S
starlord 已提交
124
        auto new_index = milvus::engine::read_index(file_location);
W
wxyu 已提交
125

X
xiaojun.lin 已提交
126
        auto dev_idx = new_index->CopyToGpu(DEVICEID);
X
xj.lin 已提交
127
        for (int i = 0; i < 10; ++i) {
X
xiaojun.lin 已提交
128
            dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
129 130
        }
        AssertResult(res_ids, res_dis);
W
wxyu 已提交
131
    }
X
xj.lin 已提交
132
}
Y
youny626 已提交
133
#endif
X
xj.lin 已提交
134

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

X
xj.lin 已提交
138 139 140
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
xiaojun.lin 已提交
141 142
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
143
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
144 145

    {
X
xj.lin 已提交
146 147 148 149 150 151 152 153 154
        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);
X
xiaojun.lin 已提交
155
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
156
        AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
157 158
    }

X
xj.lin 已提交
159
    {
S
starlord 已提交
160
        std::string file_location = "/tmp/knowhere";
X
xj.lin 已提交
161
        write_index(index_, file_location);
S
starlord 已提交
162
        auto new_index = milvus::engine::read_index(file_location);
X
xj.lin 已提交
163
        EXPECT_EQ(new_index->GetType(), ConvertToCpuIndexType(index_type));
X
xj.lin 已提交
164 165 166 167 168
        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);
X
xiaojun.lin 已提交
169
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
170 171
        AssertResult(res_ids, res_dis);
    }
X
MS-154  
xj.lin 已提交
172
}
Y
Yu Kun 已提交
173 174

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

Y
Yu Kun 已提交
176 177 178 179 180 181 182 183 184
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);
}