test_ivf.cpp 10.5 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.

X
xj.lin 已提交
18 19 20
#include <gtest/gtest.h>

#include <iostream>
21 22
#include <thread>

Y
youny626 已提交
23
#ifdef MILVUS_GPU_VERSION
24
#include <faiss/gpu/GpuIndexIVFFlat.h>
Y
youny626 已提交
25
#endif
X
xj.lin 已提交
26

X
xiaojun.lin 已提交
27 28
#include "knowhere/common/Exception.h"
#include "knowhere/common/Timer.h"
Y
youny626 已提交
29

S
starlord 已提交
30
#include "knowhere/index/vector_index/IndexIVF.h"
Y
youny626 已提交
31 32 33 34 35 36 37 38
#include "knowhere/index/vector_index/IndexIVFPQ.h"
#include "knowhere/index/vector_index/IndexIVFSQ.h"

#ifdef MILVUS_GPU_VERSION
#include "knowhere/index/vector_index/IndexGPUIVF.h"
#include "knowhere/index/vector_index/IndexGPUIVFPQ.h"
#include "knowhere/index/vector_index/IndexGPUIVFSQ.h"
#include "knowhere/index/vector_index/IndexIVFSQHybrid.h"
S
starlord 已提交
39
#include "knowhere/index/vector_index/helpers/Cloner.h"
Y
youny626 已提交
40
#endif
S
starlord 已提交
41

X
xiaojun.lin 已提交
42
#include "unittest/Helper.h"
X
xiaojun.lin 已提交
43
#include "unittest/utils.h"
X
xj.lin 已提交
44

45
using ::testing::Combine;
X
xj.lin 已提交
46 47 48
using ::testing::TestWithParam;
using ::testing::Values;

S
starlord 已提交
49
class IVFTest : public DataGen, public TestWithParam<::std::tuple<std::string, ParameterType>> {
Y
youny626 已提交
50
 protected:
S
starlord 已提交
51 52
    void
    SetUp() override {
Y
youny626 已提交
53
#ifdef MILVUS_GPU_VERSION
X
xiaojun.lin 已提交
54
        knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(DEVICEID, PINMEM, TEMPMEM, RESNUM);
Y
youny626 已提交
55
#endif
X
xiaojun.lin 已提交
56 57
        ParameterType parameter_type;
        std::tie(index_type, parameter_type) = GetParam();
S
starlord 已提交
58
        // Init_with_default();
X
xiaojun.lin 已提交
59 60 61
        //        nb = 1000000;
        //        nq = 1000;
        //        k = 1000;
X
xiaojun.lin 已提交
62
        Generate(DIM, NB, NQ);
X
xj.lin 已提交
63
        index_ = IndexFactory(index_type);
X
xiaojun.lin 已提交
64
        conf = ParamGenerator::GetInstance().Gen(parameter_type);
X
xj.lin 已提交
65
    }
S
starlord 已提交
66 67 68

    void
    TearDown() override {
Y
youny626 已提交
69
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
70
        knowhere::FaissGpuResourceMgr::GetInstance().Free();
Y
youny626 已提交
71
#endif
72
    }
X
xj.lin 已提交
73 74 75

 protected:
    std::string index_type;
S
starlord 已提交
76 77
    knowhere::Config conf;
    knowhere::IVFIndexPtr index_ = nullptr;
X
xj.lin 已提交
78 79
};

J
JinHai-CN 已提交
80
INSTANTIATE_TEST_CASE_P(IVFParameters, IVFTest,
Y
youny626 已提交
81 82
                        Values(
#ifdef MILVUS_GPU_VERSION
Y
youny626 已提交
83 84 85
                            std::make_tuple("GPUIVF", ParameterType::ivf),
                            std::make_tuple("GPUIVFPQ", ParameterType::ivfpq),
                            std::make_tuple("GPUIVFSQ", ParameterType::ivfsq),
X
xiaojun.lin 已提交
86
#ifdef CUSTOMIZATION
Y
youny626 已提交
87
                            std::make_tuple("IVFSQHybrid", ParameterType::ivfsq),
Y
youny626 已提交
88
#endif
X
xiaojun.lin 已提交
89
#endif
Y
youny626 已提交
90 91
                            std::make_tuple("IVF", ParameterType::ivf), std::make_tuple("IVFPQ", ParameterType::ivfpq),
                            std::make_tuple("IVFSQ", ParameterType::ivfsq)));
X
xj.lin 已提交
92 93 94 95

TEST_P(IVFTest, ivf_basic) {
    assert(!xb.empty());

X
xiaojun.lin 已提交
96
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
97 98
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
99
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
100
    index_->set_index_model(model);
X
xiaojun.lin 已提交
101
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
102 103
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
J
JinHai-CN 已提交
104

X
xiaojun.lin 已提交
105
    auto result = index_->Search(query_dataset, conf);
X
xiaojun.lin 已提交
106
    AssertAnns(result, nq, conf->k);
S
starlord 已提交
107
    // PrintResult(result, nq, k);
X
xj.lin 已提交
108 109 110
}

TEST_P(IVFTest, ivf_serialize) {
Y
youny626 已提交
111
    auto serialize = [](const std::string& filename, knowhere::BinaryPtr& bin, uint8_t* ret) {
X
xj.lin 已提交
112
        FileIOWriter writer(filename);
Y
youny626 已提交
113
        writer(static_cast<void*>(bin->data.get()), bin->size);
X
xj.lin 已提交
114 115 116 117 118 119 120

        FileIOReader reader(filename);
        reader(ret, bin->size);
    };

    {
        // serialize index-model
X
xiaojun.lin 已提交
121
        auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        auto binaryset = model->Serialize();
        auto bin = binaryset.GetByName("IVF");

        std::string filename = "/tmp/ivf_test_model_serialize.bin";
        auto load_data = new uint8_t[bin->size];
        serialize(filename, bin, load_data);

        binaryset.clear();
        auto data = std::make_shared<uint8_t>();
        data.reset(load_data);
        binaryset.Append("IVF", data, bin->size);

        model->Load(binaryset);

        index_->set_index_model(model);
X
xiaojun.lin 已提交
137
        index_->Add(base_dataset, conf);
X
xiaojun.lin 已提交
138
        auto result = index_->Search(query_dataset, conf);
X
xiaojun.lin 已提交
139
        AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
140 141 142 143
    }

    {
        // serialize index
X
xiaojun.lin 已提交
144
        auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
145
        index_->set_index_model(model);
X
xiaojun.lin 已提交
146
        index_->Add(base_dataset, conf);
X
xj.lin 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        auto binaryset = index_->Serialize();
        auto bin = binaryset.GetByName("IVF");

        std::string filename = "/tmp/ivf_test_serialize.bin";
        auto load_data = new uint8_t[bin->size];
        serialize(filename, bin, load_data);

        binaryset.clear();
        auto data = std::make_shared<uint8_t>();
        data.reset(load_data);
        binaryset.Append("IVF", data, bin->size);

        index_->Load(binaryset);
        EXPECT_EQ(index_->Count(), nb);
        EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
162
        auto result = index_->Search(query_dataset, conf);
X
xiaojun.lin 已提交
163
        AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
164 165 166
    }
}

Y
youny626 已提交
167
#ifdef MILVUS_GPU_VERSION
X
xj.lin 已提交
168 169 170
TEST_P(IVFTest, clone_test) {
    assert(!xb.empty());

X
xiaojun.lin 已提交
171
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
172 173
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
174
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
175
    index_->set_index_model(model);
X
xiaojun.lin 已提交
176
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
177 178
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
179
    auto result = index_->Search(query_dataset, conf);
X
xiaojun.lin 已提交
180
    AssertAnns(result, nq, conf->k);
S
starlord 已提交
181
    // PrintResult(result, nq, k);
X
xj.lin 已提交
182

S
starlord 已提交
183
    auto AssertEqual = [&](knowhere::DatasetPtr p1, knowhere::DatasetPtr p2) {
X
xj.lin 已提交
184 185 186 187
        auto ids_p1 = p1->array()[0];
        auto ids_p2 = p2->array()[0];

        for (int i = 0; i < nq * k; ++i) {
S
starlord 已提交
188
            EXPECT_EQ(*(ids_p2->data()->GetValues<int64_t>(1, i)), *(ids_p1->data()->GetValues<int64_t>(1, i)));
X
xj.lin 已提交
189 190 191
        }
    };

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    //    {
    //        // clone in place
    //        std::vector<std::string> support_idx_vec{"IVF", "GPUIVF", "IVFPQ", "IVFSQ", "GPUIVFSQ"};
    //        auto finder = std::find(support_idx_vec.cbegin(), support_idx_vec.cend(), index_type);
    //        if (finder != support_idx_vec.cend()) {
    //            EXPECT_NO_THROW({
    //                                auto clone_index = index_->Clone();
    //                                auto clone_result = clone_index->Search(query_dataset, conf);
    //                                //AssertAnns(result, nq, conf->k);
    //                                AssertEqual(result, clone_result);
    //                                std::cout << "inplace clone [" << index_type << "] success" << std::endl;
    //                            });
    //        } else {
    //            EXPECT_THROW({
    //                             std::cout << "inplace clone [" << index_type << "] failed" << std::endl;
    //                             auto clone_index = index_->Clone();
    //                         }, KnowhereException);
    //        }
    //    }
X
xiaojun.lin 已提交
211

X
xj.lin 已提交
212 213
    {
        // copy from gpu to cpu
X
xiaojun.lin 已提交
214
        std::vector<std::string> support_idx_vec{"GPUIVF", "GPUIVFSQ", "IVFSQHybrid"};
X
xj.lin 已提交
215 216 217
        auto finder = std::find(support_idx_vec.cbegin(), support_idx_vec.cend(), index_type);
        if (finder != support_idx_vec.cend()) {
            EXPECT_NO_THROW({
Y
youny626 已提交
218 219 220 221 222
                auto clone_index = knowhere::cloner::CopyGpuToCpu(index_, knowhere::Config());
                auto clone_result = clone_index->Search(query_dataset, conf);
                AssertEqual(result, clone_result);
                std::cout << "clone G <=> C [" << index_type << "] success" << std::endl;
            });
X
xj.lin 已提交
223
        } else {
S
starlord 已提交
224
            EXPECT_THROW(
Y
youny626 已提交
225 226 227 228 229
                {
                    std::cout << "clone G <=> C [" << index_type << "] failed" << std::endl;
                    auto clone_index = knowhere::cloner::CopyGpuToCpu(index_, knowhere::Config());
                },
                knowhere::KnowhereException);
X
xj.lin 已提交
230 231 232
        }
    }

X
xiaojun.lin 已提交
233 234 235 236
    if (index_type == "IVFSQHybrid") {
        return;
    }

X
xj.lin 已提交
237 238 239 240 241 242
    {
        // copy to gpu
        std::vector<std::string> support_idx_vec{"IVF", "GPUIVF", "IVFSQ", "GPUIVFSQ"};
        auto finder = std::find(support_idx_vec.cbegin(), support_idx_vec.cend(), index_type);
        if (finder != support_idx_vec.cend()) {
            EXPECT_NO_THROW({
Y
youny626 已提交
243 244 245 246 247
                auto clone_index = knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, knowhere::Config());
                auto clone_result = clone_index->Search(query_dataset, conf);
                AssertEqual(result, clone_result);
                std::cout << "clone C <=> G [" << index_type << "] success" << std::endl;
            });
X
xj.lin 已提交
248
        } else {
S
starlord 已提交
249
            EXPECT_THROW(
Y
youny626 已提交
250 251 252 253 254
                {
                    std::cout << "clone C <=> G [" << index_type << "] failed" << std::endl;
                    auto clone_index = knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, knowhere::Config());
                },
                knowhere::KnowhereException);
X
xj.lin 已提交
255 256 257
        }
    }
}
Y
youny626 已提交
258
#endif
X
xj.lin 已提交
259

Y
youny626 已提交
260
#ifdef MILVUS_GPU_VERSION
X
xiaojun.lin 已提交
261
#ifdef CUSTOMIZATION
X
xiaojun.lin 已提交
262
TEST_P(IVFTest, gpu_seal_test) {
X
xiaojun.lin 已提交
263
    std::vector<std::string> support_idx_vec{"GPUIVF", "GPUIVFSQ", "IVFSQHybrid"};
X
xj.lin 已提交
264 265 266 267 268 269 270
    auto finder = std::find(support_idx_vec.cbegin(), support_idx_vec.cend(), index_type);
    if (finder == support_idx_vec.cend()) {
        return;
    }

    assert(!xb.empty());

X
xiaojun.lin 已提交
271
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
272 273
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
274
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
275
    index_->set_index_model(model);
X
xiaojun.lin 已提交
276
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
277 278
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
279
    auto result = index_->Search(query_dataset, conf);
X
xiaojun.lin 已提交
280
    AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
281

S
starlord 已提交
282
    auto cpu_idx = knowhere::cloner::CopyGpuToCpu(index_, knowhere::Config());
X
xj.lin 已提交
283

S
starlord 已提交
284
    knowhere::TimeRecorder tc("CopyToGpu");
X
xiaojun.lin 已提交
285
    knowhere::cloner::CopyCpuToGpu(cpu_idx, DEVICEID, knowhere::Config());
X
xj.lin 已提交
286 287 288
    auto without_seal = tc.RecordSection("Without seal");
    cpu_idx->Seal();
    tc.RecordSection("seal cost");
X
xiaojun.lin 已提交
289
    knowhere::cloner::CopyCpuToGpu(cpu_idx, DEVICEID, knowhere::Config());
X
xj.lin 已提交
290 291 292
    auto with_seal = tc.RecordSection("With seal");
    ASSERT_GE(without_seal, with_seal);
}
Y
youny626 已提交
293
#endif
X
xiaojun.lin 已提交
294
#endif