2019-09-17 02:51:48 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-11-13 08:35:37 +00:00
|
|
|
#include "easyloggingpp/easylogging++.h"
|
2019-09-29 03:52:55 +00:00
|
|
|
#include "wrapper/VecIndex.h"
|
2019-11-05 02:23:59 +00:00
|
|
|
|
|
|
|
#ifdef MILVUS_GPU_VERSION
|
2019-09-19 13:21:38 +00:00
|
|
|
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
|
2019-11-05 02:23:59 +00:00
|
|
|
#endif
|
|
|
|
|
2019-09-27 07:41:20 +00:00
|
|
|
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
|
2019-09-27 10:57:46 +00:00
|
|
|
#include "wrapper/utils.h"
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-09-20 03:33:10 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2019-07-17 03:40:09 +00:00
|
|
|
INITIALIZE_EASYLOGGINGPP
|
2019-07-04 06:47:23 +00:00
|
|
|
|
|
|
|
using ::testing::TestWithParam;
|
|
|
|
using ::testing::Values;
|
2019-11-14 03:13:35 +00:00
|
|
|
using ::testing::Combine;
|
2019-07-04 06:47:23 +00:00
|
|
|
|
|
|
|
class KnowhereWrapperTest
|
2019-11-14 03:13:35 +00:00
|
|
|
: public DataGenBase,
|
|
|
|
public TestWithParam<::std::tuple<milvus::engine::IndexType, std::string, int, int, int, int>> {
|
2019-11-05 05:35:24 +00:00
|
|
|
protected:
|
2019-11-14 03:13:35 +00:00
|
|
|
void SetUp() override {
|
2019-11-05 02:23:59 +00:00
|
|
|
#ifdef MILVUS_GPU_VERSION
|
2019-10-17 08:08:43 +00:00
|
|
|
knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(DEVICEID, PINMEM, TEMPMEM, RESNUM);
|
2019-11-05 02:23:59 +00:00
|
|
|
#endif
|
2019-07-04 06:47:23 +00:00
|
|
|
std::string generator_type;
|
2019-09-27 07:41:20 +00:00
|
|
|
std::tie(index_type, generator_type, dim, nb, nq, k) = GetParam();
|
2019-10-17 08:08:43 +00:00
|
|
|
GenData(dim, nb, nq, xb, xq, ids, k, gt_ids, gt_dis);
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-11-14 03:13:35 +00:00
|
|
|
milvus::engine::TempMetaConf tempconf;
|
|
|
|
tempconf.metric_type = knowhere::METRICTYPE::L2;
|
|
|
|
tempconf.gpu_id = DEVICEID;
|
|
|
|
tempconf.size = nb;
|
|
|
|
tempconf.dim = dim;
|
|
|
|
tempconf.k = k;
|
2019-11-15 10:54:19 +00:00
|
|
|
tempconf.nprobe = 16;
|
2019-11-14 03:13:35 +00:00
|
|
|
|
2019-07-04 06:47:23 +00:00
|
|
|
index_ = GetVecIndexFactory(index_type);
|
2019-11-14 03:13:35 +00:00
|
|
|
conf = ParamGenerator::GetInstance().GenBuild(index_type, tempconf);
|
|
|
|
searchconf = ParamGenerator::GetInstance().GenSearchConf(index_type, tempconf);
|
|
|
|
|
|
|
|
// conf->k = k;
|
|
|
|
// conf->d = dim;
|
|
|
|
// conf->gpu_id = DEVICEID;
|
2019-07-04 06:47:23 +00:00
|
|
|
}
|
2019-09-27 11:07:02 +00:00
|
|
|
|
2019-11-14 03:13:35 +00:00
|
|
|
void TearDown() override {
|
2019-11-05 02:23:59 +00:00
|
|
|
#ifdef MILVUS_GPU_VERSION
|
2019-09-29 08:37:34 +00:00
|
|
|
knowhere::FaissGpuResourceMgr::GetInstance().Free();
|
2019-11-05 02:23:59 +00:00
|
|
|
#endif
|
2019-09-06 07:55:23 +00:00
|
|
|
}
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-11-05 05:35:24 +00:00
|
|
|
protected:
|
2019-10-10 08:03:29 +00:00
|
|
|
milvus::engine::IndexType index_type;
|
|
|
|
milvus::engine::VecIndexPtr index_ = nullptr;
|
2019-10-17 08:08:43 +00:00
|
|
|
knowhere::Config conf;
|
2019-11-14 03:13:35 +00:00
|
|
|
knowhere::Config searchconf;
|
2019-07-04 06:47:23 +00:00
|
|
|
};
|
|
|
|
|
2019-11-14 03:13:35 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(WrapperParam, KnowhereWrapperTest,
|
|
|
|
Values(
|
|
|
|
//["Index type", "Generator type", "dim", "nb", "nq", "k", "build config", "search config"]
|
2019-11-14 08:40:36 +00:00
|
|
|
|
2019-11-05 02:23:59 +00:00
|
|
|
#ifdef MILVUS_GPU_VERSION
|
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_GPU, "Default", DIM, NB, 10, 10),
|
2019-11-14 13:04:10 +00:00
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_MIX, "Default", 64, 1000, 10, 10),
|
2019-11-05 05:35:24 +00:00
|
|
|
// std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB,
|
|
|
|
// 10, 10),
|
2019-11-05 02:23:59 +00:00
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_GPU, "Default", DIM, NB, 10, 10),
|
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_MIX, "Default", DIM, NB, 10, 10),
|
2019-11-14 13:04:10 +00:00
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFPQ_MIX, "Default", 64, 1000, 10, 10),
|
2019-11-14 08:40:36 +00:00
|
|
|
|
2019-09-27 07:41:20 +00:00
|
|
|
// std::make_tuple(IndexType::NSG_MIX, "Default", 128, 250000, 10, 10),
|
2019-11-05 02:23:59 +00:00
|
|
|
#endif
|
2019-11-05 05:35:24 +00:00
|
|
|
// std::make_tuple(IndexType::SPTAG_KDT_RNT_CPU, "Default", 128, 250000, 10, 10),
|
2019-11-14 13:04:10 +00:00
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IDMAP, "Default", 64, 1000, 10, 10),
|
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFFLAT_CPU, "Default", 64, 1000, 10, 10),
|
2019-11-05 05:35:24 +00:00
|
|
|
std::make_tuple(milvus::engine::IndexType::FAISS_IVFSQ8_CPU, "Default", DIM, NB, 10, 10)));
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-09-11 03:37:19 +00:00
|
|
|
TEST_P(KnowhereWrapperTest, BASE_TEST) {
|
2019-07-10 08:28:11 +00:00
|
|
|
EXPECT_EQ(index_->GetType(), index_type);
|
|
|
|
|
|
|
|
auto elems = nq * k;
|
|
|
|
std::vector<int64_t> res_ids(elems);
|
|
|
|
std::vector<float> res_dis(elems);
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-09-27 07:41:20 +00:00
|
|
|
index_->BuildAll(nb, xb.data(), ids.data(), conf);
|
2019-11-14 03:13:35 +00:00
|
|
|
index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-07-10 08:28:11 +00:00
|
|
|
AssertResult(res_ids, res_dis);
|
2019-07-04 06:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 02:23:59 +00:00
|
|
|
#ifdef MILVUS_GPU_VERSION
|
2019-09-11 03:37:19 +00:00
|
|
|
TEST_P(KnowhereWrapperTest, TO_GPU_TEST) {
|
2019-08-31 03:33:46 +00:00
|
|
|
EXPECT_EQ(index_->GetType(), index_type);
|
|
|
|
|
|
|
|
auto elems = nq * k;
|
|
|
|
std::vector<int64_t> res_ids(elems);
|
|
|
|
std::vector<float> res_dis(elems);
|
|
|
|
|
2019-09-27 07:41:20 +00:00
|
|
|
index_->BuildAll(nb, xb.data(), ids.data(), conf);
|
2019-11-14 03:13:35 +00:00
|
|
|
index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-08-31 03:33:46 +00:00
|
|
|
AssertResult(res_ids, res_dis);
|
2019-09-03 01:59:28 +00:00
|
|
|
|
2019-08-31 03:33:46 +00:00
|
|
|
{
|
2019-10-17 08:08:43 +00:00
|
|
|
auto dev_idx = index_->CopyToGpu(DEVICEID);
|
2019-09-03 01:59:28 +00:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-11-14 03:13:35 +00:00
|
|
|
dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-09-03 01:59:28 +00:00
|
|
|
}
|
|
|
|
AssertResult(res_ids, res_dis);
|
2019-08-31 03:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 01:59:28 +00:00
|
|
|
{
|
2019-09-11 03:37:19 +00:00
|
|
|
std::string file_location = "/tmp/knowhere_gpu_file";
|
2019-09-03 01:59:28 +00:00
|
|
|
write_index(index_, file_location);
|
2019-10-10 08:03:29 +00:00
|
|
|
auto new_index = milvus::engine::read_index(file_location);
|
2019-08-31 03:33:46 +00:00
|
|
|
|
2019-10-17 08:08:43 +00:00
|
|
|
auto dev_idx = new_index->CopyToGpu(DEVICEID);
|
2019-09-03 01:59:28 +00:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2019-11-14 03:13:35 +00:00
|
|
|
dev_idx->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-09-03 01:59:28 +00:00
|
|
|
}
|
|
|
|
AssertResult(res_ids, res_dis);
|
2019-08-31 03:33:46 +00:00
|
|
|
}
|
2019-09-03 01:59:28 +00:00
|
|
|
}
|
2019-11-05 02:23:59 +00:00
|
|
|
#endif
|
2019-09-03 01:59:28 +00:00
|
|
|
|
2019-09-11 03:37:19 +00:00
|
|
|
TEST_P(KnowhereWrapperTest, SERIALIZE_TEST) {
|
2019-07-10 08:28:11 +00:00
|
|
|
EXPECT_EQ(index_->GetType(), index_type);
|
2019-07-04 06:47:23 +00:00
|
|
|
|
2019-07-10 08:28:11 +00:00
|
|
|
auto elems = nq * k;
|
|
|
|
std::vector<int64_t> res_ids(elems);
|
|
|
|
std::vector<float> res_dis(elems);
|
2019-09-27 07:41:20 +00:00
|
|
|
index_->BuildAll(nb, xb.data(), ids.data(), conf);
|
2019-11-14 03:13:35 +00:00
|
|
|
index_->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-07-10 08:28:11 +00:00
|
|
|
AssertResult(res_ids, res_dis);
|
2019-07-04 06:47:23 +00:00
|
|
|
|
|
|
|
{
|
2019-07-10 08:28:11 +00:00
|
|
|
auto binary = index_->Serialize();
|
|
|
|
auto type = index_->GetType();
|
|
|
|
auto new_index = GetVecIndexFactory(type);
|
|
|
|
new_index->Load(binary);
|
|
|
|
EXPECT_EQ(new_index->Dimension(), index_->Dimension());
|
|
|
|
EXPECT_EQ(new_index->Count(), index_->Count());
|
|
|
|
|
|
|
|
std::vector<int64_t> res_ids(elems);
|
|
|
|
std::vector<float> res_dis(elems);
|
2019-11-14 03:13:35 +00:00
|
|
|
new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-07-10 08:28:11 +00:00
|
|
|
AssertResult(res_ids, res_dis);
|
2019-07-04 06:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:28:11 +00:00
|
|
|
{
|
2019-09-11 03:37:19 +00:00
|
|
|
std::string file_location = "/tmp/knowhere";
|
2019-07-10 08:28:11 +00:00
|
|
|
write_index(index_, file_location);
|
2019-10-10 08:03:29 +00:00
|
|
|
auto new_index = milvus::engine::read_index(file_location);
|
2019-09-03 01:59:28 +00:00
|
|
|
EXPECT_EQ(new_index->GetType(), ConvertToCpuIndexType(index_type));
|
2019-07-10 08:28:11 +00:00
|
|
|
EXPECT_EQ(new_index->Dimension(), index_->Dimension());
|
|
|
|
EXPECT_EQ(new_index->Count(), index_->Count());
|
|
|
|
|
|
|
|
std::vector<int64_t> res_ids(elems);
|
|
|
|
std::vector<float> res_dis(elems);
|
2019-11-14 03:13:35 +00:00
|
|
|
new_index->Search(nq, xq.data(), res_dis.data(), res_ids.data(), searchconf);
|
2019-07-10 08:28:11 +00:00
|
|
|
AssertResult(res_ids, res_dis);
|
|
|
|
}
|
2019-07-04 06:47:23 +00:00
|
|
|
}
|
2019-10-17 11:33:35 +00:00
|
|
|
|
|
|
|
#include "wrapper/ConfAdapter.h"
|
2019-11-05 02:23:59 +00:00
|
|
|
|
2019-10-17 11:33:35 +00:00
|
|
|
TEST(whatever, test_config) {
|
|
|
|
milvus::engine::TempMetaConf conf;
|
|
|
|
auto nsg_conf = std::make_shared<milvus::engine::NSGConfAdapter>();
|
|
|
|
nsg_conf->Match(conf);
|
|
|
|
nsg_conf->MatchSearch(conf, milvus::engine::IndexType::FAISS_IVFPQ_GPU);
|
|
|
|
|
|
|
|
auto pq_conf = std::make_shared<milvus::engine::IVFPQConfAdapter>();
|
|
|
|
pq_conf->Match(conf);
|
|
|
|
}
|