Skip to content

Commit

Permalink
add more pragma imprecise to distances_ref.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderguzhva committed Sep 20, 2023
1 parent 0fe5ba5 commit 4d7d7e2
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/simd/distances_ref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@

namespace faiss {

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
fvec_L2sqr_ref(const float* x, const float* y, size_t d) {
size_t i;
float res = 0;
FAISS_PRAGMA_IMPRECISE_LOOP
for (i = 0; i < d; i++) {
const float tmp = x[i] - y[i];
res += tmp * tmp;
}
return res;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
fvec_L1_ref(const float* x, const float* y, size_t d) {
size_t i;
float res = 0;
FAISS_PRAGMA_IMPRECISE_LOOP
for (i = 0; i < d; i++) {
const float tmp = x[i] - y[i];
res += std::fabs(tmp);
}
return res;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
fvec_Linf_ref(const float* x, const float* y, size_t d) {
size_t i;
Expand All @@ -46,41 +53,56 @@ fvec_Linf_ref(const float* x, const float* y, size_t d) {
}
return res;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
fvec_inner_product_ref(const float* x, const float* y, size_t d) {
size_t i;
float res = 0;
for (i = 0; i < d; i++) res += x[i] * y[i];
for (i = 0; i < d; i++) {
res += x[i] * y[i];
}
return res;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
float
fvec_norm_L2sqr_ref(const float* x, size_t d) {
size_t i;
double res = 0;
for (i = 0; i < d; i++) res += x[i] * x[i];
FAISS_PRAGMA_IMPRECISE_LOOP
for (i = 0; i < d; i++) {
res += x[i] * x[i];
}
return res;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
void
fvec_L2sqr_ny_ref(float* dis, const float* x, const float* y, size_t d, size_t ny) {
for (size_t i = 0; i < ny; i++) {
dis[i] = fvec_L2sqr_ref(x, y, d);
y += d;
}
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
void
fvec_inner_products_ny_ref(float* ip, const float* x, const float* y, size_t d, size_t ny) {
for (size_t i = 0; i < ny; i++) {
ip[i] = fvec_inner_product_ref(x, y, d);
y += d;
}
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

/// compute ny square L2 distance between x and a set of transposed contiguous
/// y vectors. squared lengths of y should be provided as well
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
void fvec_L2sqr_ny_transposed_ref(
float* __restrict dis,
const float* __restrict x,
Expand All @@ -90,23 +112,27 @@ void fvec_L2sqr_ny_transposed_ref(
size_t d_offset,
size_t ny) {
float x_sqlen = 0;
FAISS_PRAGMA_IMPRECISE_LOOP
for (size_t j = 0; j < d; j++) {
x_sqlen += x[j] * x[j];
}

for (size_t i = 0; i < ny; i++) {
float dp = 0;
FAISS_PRAGMA_IMPRECISE_LOOP
for (size_t j = 0; j < d; j++) {
dp += x[j] * y[i + j * d_offset];
}

dis[i] = x_sqlen + y_sqlen[i] - 2 * dp;
}
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

/// compute ny square L2 distance between x and a set of contiguous y vectors
/// and return the index of the nearest vector.
/// return 0 if ny == 0.
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
size_t fvec_L2sqr_ny_nearest_ref(
float* __restrict distances_tmp_buffer,
const float* __restrict x,
Expand All @@ -127,11 +153,13 @@ size_t fvec_L2sqr_ny_nearest_ref(

return nearest_idx;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

/// compute ny square L2 distance between x and a set of transposed contiguous
/// y vectors and return the index of the nearest vector.
/// squared lengths of y should be provided as well
/// return 0 if ny == 0.
FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
size_t fvec_L2sqr_ny_nearest_y_transposed_ref(
float* __restrict distances_tmp_buffer,
const float* __restrict x,
Expand All @@ -155,12 +183,19 @@ size_t fvec_L2sqr_ny_nearest_y_transposed_ref(

return nearest_idx;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
void
fvec_madd_ref(size_t n, const float* a, float bf, const float* b, float* c) {
for (size_t i = 0; i < n; i++) c[i] = a[i] + bf * b[i];
FAISS_PRAGMA_IMPRECISE_LOOP
for (size_t i = 0; i < n; i++) {
c[i] = a[i] + bf * b[i];
}
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
int
fvec_madd_and_argmin_ref(size_t n, const float* a, float bf, const float* b, float* c) {
float vmin = 1e20;
Expand All @@ -175,6 +210,7 @@ fvec_madd_and_argmin_ref(size_t n, const float* a, float bf, const float* b, flo
}
return imin;
}
FAISS_PRAGMA_IMPRECISE_FUNCTION_END

FAISS_PRAGMA_IMPRECISE_FUNCTION_BEGIN
void
Expand Down

0 comments on commit 4d7d7e2

Please sign in to comment.