2020-12-25 03:10:31 +00: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
|
|
|
|
|
2020-12-29 08:31:03 +00:00
|
|
|
#include <google/protobuf/text_format.h>
|
2021-10-12 04:44:33 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <map>
|
|
|
|
#include <tuple>
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2022-09-21 12:16:51 +00:00
|
|
|
#include "indexbuilder/IndexFactory.h"
|
2022-03-21 06:23:24 +00:00
|
|
|
#include "indexbuilder/VecIndexCreator.h"
|
2022-09-21 12:16:51 +00:00
|
|
|
#include "common/QueryResult.h"
|
2021-03-23 10:44:57 +00:00
|
|
|
#include "test_utils/indexbuilder_test_utils.h"
|
2022-09-21 12:16:51 +00:00
|
|
|
#include "knowhere/archive/KnowhereConfig.h"
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2022-09-21 12:16:51 +00:00
|
|
|
using namespace milvus;
|
|
|
|
using namespace milvus::segcore;
|
|
|
|
using namespace milvus::proto::indexcgo;
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2022-03-25 05:49:25 +00:00
|
|
|
using Param = std::pair<knowhere::IndexType, knowhere::MetricType>;
|
2020-12-28 02:00:02 +00:00
|
|
|
|
|
|
|
class IndexWrapperTest : public ::testing::TestWithParam<Param> {
|
|
|
|
protected:
|
|
|
|
void
|
|
|
|
SetUp() override {
|
2022-03-25 05:49:25 +00:00
|
|
|
knowhere::KnowhereConfig::SetStatisticsLevel(3);
|
2022-10-14 06:45:23 +00:00
|
|
|
storage_config_ = get_default_storage_config();
|
2021-10-28 10:54:35 +00:00
|
|
|
|
2020-12-28 02:00:02 +00:00
|
|
|
auto param = GetParam();
|
|
|
|
index_type = param.first;
|
|
|
|
metric_type = param.second;
|
|
|
|
std::tie(type_params, index_params) = generate_params(index_type, metric_type);
|
2022-09-21 12:16:51 +00:00
|
|
|
bool ok;
|
|
|
|
ok = google::protobuf::TextFormat::PrintToString(type_params, &type_params_str);
|
|
|
|
assert(ok);
|
|
|
|
ok = google::protobuf::TextFormat::PrintToString(index_params, &index_params_str);
|
|
|
|
assert(ok);
|
|
|
|
|
|
|
|
search_conf = generate_search_conf(index_type, metric_type);
|
2020-12-28 02:00:02 +00:00
|
|
|
|
2022-06-24 02:34:18 +00:00
|
|
|
std::map<knowhere::MetricType, bool> is_binary_map = {
|
2022-03-25 05:49:25 +00:00
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_IDMAP, false},
|
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_IVFPQ, false},
|
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_IVFFLAT, false},
|
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_IVFSQ8, false},
|
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_BIN_IVFFLAT, true},
|
|
|
|
{knowhere::IndexEnum::INDEX_FAISS_BIN_IDMAP, true},
|
|
|
|
{knowhere::IndexEnum::INDEX_HNSW, false},
|
|
|
|
{knowhere::IndexEnum::INDEX_ANNOY, false},
|
2021-01-07 07:39:20 +00:00
|
|
|
};
|
2020-12-28 02:00:02 +00:00
|
|
|
|
|
|
|
is_binary = is_binary_map[index_type];
|
2022-09-21 12:16:51 +00:00
|
|
|
if (is_binary) {
|
|
|
|
vec_field_data_type = CDataType::FloatVector;
|
|
|
|
} else {
|
|
|
|
vec_field_data_type = CDataType::BinaryVector;
|
|
|
|
}
|
2020-12-28 02:00:02 +00:00
|
|
|
|
|
|
|
auto dataset = GenDataset(NB, metric_type, is_binary);
|
|
|
|
if (!is_binary) {
|
2022-04-29 05:35:49 +00:00
|
|
|
xb_data = dataset.get_col<float>(milvus::FieldId(100));
|
2022-03-25 05:49:25 +00:00
|
|
|
xb_dataset = knowhere::GenDataset(NB, DIM, xb_data.data());
|
2022-09-21 12:16:51 +00:00
|
|
|
xq_dataset = knowhere::GenDataset(NQ, DIM, xb_data.data() + DIM * query_offset);
|
2020-12-28 02:00:02 +00:00
|
|
|
} else {
|
2022-04-29 05:35:49 +00:00
|
|
|
xb_bin_data = dataset.get_col<uint8_t>(milvus::FieldId(100));
|
2022-03-25 05:49:25 +00:00
|
|
|
xb_dataset = knowhere::GenDataset(NB, DIM, xb_bin_data.data());
|
2022-09-21 12:16:51 +00:00
|
|
|
xq_dataset = knowhere::GenDataset(NQ, DIM, xb_bin_data.data() + DIM * query_offset);
|
2020-12-28 02:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TearDown() override {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::string index_type, metric_type;
|
|
|
|
indexcgo::TypeParams type_params;
|
|
|
|
indexcgo::IndexParams index_params;
|
2020-12-25 03:10:31 +00:00
|
|
|
std::string type_params_str, index_params_str;
|
2022-09-21 12:16:51 +00:00
|
|
|
milvus::Config search_conf;
|
2020-12-28 02:00:02 +00:00
|
|
|
bool is_binary;
|
2022-09-21 12:16:51 +00:00
|
|
|
CDataType vec_field_data_type;
|
2022-03-25 05:49:25 +00:00
|
|
|
knowhere::DatasetPtr xb_dataset;
|
2020-12-28 02:00:02 +00:00
|
|
|
std::vector<float> xb_data;
|
|
|
|
std::vector<uint8_t> xb_bin_data;
|
2022-03-25 05:49:25 +00:00
|
|
|
knowhere::DatasetPtr xq_dataset;
|
2022-09-21 12:16:51 +00:00
|
|
|
int64_t query_offset = 100;
|
|
|
|
int64_t NB = 10000;
|
2022-10-14 06:45:23 +00:00
|
|
|
StorageConfig storage_config_;
|
2020-12-28 02:00:02 +00:00
|
|
|
};
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2020-12-30 10:16:34 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
|
|
IndexTypeParameters,
|
|
|
|
IndexWrapperTest,
|
2022-06-24 02:34:18 +00:00
|
|
|
::testing::Values(std::pair(knowhere::IndexEnum::INDEX_FAISS_IDMAP, knowhere::metric::L2),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_IVFPQ, knowhere::metric::L2),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_IVFFLAT, knowhere::metric::L2),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_IVFSQ8, knowhere::metric::L2),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_BIN_IVFFLAT, knowhere::metric::JACCARD),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_BIN_IVFFLAT, knowhere::metric::TANIMOTO),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_FAISS_BIN_IDMAP, knowhere::metric::JACCARD),
|
|
|
|
std::pair(knowhere::IndexEnum::INDEX_HNSW, knowhere::metric::L2),
|
2022-09-09 14:12:34 +00:00
|
|
|
std::pair(knowhere::IndexEnum::INDEX_ANNOY, knowhere::metric::L2)));
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2022-09-21 12:16:51 +00:00
|
|
|
TEST_P(IndexWrapperTest, BuildAndQuery) {
|
|
|
|
auto index = milvus::indexbuilder::IndexFactory::GetInstance().CreateIndex(
|
2022-10-14 06:45:23 +00:00
|
|
|
vec_field_data_type, type_params_str.c_str(), index_params_str.c_str(), storage_config_);
|
2020-12-25 03:10:31 +00:00
|
|
|
|
2022-09-21 12:16:51 +00:00
|
|
|
auto dataset = GenDataset(NB, metric_type, is_binary);
|
|
|
|
auto xb_data = dataset.get_col<float>(milvus::FieldId(100));
|
|
|
|
auto xb_dataset = knowhere::GenDataset(NB, DIM, xb_data.data());
|
|
|
|
ASSERT_NO_THROW(index->Build(xb_dataset));
|
|
|
|
auto binary_set = index->Serialize();
|
|
|
|
auto copy_index = milvus::indexbuilder::IndexFactory::GetInstance().CreateIndex(
|
2022-10-14 06:45:23 +00:00
|
|
|
vec_field_data_type, type_params_str.c_str(), index_params_str.c_str(), storage_config_);
|
2022-09-21 12:16:51 +00:00
|
|
|
auto vec_index = static_cast<milvus::indexbuilder::VecIndexCreator*>(copy_index.get());
|
|
|
|
ASSERT_EQ(vec_index->dim(), DIM);
|
|
|
|
ASSERT_NO_THROW(vec_index->Load(binary_set));
|
|
|
|
|
|
|
|
milvus::SearchInfo search_info;
|
|
|
|
search_info.topk_ = K;
|
|
|
|
search_info.metric_type_ = metric_type;
|
|
|
|
search_info.search_params_ = search_conf;
|
|
|
|
auto result = vec_index->Query(xq_dataset, search_info, nullptr);
|
|
|
|
|
|
|
|
EXPECT_EQ(result->total_nq_, NQ);
|
|
|
|
EXPECT_EQ(result->unity_topK_, K);
|
|
|
|
EXPECT_EQ(result->distances_.size(), NQ * K);
|
|
|
|
EXPECT_EQ(result->seg_offsets_.size(), NQ * K);
|
|
|
|
if (!is_binary) {
|
|
|
|
EXPECT_EQ(result->seg_offsets_[0], query_offset);
|
2020-12-30 10:16:34 +00:00
|
|
|
}
|
2020-12-29 08:31:03 +00:00
|
|
|
}
|