|
| 1 | +// Copyright (C) 2019-2023 Zilliz. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| 4 | +// with the License. You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 | +// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 | +// or implied. See the License for the specific language governing permissions and limitations under the License. |
| 11 | + |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#include "catch2/catch_approx.hpp" |
| 16 | +#include "catch2/catch_test_macros.hpp" |
| 17 | +#include "knowhere/comp/brute_force.h" |
| 18 | +#include "knowhere/comp/index_param.h" |
| 19 | +#include "knowhere/index/index_factory.h" |
| 20 | +#include "knowhere/version.h" |
| 21 | +#include "utils.h" |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +// Build a SCANN index over train_ds with the given use_super_kmeans value |
| 26 | +// and return recall@k against brute-force ground truth. |
| 27 | +float |
| 28 | +BuildScannAndRecall( |
| 29 | + const knowhere::DataSetPtr& train_ds, |
| 30 | + const knowhere::DataSetPtr& query_ds, |
| 31 | + int64_t nlist, |
| 32 | + int64_t nprobe, |
| 33 | + int64_t topk, |
| 34 | + bool use_super_kmeans) { |
| 35 | + const auto version = knowhere::Version::GetCurrentVersion().VersionNumber(); |
| 36 | + auto idx = knowhere::IndexFactory::Instance() |
| 37 | + .Create<knowhere::fp32>(knowhere::IndexEnum::INDEX_FAISS_SCANN, version) |
| 38 | + .value(); |
| 39 | + |
| 40 | + knowhere::Json cfg; |
| 41 | + cfg[knowhere::meta::METRIC_TYPE] = knowhere::metric::IP; |
| 42 | + cfg[knowhere::indexparam::NLIST] = nlist; |
| 43 | + cfg[knowhere::indexparam::NPROBE] = nprobe; |
| 44 | + cfg[knowhere::indexparam::SUB_DIM] = 4; |
| 45 | + cfg[knowhere::indexparam::WITH_RAW_DATA] = false; |
| 46 | + cfg[knowhere::indexparam::USE_SUPER_KMEANS] = use_super_kmeans; |
| 47 | + |
| 48 | + REQUIRE(idx.Build(train_ds, cfg) == knowhere::Status::success); |
| 49 | + |
| 50 | + knowhere::Json search_cfg; |
| 51 | + search_cfg[knowhere::meta::METRIC_TYPE] = knowhere::metric::IP; |
| 52 | + search_cfg[knowhere::meta::TOPK] = topk; |
| 53 | + search_cfg[knowhere::indexparam::NPROBE] = nprobe; |
| 54 | + auto results = idx.Search(query_ds, search_cfg, nullptr); |
| 55 | + REQUIRE(results.has_value()); |
| 56 | + |
| 57 | + auto gt = knowhere::BruteForce::Search<knowhere::fp32>( |
| 58 | + train_ds, query_ds, |
| 59 | + knowhere::Json{{knowhere::meta::METRIC_TYPE, knowhere::metric::IP}, |
| 60 | + {knowhere::meta::TOPK, topk}}, |
| 61 | + nullptr); |
| 62 | + REQUIRE(gt.has_value()); |
| 63 | + |
| 64 | + return GetKNNRecall(*gt.value(), *results.value()); |
| 65 | +} |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +TEST_CASE("SCANN use_super_kmeans default matches Clustering recall", "[scann]") { |
| 70 | + constexpr int64_t nb = 2000; |
| 71 | + constexpr int64_t nq = 100; |
| 72 | + constexpr int64_t dim = 64; |
| 73 | + constexpr int64_t topk = 10; |
| 74 | + constexpr int64_t nlist = 128; |
| 75 | + constexpr int64_t nprobe = 8; |
| 76 | + |
| 77 | + const auto train_ds = GenDataSet(nb, dim, kSeed); |
| 78 | + const auto query_ds = GenDataSet(nq, dim, kSeed); |
| 79 | + |
| 80 | + const float super_recall = BuildScannAndRecall(train_ds, query_ds, nlist, nprobe, topk, true); |
| 81 | + const float cluster_recall = BuildScannAndRecall(train_ds, query_ds, nlist, nprobe, topk, false); |
| 82 | + |
| 83 | + CAPTURE(super_recall, cluster_recall); |
| 84 | + // SuperKMeans coarse quantizer training is recall-equivalent to Clustering. |
| 85 | + REQUIRE(super_recall == Catch::Approx(cluster_recall).margin(0.02f)); |
| 86 | + // Sanity: recall should be reasonable for nprobe=8. |
| 87 | + REQUIRE(super_recall > 0.5f); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_CASE("SCANN use_super_kmeans field is honored", "[scann]") { |
| 91 | + // Explicitly disabled must build and search successfully too. |
| 92 | + constexpr int64_t nb = 1000; |
| 93 | + constexpr int64_t nq = 50; |
| 94 | + constexpr int64_t dim = 32; |
| 95 | + constexpr int64_t topk = 10; |
| 96 | + constexpr int64_t nlist = 64; |
| 97 | + constexpr int64_t nprobe = 4; |
| 98 | + |
| 99 | + const auto train_ds = GenDataSet(nb, dim, kSeed + 1); |
| 100 | + const auto query_ds = GenDataSet(nq, dim, kSeed + 1); |
| 101 | + |
| 102 | + const float cluster_recall = BuildScannAndRecall(train_ds, query_ds, nlist, nprobe, topk, false); |
| 103 | + REQUIRE(cluster_recall > 0.0f); |
| 104 | +} |
0 commit comments