Debian Codegraph @

perf_tests/bench_scalar_quantizer_accuracy.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 <faiss/perf_tests/utils.h>
9 #include <gflags/gflags.h>
10 #include <cstdio>
11 #include <map>
12
13 #include <benchmark/benchmark.h>
14 #include <faiss/impl/ScalarQuantizer.h>
15 #include <faiss/utils/distances.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_reconstruction_error(
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 sq.train(n, x.data());
36
37 size_t code_size = sq.code_size;
38 state.counters["code_size"] = sq.code_size;
39
40 // encode
41 std::vector<uint8_t> codes(code_size * n);
42 sq.compute_codes(x.data(), codes.data(), n);
43
44 // decode
45 std::vector<float> x2(d * n);
46 sq.decode(codes.data(), x2.data(), n);
47
48 state.counters["sql2_recons_error"] =
49 fvec_L2sqr(x.data(), x2.data(), n * d) / n;
50
51 // encode again
52 std::vector<uint8_t> codes2(code_size * n);
53 sq.compute_codes(x2.data(), codes2.data(), n);
54
55 size_t ndiff = 0;
56 for (size_t i = 0; i < codes.size(); i++) {
57 if (codes[i] != codes2[i]) {
58 ndiff++;
59 }
60 }
61
62 state.counters["ndiff_for_idempotence"] = ndiff;
63
64 state.counters["code_size_two"] = codes.size();
65 }
66
67 int main(int argc, char** argv) {
68 benchmark::Initialize(&argc, argv);
69 gflags::AllowCommandLineReparsing();
70 gflags::ParseCommandLineFlags(&argc, &argv, true);
71 int iterations = FLAGS_iterations;
72 int d = FLAGS_d;
73 int n = FLAGS_n;
74 auto benchs = ::perf_tests::sq_types();
75
76 for (auto& [bench_name, quantizer_type] : benchs) {
77 benchmark::RegisterBenchmark(
78 bench_name.c_str(),
79 bench_reconstruction_error,
80 quantizer_type,
81 d,
82 n)
83 ->Iterations(iterations);
84 }
85
86 benchmark::RunSpecifiedBenchmarks();
87 benchmark::Shutdown();
88 }