test_customized_index.cpp 4.0 KB
Newer Older
X
xiaojun.lin 已提交
1 2 3 4 5 6 7
// 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
X
xiaojun.lin 已提交
8
//
X
xiaojun.lin 已提交
9
//   http://www.apache.org/licenses/LICENSE-2.0
X
xiaojun.lin 已提交
10
//
X
xiaojun.lin 已提交
11 12 13 14 15 16 17
// 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
xiaojun.lin 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <gtest/gtest.h>

#include "unittest/Helper.h"
#include "unittest/utils.h"

class SingleIndexTest : public DataGen, public TestGpuIndexBase {
 protected:
    void
    SetUp() override {
        TestGpuIndexBase::SetUp();
        Generate(DIM, NB, NQ);
        k = K;
    }

    void
    TearDown() override {
        TestGpuIndexBase::TearDown();
    }

 protected:
    std::string index_type;
    knowhere::IVFIndexPtr index_ = nullptr;
};

#ifdef CUSTOMIZATION
TEST_F(SingleIndexTest, IVFSQHybrid) {
    assert(!xb.empty());

    index_type = "IVFSQHybrid";
    index_ = IndexFactory(index_type);
    auto conf = ParamGenerator::GetInstance().Gen(ParameterType::ivfsq);
    auto preprocessor = index_->BuildPreprocessor(base_dataset, conf);
    index_->set_preprocessor(preprocessor);

    auto model = index_->Train(base_dataset, conf);
    index_->set_index_model(model);
    index_->Add(base_dataset, conf);
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);

    auto binaryset = index_->Serialize();
    {
X
xiaojun.lin 已提交
60
        // copy cpu to gpu
X
xiaojun.lin 已提交
61 62 63 64 65 66 67 68
        auto cpu_idx = std::make_shared<knowhere::IVFSQHybrid>(DEVICEID);
        cpu_idx->Load(binaryset);

        {
            for (int i = 0; i < 3; ++i) {
                auto gpu_idx = cpu_idx->CopyCpuToGpu(DEVICEID, conf);
                auto result = gpu_idx->Search(query_dataset, conf);
                AssertAnns(result, nq, conf->k);
X
xiaojun.lin 已提交
69
                // PrintResult(result, nq, k);
X
xiaojun.lin 已提交
70 71 72 73 74
            }
        }
    }

    {
X
xiaojun.lin 已提交
75
        // quantization already in gpu, only copy data
X
xiaojun.lin 已提交
76 77 78 79 80 81 82 83 84
        auto cpu_idx = std::make_shared<knowhere::IVFSQHybrid>(DEVICEID);
        cpu_idx->Load(binaryset);

        auto pair = cpu_idx->CopyCpuToGpuWithQuantizer(DEVICEID, conf);
        auto gpu_idx = pair.first;
        auto quantization = pair.second;

        auto result = gpu_idx->Search(query_dataset, conf);
        AssertAnns(result, nq, conf->k);
X
xiaojun.lin 已提交
85
        //        PrintResult(result, nq, k);
X
xiaojun.lin 已提交
86 87

        auto quantizer_conf = std::make_shared<knowhere::QuantizerCfg>();
X
xiaojun.lin 已提交
88
        quantizer_conf->mode = 2;  // only copy data
X
xiaojun.lin 已提交
89 90 91 92 93 94 95 96
        quantizer_conf->gpu_id = DEVICEID;
        for (int i = 0; i < 2; ++i) {
            auto hybrid_idx = std::make_shared<knowhere::IVFSQHybrid>(DEVICEID);
            hybrid_idx->Load(binaryset);

            auto new_idx = hybrid_idx->LoadData(quantization, quantizer_conf);
            auto result = new_idx->Search(query_dataset, conf);
            AssertAnns(result, nq, conf->k);
X
xiaojun.lin 已提交
97
            //            PrintResult(result, nq, k);
X
xiaojun.lin 已提交
98 99 100 101
        }
    }

    {
X
xiaojun.lin 已提交
102
        // quantization already in gpu, only set quantization
X
xiaojun.lin 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
        auto cpu_idx = std::make_shared<knowhere::IVFSQHybrid>(DEVICEID);
        cpu_idx->Load(binaryset);

        auto pair = cpu_idx->CopyCpuToGpuWithQuantizer(DEVICEID, conf);
        auto quantization = pair.second;

        for (int i = 0; i < 2; ++i) {
            auto hybrid_idx = std::make_shared<knowhere::IVFSQHybrid>(DEVICEID);
            hybrid_idx->Load(binaryset);

            hybrid_idx->SetQuantizer(quantization);
            auto result = hybrid_idx->Search(query_dataset, conf);
            AssertAnns(result, nq, conf->k);
X
xiaojun.lin 已提交
116
            //            PrintResult(result, nq, k);
X
xiaojun.lin 已提交
117 118 119 120 121 122
            hybrid_idx->UnsetQuantizer();
        }
    }
}

#endif