Skip to content

Commit f969d7a

Browse files
mdouzefacebook-github-bot
authored andcommitted
better docs
Summary: Improved comments. Reviewed By: algoriddle Differential Revision: D50259422 fbshipit-source-id: 92ba0840468eb8724f21d8fbe406b1bc43c64706
1 parent edcf743 commit f969d7a

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

faiss/utils/distances.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,11 @@ void fvec_inner_products_by_idx(
786786
const float* xj = x + j * d;
787787
float* __restrict ipj = ip + j * ny;
788788
for (size_t i = 0; i < ny; i++) {
789-
if (idsj[i] < 0)
790-
continue;
791-
ipj[i] = fvec_inner_product(xj, y + d * idsj[i], d);
789+
if (idsj[i] < 0) {
790+
ipj[i] = -INFINITY;
791+
} else {
792+
ipj[i] = fvec_inner_product(xj, y + d * idsj[i], d);
793+
}
792794
}
793795
}
794796
}
@@ -809,9 +811,11 @@ void fvec_L2sqr_by_idx(
809811
const float* xj = x + j * d;
810812
float* __restrict disj = dis + j * ny;
811813
for (size_t i = 0; i < ny; i++) {
812-
if (idsj[i] < 0)
813-
continue;
814-
disj[i] = fvec_L2sqr(xj, y + d * idsj[i], d);
814+
if (idsj[i] < 0) {
815+
disj[i] = INFINITY;
816+
} else {
817+
disj[i] = fvec_L2sqr(xj, y + d * idsj[i], d);
818+
}
815819
}
816820
}
817821
}
@@ -828,6 +832,8 @@ void pairwise_indexed_L2sqr(
828832
for (int64_t j = 0; j < n; j++) {
829833
if (ix[j] >= 0 && iy[j] >= 0) {
830834
dis[j] = fvec_L2sqr(x + d * ix[j], y + d * iy[j], d);
835+
} else {
836+
dis[j] = INFINITY;
831837
}
832838
}
833839
}
@@ -844,6 +850,8 @@ void pairwise_indexed_inner_product(
844850
for (int64_t j = 0; j < n; j++) {
845851
if (ix[j] >= 0 && iy[j] >= 0) {
846852
dis[j] = fvec_inner_product(x + d * ix[j], y + d * iy[j], d);
853+
} else {
854+
dis[j] = -INFINITY;
847855
}
848856
}
849857
}

faiss/utils/distances.h

+28-4
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,16 @@ void fvec_sub(size_t d, const float* a, const float* b, float* c);
198198
* Compute a subset of distances
199199
***************************************************************************/
200200

201-
/* compute the inner product between x and a subset y of ny vectors,
202-
whose indices are given by idy. */
201+
/** compute the inner product between x and a subset y of ny vectors defined by
202+
* ids
203+
*
204+
* ip(i, j) = inner_product(x(i, :), y(ids(i, j), :))
205+
*
206+
* @param ip output array, size nx * ny
207+
* @param x first-term vector, size nx * d
208+
* @param y second-term vector, size (max(ids) + 1) * d
209+
* @param ids ids to sample from y, size nx * ny
210+
*/
203211
void fvec_inner_products_by_idx(
204212
float* ip,
205213
const float* x,
@@ -209,7 +217,16 @@ void fvec_inner_products_by_idx(
209217
size_t nx,
210218
size_t ny);
211219

212-
/* same but for a subset in y indexed by idsy (ny vectors in total) */
220+
/** compute the squared L2 distances between x and a subset y of ny vectors
221+
* defined by ids
222+
*
223+
* dis(i, j) = inner_product(x(i, :), y(ids(i, j), :))
224+
*
225+
* @param dis output array, size nx * ny
226+
* @param x first-term vector, size nx * d
227+
* @param y second-term vector, size (max(ids) + 1) * d
228+
* @param ids ids to sample from y, size nx * ny
229+
*/
213230
void fvec_L2sqr_by_idx(
214231
float* dis,
215232
const float* x,
@@ -236,7 +253,14 @@ void pairwise_indexed_L2sqr(
236253
const int64_t* iy,
237254
float* dis);
238255

239-
/* same for inner product */
256+
/** compute dis[j] = inner_product(x[ix[j]], y[iy[j]]) forall j=0..n-1
257+
*
258+
* @param x size (max(ix) + 1, d)
259+
* @param y size (max(iy) + 1, d)
260+
* @param ix size n
261+
* @param iy size n
262+
* @param dis size n
263+
*/
240264
void pairwise_indexed_inner_product(
241265
size_t d,
242266
size_t n,

0 commit comments

Comments
 (0)