-
Notifications
You must be signed in to change notification settings - Fork 934
Add a preprocessor in Turbo #548
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
zzlin237
wants to merge
62
commits into
alibaba:main
Choose a base branch
from
zzlin237:refactor/turbo_preprocessor
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
62 commits
Select commit
Hold shift + click to select a range
b1313a4
refactor: add quantizer and distance
richyreachy 45d9b54
fix: add turo quantizer
richyreachy e82d73f
refactor: add quantizer
richyreachy 829f53e
refactor: update meta
richyreachy f084630
refactor: update meta
richyreachy c3c4ca3
refactor: add fp32 quantizer
richyreachy ec4ea1f
refactor: add quantizer
richyreachy e36f0c5
refactor: quantizer
richyreachy c1cdd0b
refactor: add scalar
richyreachy 628982d
fix: fix cosine
richyreachy de33220
fix: add quantizer
richyreachy 49780f8
fix: fix quantizer
richyreachy b1d4849
fix: remove unused var
richyreachy f0fdf5d
fix: header
richyreachy 0c4cec6
fix: header
richyreachy 73f5bc3
fix: header
richyreachy 89843dd
add preprocessor
zzlin237 437a754
fix: fix symbol
richyreachy e16cda5
add untest
zzlin237 22b87d8
Merge remote-tracking branch 'origin/refactor/turbo_quantizer' into r…
zzlin237 f3a6782
remove extra doc
zzlin237 642b8bf
Merge remote-tracking branch 'upstream/main' into refactor/turbo_prep…
zzlin237 79a08eb
style: fix clang-format issues from upstream merge
zzlin237 78ace2b
style: fix clang-format issues in turbo module
zzlin237 b80bc4d
fix: LNK1120
zzlin237 281fdb4
Rename to conform to the style guide
zzlin237 10cbe03
Rename to conform to the style guide
zzlin237 c2026cb
Merge branch 'main' into refactor/turbo_quantizer
richyreachy c468e12
Merge branch 'main' into refactor/turbo_preprocessor
zzlin237 cb66180
Merge branch 'main' into refactor/turbo_quantizer
richyreachy 3470e4b
Merge remote-tracking branch 'upstream/main' into refactor/turbo_prep…
zzlin237 a6ecfb6
Merge remote-tracking branch 'richyreachy/refactor/turbo_quantizer' i…
zzlin237 cb36a54
Merge branch 'refactor/turbo_preprocessor' of https://github.com/zzli…
zzlin237 1c2de23
Merge remote-tracking branch 'upstream/main' into refactor/turbo_prep…
zzlin237 8406e9f
remove train to create int fht
zzlin237 ac17ee9
fix: add neon
richyreachy 7bb01c3
fix: add arm arch
richyreachy bd1bd50
Merge branch 'refactor/turbo_quantizer' of github.com:richyreachy/zve…
richyreachy 6abf1b8
CpuArchType FHT
zzlin237 ea816cc
reset preprocessor
zzlin237 03700da
Merge remote-tracking branch 'richyreachy/refactor/turbo_quantizer' i…
zzlin237 b909b4d
add neon
zzlin237 c826ab8
Merge branch 'main' into refactor/turbo_quantizer
richyreachy 403c123
fix: add meta
richyreachy b3e6a56
fix: query meta
richyreachy 11e8966
Merge remote-tracking branch 'richyreachy/refactor/turbo_quantizer' i…
zzlin237 3cd090c
clang-format
zzlin237 4287131
fix: fix alignment
richyreachy 3943566
refactor: move header file
richyreachy 05d43de
Merge branch 'main' into refactor/turbo_quantizer
richyreachy 084b475
fix: remove comment
richyreachy e6fa14e
Merge branch 'refactor/turbo_quantizer' of github.com:richyreachy/zve…
richyreachy 931c61d
tmp
zzlin237 7552df5
fix: fix empty line
richyreachy 28fe49a
add RotateType
zzlin237 bc9149d
add RotateType
zzlin237 7606006
Merge remote-tracking branch 'richyreachy/refactor/turbo_quantizer' i…
zzlin237 19c6578
merge main
zzlin237 8997541
Unified Scheduling Mode
zzlin237 ec946b1
Adjust Directory
zzlin237 6b20747
fix enum
zzlin237 4065035
add helper
zzlin237 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
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,154 @@ | ||
| // 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. | ||
|
|
||
| #if defined(__AVX2__) | ||
|
|
||
| #include "fht.h" | ||
| #include <immintrin.h> | ||
| #include <cmath> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include "common/fht_common.h" | ||
|
|
||
| namespace zvec::turbo::avx2 { | ||
|
|
||
| void fht_flip_sign_avx2(const uint8_t *flip, float *data, size_t dim) { | ||
| size_t simd_end = dim & ~31u; | ||
| constexpr size_t kChunk = 32; | ||
| const __m256i bit_select = | ||
| _mm256_setr_epi32(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); | ||
| const __m256 sign_flip = _mm256_castsi256_ps(_mm256_set1_epi32(0x80000000)); | ||
| for (size_t i = 0; i < simd_end; i += kChunk) { | ||
| uint32_t mask_bits; | ||
| std::memcpy(&mask_bits, &flip[i / 8], sizeof(mask_bits)); | ||
| for (int b = 0; b < 4; ++b) { | ||
| __m256i mb = _mm256_set1_epi32((mask_bits >> (b * 8)) & 0xFF); | ||
| __m256i test = _mm256_and_si256(mb, bit_select); | ||
| __m256i cmp = _mm256_cmpeq_epi32(test, bit_select); | ||
| __m256 xor_mask = _mm256_and_ps(_mm256_castsi256_ps(cmp), sign_flip); | ||
| __m256 v = _mm256_loadu_ps(&data[i + b * 8]); | ||
| v = _mm256_xor_ps(v, xor_mask); | ||
| _mm256_storeu_ps(&data[i + b * 8], v); | ||
| } | ||
| } | ||
| // Scalar tail | ||
| for (size_t i = simd_end; i < dim; ++i) { | ||
| if (flip[i / 8] & (1u << (i % 8))) { | ||
| data[i] = -data[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void fht_kacs_walk_avx2(float *data, size_t len) { | ||
| size_t half = len / 2; | ||
| size_t base = len % 2; | ||
| size_t offset = base + half; | ||
| size_t half_end = half & ~7u; | ||
| for (size_t i = 0; i < half_end; i += 8) { | ||
| __m256 x = _mm256_loadu_ps(&data[i]); | ||
| __m256 y = _mm256_loadu_ps(&data[i + offset]); | ||
| _mm256_storeu_ps(&data[i], _mm256_add_ps(x, y)); | ||
| _mm256_storeu_ps(&data[i + offset], _mm256_sub_ps(x, y)); | ||
| } | ||
| // Scalar tail | ||
| for (size_t i = half_end; i < half; ++i) { | ||
| float x = data[i]; | ||
| float y = data[i + offset]; | ||
| data[i] = x + y; | ||
| data[i + offset] = x - y; | ||
| } | ||
| if (base != 0) { | ||
| data[half] *= std::sqrt(2.0f); | ||
| } | ||
| } | ||
|
|
||
| void fht_inv_kacs_walk_avx2(float *data, size_t len) { | ||
| size_t half = len / 2; | ||
| size_t base = len % 2; | ||
| size_t offset = base + half; | ||
| if (base != 0) { | ||
| data[half] *= std::sqrt(0.5f); | ||
| } | ||
| size_t half_end = half & ~7u; | ||
| const __m256 half_fac = _mm256_set1_ps(0.5f); | ||
| for (size_t i = 0; i < half_end; i += 8) { | ||
| __m256 a = _mm256_loadu_ps(&data[i]); | ||
| __m256 b = _mm256_loadu_ps(&data[i + offset]); | ||
| _mm256_storeu_ps(&data[i], _mm256_mul_ps(_mm256_add_ps(a, b), half_fac)); | ||
| _mm256_storeu_ps(&data[i + offset], | ||
| _mm256_mul_ps(_mm256_sub_ps(a, b), half_fac)); | ||
| } | ||
| // Scalar tail | ||
| for (size_t i = half_end; i < half; ++i) { | ||
| float a = data[i]; | ||
| float b = data[i + offset]; | ||
| data[i] = (a + b) * 0.5f; | ||
| data[i + offset] = (a - b) * 0.5f; | ||
| } | ||
| } | ||
|
|
||
| void fht_inplace_avx2(float *data, size_t n) { | ||
| for (size_t len = 1; len < n; len <<= 1) { | ||
| size_t step = len << 1; | ||
| size_t simd_end = len & ~7u; | ||
| for (size_t i = 0; i < n; i += step) { | ||
| for (size_t j = 0; j < simd_end; j += 8) { | ||
| __m256 u = _mm256_loadu_ps(&data[i + j]); | ||
| __m256 v = _mm256_loadu_ps(&data[i + j + len]); | ||
| _mm256_storeu_ps(&data[i + j], _mm256_add_ps(u, v)); | ||
| _mm256_storeu_ps(&data[i + j + len], _mm256_sub_ps(u, v)); | ||
| } | ||
| for (size_t j = simd_end; j < len; ++j) { | ||
| float u = data[i + j]; | ||
| float v = data[i + j + len]; | ||
| data[i + j] = u + v; | ||
| data[i + j + len] = u - v; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void fht_vec_rescale_avx2(float *data, size_t n, float factor) { | ||
| const __m256 fac = _mm256_set1_ps(factor); | ||
| size_t simd_end = n & ~7u; | ||
| for (size_t i = 0; i < simd_end; i += 8) { | ||
| __m256 v = _mm256_loadu_ps(&data[i]); | ||
| _mm256_storeu_ps(&data[i], _mm256_mul_ps(v, fac)); | ||
| } | ||
| // Scalar tail | ||
| for (size_t i = simd_end; i < n; ++i) { | ||
| data[i] *= factor; | ||
| } | ||
| } | ||
|
|
||
| void fht_rotate_avx2(const float * /*in*/, float *out, size_t in_dim, | ||
| size_t /*out_dim*/, void *ctx) { | ||
| static constexpr FhtPrimitives kPrim = { | ||
| fht_flip_sign_avx2, fht_inplace_avx2, fht_kacs_walk_avx2, | ||
| fht_inv_kacs_walk_avx2, fht_vec_rescale_avx2}; | ||
| fht_rotate_impl(out, in_dim, ctx, kPrim); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in参数完全没用到,要求输入已经在out了,这个接口有问题吧?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为操作全是原地操作,地址分配已经放到在上层完成,此处我就直接输入out了 |
||
| } | ||
|
|
||
| void fht_unrotate_avx2(const float * /*in*/, float *out, size_t in_dim, | ||
| size_t /*out_dim*/, void *ctx) { | ||
| static constexpr FhtPrimitives kPrim = { | ||
| fht_flip_sign_avx2, fht_inplace_avx2, fht_kacs_walk_avx2, | ||
| fht_inv_kacs_walk_avx2, fht_vec_rescale_avx2}; | ||
| fht_unrotate_impl(out, in_dim, ctx, kPrim); | ||
| } | ||
|
|
||
| } // namespace zvec::turbo::avx2 | ||
|
|
||
| #endif // __AVX2__ | ||
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,49 @@ | ||
| // 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__) | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| namespace zvec::turbo::avx2 { | ||
|
|
||
| //! Apply bitwise sign-flip mask to a float vector (AVX2). | ||
| void fht_flip_sign_avx2(const uint8_t *flip, float *data, size_t dim); | ||
|
|
||
| //! Apply KacsWalk butterfly operation (AVX2). | ||
| void fht_kacs_walk_avx2(float *data, size_t len); | ||
|
|
||
| //! Inverse KacsWalk butterfly operation (AVX2). | ||
| void fht_inv_kacs_walk_avx2(float *data, size_t len); | ||
|
|
||
| //! In-place Fast Hadamard Transform (AVX2, n must be power-of-2). | ||
| void fht_inplace_avx2(float *data, size_t n); | ||
|
|
||
| //! Element-wise rescale: data[i] *= factor (AVX2). | ||
| void fht_vec_rescale_avx2(float *data, size_t n, float factor); | ||
|
|
||
| //! Forward FHT rotation (AVX2). | ||
| void fht_rotate_avx2(const float *in, float *out, size_t in_dim, size_t out_dim, | ||
| void *ctx); | ||
|
|
||
| //! Inverse FHT rotation (AVX2). | ||
| void fht_unrotate_avx2(const float *in, float *out, size_t in_dim, | ||
| size_t out_dim, void *ctx); | ||
|
|
||
| } // namespace zvec::turbo::avx2 | ||
|
|
||
| #endif // __AVX2__ |
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.