test_idmap.cpp 6.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>

X
xiaojun.lin 已提交
21 22
#include "knowhere/adapter/Structure.h"
#include "knowhere/common/Exception.h"
S
starlord 已提交
23
#include "knowhere/index/vector_index/IndexIDMAP.h"
Y
youny626 已提交
24
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
25
#include "knowhere/index/vector_index/helpers/Cloner.h"
Y
youny626 已提交
26 27
#include "knowhere/index/vector_index/IndexGPUIDMAP.h"
#endif
X
xiaojun.lin 已提交
28
#include "Helper.h"
X
xiaojun.lin 已提交
29
#include "unittest/utils.h"
X
xj.lin 已提交
30

X
xiaojun.lin 已提交
31
class IDMAPTest : public DataGen, public TestGpuIndexBase {
X
xj.lin 已提交
32
 protected:
S
starlord 已提交
33 34
    void
    SetUp() override {
X
xiaojun.lin 已提交
35 36
        TestGpuIndexBase::SetUp();

X
xj.lin 已提交
37
        Init_with_default();
S
starlord 已提交
38
        index_ = std::make_shared<knowhere::IDMAP>();
X
xj.lin 已提交
39
    }
40

S
starlord 已提交
41 42
    void
    TearDown() override {
X
xiaojun.lin 已提交
43
        TestGpuIndexBase::TearDown();
44 45
    }

X
xj.lin 已提交
46
 protected:
S
starlord 已提交
47
    knowhere::IDMAPPtr index_ = nullptr;
X
xj.lin 已提交
48 49 50
};

TEST_F(IDMAPTest, idmap_basic) {
X
xj.lin 已提交
51
    ASSERT_TRUE(!xb.empty());
X
xj.lin 已提交
52

S
starlord 已提交
53
    auto conf = std::make_shared<knowhere::Cfg>();
X
xiaojun.lin 已提交
54 55
    conf->d = dim;
    conf->k = k;
S
starlord 已提交
56
    conf->metric_type = knowhere::METRICTYPE::L2;
X
xiaojun.lin 已提交
57 58 59

    index_->Train(conf);
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
60 61
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xj.lin 已提交
62 63
    ASSERT_TRUE(index_->GetRawVectors() != nullptr);
    ASSERT_TRUE(index_->GetRawIds() != nullptr);
X
xiaojun.lin 已提交
64
    auto result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
65
    AssertAnns(result, nq, k);
X
xiaojun.lin 已提交
66
    //    PrintResult(result, nq, k);
X
xj.lin 已提交
67

X
xj.lin 已提交
68
    index_->Seal();
X
xj.lin 已提交
69
    auto binaryset = index_->Serialize();
S
starlord 已提交
70
    auto new_index = std::make_shared<knowhere::IDMAP>();
X
xj.lin 已提交
71
    new_index->Load(binaryset);
X
xiaojun.lin 已提交
72
    auto re_result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
73
    AssertAnns(re_result, nq, k);
X
xiaojun.lin 已提交
74
    //    PrintResult(re_result, nq, k);
X
xj.lin 已提交
75 76 77
}

TEST_F(IDMAPTest, idmap_serialize) {
S
starlord 已提交
78
    auto serialize = [](const std::string& filename, knowhere::BinaryPtr& bin, uint8_t* ret) {
X
xj.lin 已提交
79
        FileIOWriter writer(filename);
S
starlord 已提交
80
        writer(static_cast<void*>(bin->data.get()), bin->size);
X
xj.lin 已提交
81 82 83 84 85

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

S
starlord 已提交
86
    auto conf = std::make_shared<knowhere::Cfg>();
X
xiaojun.lin 已提交
87 88
    conf->d = dim;
    conf->k = k;
S
starlord 已提交
89
    conf->metric_type = knowhere::METRICTYPE::L2;
X
xiaojun.lin 已提交
90

X
xj.lin 已提交
91 92
    {
        // serialize index
X
xiaojun.lin 已提交
93
        index_->Train(conf);
S
starlord 已提交
94
        index_->Add(base_dataset, knowhere::Config());
X
xiaojun.lin 已提交
95
        auto re_result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
96
        AssertAnns(re_result, nq, k);
X
xiaojun.lin 已提交
97
        //        PrintResult(re_result, nq, k);
X
xj.lin 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        EXPECT_EQ(index_->Count(), nb);
        EXPECT_EQ(index_->Dimension(), dim);
        auto binaryset = index_->Serialize();
        auto bin = binaryset.GetByName("IVF");

        std::string filename = "/tmp/idmap_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 已提交
115
        auto result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
116
        AssertAnns(result, nq, k);
X
xiaojun.lin 已提交
117
        //        PrintResult(result, nq, k);
X
xj.lin 已提交
118 119 120
    }
}

Y
youny626 已提交
121
#ifdef MILVUS_GPU_VERSION
X
xj.lin 已提交
122
TEST_F(IDMAPTest, copy_test) {
X
xj.lin 已提交
123
    ASSERT_TRUE(!xb.empty());
X
xj.lin 已提交
124

S
starlord 已提交
125
    auto conf = std::make_shared<knowhere::Cfg>();
X
xiaojun.lin 已提交
126 127
    conf->d = dim;
    conf->k = k;
S
starlord 已提交
128
    conf->metric_type = knowhere::METRICTYPE::L2;
X
xiaojun.lin 已提交
129 130 131

    index_->Train(conf);
    index_->Add(base_dataset, conf);
X
xj.lin 已提交
132 133
    EXPECT_EQ(index_->Count(), nb);
    EXPECT_EQ(index_->Dimension(), dim);
X
xj.lin 已提交
134 135
    ASSERT_TRUE(index_->GetRawVectors() != nullptr);
    ASSERT_TRUE(index_->GetRawIds() != nullptr);
X
xiaojun.lin 已提交
136
    auto result = index_->Search(query_dataset, conf);
X
xj.lin 已提交
137
    AssertAnns(result, nq, k);
S
starlord 已提交
138
    // PrintResult(result, nq, k);
X
xj.lin 已提交
139 140 141 142

    {
        // clone
        auto clone_index = index_->Clone();
X
xiaojun.lin 已提交
143
        auto clone_result = clone_index->Search(query_dataset, conf);
X
xj.lin 已提交
144 145 146 147 148
        AssertAnns(clone_result, nq, k);
    }

    {
        // cpu to gpu
X
xiaojun.lin 已提交
149
        auto clone_index = knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, conf);
X
xiaojun.lin 已提交
150
        auto clone_result = clone_index->Search(query_dataset, conf);
X
xj.lin 已提交
151
        AssertAnns(clone_result, nq, k);
S
starlord 已提交
152
        ASSERT_THROW({ std::static_pointer_cast<knowhere::GPUIDMAP>(clone_index)->GetRawVectors(); },
S
starlord 已提交
153
                     knowhere::KnowhereException);
S
starlord 已提交
154
        ASSERT_THROW({ std::static_pointer_cast<knowhere::GPUIDMAP>(clone_index)->GetRawIds(); },
S
starlord 已提交
155
                     knowhere::KnowhereException);
X
xj.lin 已提交
156 157 158

        auto binary = clone_index->Serialize();
        clone_index->Load(binary);
X
xiaojun.lin 已提交
159
        auto new_result = clone_index->Search(query_dataset, conf);
X
xj.lin 已提交
160 161
        AssertAnns(new_result, nq, k);

X
xj.lin 已提交
162
        auto clone_gpu_idx = clone_index->Clone();
X
xiaojun.lin 已提交
163
        auto clone_gpu_res = clone_gpu_idx->Search(query_dataset, conf);
X
xj.lin 已提交
164 165 166
        AssertAnns(clone_gpu_res, nq, k);

        // gpu to cpu
S
starlord 已提交
167
        auto host_index = knowhere::cloner::CopyGpuToCpu(clone_index, conf);
X
xiaojun.lin 已提交
168
        auto host_result = host_index->Search(query_dataset, conf);
X
xj.lin 已提交
169
        AssertAnns(host_result, nq, k);
S
starlord 已提交
170 171
        ASSERT_TRUE(std::static_pointer_cast<knowhere::IDMAP>(host_index)->GetRawVectors() != nullptr);
        ASSERT_TRUE(std::static_pointer_cast<knowhere::IDMAP>(host_index)->GetRawIds() != nullptr);
X
xj.lin 已提交
172 173

        // gpu to gpu
X
xiaojun.lin 已提交
174
        auto device_index = knowhere::cloner::CopyCpuToGpu(index_, DEVICEID, conf);
S
starlord 已提交
175
        auto new_device_index =
X
xiaojun.lin 已提交
176
            std::static_pointer_cast<knowhere::GPUIDMAP>(device_index)->CopyGpuToGpu(DEVICEID, conf);
X
xiaojun.lin 已提交
177
        auto device_result = new_device_index->Search(query_dataset, conf);
X
xj.lin 已提交
178 179 180
        AssertAnns(device_result, nq, k);
    }
}
Y
youny626 已提交
181
#endif