Debian Codegraph @

perf_tests/bench_scalar_quantizer_encode.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_encode(
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 ScalarQuantizer sq(d, type);
32
33 omp_set_num_threads(1);
34 size_t code_size = sq.code_size;
35
36 sq.train(n, x.data());
37 state.counters["code_size"] = sq.code_size;
38 std::vector<uint8_t> codes(code_size * n);
39
40 for (auto _ : state) {
41 // encode
42 sq.compute_codes(x.data(), codes.data(), n);
43 }
44 }
45
46 int main(int argc, char** argv) {
47 benchmark::Initialize(&argc, argv);
48 gflags::AllowCommandLineReparsing();
49 gflags::ParseCommandLineFlags(&argc, &argv, true);
50 int iterations = FLAGS_iterations;
51 int d = FLAGS_d;
52 int n = FLAGS_n;
53 auto benchs = ::perf_tests::sq_types();
54
55 for (auto& [bench_name, quantizer_type] : benchs) {
56 benchmark::RegisterBenchmark(
57 bench_name.c_str(), bench_encode, quantizer_type, d, n)
58 ->Iterations(iterations);
59 }
60
61 benchmark::RunSpecifiedBenchmarks();
62 benchmark::Shutdown();
63 }