2021-01-06 14:45:50 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <queue>
|
|
|
|
#include <random>
|
2021-10-15 17:00:43 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2023-02-10 14:24:32 +08:00
|
|
|
#include "knowhere/comp/index_param.h"
|
2021-10-15 17:00:43 +08:00
|
|
|
#include "query/SubSearchResult.h"
|
2021-01-06 14:45:50 +08:00
|
|
|
|
|
|
|
using namespace milvus;
|
|
|
|
using namespace milvus::query;
|
|
|
|
|
2022-09-01 11:09:00 +08:00
|
|
|
using SubSearchResultUniq = std::unique_ptr<SubSearchResult>;
|
|
|
|
|
|
|
|
std::default_random_engine e(42);
|
2021-01-06 14:45:50 +08:00
|
|
|
|
2022-09-01 16:59:02 +08:00
|
|
|
SubSearchResultUniq
|
2022-09-01 11:09:00 +08:00
|
|
|
GenSubSearchResult(const int64_t nq,
|
|
|
|
const int64_t topk,
|
2022-09-09 22:12:34 +08:00
|
|
|
const knowhere::MetricType& metric_type,
|
2022-09-01 11:09:00 +08:00
|
|
|
const int64_t round_decimal) {
|
|
|
|
constexpr int64_t limit = 1000000L;
|
|
|
|
bool is_ip = (metric_type == knowhere::metric::IP);
|
2023-03-10 09:47:54 +08:00
|
|
|
SubSearchResultUniq sub_result =
|
|
|
|
std::make_unique<SubSearchResult>(nq, topk, metric_type, round_decimal);
|
2022-09-01 11:09:00 +08:00
|
|
|
std::vector<int64_t> ids;
|
|
|
|
std::vector<float> distances;
|
2022-09-01 16:59:02 +08:00
|
|
|
for (auto n = 0; n < nq; ++n) {
|
|
|
|
for (auto k = 0; k < topk; ++k) {
|
2022-09-01 11:09:00 +08:00
|
|
|
auto gen_x = e() % limit;
|
|
|
|
ids.push_back(gen_x);
|
|
|
|
distances.push_back(gen_x);
|
2021-01-06 14:45:50 +08:00
|
|
|
}
|
2022-09-01 11:09:00 +08:00
|
|
|
if (is_ip) {
|
2023-03-10 09:47:54 +08:00
|
|
|
std::sort(ids.begin() + n * topk,
|
|
|
|
ids.begin() + (n + 1) * topk,
|
|
|
|
std::greater<int64_t>());
|
|
|
|
std::sort(distances.begin() + n * topk,
|
|
|
|
distances.begin() + (n + 1) * topk,
|
|
|
|
std::greater<float>());
|
2022-09-01 11:09:00 +08:00
|
|
|
} else {
|
|
|
|
std::sort(ids.begin() + n * topk, ids.begin() + (n + 1) * topk);
|
2023-03-10 09:47:54 +08:00
|
|
|
std::sort(distances.begin() + n * topk,
|
|
|
|
distances.begin() + (n + 1) * topk);
|
2021-01-06 14:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 11:09:00 +08:00
|
|
|
sub_result->mutable_distances() = std::move(distances);
|
|
|
|
sub_result->mutable_seg_offsets() = std::move(ids);
|
|
|
|
return sub_result;
|
|
|
|
}
|
2021-01-06 14:45:50 +08:00
|
|
|
|
2022-09-09 22:12:34 +08:00
|
|
|
template <class queue_type>
|
2022-09-01 11:09:00 +08:00
|
|
|
void
|
|
|
|
CheckSubSearchResult(const int64_t nq,
|
|
|
|
const int64_t topk,
|
2022-09-01 16:59:02 +08:00
|
|
|
SubSearchResult& result,
|
2022-09-01 11:09:00 +08:00
|
|
|
std::vector<queue_type>& result_ref) {
|
|
|
|
ASSERT_EQ(result_ref.size(), nq);
|
|
|
|
for (int n = 0; n < nq; ++n) {
|
|
|
|
ASSERT_EQ(result_ref[n].size(), topk);
|
2021-01-06 14:45:50 +08:00
|
|
|
for (int k = 0; k < topk; ++k) {
|
2022-09-01 11:09:00 +08:00
|
|
|
auto ref_x = result_ref[n].top();
|
|
|
|
result_ref[n].pop();
|
2021-01-06 14:45:50 +08:00
|
|
|
auto index = n * topk + topk - 1 - k;
|
2022-09-01 16:59:02 +08:00
|
|
|
auto id = result.get_seg_offsets()[index];
|
|
|
|
auto distance = result.get_distances()[index];
|
2021-11-29 17:07:40 +08:00
|
|
|
ASSERT_EQ(id, ref_x);
|
|
|
|
ASSERT_EQ(distance, ref_x);
|
2021-01-06 14:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:12:34 +08:00
|
|
|
template <class queue_type>
|
2022-09-01 11:09:00 +08:00
|
|
|
void
|
2022-09-01 16:59:02 +08:00
|
|
|
TestSubSearchResultMerge(const knowhere::MetricType& metric_type,
|
|
|
|
const int64_t iteration,
|
|
|
|
const int64_t nq,
|
|
|
|
const int64_t topk) {
|
|
|
|
const int64_t round_decimal = 3;
|
2021-01-06 14:45:50 +08:00
|
|
|
|
2022-09-01 16:59:02 +08:00
|
|
|
std::vector<queue_type> result_ref(nq);
|
2022-09-01 11:09:00 +08:00
|
|
|
|
2022-09-01 16:59:02 +08:00
|
|
|
SubSearchResult final_result(nq, topk, metric_type, round_decimal);
|
2021-01-06 14:45:50 +08:00
|
|
|
for (int i = 0; i < iteration; ++i) {
|
2023-03-10 09:47:54 +08:00
|
|
|
SubSearchResultUniq sub_result =
|
|
|
|
GenSubSearchResult(nq, topk, metric_type, round_decimal);
|
2022-09-01 11:09:00 +08:00
|
|
|
auto ids = sub_result->get_ids();
|
2022-09-01 16:59:02 +08:00
|
|
|
for (int n = 0; n < nq; ++n) {
|
2021-01-06 14:45:50 +08:00
|
|
|
for (int k = 0; k < topk; ++k) {
|
2022-09-01 11:09:00 +08:00
|
|
|
int64_t x = ids[n * topk + k];
|
|
|
|
result_ref[n].push(x);
|
|
|
|
if (result_ref[n].size() > topk) {
|
|
|
|
result_ref[n].pop();
|
|
|
|
}
|
2021-01-06 14:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 11:09:00 +08:00
|
|
|
final_result.merge(*sub_result);
|
2021-01-06 14:45:50 +08:00
|
|
|
}
|
2022-09-01 16:59:02 +08:00
|
|
|
CheckSubSearchResult<queue_type>(nq, topk, final_result, result_ref);
|
2022-09-01 11:09:00 +08:00
|
|
|
}
|
2021-01-06 14:45:50 +08:00
|
|
|
|
2022-09-01 11:09:00 +08:00
|
|
|
TEST(Reduce, SubSearchResult) {
|
2023-03-10 09:47:54 +08:00
|
|
|
using queue_type_l2 =
|
|
|
|
std::priority_queue<int64_t, std::vector<int64_t>, std::less<int64_t>>;
|
|
|
|
using queue_type_ip = std::
|
|
|
|
priority_queue<int64_t, std::vector<int64_t>, std::greater<int64_t>>;
|
2022-09-01 16:59:02 +08:00
|
|
|
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 1, 1, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 1, 1, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 1, 16, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 1, 16, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 4, 1, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 4, 1, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 4, 16, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_l2>(knowhere::metric::L2, 4, 16, 10);
|
|
|
|
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 1, 1, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 1, 1, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 1, 16, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 1, 16, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 4, 1, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 4, 1, 10);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 4, 16, 1);
|
|
|
|
TestSubSearchResultMerge<queue_type_ip>(knowhere::metric::IP, 4, 16, 10);
|
2023-02-10 14:24:32 +08:00
|
|
|
}
|