mirror of
https://github.com/facebookresearch/faiss.git
synced 2026-10-12 02:13:36 +00:00
Summary: This PR accelerates `ScalarQuantizer::QT_bf16` by vectorizing the BF16 encode/decode path with AVX-512 BF16 and integer instructions, targeting Intel® Sapphire Rapids and newer processors. Distance computations are also optimized to operate directly on BF16 data, avoiding the overhead of FP32 conversion: - **Inner product:** The query is encoded to BF16 once, and dot products are computed natively in BF16. - **L2 distance:** Leverages the norm expansion identity, with all terms evaluated using the native BF16 dot-product instruction. Scalar quantizer benchmarks demonstrate speedups of **1.42×** for encoding, **1.32×** for decoding, and **1.16×** for distance computation. --- `perf_tests/bench_scalar_quantizer_encode --d=768 --n=2000 --iterations=20 --benchmark_filter="QT_bf16"` ``` Existing impl: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... -------------------------------------------------------------------------------- QT_bf16/iterations:20 689149 ns 689010 ns 20 code_size=1.536k This PR: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... -------------------------------------------------------------------------------- QT_bf16/iterations:20 485337 ns 484511 ns 20 code_size=1.536k ``` `perf_tests/bench_scalar_quantizer_decode --d=768 --n=2000 --iterations=20 --benchmark_filter="QT_bf16"` ``` Existing impl: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... -------------------------------------------------------------------------------- QT_bf16/iterations:20 498607 ns 498539 ns 20 code_size=1.536k This PR: -------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... -------------------------------------------------------------------------------- QT_bf16/iterations:20 376614 ns 374200 ns 20 code_size=1.536k ``` `perf_tests/bench_scalar_quantizer_distance --d=768 --n=2000 --iterations=20 --benchmark_filter="QT_bf16"` ``` Existing impl: ----------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... ----------------------------------------------------------------------------------- QT_bf16/L2/iterations:20 254155481 ns 253585062 ns 20 code_size=1.536k QT_bf16/IP/iterations:20 221556582 ns 221156894 ns 20 code_size=1.536k This PR: ----------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... ----------------------------------------------------------------------------------- QT_bf16/L2/iterations:20 226742244 ns 226270273 ns 20 code_size=1.536k QT_bf16/IP/iterations:20 190916826 ns 190591678 ns 20 code_size=1.536k ``` Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4889 Reviewed By: mdouze Differential Revision: D106149331 Pulled By: mnorris11 fbshipit-source-id: 114097d0597028c43c19727b677dcc494250be41
98 lines
2.5 KiB
C++
98 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include <gflags/gflags.h>
|
|
#include <omp.h>
|
|
#include <cstdio>
|
|
#include <map>
|
|
|
|
#include <benchmark/benchmark.h>
|
|
#include <faiss/MetricType.h>
|
|
#include <faiss/impl/ScalarQuantizer.h>
|
|
#include <faiss/utils/random.h>
|
|
#include "utils.h"
|
|
|
|
using namespace faiss;
|
|
|
|
DEFINE_uint32(d, 128, "dimension");
|
|
DEFINE_uint32(n, 2000, "number of vectors");
|
|
DEFINE_uint32(iterations, 20, "iterations");
|
|
|
|
static void bench_distance(
|
|
benchmark::State& state,
|
|
ScalarQuantizer::QuantizerType type,
|
|
MetricType metric,
|
|
int d,
|
|
int n) {
|
|
std::vector<float> x(d * n);
|
|
|
|
float_rand(x.data(), d * n, 12345);
|
|
|
|
ScalarQuantizer sq(d, type);
|
|
|
|
omp_set_num_threads(1);
|
|
|
|
sq.train(n, x.data());
|
|
|
|
size_t code_size = sq.code_size;
|
|
state.counters["code_size"] = sq.code_size;
|
|
|
|
// encode
|
|
std::vector<uint8_t> codes(code_size * n);
|
|
sq.compute_codes(x.data(), codes.data(), n);
|
|
|
|
std::unique_ptr<ScalarQuantizer::SQDistanceComputer> dc(
|
|
sq.get_distance_computer(metric));
|
|
dc->codes = codes.data();
|
|
dc->code_size = sq.code_size;
|
|
|
|
for (auto _ : state) {
|
|
float sum_dis = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
dc->set_query(&x[i * d]);
|
|
for (int j = 0; j < n; j++) {
|
|
benchmark::DoNotOptimize(sum_dis += (*dc)(j));
|
|
}
|
|
}
|
|
benchmark::ClobberMemory();
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
benchmark::Initialize(&argc, argv);
|
|
gflags::AllowCommandLineReparsing();
|
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
int iterations = FLAGS_iterations;
|
|
int d = FLAGS_d;
|
|
int n = FLAGS_n;
|
|
auto benchs = ::perf_tests::sq_types();
|
|
|
|
for (auto& [bench_name, quantizer_type] : benchs) {
|
|
benchmark::RegisterBenchmark(
|
|
(bench_name + "/L2").c_str(),
|
|
bench_distance,
|
|
quantizer_type,
|
|
METRIC_L2,
|
|
d,
|
|
n)
|
|
->Iterations(iterations);
|
|
|
|
benchmark::RegisterBenchmark(
|
|
(bench_name + "/IP").c_str(),
|
|
bench_distance,
|
|
quantizer_type,
|
|
METRIC_INNER_PRODUCT,
|
|
d,
|
|
n)
|
|
->Iterations(iterations);
|
|
}
|
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
|
benchmark::Shutdown();
|
|
}
|