test_wrapper.cpp 6.8 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"
G
groot 已提交
19
#include "wrapper/VecIndex.h"
X
xiaojun.lin 已提交
20
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
X
xiaojun.lin 已提交
21
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
G
groot 已提交
22
#include "wrapper/utils.h"
X
MS-154  
xj.lin 已提交
23

24 25
#include <gtest/gtest.h>

X
xj.lin 已提交
26
INITIALIZE_EASYLOGGINGPP
X
MS-154  
xj.lin 已提交
27 28 29 30 31 32

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

class KnowhereWrapperTest
X
xiaojun.lin 已提交
33 34
    : public DataGenBase,
      public TestWithParam<::std::tuple<milvus::engine::IndexType, std::string, int, int, int, int>> {
X
MS-154  
xj.lin 已提交
35 36
 protected:
    void SetUp() override {
X
xiaojun.lin 已提交
37
        knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(DEVICEID, PINMEM, TEMPMEM, RESNUM);
G
groot 已提交
38

X
MS-154  
xj.lin 已提交
39
        std::string generator_type;
X
xiaojun.lin 已提交
40
        std::tie(index_type, generator_type, dim, nb, nq, k) = GetParam();
X
xiaojun.lin 已提交
41
        GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
X
MS-154  
xj.lin 已提交
42 43

        index_ = GetVecIndexFactory(index_type);
X
xiaojun.lin 已提交
44 45 46
        conf = ParamGenerator::GetInstance().Gen(index_type);
        conf->k = k;
        conf->d = dim;
X
xiaojun.lin 已提交
47
        conf->gpu_id = DEVICEID;
X
MS-154  
xj.lin 已提交
48
    }
G
groot 已提交
49

50
    void TearDown() override {
G
groot 已提交
51
        knowhere::FaissGpuResourceMgr::GetInstance().Free();
52
    }
X
MS-154  
xj.lin 已提交
53 54

 protected:
G
groot 已提交
55 56
    milvus::engine::IndexType index_type;
    milvus::engine::VecIndexPtr index_ = nullptr;
X
xiaojun.lin 已提交
57
    knowhere::Config conf;
X
MS-154  
xj.lin 已提交
58 59 60 61
};

INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
                        Values(
X
xj.lin 已提交
62
                            //["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
G
groot 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_CPU,
                                            "Default",
                                            64,
                                            100000,
                                            10,
                                            10),
                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_GPU, "Default", DIM, NB, 10, 10),
                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_MIX,
                                            "Default",
                                            64,
                                            100000,
                                            10,
                                            10),
                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_CPU, "Default", DIM, NB, 10, 10),
X
xiaojun.lin 已提交
77
//                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB, 10, 10),
G
groot 已提交
78
                            std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_MIX, "Default", DIM, NB, 10, 10),
X
xiaojun.lin 已提交
79 80
//                            std::make_tuple(IndexType::NSG_MIX, "Default", 128, 250000, 10, 10),
//                            std::make_tuple(IndexType::SPTAG_KDT_RNT_CPU, "Default", 128, 250000, 10, 10),
G
groot 已提交
81
                            std::make_tuple(milvus::engine::IndexType::FAISS_IDMAP, "Default", 64, 100000, 10, 10)
X
MS-154  
xj.lin 已提交
82 83 84
                        )
);

G
groot 已提交
85
TEST_P(KnowhereWrapperTest, BASE_TEST) {
X
xj.lin 已提交
86 87 88 89 90
    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 已提交
91

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

G
groot 已提交
97
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
W
wxyu 已提交
98 99 100 101 102 103
    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 已提交
104 105
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
W
wxyu 已提交
106
    AssertResult(res_ids, res_dis);
X
xj.lin 已提交
107

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

X
xj.lin 已提交
116
    {
G
groot 已提交
117
        std::string file_location = "/tmp/knowhere_gpu_file";
X
xj.lin 已提交
118
        write_index(index_, file_location);
G
groot 已提交
119
        auto new_index = milvus::engine::read_index(file_location);
W
wxyu 已提交
120

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

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

X
xj.lin 已提交
132 133 134
    auto elems = nq * k;
    std::vector<int64_t> res_ids(elems);
    std::vector<float> res_dis(elems);
X
xiaojun.lin 已提交
135 136
    index_->BuildAll(nb, xb.data(), ids.data(), conf);
    index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
137
    AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
138 139

    {
X
xj.lin 已提交
140 141 142 143 144 145 146 147 148
        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 已提交
149
        new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), conf);
X
xj.lin 已提交
150
        AssertResult(res_ids, res_dis);
X
MS-154  
xj.lin 已提交
151 152
    }

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

#include "wrapper/ConfAdapter.h"
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);
}