Debian Codegraph @

perf_tests/bench_scalar_quantizer_distance.cpp

raw
1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 #include <gflags/gflags.h>
9 #include <omp.h>
10 #include <cstdio>
11 #include <map>
12
13 #include <benchmark/benchmark.h>
14 #include <faiss/impl/ScalarQuantizer.h>
15 #include <faiss/perf_tests/utils.h>
16 #include <faiss/utils/random.h>
17
18 using namespace faiss;
19 DEFINE_uint32(d, 128, "dimension");
20 DEFINE_uint32(n, 2000, "dimension");
21 DEFINE_uint32(iterations, 20, "iterations");
22
23 static void bench_distance(
24 benchmark::State& state,
25 ScalarQuantizer::QuantizerType type,
26 int d,
27 int n) {
28 std::vector<float> x(d * n);
29
30 float_rand(x.data(), d * n, 12345);
31
32 // make sure it's idempotent
33 ScalarQuantizer sq(d, type);
34
35 omp_set_num_threads(1);
36
37 sq.train(n, x.data());
38
39 size_t code_size = sq.code_size;
40 state.counters["code_size"] = sq.code_size;
41
42 // encode
43 std::vector<uint8_t> codes(code_size * n);
44 sq.compute_codes(x.data(), codes.data(), n);
45
46 std::unique_ptr<ScalarQuantizer::SQDistanceComputer> dc(
47 sq.get_distance_computer());
48 dc->codes = codes.data();
49 dc->code_size = sq.code_size;
50
51 for (auto _ : state) {
52 float sum_dis = 0;
53 for (int i = 0; i < n; i++) {
54 dc->set_query(&x[i * d]);
55 for (int j = 0; j < n; j++) {
56 benchmark::DoNotOptimize(sum_dis += (*dc)(j));
57 }
58 }
59 }
60 }
61
62 int main(int argc, char** argv) {
63 benchmark::Initialize(&argc, argv);
64 gflags::AllowCommandLineReparsing();
65 gflags::ParseCommandLineFlags(&argc, &argv, true);
66 int iterations = FLAGS_iterations;
67 int d = FLAGS_d;
68 int n = FLAGS_n;
69 auto benchs = ::perf_tests::sq_types();
70
71 for (auto& [bench_name, quantizer_type] : benchs) {
72 benchmark::RegisterBenchmark(
73 bench_name.c_str(), bench_distance, quantizer_type, d, n)
74 ->Iterations(iterations);
75 }
76 benchmark::RunSpecifiedBenchmarks();
77 benchmark::Shutdown();
78 }