-
Notifications
You must be signed in to change notification settings - Fork 22
Implement optimized FP16 support for ARM architecture - [MOD-9078] #620
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
37 commits
Select commit
Hold shift + click to select a range
afb8e9e
implement L2 SVE with intermediate casting to f32
GuyAv46 fc3538a
implement IP SVE with f16 ops only
GuyAv46 c23cecb
implements L2 sve with no intermediate casting
GuyAv46 1f8b40a
add SVE and SVE2 functions files
GuyAv46 cfc78db
add new files to cmake and use new implementations
GuyAv46 af9759d
added benchmarks
GuyAv46 0322729
fix and switch implementation (due to sve2-only op)
GuyAv46 145314d
test with SVE2 intrinsics
GuyAv46 80e0f8a
Revert "test with SVE2 intrinsics"
GuyAv46 4c7838b
remove redundant implementation
GuyAv46 6934861
move to 4 steps per iteration implementations
GuyAv46 54122ef
add macro cleanup
GuyAv46 e35e3df
fix implementation
GuyAv46 f145d11
refactor to use 4 accumulators
GuyAv46 607f9cf
added tests
GuyAv46 363962c
refactor accumulation
GuyAv46 9e7553f
add initial neon implementation
GuyAv46 380ebbe
fix build flags and file layout
GuyAv46 854bf1c
fix tests
GuyAv46 e07ef6e
cleanup and L2 implementation with neon+fp16
GuyAv46 f0e150a
format
GuyAv46 4d61cd1
fix test for any arch
GuyAv46 9deb47f
another attempt
GuyAv46 7c459fa
fix test
GuyAv46 e5a45f4
rename step functions
GuyAv46 e20461a
comment-in neon benchmarks
GuyAv46 7a768aa
fix benchmark
GuyAv46 7b034b6
review fixes
GuyAv46 5bdf91e
more review fixes
GuyAv46 e67929c
fixes and cleanup
GuyAv46 7831653
fix svwhilelt_b16 calls
GuyAv46 a754e10
use vbslq_f16
GuyAv46 502a52e
typo fix
GuyAv46 31cb687
Merge branch 'main' into guyav-arm_fp16_support
GuyAv46 ea46a83
fix test for OSs that don't support fp16
GuyAv46 43bd7b5
added back guards for a specific x86 test
GuyAv46 cb63b7b
Merge branch 'main' into guyav-arm_fp16_support
GuyAv46 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,93 @@ | ||
| /* | ||
| *Copyright Redis Ltd. 2021 - present | ||
| *Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or | ||
| *the Server Side Public License v1 (SSPLv1). | ||
| */ | ||
|
|
||
| #include <arm_neon.h> | ||
|
|
||
| inline void InnerProduct_Step(const float16_t *&vec1, const float16_t *&vec2, float16x8_t &acc) { | ||
| // Load half-precision vectors | ||
| float16x8_t v1 = vld1q_f16(vec1); | ||
| float16x8_t v2 = vld1q_f16(vec2); | ||
| vec1 += 8; | ||
| vec2 += 8; | ||
|
|
||
| // Multiply and accumulate | ||
| acc = vfmaq_f16(acc, v1, v2); | ||
| } | ||
|
|
||
| template <unsigned char residual> // 0..31 | ||
| float FP16_InnerProduct_NEON_HP(const void *pVect1v, const void *pVect2v, size_t dimension) { | ||
| const auto *vec1 = static_cast<const float16_t *>(pVect1v); | ||
| const auto *vec2 = static_cast<const float16_t *>(pVect2v); | ||
| const auto *const v1End = vec1 + dimension; | ||
| float16x8_t acc1 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc2 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc3 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc4 = vdupq_n_f16(0.0f); | ||
|
|
||
| // First, handle the partial chunk residual | ||
| if constexpr (residual % 8) { | ||
| auto constexpr chunk_residual = residual % 8; | ||
| // TODO: spacial cases for some residuals and benchmark if its better | ||
| constexpr uint16x8_t mask = { | ||
| 0xFFFF, | ||
| (chunk_residual >= 2) ? 0xFFFF : 0, | ||
| (chunk_residual >= 3) ? 0xFFFF : 0, | ||
| (chunk_residual >= 4) ? 0xFFFF : 0, | ||
| (chunk_residual >= 5) ? 0xFFFF : 0, | ||
| (chunk_residual >= 6) ? 0xFFFF : 0, | ||
| (chunk_residual >= 7) ? 0xFFFF : 0, | ||
| 0, | ||
| }; | ||
|
|
||
| // Load partial vectors | ||
| float16x8_t v1 = vld1q_f16(vec1); | ||
| float16x8_t v2 = vld1q_f16(vec2); | ||
|
|
||
| // Apply mask to both vectors | ||
| float16x8_t masked_v1 = vbslq_f16(mask, v1, acc1); // `acc1` should be all zeros here | ||
| float16x8_t masked_v2 = vbslq_f16(mask, v2, acc2); // `acc2` should be all zeros here | ||
|
|
||
| // Multiply and accumulate | ||
| acc1 = vfmaq_f16(acc1, masked_v1, masked_v2); | ||
|
|
||
| // Advance pointers | ||
| vec1 += chunk_residual; | ||
| vec2 += chunk_residual; | ||
| } | ||
|
|
||
| // Handle (residual - (residual % 8)) in chunks of 8 float16 | ||
| if constexpr (residual >= 8) | ||
| InnerProduct_Step(vec1, vec2, acc2); | ||
| if constexpr (residual >= 16) | ||
| InnerProduct_Step(vec1, vec2, acc3); | ||
| if constexpr (residual >= 24) | ||
| InnerProduct_Step(vec1, vec2, acc4); | ||
|
|
||
| // Process the rest of the vectors (the full chunks part) | ||
| while (vec1 < v1End) { | ||
| // TODO: use `vld1q_f16_x4` for quad-loading? | ||
| InnerProduct_Step(vec1, vec2, acc1); | ||
| InnerProduct_Step(vec1, vec2, acc2); | ||
| InnerProduct_Step(vec1, vec2, acc3); | ||
| InnerProduct_Step(vec1, vec2, acc4); | ||
| } | ||
|
|
||
| // Accumulate accumulators | ||
| acc1 = vpaddq_f16(acc1, acc3); | ||
| acc2 = vpaddq_f16(acc2, acc4); | ||
| acc1 = vpaddq_f16(acc1, acc2); | ||
|
|
||
| // Horizontal sum of the accumulated values | ||
| float32x4_t sum_f32 = vcvt_f32_f16(vget_low_f16(acc1)); | ||
| sum_f32 = vaddq_f32(sum_f32, vcvt_f32_f16(vget_high_f16(acc1))); | ||
|
|
||
| // Pairwise add to get horizontal sum | ||
| float32x2_t sum_2 = vadd_f32(vget_low_f32(sum_f32), vget_high_f32(sum_f32)); | ||
| sum_2 = vpadd_f32(sum_2, sum_2); | ||
|
|
||
| // Extract result | ||
| return 1.0f - vget_lane_f32(sum_2, 0); | ||
| } | ||
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,72 @@ | ||
| /* | ||
| *Copyright Redis Ltd. 2021 - present | ||
| *Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or | ||
| *the Server Side Public License v1 (SSPLv1). | ||
| */ | ||
|
|
||
| #include <arm_sve.h> | ||
|
|
||
| inline void InnerProduct_Step(const float16_t *vec1, const float16_t *vec2, svfloat16_t &acc, | ||
| size_t &offset, const size_t chunk) { | ||
| svbool_t all = svptrue_b16(); | ||
|
|
||
| // Load half-precision vectors. | ||
| svfloat16_t v1 = svld1_f16(all, vec1 + offset); | ||
| svfloat16_t v2 = svld1_f16(all, vec2 + offset); | ||
| // Compute multiplications and add to the accumulator | ||
| acc = svmla_f16_x(all, acc, v1, v2); | ||
|
|
||
| // Move to next chunk | ||
| offset += chunk; | ||
| } | ||
|
|
||
| template <bool partial_chunk, unsigned char additional_steps> // [t/f, 0..3] | ||
| float FP16_InnerProduct_SVE(const void *pVect1v, const void *pVect2v, size_t dimension) { | ||
| const auto *vec1 = static_cast<const float16_t *>(pVect1v); | ||
| const auto *vec2 = static_cast<const float16_t *>(pVect2v); | ||
| const size_t chunk = svcnth(); // number of 16-bit elements in a register | ||
| svbool_t all = svptrue_b16(); | ||
| svfloat16_t acc1 = svdup_f16(0.0f); | ||
| svfloat16_t acc2 = svdup_f16(0.0f); | ||
| svfloat16_t acc3 = svdup_f16(0.0f); | ||
| svfloat16_t acc4 = svdup_f16(0.0f); | ||
| size_t offset = 0; | ||
|
|
||
| // Process all full vectors | ||
| const size_t full_iterations = dimension / chunk / 4; | ||
| for (size_t iter = 0; iter < full_iterations; iter++) { | ||
| InnerProduct_Step(vec1, vec2, acc1, offset, chunk); | ||
| InnerProduct_Step(vec1, vec2, acc2, offset, chunk); | ||
| InnerProduct_Step(vec1, vec2, acc3, offset, chunk); | ||
| InnerProduct_Step(vec1, vec2, acc4, offset, chunk); | ||
| } | ||
|
|
||
| // Perform between 0 and 3 additional steps, according to `additional_steps` value | ||
| if constexpr (additional_steps >= 1) | ||
| InnerProduct_Step(vec1, vec2, acc1, offset, chunk); | ||
| if constexpr (additional_steps >= 2) | ||
| InnerProduct_Step(vec1, vec2, acc2, offset, chunk); | ||
| if constexpr (additional_steps >= 3) | ||
| InnerProduct_Step(vec1, vec2, acc3, offset, chunk); | ||
|
|
||
| // Handle the tail with the residual predicate | ||
| if constexpr (partial_chunk) { | ||
| svbool_t pg = svwhilelt_b16_u64(offset, dimension); | ||
|
|
||
| // Load half-precision vectors. | ||
| svfloat16_t v1 = svld1_f16(pg, vec1 + offset); | ||
| svfloat16_t v2 = svld1_f16(pg, vec2 + offset); | ||
| // Compute multiplications and add to the accumulator. | ||
| // use the existing value of `acc` for the inactive elements (by the `m` suffix) | ||
| acc4 = svmla_f16_m(pg, acc4, v1, v2); | ||
| } | ||
|
|
||
| // Accumulate accumulators | ||
| acc1 = svadd_f16_x(all, acc1, acc3); | ||
| acc2 = svadd_f16_x(all, acc2, acc4); | ||
| acc1 = svadd_f16_x(all, acc1, acc2); | ||
|
|
||
| // Reduce the accumulated sum. | ||
| float result = svaddv_f16(all, acc1); | ||
| return 1.0f - result; | ||
| } |
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,97 @@ | ||
| /* | ||
| *Copyright Redis Ltd. 2021 - present | ||
| *Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or | ||
| *the Server Side Public License v1 (SSPLv1). | ||
| */ | ||
|
|
||
| #include <arm_neon.h> | ||
|
|
||
| inline void L2Sqr_Step(const float16_t *&vec1, const float16_t *&vec2, float16x8_t &acc) { | ||
| // Load half-precision vectors | ||
| float16x8_t v1 = vld1q_f16(vec1); | ||
| float16x8_t v2 = vld1q_f16(vec2); | ||
| vec1 += 8; | ||
| vec2 += 8; | ||
|
|
||
| // Calculate differences | ||
| float16x8_t diff = vsubq_f16(v1, v2); | ||
| // Square and accumulate | ||
| acc = vfmaq_f16(acc, diff, diff); | ||
| } | ||
|
|
||
| template <unsigned char residual> // 0..31 | ||
| float FP16_L2Sqr_NEON_HP(const void *pVect1v, const void *pVect2v, size_t dimension) { | ||
| const auto *vec1 = static_cast<const float16_t *>(pVect1v); | ||
| const auto *vec2 = static_cast<const float16_t *>(pVect2v); | ||
| const auto *const v1End = vec1 + dimension; | ||
| float16x8_t acc1 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc2 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc3 = vdupq_n_f16(0.0f); | ||
| float16x8_t acc4 = vdupq_n_f16(0.0f); | ||
|
|
||
| // First, handle the partial chunk residual | ||
| if constexpr (residual % 8) { | ||
| auto constexpr chunk_residual = residual % 8; | ||
| // TODO: spacial cases for some residuals and benchmark if its better | ||
| constexpr uint16x8_t mask = { | ||
| 0xFFFF, | ||
| (chunk_residual >= 2) ? 0xFFFF : 0, | ||
| (chunk_residual >= 3) ? 0xFFFF : 0, | ||
| (chunk_residual >= 4) ? 0xFFFF : 0, | ||
| (chunk_residual >= 5) ? 0xFFFF : 0, | ||
| (chunk_residual >= 6) ? 0xFFFF : 0, | ||
| (chunk_residual >= 7) ? 0xFFFF : 0, | ||
| 0, | ||
| }; | ||
|
|
||
| // Load partial vectors | ||
| float16x8_t v1 = vld1q_f16(vec1); | ||
| float16x8_t v2 = vld1q_f16(vec2); | ||
|
|
||
| // Apply mask to both vectors | ||
| float16x8_t masked_v1 = vbslq_f16(mask, v1, acc1); // `acc1` should be all zeros here | ||
| float16x8_t masked_v2 = vbslq_f16(mask, v2, acc2); // `acc2` should be all zeros here | ||
|
|
||
| // Calculate differences | ||
| float16x8_t diff = vsubq_f16(masked_v1, masked_v2); | ||
| // Square and accumulate | ||
| acc1 = vfmaq_f16(acc1, diff, diff); | ||
|
|
||
| // Advance pointers | ||
| vec1 += chunk_residual; | ||
| vec2 += chunk_residual; | ||
| } | ||
|
|
||
| // Handle (residual - (residual % 8)) in chunks of 8 float16 | ||
| if constexpr (residual >= 8) | ||
| L2Sqr_Step(vec1, vec2, acc2); | ||
| if constexpr (residual >= 16) | ||
| L2Sqr_Step(vec1, vec2, acc3); | ||
| if constexpr (residual >= 24) | ||
| L2Sqr_Step(vec1, vec2, acc4); | ||
|
|
||
| // Process the rest of the vectors (the full chunks part) | ||
| while (vec1 < v1End) { | ||
| // TODO: use `vld1q_f16_x4` for quad-loading? | ||
| L2Sqr_Step(vec1, vec2, acc1); | ||
| L2Sqr_Step(vec1, vec2, acc2); | ||
| L2Sqr_Step(vec1, vec2, acc3); | ||
| L2Sqr_Step(vec1, vec2, acc4); | ||
| } | ||
|
|
||
| // Accumulate accumulators | ||
| acc1 = vpaddq_f16(acc1, acc3); | ||
| acc2 = vpaddq_f16(acc2, acc4); | ||
| acc1 = vpaddq_f16(acc1, acc2); | ||
|
|
||
| // Horizontal sum of the accumulated values | ||
| float32x4_t sum_f32 = vcvt_f32_f16(vget_low_f16(acc1)); | ||
| sum_f32 = vaddq_f32(sum_f32, vcvt_f32_f16(vget_high_f16(acc1))); | ||
|
|
||
| // Pairwise add to get horizontal sum | ||
| float32x2_t sum_2 = vadd_f32(vget_low_f32(sum_f32), vget_high_f32(sum_f32)); | ||
| sum_2 = vpadd_f32(sum_2, sum_2); | ||
|
|
||
| // Extract result | ||
| return vget_lane_f32(sum_2, 0); | ||
| } |
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.