| 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 <omp.h> |
| 11 |
#include <cstdio> |
| 12 |
#include <map> |
| 13 |
|
| 14 |
#include <benchmark/benchmark.h> |
| 15 |
#include <faiss/impl/ScalarQuantizer.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_decode( |
| 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 |
std::vector<float> x2(d * n); |
| 46 |
|
| 47 |
for (auto _ : state) { |
| 48 |
// decode |
| 49 |
sq.decode(codes.data(), x2.data(), n); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
int main(int argc, char** argv) { |
| 54 |
benchmark::Initialize(&argc, argv); |
| 55 |
gflags::AllowCommandLineReparsing(); |
| 56 |
gflags::ParseCommandLineFlags(&argc, &argv, true); |
| 57 |
int iterations = FLAGS_iterations; |
| 58 |
int d = FLAGS_d; |
| 59 |
int n = FLAGS_n; |
| 60 |
auto benchs = ::perf_tests::sq_types(); |
| 61 |
|
| 62 |
for (auto& [bench_name, quantizer_type] : benchs) { |
| 63 |
benchmark::RegisterBenchmark( |
| 64 |
bench_name.c_str(), bench_decode, quantizer_type, d, n) |
| 65 |
->Iterations(iterations); |
| 66 |
} |
| 67 |
|
| 68 |
benchmark::RunSpecifiedBenchmarks(); |
| 69 |
benchmark::Shutdown(); |
| 70 |
} |