test_ivf.cpp 23.2 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 23 24 25
#include <thread>

#include <faiss/AutoTune.h>
#include <faiss/gpu/GpuAutoTune.h>
#include <faiss/gpu/GpuIndexIVFFlat.h>
X
xj.lin 已提交
26

S
starlord 已提交
27
#include "knowhere/adapter/Structure.h"
X
xiaojun.lin 已提交
28 29
#include "knowhere/common/Exception.h"
#include "knowhere/common/Timer.h"
X
xiaojun.lin 已提交
30 31 32
#include "knowhere/index/vector_index/IndexGPUIVF.h"
#include "knowhere/index/vector_index/IndexGPUIVFPQ.h"
#include "knowhere/index/vector_index/IndexGPUIVFSQ.h"
S
starlord 已提交
33 34 35 36 37
#include "knowhere/index/vector_index/IndexIVF.h"
#include "knowhere/index/vector_index/IndexIVFPQ.h"
#include "knowhere/index/vector_index/IndexIVFSQ.h"
#include "knowhere/index/vector_index/helpers/Cloner.h"

38
#include "unittest/utils.h"
X
xj.lin 已提交
39

S
starlord 已提交
40
namespace {
X
xj.lin 已提交
41

S
starlord 已提交
42
namespace kn = knowhere;
S
starlord 已提交
43 44

}  // namespace
X
xj.lin 已提交
45 46 47 48 49

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

X
xiaojun.lin 已提交
50 51
constexpr int device_id = 0;
constexpr int64_t DIM = 128;
S
starlord 已提交
52
constexpr int64_t NB = 1000000 / 100;
X
xiaojun.lin 已提交
53 54 55
constexpr int64_t NQ = 10;
constexpr int64_t K = 10;

S
starlord 已提交
56 57
kn::IVFIndexPtr
IndexFactory(const std::string& type) {
X
xj.lin 已提交
58
    if (type == "IVF") {
S
starlord 已提交
59
        return std::make_shared<kn::IVF>();
X
xj.lin 已提交
60
    } else if (type == "IVFPQ") {
S
starlord 已提交
61
        return std::make_shared<kn::IVFPQ>();
X
xj.lin 已提交
62
    } else if (type == "GPUIVF") {
S
starlord 已提交
63
        return std::make_shared<kn::GPUIVF>(device_id);
X
xj.lin 已提交
64
    } else if (type == "GPUIVFPQ") {
S
starlord 已提交
65
        return std::make_shared<kn::GPUIVFPQ>(device_id);
X
xj.lin 已提交
66
    } else if (type == "IVFSQ") {
S
starlord 已提交
67
        return std::make_shared<kn::IVFSQ>();
X
xj.lin 已提交
68
    } else if (type == "GPUIVFSQ") {
S
starlord 已提交
69
        return std::make_shared<kn::GPUIVFSQ>(device_id);
X
xj.lin 已提交
70 71 72
    }
}

X
xiaojun.lin 已提交
73 74 75 76 77 78 79 80 81
enum class ParameterType {
    ivf,
    ivfpq,
    ivfsq,
    nsg,
};

class ParamGenerator {
 public:
S
starlord 已提交
82 83
    static ParamGenerator&
    GetInstance() {
X
xiaojun.lin 已提交
84 85 86 87
        static ParamGenerator instance;
        return instance;
    }

S
starlord 已提交
88 89
    kn::Config
    Gen(const ParameterType& type) {
X
xiaojun.lin 已提交
90
        if (type == ParameterType::ivf) {
S
starlord 已提交
91
            auto tempconf = std::make_shared<kn::IVFCfg>();
X
xiaojun.lin 已提交
92 93 94 95 96
            tempconf->d = DIM;
            tempconf->gpu_id = device_id;
            tempconf->nlist = 100;
            tempconf->nprobe = 16;
            tempconf->k = K;
S
starlord 已提交
97
            tempconf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
98
            return tempconf;
S
starlord 已提交
99 100
        } else if (type == ParameterType::ivfpq) {
            auto tempconf = std::make_shared<kn::IVFPQCfg>();
X
xiaojun.lin 已提交
101 102 103 104 105 106 107
            tempconf->d = DIM;
            tempconf->gpu_id = device_id;
            tempconf->nlist = 100;
            tempconf->nprobe = 16;
            tempconf->k = K;
            tempconf->m = 8;
            tempconf->nbits = 8;
S
starlord 已提交
108
            tempconf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
109
            return tempconf;
S
starlord 已提交
110 111
        } else if (type == ParameterType::ivfsq) {
            auto tempconf = std::make_shared<kn::IVFSQCfg>();
X
xiaojun.lin 已提交
112 113 114 115 116 117
            tempconf->d = DIM;
            tempconf->gpu_id = device_id;
            tempconf->nlist = 100;
            tempconf->nprobe = 16;
            tempconf->k = K;
            tempconf->nbits = 8;
S
starlord 已提交
118
            tempconf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
119 120 121 122 123
            return tempconf;
        }
    }
};

S
starlord 已提交
124
class IVFTest : public DataGen, public TestWithParam<::std::tuple<std::string, ParameterType>> {
X
xj.lin 已提交
125
 protected:
S
starlord 已提交
126 127
    void
    SetUp() override {
X
xiaojun.lin 已提交
128 129
        ParameterType parameter_type;
        std::tie(index_type, parameter_type) = GetParam();
S
starlord 已提交
130
        // Init_with_default();
X
xiaojun.lin 已提交
131
        Generate(DIM, NB, NQ);
X
xj.lin 已提交
132
        index_ = IndexFactory(index_type);
X
xiaojun.lin 已提交
133
        conf = ParamGenerator::GetInstance().Gen(parameter_type);
S
starlord 已提交
134
        kn::FaissGpuResourceMgr::GetInstance().InitDevice(device_id, 1024 * 1024 * 200, 1024 * 1024 * 600, 2);
X
xj.lin 已提交
135
    }
S
starlord 已提交
136 137 138 139

    void
    TearDown() override {
        kn::FaissGpuResourceMgr::GetInstance().Free();
140
    }
X
xj.lin 已提交
141 142 143

 protected:
    std::string index_type;
S
starlord 已提交
144 145
    kn::Config conf;
    kn::IVFIndexPtr index_ = nullptr;
X
xj.lin 已提交
146 147 148
};

INSTANTIATE_TEST_CASE_P(IVFParameters, IVFTest,
S
starlord 已提交
149 150 151 152 153 154 155 156 157
                        Values(std::make_tuple("IVF", ParameterType::ivf),
                               std::make_tuple("GPUIVF", ParameterType::ivf),
                               //                            std::make_tuple("IVFPQ", ParameterType::ivfpq),
                               //                            std::make_tuple("GPUIVFPQ", ParameterType::ivfpq),
                               std::make_tuple("IVFSQ", ParameterType::ivfsq),
                               std::make_tuple("GPUIVFSQ", ParameterType::ivfsq)));

void
AssertAnns(const kn::DatasetPtr& result, const int& nq, const int& k) {
X
xj.lin 已提交
158 159 160 161 162 163
    auto ids = result->array()[0];
    for (auto i = 0; i < nq; i++) {
        EXPECT_EQ(i, *(ids->data()->GetValues<int64_t>(1, i * k)));
    }
}

S
starlord 已提交
164 165
void
PrintResult(const kn::DatasetPtr& result, const int& nq, const int& k) {
X
xj.lin 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    auto ids = result->array()[0];
    auto dists = result->array()[1];

    std::stringstream ss_id;
    std::stringstream ss_dist;
    for (auto i = 0; i < 10; i++) {
        for (auto j = 0; j < k; ++j) {
            ss_id << *(ids->data()->GetValues<int64_t>(1, i * k + j)) << " ";
            ss_dist << *(dists->data()->GetValues<float>(1, i * k + j)) << " ";
        }
        ss_id << std::endl;
        ss_dist << std::endl;
    }
    std::cout << "id\n" << ss_id.str() << std::endl;
    std::cout << "dist\n" << ss_dist.str() << std::endl;
}

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

X
xiaojun.lin 已提交
186
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
187 188
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
189
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
190
    index_->set_index_model(model);
X
xiaojun.lin 已提交
191
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
192 193
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
194 195
    auto result = index_->Search(query_dataset, conf);
    AssertAnns(result, nq, conf->k);
S
starlord 已提交
196
    // PrintResult(result, nq, k);
X
xj.lin 已提交
197 198
}

S
starlord 已提交
199
// TEST_P(IVFTest, gpu_to_cpu) {
X
xj.lin 已提交
200 201 202 203 204
//    if (index_type.find("GPU") == std::string::npos) { return; }
//
//    // else
//    assert(!xb.empty());
//
X
xiaojun.lin 已提交
205
//    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
206 207
//    index_->set_preprocessor(preprocessor);
//
X
xiaojun.lin 已提交
208
//    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
209
//    index_->set_index_model(model);
X
xiaojun.lin 已提交
210
//    index_->Add(base_dataset, conf);
X
xj.lin 已提交
211 212
//    EXPECT_EQ(index_->Count(), nb);
//    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
213
//    auto result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
214 215 216 217
//    AssertAnns(result, nq, k);
//
//    if (auto device_index = std::dynamic_pointer_cast<GPUIVF>(index_)) {
//        auto host_index = device_index->Copy_index_gpu_to_cpu();
X
xiaojun.lin 已提交
218
//        auto result = host_index->Search(query_dataset, conf);
X
xj.lin 已提交
219 220 221 222 223
//        AssertAnns(result, nq, k);
//    }
//}

TEST_P(IVFTest, ivf_serialize) {
S
starlord 已提交
224
    auto serialize = [](const std::string& filename, kn::BinaryPtr& bin, uint8_t* ret) {
X
xj.lin 已提交
225
        FileIOWriter writer(filename);
S
starlord 已提交
226
        writer(static_cast<void*>(bin->data.get()), bin->size);
X
xj.lin 已提交
227 228 229 230 231 232 233

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

    {
        // serialize index-model
X
xiaojun.lin 已提交
234
        auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        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 已提交
250 251 252
        index_->Add(base_dataset, conf);
        auto result = index_->Search(query_dataset, conf);
        AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
253 254 255 256
    }

    {
        // serialize index
X
xiaojun.lin 已提交
257
        auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
258
        index_->set_index_model(model);
X
xiaojun.lin 已提交
259
        index_->Add(base_dataset, conf);
X
xj.lin 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        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 已提交
275 276
        auto result = index_->Search(query_dataset, conf);
        AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
277 278 279 280 281 282
    }
}

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

X
xiaojun.lin 已提交
283
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
284 285
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
286
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
287
    index_->set_index_model(model);
X
xiaojun.lin 已提交
288
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
289 290
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
291 292
    auto result = index_->Search(query_dataset, conf);
    AssertAnns(result, nq, conf->k);
S
starlord 已提交
293
    // PrintResult(result, nq, k);
X
xj.lin 已提交
294

S
starlord 已提交
295
    auto AssertEqual = [&](kn::DatasetPtr p1, kn::DatasetPtr p2) {
X
xj.lin 已提交
296 297 298 299
        auto ids_p1 = p1->array()[0];
        auto ids_p2 = p2->array()[0];

        for (int i = 0; i < nq * k; ++i) {
S
starlord 已提交
300
            EXPECT_EQ(*(ids_p2->data()->GetValues<int64_t>(1, i)), *(ids_p1->data()->GetValues<int64_t>(1, i)));
X
xj.lin 已提交
301 302 303 304 305 306 307 308 309
        }
    };

    {
        // 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({
S
starlord 已提交
310 311 312 313 314 315
                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;
            });
X
xj.lin 已提交
316
        } else {
S
starlord 已提交
317 318 319 320 321 322
            EXPECT_THROW(
                {
                    std::cout << "inplace clone [" << index_type << "] failed" << std::endl;
                    auto clone_index = index_->Clone();
                },
                kn::KnowhereException);
X
xj.lin 已提交
323 324 325 326 327 328 329 330 331
        }
    }

    {
        // copy from gpu to cpu
        std::vector<std::string> support_idx_vec{"GPUIVF", "GPUIVFSQ"};
        auto finder = std::find(support_idx_vec.cbegin(), support_idx_vec.cend(), index_type);
        if (finder != support_idx_vec.cend()) {
            EXPECT_NO_THROW({
S
starlord 已提交
332 333 334 335 336
                auto clone_index = kn::cloner::CopyGpuToCpu(index_, kn::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 已提交
337
        } else {
S
starlord 已提交
338 339 340 341 342 343
            EXPECT_THROW(
                {
                    std::cout << "clone G <=> C [" << index_type << "] failed" << std::endl;
                    auto clone_index = kn::cloner::CopyGpuToCpu(index_, kn::Config());
                },
                kn::KnowhereException);
X
xj.lin 已提交
344 345 346 347 348 349 350 351 352
        }
    }

    {
        // 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({
S
starlord 已提交
353 354 355 356 357
                auto clone_index = kn::cloner::CopyCpuToGpu(index_, device_id, kn::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 已提交
358
        } else {
S
starlord 已提交
359 360 361 362 363 364
            EXPECT_THROW(
                {
                    std::cout << "clone C <=> G [" << index_type << "] failed" << std::endl;
                    auto clone_index = kn::cloner::CopyCpuToGpu(index_, device_id, kn::Config());
                },
                kn::KnowhereException);
X
xj.lin 已提交
365 366 367 368 369
        }
    }
}

TEST_P(IVFTest, seal_test) {
S
starlord 已提交
370
    // FaissGpuResourceMgr::GetInstance().InitDevice(device_id);
X
xj.lin 已提交
371 372 373 374 375 376 377 378 379

    std::vector<std::string> support_idx_vec{"GPUIVF", "GPUIVFSQ"};
    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 已提交
380
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
X
xj.lin 已提交
381 382
    index_->set_preprocessor(preprocessor);

X
xiaojun.lin 已提交
383
    auto model = index_->Train(base_dataset, conf);
X
xj.lin 已提交
384
    index_->set_index_model(model);
X
xiaojun.lin 已提交
385
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
386 387
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xiaojun.lin 已提交
388 389
    auto result = index_->Search(query_dataset, conf);
    AssertAnns(result, nq, conf->k);
X
xj.lin 已提交
390

S
starlord 已提交
391
    auto cpu_idx = kn::cloner::CopyGpuToCpu(index_, kn::Config());
X
xj.lin 已提交
392

S
starlord 已提交
393 394
    kn::TimeRecorder tc("CopyToGpu");
    kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
X
xj.lin 已提交
395 396 397
    auto without_seal = tc.RecordSection("Without seal");
    cpu_idx->Seal();
    tc.RecordSection("seal cost");
S
starlord 已提交
398
    kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
X
xj.lin 已提交
399 400 401 402
    auto with_seal = tc.RecordSection("With seal");
    ASSERT_GE(without_seal, with_seal);
}

S
starlord 已提交
403
class GPURESTEST : public DataGen, public ::testing::Test {
404
 protected:
S
starlord 已提交
405 406
    void
    SetUp() override {
407
        Generate(128, 1000000, 1000);
S
starlord 已提交
408
        kn::FaissGpuResourceMgr::GetInstance().InitDevice(device_id, 1024 * 1024 * 200, 1024 * 1024 * 300, 2);
409

X
xiaojun.lin 已提交
410
        k = 100;
411
        elems = nq * k;
S
starlord 已提交
412 413
        ids = (int64_t*)malloc(sizeof(int64_t) * elems);
        dis = (float*)malloc(sizeof(float) * elems);
414 415
    }

S
starlord 已提交
416 417
    void
    TearDown() override {
418 419
        delete ids;
        delete dis;
S
starlord 已提交
420
        kn::FaissGpuResourceMgr::GetInstance().Free();
421 422 423 424
    }

 protected:
    std::string index_type;
S
starlord 已提交
425
    kn::IVFIndexPtr index_ = nullptr;
426

S
starlord 已提交
427 428
    int64_t* ids = nullptr;
    float* dis = nullptr;
429 430 431
    int64_t elems = 0;
};

X
xj.lin 已提交
432
const int search_count = 18;
X
xj.lin 已提交
433
const int load_count = 3;
434 435 436 437 438

TEST_F(GPURESTEST, gpu_ivf_resource_test) {
    assert(!xb.empty());

    {
S
starlord 已提交
439 440 441 442
        index_ = std::make_shared<kn::GPUIVF>(-1);
        ASSERT_EQ(std::dynamic_pointer_cast<kn::GPUIVF>(index_)->GetGpuDevice(), -1);
        std::dynamic_pointer_cast<kn::GPUIVF>(index_)->SetGpuDevice(device_id);
        ASSERT_EQ(std::dynamic_pointer_cast<kn::GPUIVF>(index_)->GetGpuDevice(), device_id);
X
xj.lin 已提交
443

S
starlord 已提交
444
        auto conf = std::make_shared<kn::IVFCfg>();
X
xiaojun.lin 已提交
445 446 447
        conf->nlist = 1638;
        conf->d = dim;
        conf->gpu_id = device_id;
S
starlord 已提交
448
        conf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
449 450 451 452
        conf->k = k;
        conf->nprobe = 1;

        auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
453
        index_->set_preprocessor(preprocessor);
X
xiaojun.lin 已提交
454
        auto model = index_->Train(base_dataset, conf);
455
        index_->set_index_model(model);
X
xiaojun.lin 已提交
456
        index_->Add(base_dataset, conf);
457 458 459
        EXPECT_EQ(index_->Count(), nb);
        EXPECT_EQ(index_->Dimension(), dim);

S
starlord 已提交
460
        kn::TimeRecorder tc("knowere GPUIVF");
461
        for (int i = 0; i < search_count; ++i) {
X
xiaojun.lin 已提交
462
            index_->Search(query_dataset, conf);
463 464 465
            if (i > search_count - 6 || i < 5)
                tc.RecordSection("search once");
        }
X
xj.lin 已提交
466
        tc.ElapseFromBegin("search all");
467
    }
S
starlord 已提交
468
    kn::FaissGpuResourceMgr::GetInstance().Dump();
469 470 471 472 473 474 475 476 477 478

    {
        // IVF-Search
        faiss::gpu::StandardGpuResources res;
        faiss::gpu::GpuIndexIVFFlatConfig idx_config;
        idx_config.device = device_id;
        faiss::gpu::GpuIndexIVFFlat device_index(&res, dim, 1638, faiss::METRIC_L2, idx_config);
        device_index.train(nb, xb.data());
        device_index.add(nb, xb.data());

S
starlord 已提交
479
        kn::TimeRecorder tc("ori IVF");
480 481 482 483 484
        for (int i = 0; i < search_count; ++i) {
            device_index.search(nq, xq.data(), k, dis, ids);
            if (i > search_count - 6 || i < 5)
                tc.RecordSection("search once");
        }
X
xj.lin 已提交
485
        tc.ElapseFromBegin("search all");
486 487 488 489 490 491 492 493
    }
}

TEST_F(GPURESTEST, gpuivfsq) {
    {
        // knowhere gpu ivfsq
        index_type = "GPUIVFSQ";
        index_ = IndexFactory(index_type);
X
xiaojun.lin 已提交
494

S
starlord 已提交
495
        auto conf = std::make_shared<kn::IVFSQCfg>();
X
xiaojun.lin 已提交
496 497 498
        conf->nlist = 1638;
        conf->d = dim;
        conf->gpu_id = device_id;
S
starlord 已提交
499
        conf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
500 501 502 503 504
        conf->k = k;
        conf->nbits = 8;
        conf->nprobe = 1;

        auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
505
        index_->set_preprocessor(preprocessor);
X
xiaojun.lin 已提交
506
        auto model = index_->Train(base_dataset, conf);
507
        index_->set_index_model(model);
X
xiaojun.lin 已提交
508 509
        index_->Add(base_dataset, conf);
        auto result = index_->Search(query_dataset, conf);
510 511
        AssertAnns(result, nq, k);

S
starlord 已提交
512
        auto cpu_idx = kn::cloner::CopyGpuToCpu(index_, kn::Config());
513 514
        cpu_idx->Seal();

S
starlord 已提交
515 516
        kn::TimeRecorder tc("knowhere GPUSQ8");
        auto search_idx = kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
517 518
        tc.RecordSection("Copy to gpu");
        for (int i = 0; i < search_count; ++i) {
X
xiaojun.lin 已提交
519
            search_idx->Search(query_dataset, conf);
520 521 522
            if (i > search_count - 6 || i < 5)
                tc.RecordSection("search once");
        }
X
xj.lin 已提交
523
        tc.ElapseFromBegin("search all");
524 525 526 527
    }

    {
        // Ori gpuivfsq Test
S
starlord 已提交
528 529
        const char* index_description = "IVF1638,SQ8";
        faiss::Index* ori_index = faiss::index_factory(dim, index_description, faiss::METRIC_L2);
530 531 532 533 534 535 536

        faiss::gpu::StandardGpuResources res;
        auto device_index = faiss::gpu::index_cpu_to_gpu(&res, device_id, ori_index);
        device_index->train(nb, xb.data());
        device_index->add(nb, xb.data());

        auto cpu_index = faiss::gpu::index_gpu_to_cpu(device_index);
S
starlord 已提交
537
        auto idx = dynamic_cast<faiss::IndexIVF*>(cpu_index);
538 539 540 541 542 543 544 545 546
        if (idx != nullptr) {
            idx->to_readonly();
        }
        delete device_index;
        delete ori_index;

        faiss::gpu::GpuClonerOptions option;
        option.allInGpu = true;

S
starlord 已提交
547 548
        kn::TimeRecorder tc("ori GPUSQ8");
        faiss::Index* search_idx = faiss::gpu::index_cpu_to_gpu(&res, device_id, cpu_index, &option);
549 550 551 552 553 554
        tc.RecordSection("Copy to gpu");
        for (int i = 0; i < search_count; ++i) {
            search_idx->search(nq, xq.data(), k, dis, ids);
            if (i > search_count - 6 || i < 5)
                tc.RecordSection("search once");
        }
X
xj.lin 已提交
555
        tc.ElapseFromBegin("search all");
556 557 558 559 560 561
        delete cpu_index;
        delete search_idx;
    }
}

TEST_F(GPURESTEST, copyandsearch) {
X
xiaojun.lin 已提交
562
    // search and copy at the same time
563 564 565 566
    printf("==================\n");

    index_type = "GPUIVFSQ";
    index_ = IndexFactory(index_type);
X
xiaojun.lin 已提交
567

S
starlord 已提交
568
    auto conf = std::make_shared<kn::IVFSQCfg>();
X
xiaojun.lin 已提交
569 570 571
    conf->nlist = 1638;
    conf->d = dim;
    conf->gpu_id = device_id;
S
starlord 已提交
572
    conf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
573 574 575 576 577
    conf->k = k;
    conf->nbits = 8;
    conf->nprobe = 1;

    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
578
    index_->set_preprocessor(preprocessor);
X
xiaojun.lin 已提交
579
    auto model = index_->Train(base_dataset, conf);
580
    index_->set_index_model(model);
X
xiaojun.lin 已提交
581 582
    index_->Add(base_dataset, conf);
    auto result = index_->Search(query_dataset, conf);
583 584
    AssertAnns(result, nq, k);

S
starlord 已提交
585
    auto cpu_idx = kn::cloner::CopyGpuToCpu(index_, kn::Config());
586 587
    cpu_idx->Seal();

S
starlord 已提交
588
    auto search_idx = kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
589 590

    auto search_func = [&] {
S
starlord 已提交
591
        // TimeRecorder tc("search&load");
592
        for (int i = 0; i < search_count; ++i) {
X
xiaojun.lin 已提交
593
            search_idx->Search(query_dataset, conf);
S
starlord 已提交
594
            // if (i > search_count - 6 || i == 0)
595 596
            //    tc.RecordSection("search once");
        }
S
starlord 已提交
597
        // tc.ElapseFromBegin("search finish");
598 599
    };
    auto load_func = [&] {
S
starlord 已提交
600
        // TimeRecorder tc("search&load");
601
        for (int i = 0; i < load_count; ++i) {
S
starlord 已提交
602 603 604
            kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
            // if (i > load_count -5 || i < 5)
            // tc.RecordSection("Copy to gpu");
605
        }
S
starlord 已提交
606
        // tc.ElapseFromBegin("load finish");
607 608
    };

S
starlord 已提交
609 610
    kn::TimeRecorder tc("basic");
    kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
611
    tc.RecordSection("Copy to gpu once");
X
xiaojun.lin 已提交
612
    search_idx->Search(query_dataset, conf);
613 614 615 616 617 618 619 620 621 622 623 624 625
    tc.RecordSection("search once");
    search_func();
    tc.RecordSection("only search total");
    load_func();
    tc.RecordSection("only copy total");

    std::thread search_thread(search_func);
    std::thread load_thread(load_func);
    search_thread.join();
    load_thread.join();
    tc.RecordSection("Copy&search total");
}

626 627 628
TEST_F(GPURESTEST, TrainAndSearch) {
    index_type = "GPUIVFSQ";
    index_ = IndexFactory(index_type);
X
xiaojun.lin 已提交
629

S
starlord 已提交
630
    auto conf = std::make_shared<kn::IVFSQCfg>();
X
xiaojun.lin 已提交
631 632 633
    conf->nlist = 1638;
    conf->d = dim;
    conf->gpu_id = device_id;
S
starlord 已提交
634
    conf->metric_type = kn::METRICTYPE::L2;
X
xiaojun.lin 已提交
635 636 637 638 639
    conf->k = k;
    conf->nbits = 8;
    conf->nprobe = 1;

    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
640
    index_->set_preprocessor(preprocessor);
X
xiaojun.lin 已提交
641
    auto model = index_->Train(base_dataset, conf);
642 643
    auto new_index = IndexFactory(index_type);
    new_index->set_index_model(model);
X
xiaojun.lin 已提交
644
    new_index->Add(base_dataset, conf);
S
starlord 已提交
645
    auto cpu_idx = kn::cloner::CopyGpuToCpu(new_index, kn::Config());
646
    cpu_idx->Seal();
S
starlord 已提交
647
    auto search_idx = kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
648

X
xiaojun.lin 已提交
649 650
    constexpr int train_count = 1;
    constexpr int search_count = 5000;
651 652
    auto train_stage = [&] {
        for (int i = 0; i < train_count; ++i) {
X
xiaojun.lin 已提交
653
            auto model = index_->Train(base_dataset, conf);
654 655
            auto test_idx = IndexFactory(index_type);
            test_idx->set_index_model(model);
X
xiaojun.lin 已提交
656
            test_idx->Add(base_dataset, conf);
657 658
        }
    };
S
starlord 已提交
659
    auto search_stage = [&](kn::VectorIndexPtr& search_idx) {
660
        for (int i = 0; i < search_count; ++i) {
X
xiaojun.lin 已提交
661
            auto result = search_idx->Search(query_dataset, conf);
662 663 664 665
            AssertAnns(result, nq, k);
        }
    };

S
starlord 已提交
666 667 668 669 670
    // TimeRecorder tc("record");
    // train_stage();
    // tc.RecordSection("train cost");
    // search_stage(search_idx);
    // tc.RecordSection("search cost");
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687

    {
        // search and build parallel
        std::thread search_thread(search_stage, std::ref(search_idx));
        std::thread train_thread(train_stage);
        train_thread.join();
        search_thread.join();
    }
    {
        // build parallel
        std::thread train_1(train_stage);
        std::thread train_2(train_stage);
        train_1.join();
        train_2.join();
    }
    {
        // search parallel
S
starlord 已提交
688
        auto search_idx_2 = kn::cloner::CopyCpuToGpu(cpu_idx, device_id, kn::Config());
689 690 691 692 693 694 695
        std::thread search_1(search_stage, std::ref(search_idx));
        std::thread search_2(search_stage, std::ref(search_idx_2));
        search_1.join();
        search_2.join();
    }
}

S
starlord 已提交
696
// TODO(lxj): Add exception test