-
Notifications
You must be signed in to change notification settings - Fork 155
Add native SIMD testing and benchmark module #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
r-devulap
wants to merge
9
commits into
datastax:main
Choose a base branch
from
r-devulap:simd-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
190ef5c
Move native files into a src directory, prep to make this a separate …
r-devulap b5ceef2
tests(native): parametrize GTest over all SIMD kernel tiers
r-devulap 56f9afb
benchmarks: add fp32 similarity micro-benchmarks via Google Benchmark
r-devulap 63e921d
ci: build and run test_simd_kernels on avx512 runner
r-devulap 143ee41
fix: update script paths after native files moved into src/
r-devulap 4578555
fix: copy libjvector.so to src/main/resources so Maven packages it in…
r-devulap a915920
Update native README to include test and bench
r-devulap 9f08017
meson: add helpful error messages when gtest/benchmark deps are missing
r-devulap 9958846
set +x --> set -x to print bash commands as they run
r-devulap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
jvector-native/src/main/native/benchmarks/bench_similarity_f32.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Google Benchmark micro-benchmarks for the fp32 vector similarity kernels: | ||
| // cosine_f32, dot_product_f32, euclidean_f32 | ||
| // | ||
| // Parameterised over the realistic embedding dimensions used in production: | ||
| // 128, 256, 512, 1024, 1536, 3072 | ||
| // | ||
| // Build (requires google-benchmark installed or available via pkg-config): | ||
| // meson setup build && ninja -C build bench_simd_kernels | ||
|
MarkWolters marked this conversation as resolved.
|
||
| // | ||
| // Run: | ||
| // ./build/bench_simd_kernels [--benchmark_filter=<pattern>] | ||
|
|
||
| #include <benchmark/benchmark.h> | ||
| #include <cmath> | ||
| #include <vector> | ||
|
|
||
| #include "jvector_simd.h" | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| // Deterministic, non-zero float vector: avoids degenerate cosine=NaN cases. | ||
| static std::vector<float> make_vec(size_t n, float seed) | ||
| { | ||
| std::vector<float> v(n); | ||
| for (size_t i = 0; i < n; ++i) { | ||
| v[i] = seed * (1.0f + static_cast<float>(i % 7) * 0.13f); | ||
| if (i % 3 == 0) v[i] = -v[i]; | ||
| v[i] += 0.5f; | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| // Benchmark sizes matching production embedding dimensions. | ||
| static const std::vector<int64_t> kBenchSizes = {128, 256, 512, 1024, 1536, 3072}; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // dot_product_f32 | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| static void BM_dot_product_f32(benchmark::State& state) | ||
| { | ||
| const size_t n = static_cast<size_t>(state.range(0)); | ||
| auto a = make_vec(n, 0.7f); | ||
| auto b = make_vec(n, 1.3f); | ||
|
|
||
| for (auto _ : state) { | ||
| float result = dot_product_f32(a.data(), 0, b.data(), 0, n); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
|
|
||
| state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(n)); | ||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(n) * 2 * sizeof(float)); | ||
| } | ||
| BENCHMARK(BM_dot_product_f32)->ArgsProduct({kBenchSizes}); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // euclidean_f32 | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| static void BM_euclidean_f32(benchmark::State& state) | ||
| { | ||
| const size_t n = static_cast<size_t>(state.range(0)); | ||
| auto a = make_vec(n, 0.7f); | ||
| auto b = make_vec(n, 1.3f); | ||
|
|
||
| for (auto _ : state) { | ||
| float result = euclidean_f32(a.data(), 0, b.data(), 0, n); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
|
|
||
| state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(n)); | ||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(n) * 2 * sizeof(float)); | ||
| } | ||
| BENCHMARK(BM_euclidean_f32)->ArgsProduct({kBenchSizes}); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // cosine_f32 | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| static void BM_cosine_f32(benchmark::State& state) | ||
| { | ||
| const size_t n = static_cast<size_t>(state.range(0)); | ||
| auto a = make_vec(n, 0.7f); | ||
| auto b = make_vec(n, 1.3f); | ||
|
|
||
| for (auto _ : state) { | ||
| float result = cosine_f32(a.data(), 0, b.data(), 0, n); | ||
| benchmark::DoNotOptimize(result); | ||
| } | ||
|
|
||
| state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(n)); | ||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(n) * 2 * sizeof(float)); | ||
| } | ||
| BENCHMARK(BM_cosine_f32)->ArgsProduct({kBenchSizes}); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Entry point — benchmark::Initialize parses --benchmark_* flags. | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| BENCHMARK_MAIN(); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.