-
Notifications
You must be signed in to change notification settings - Fork 82
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
Enable SVE Support for L2 Metric Computation in FP32 #969
Open
adarshs1310
wants to merge
1
commit into
zilliztech:main
Choose a base branch
from
adarshs1310:sve-l2-fp32
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.
+288
−10
Open
Changes from all commits
Commits
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 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 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,184 @@ | ||
// Copyright (C) 2019-2023 Zilliz. All rights reserved. | ||
// | ||
// 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. | ||
|
||
#include "distances_sve.h" | ||
|
||
#include <arm_sve.h> | ||
|
||
#include <cmath> | ||
|
||
#include "faiss/impl/platform_macros.h" | ||
#if defined(__ARM_FEATURE_SVE) | ||
namespace faiss { | ||
|
||
float | ||
fvec_L2sqr_sve(const float* x, const float* y, size_t d) { | ||
svfloat32_t sum = svdup_f32(0.0f); | ||
size_t i = 0; | ||
|
||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < d) { | ||
if (d - i < svcntw()) | ||
pg = svwhilelt_b32(i, d); | ||
|
||
svfloat32_t a = svld1_f32(pg, x + i); | ||
svfloat32_t b = svld1_f32(pg, y + i); | ||
svfloat32_t diff = svsub_f32_m(pg, a, b); | ||
sum = svmla_f32_m(pg, sum, diff, diff); | ||
i += svcntw(); | ||
} | ||
|
||
return svaddv_f32(svptrue_b32(), sum); | ||
} | ||
|
||
float | ||
fvec_L1_sve(const float* x, const float* y, size_t d) { | ||
svfloat32_t sum = svdup_f32(0.0f); | ||
size_t i = 0; | ||
|
||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < d) { | ||
if (d - i < svcntw()) | ||
pg = svwhilelt_b32(i, d); | ||
|
||
svfloat32_t a = svld1_f32(pg, x + i); | ||
svfloat32_t b = svld1_f32(pg, y + i); | ||
svfloat32_t diff = svabs_f32_x(pg, svsub_f32_m(pg, a, b)); | ||
sum = svadd_f32_m(pg, sum, diff); | ||
i += svcntw(); | ||
} | ||
|
||
return svaddv_f32(svptrue_b32(), sum); | ||
} | ||
|
||
float | ||
fvec_Linf_sve(const float* x, const float* y, size_t d) { | ||
svfloat32_t max_val = svdup_f32(0.0f); | ||
size_t i = 0; | ||
|
||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < d) { | ||
if (d - i < svcntw()) | ||
pg = svwhilelt_b32(i, d); | ||
|
||
svfloat32_t a = svld1_f32(pg, x + i); | ||
svfloat32_t b = svld1_f32(pg, y + i); | ||
svfloat32_t diff = svabs_f32_x(pg, svsub_f32_m(pg, a, b)); | ||
max_val = svmax_f32_m(pg, max_val, diff); | ||
i += svcntw(); | ||
} | ||
|
||
return svmaxv_f32(svptrue_b32(), max_val); | ||
} | ||
|
||
float | ||
fvec_norm_L2sqr_sve(const float* x, size_t d) { | ||
svfloat32_t sum = svdup_f32(0.0f); | ||
size_t i = 0; | ||
|
||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < d) { | ||
if (d - i < svcntw()) | ||
pg = svwhilelt_b32(i, d); | ||
|
||
svfloat32_t a = svld1_f32(pg, x + i); | ||
sum = svmla_f32_m(pg, sum, a, a); | ||
i += svcntw(); | ||
} | ||
|
||
return svaddv_f32(svptrue_b32(), sum); | ||
} | ||
|
||
void | ||
fvec_madd_sve(size_t n, const float* a, float bf, const float* b, float* c) { | ||
size_t i = 0; | ||
svfloat32_t bf_vec = svdup_f32(bf); | ||
|
||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < n) { | ||
if (n - i < svcntw()) | ||
pg = svwhilelt_b32(i, n); | ||
|
||
svfloat32_t a_vec = svld1_f32(pg, a + i); | ||
svfloat32_t b_vec = svld1_f32(pg, b + i); | ||
svfloat32_t c_vec = svmla_f32_m(pg, a_vec, b_vec, bf_vec); | ||
svst1_f32(pg, c + i, c_vec); | ||
i += svcntw(); | ||
} | ||
} | ||
|
||
int | ||
fvec_madd_and_argmin_sve(size_t n, const float* a, float bf, const float* b, float* c) { | ||
size_t i = 0; | ||
svfloat32_t min_val = svdup_f32(INFINITY); | ||
svuint32_t min_idx = svdup_u32(0); | ||
svuint32_t idx_base = svindex_u32(0, 1); | ||
|
||
svfloat32_t bf_vec = svdup_f32(bf); | ||
svbool_t pg = svptrue_b32(); | ||
|
||
while (i < n) { | ||
if (n - i < svcntw()) | ||
pg = svwhilelt_b32(i, n); | ||
|
||
svuint32_t idx = svadd_u32_z(pg, idx_base, svdup_u32(i)); | ||
svfloat32_t a_vec = svld1_f32(pg, a + i); | ||
svfloat32_t b_vec = svld1_f32(pg, b + i); | ||
svfloat32_t c_vec = svmla_f32_m(pg, a_vec, b_vec, bf_vec); | ||
svst1_f32(pg, c + i, c_vec); | ||
|
||
svbool_t cmp = svcmplt(pg, c_vec, min_val); | ||
min_val = svsel_f32(cmp, c_vec, min_val); | ||
min_idx = svsel_u32(cmp, idx, min_idx); | ||
|
||
i += svcntw(); | ||
} | ||
|
||
float min_value = svminv_f32(svptrue_b32(), min_val); | ||
svbool_t pg_min = svcmpeq(svptrue_b32(), min_val, svdup_f32(min_value)); | ||
uint32_t min_index = svlastb_u32(pg_min, min_idx); | ||
|
||
return static_cast<int>(min_index); | ||
} | ||
|
||
void | ||
fvec_L2sqr_batch_4_sve(const float* x, const float* y0, const float* y1, const float* y2, const float* y3, | ||
const size_t d, float& dis0, float& dis1, float& dis2, float& dis3) { | ||
float d0 = 0; | ||
float d1 = 0; | ||
float d2 = 0; | ||
float d3 = 0; | ||
|
||
for (size_t i = 0; i < d; ++i) { | ||
const float q0 = x[i] - y0[i]; | ||
const float q1 = x[i] - y1[i]; | ||
const float q2 = x[i] - y2[i]; | ||
const float q3 = x[i] - y3[i]; | ||
d0 += q0 * q0; | ||
d1 += q1 * q1; | ||
d2 += q2 * q2; | ||
d3 += q3 * q3; | ||
} | ||
|
||
dis0 = d0; | ||
dis1 = d1; | ||
dis2 = d2; | ||
dis3 = d3; | ||
} | ||
|
||
} // namespace faiss | ||
|
||
#endif |
This file contains 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,45 @@ | ||
// Copyright (C) 2019-2023 Zilliz. All rights reserved. | ||
// | ||
// 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. | ||
|
||
#include <arm_sve.h> | ||
|
||
#include <cstdint> | ||
#include <cstdio> | ||
#if defined(__ARM_FEATURE_SVE) | ||
namespace faiss { | ||
|
||
float | ||
fvec_L2sqr_sve(const float* x, const float* y, size_t d); | ||
|
||
float | ||
fvec_L1_sve(const float* x, const float* y, size_t d); | ||
|
||
float | ||
fvec_Linf_sve(const float* x, const float* y, size_t d); | ||
|
||
float | ||
fvec_norm_L2sqr_sve(const float* x, size_t d); | ||
|
||
void | ||
fvec_madd_sve(size_t n, const float* a, float bf, const float* b, float* c); | ||
|
||
int | ||
fvec_madd_and_argmin_sve(size_t n, const float* a, float bf, const float* b, float* c); | ||
|
||
int32_t | ||
ivec_L2sqr_sve(const int8_t* x, const int8_t* y, size_t d); | ||
|
||
void | ||
fvec_L2sqr_batch_4_sve(const float* x, const float* y0, const float* y1, const float* y2, const float* y3, | ||
const size_t d, float& dis0, float& dis1, float& dis2, float& dis3); | ||
|
||
} // namespace faiss | ||
#endif |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that this
if
condition is not needed, justpg = svwhilelt_b32(i, d);
should be sufficientThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much, @alexanderguzhva , for the valuable suggestions. During development, we considered this approach as well, and our reason for going with the current approach is as follows:
Using the if condition to update pg only in the last iteration avoids unnecessary updates and reduces the dependency chain introduced by the svwhilelt instruction. This optimization minimizes stalls caused by these dependencies, allowing the processor pipeline to operate more efficiently.