-
Notifications
You must be signed in to change notification settings - Fork 522
feat: add euclidean one2many #188
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
09236a6
add euclidean one2many implementation
richyreachy 6370754
add euclidean one2many implementation
richyreachy 200cc06
add euclidean one2many implementation
richyreachy 9c07ff9
format codes
richyreachy 9545fba
format codes
richyreachy 601de2a
fix macro missing
richyreachy b766fb1
fix testcase
richyreachy 4cb66f1
merge: merge from main
richyreachy e20e4e6
refactor: euclidean
richyreachy 30470e8
fix: update code
richyreachy 05e3dcf
fix: clang format
richyreachy 4cfc424
fix: clang format
richyreachy e5590fe
fix: fix format
richyreachy 93ee2a0
fix: fix index
richyreachy 91e9813
fix: fix format
richyreachy 407ee5c
fix: fix format
richyreachy 55eada0
fix: fix format
richyreachy 19b094b
Merge branch 'main' into feat/euclidean_one2many
richyreachy 093357e
fix: update
richyreachy d7e7a64
fix: fix distance computation
richyreachy 928b620
fix: fix distance computation
richyreachy 27cdb43
Merge branch 'main' into feat/euclidean_one2many
richyreachy 1de3930
Update src/ailego/math_batch/euclidean_distance_batch_impl_fp16_avx51…
richyreachy bef677e
fix: fix distance computation
richyreachy d4acef5
fix: fix distance computation
richyreachy 6a82545
fix: remove unnecessary file
richyreachy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #if defined(__AVX2__) | ||
|
|
||
| inline float sum4(__m128 v) { | ||
| v = _mm_add_ps(v, _mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(v), 8))); | ||
| return v[0] + v[1]; | ||
| } | ||
|
|
||
| inline __m128 sum_top_bottom_avx(__m256 v) { | ||
| const __m128 high = _mm256_extractf128_ps(v, 1); | ||
| const __m128 low = _mm256_castps256_ps128(v); | ||
| return _mm_add_ps(high, low); | ||
| } | ||
|
|
||
| #endif |
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,142 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <vector> | ||
| #include <ailego/math/euclidean_distance_matrix.h> | ||
| #include <ailego/utility/math_helper.h> | ||
| #include <zvec/ailego/internal/platform.h> | ||
| #include <zvec/ailego/math_batch/utils.h> | ||
| #include <zvec/ailego/utility/type_helper.h> | ||
|
|
||
| namespace zvec::ailego::DistanceBatch { | ||
|
|
||
| // SquaredEuclideanDistanceBatch | ||
| template <typename T, size_t BatchSize, size_t PrefetchStep, typename = void> | ||
| struct SquaredEuclideanDistanceBatch; | ||
|
|
||
| template <typename ValueType, size_t BatchSize> | ||
| static void compute_one_to_many_squared_euclidean_fallback( | ||
| const ValueType *query, const ValueType **ptrs, | ||
| std::array<const ValueType *, BatchSize> &prefetch_ptrs, size_t dim, | ||
| float *sums) { | ||
| for (size_t j = 0; j < BatchSize; ++j) { | ||
| sums[j] = 0.0; | ||
| SquaredEuclideanDistanceMatrix<ValueType, 1, 1>::Compute(ptrs[j], query, | ||
| dim, sums + j); | ||
| ailego_prefetch(&prefetch_ptrs[j]); | ||
| } | ||
| } | ||
|
|
||
| // Function template partial specialization is not allowed, | ||
| // therefore the wrapper struct is required. | ||
| template <typename T, size_t BatchSize> | ||
| struct SquaredEuclideanDistanceBatchImpl { | ||
| using ValueType = typename std::remove_cv<T>::type; | ||
| static void compute_one_to_many( | ||
| const ValueType *query, const ValueType **ptrs, | ||
| std::array<const ValueType *, BatchSize> &prefetch_ptrs, size_t dim, | ||
| float *sums) { | ||
| return compute_one_to_many_squared_euclidean_fallback( | ||
| query, ptrs, prefetch_ptrs, dim, sums); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T, size_t BatchSize, size_t PrefetchStep, typename> | ||
| struct SquaredEuclideanDistanceBatch { | ||
| using ValueType = typename std::remove_cv<T>::type; | ||
|
|
||
| static inline void ComputeBatch(const ValueType **vecs, | ||
| const ValueType *query, size_t num_vecs, | ||
| size_t dim, float *results) { | ||
| size_t i = 0; | ||
| for (; i + BatchSize <= num_vecs; i += BatchSize) { | ||
| std::array<const ValueType *, BatchSize> prefetch_ptrs; | ||
| for (size_t j = 0; j < BatchSize; ++j) { | ||
| if (i + j + BatchSize * PrefetchStep < num_vecs) { | ||
| prefetch_ptrs[j] = vecs[i + j + BatchSize * PrefetchStep]; | ||
| } else { | ||
| prefetch_ptrs[j] = nullptr; | ||
| } | ||
| } | ||
| SquaredEuclideanDistanceBatchImpl< | ||
| ValueType, BatchSize>::compute_one_to_many(query, &vecs[i], | ||
| prefetch_ptrs, dim, | ||
| &results[i]); | ||
| } | ||
| for (; i < num_vecs; ++i) { // TODO: unroll by 1, 2, 4, 8, etc. | ||
| std::array<const ValueType *, 1> prefetch_ptrs{nullptr}; | ||
| SquaredEuclideanDistanceBatchImpl<ValueType, 1>::compute_one_to_many( | ||
| query, &vecs[i], prefetch_ptrs, dim, &results[i]); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // EuclideanDistanceBatch | ||
| template <typename T, size_t BatchSize, size_t PrefetchStep, typename = void> | ||
| struct EuclideanDistanceBatch; | ||
|
|
||
| template <typename T, size_t BatchSize, size_t PrefetchStep, typename> | ||
| struct EuclideanDistanceBatch { | ||
| using ValueType = typename std::remove_cv<T>::type; | ||
|
|
||
| static inline void ComputeBatch(const ValueType **vecs, | ||
| const ValueType *query, size_t num_vecs, | ||
| size_t dim, float *results) { | ||
| SquaredEuclideanDistanceBatch<T, BatchSize, PrefetchStep>::ComputeBatch( | ||
| vecs, query, num_vecs, dim, results); | ||
|
|
||
| for (size_t i = 0; i < num_vecs; ++i) { | ||
| results[i] = std::sqrt(results[i]); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| template <> | ||
| struct SquaredEuclideanDistanceBatchImpl<ailego::Float16, 1> { | ||
| using ValueType = ailego::Float16; | ||
| static void compute_one_to_many( | ||
| const ailego::Float16 *query, const ailego::Float16 **ptrs, | ||
| std::array<const ailego::Float16 *, 1> &prefetch_ptrs, size_t dim, | ||
| float *sums); | ||
| }; | ||
|
|
||
| template <> | ||
| struct SquaredEuclideanDistanceBatchImpl<float, 1> { | ||
| using ValueType = float; | ||
| static void compute_one_to_many(const float *query, const float **ptrs, | ||
| std::array<const float *, 1> &prefetch_ptrs, | ||
| size_t dim, float *sums); | ||
| }; | ||
|
|
||
| template <> | ||
| struct SquaredEuclideanDistanceBatchImpl<ailego::Float16, 12> { | ||
| using ValueType = ailego::Float16; | ||
| static void compute_one_to_many( | ||
| const ailego::Float16 *query, const ailego::Float16 **ptrs, | ||
| std::array<const ailego::Float16 *, 12> &prefetch_ptrs, size_t dim, | ||
| float *sums); | ||
| }; | ||
|
|
||
| template <> | ||
| struct SquaredEuclideanDistanceBatchImpl<float, 12> { | ||
| using ValueType = float; | ||
| static void compute_one_to_many(const float *query, const float **ptrs, | ||
| std::array<const float *, 12> &prefetch_ptrs, | ||
| size_t dim, float *sums); | ||
| }; | ||
|
|
||
| } // namespace zvec::ailego::DistanceBatch | ||
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.