forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistances.cpp
336 lines (261 loc) · 8.08 KB
/
distances.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// -*- c++ -*-
#include "distances.h"
#include <cmath>
#include <omp.h>
#include "utils.h"
#include "FaissAssert.h"
#include "AuxIndexStructures.h"
namespace faiss {
/***************************************************************************
* Distance functions (other than L2 and IP)
***************************************************************************/
struct VectorDistanceL2 {
size_t d;
float operator () (const float *x, const float *y) const {
return fvec_L2sqr (x, y, d);
}
};
struct VectorDistanceL1 {
size_t d;
float operator () (const float *x, const float *y) const {
return fvec_L1 (x, y, d);
}
};
struct VectorDistanceLinf {
size_t d;
float operator () (const float *x, const float *y) const {
return fvec_Linf (x, y, d);
/*
float vmax = 0;
for (size_t i = 0; i < d; i++) {
float diff = fabs (x[i] - y[i]);
if (diff > vmax) vmax = diff;
}
return vmax;*/
}
};
struct VectorDistanceLp {
size_t d;
const float p;
float operator () (const float *x, const float *y) const {
float accu = 0;
for (size_t i = 0; i < d; i++) {
float diff = fabs (x[i] - y[i]);
accu += powf (diff, p);
}
return accu;
}
};
struct VectorDistanceCanberra {
size_t d;
float operator () (const float *x, const float *y) const {
float accu = 0;
for (size_t i = 0; i < d; i++) {
float xi = x[i], yi = y[i];
accu += fabs (xi - yi) / (fabs(xi) + fabs(yi));
}
return accu;
}
};
struct VectorDistanceBrayCurtis {
size_t d;
float operator () (const float *x, const float *y) const {
float accu_num = 0, accu_den = 0;
for (size_t i = 0; i < d; i++) {
float xi = x[i], yi = y[i];
accu_num += fabs (xi - yi);
accu_den += fabs (xi + yi);
}
return accu_num / accu_den;
}
};
struct VectorDistanceJensenShannon {
size_t d;
float operator () (const float *x, const float *y) const {
float accu = 0;
for (size_t i = 0; i < d; i++) {
float xi = x[i], yi = y[i];
float mi = 0.5 * (xi + yi);
float kl1 = - xi * log(mi / xi);
float kl2 = - yi * log(mi / yi);
accu += kl1 + kl2;
}
return 0.5 * accu;
}
};
namespace {
template<class VD>
void pairwise_extra_distances_template (
VD vd,
int64_t nq, const float *xq,
int64_t nb, const float *xb,
float *dis,
int64_t ldq, int64_t ldb, int64_t ldd)
{
#pragma omp parallel for if(nq > 10)
for (int64_t i = 0; i < nq; i++) {
const float *xqi = xq + i * ldq;
const float *xbj = xb;
float *disi = dis + ldd * i;
for (int64_t j = 0; j < nb; j++) {
disi[j] = vd (xqi, xbj);
xbj += ldb;
}
}
}
template<class VD>
void knn_extra_metrics_template (
VD vd,
const float * x,
const float * y,
size_t nx, size_t ny,
float_maxheap_array_t * res)
{
size_t k = res->k;
size_t d = vd.d;
size_t check_period = InterruptCallback::get_period_hint (ny * d);
check_period *= omp_get_max_threads();
for (size_t i0 = 0; i0 < nx; i0 += check_period) {
size_t i1 = std::min(i0 + check_period, nx);
#pragma omp parallel for
for (size_t i = i0; i < i1; i++) {
const float * x_i = x + i * d;
const float * y_j = y;
size_t j;
float * simi = res->get_val(i);
int64_t * idxi = res->get_ids (i);
maxheap_heapify (k, simi, idxi);
for (j = 0; j < ny; j++) {
float disij = vd (x_i, y_j);
if (disij < simi[0]) {
maxheap_pop (k, simi, idxi);
maxheap_push (k, simi, idxi, disij, j);
}
y_j += d;
}
maxheap_reorder (k, simi, idxi);
}
InterruptCallback::check ();
}
}
template<class VD>
struct ExtraDistanceComputer : DistanceComputer {
VD vd;
Index::idx_t nb;
const float *q;
const float *b;
float operator () (idx_t i) override {
return vd (q, b + i * vd.d);
}
float symmetric_dis(idx_t i, idx_t j) override {
return vd (b + j * vd.d, b + i * vd.d);
}
ExtraDistanceComputer(const VD & vd, const float *xb,
size_t nb, const float *q = nullptr)
: vd(vd), nb(nb), q(q), b(xb) {}
void set_query(const float *x) override {
q = x;
}
};
} // anonymous namespace
void pairwise_extra_distances (
int64_t d,
int64_t nq, const float *xq,
int64_t nb, const float *xb,
MetricType mt, float metric_arg,
float *dis,
int64_t ldq, int64_t ldb, int64_t ldd)
{
if (nq == 0 || nb == 0) return;
if (ldq == -1) ldq = d;
if (ldb == -1) ldb = d;
if (ldd == -1) ldd = nb;
switch(mt) {
#define HANDLE_VAR(kw) \
case METRIC_ ## kw: { \
VectorDistance ## kw vd({(size_t)d}); \
pairwise_extra_distances_template (vd, nq, xq, nb, xb, \
dis, ldq, ldb, ldd); \
break; \
}
HANDLE_VAR(L2);
HANDLE_VAR(L1);
HANDLE_VAR(Linf);
HANDLE_VAR(Canberra);
HANDLE_VAR(BrayCurtis);
HANDLE_VAR(JensenShannon);
#undef HANDLE_VAR
case METRIC_Lp: {
VectorDistanceLp vd({(size_t)d, metric_arg});
pairwise_extra_distances_template (vd, nq, xq, nb, xb,
dis, ldq, ldb, ldd);
break;
}
default:
FAISS_THROW_MSG ("metric type not implemented");
}
}
void knn_extra_metrics (
const float * x,
const float * y,
size_t d, size_t nx, size_t ny,
MetricType mt, float metric_arg,
float_maxheap_array_t * res)
{
switch(mt) {
#define HANDLE_VAR(kw) \
case METRIC_ ## kw: { \
VectorDistance ## kw vd({(size_t)d}); \
knn_extra_metrics_template (vd, x, y, nx, ny, res); \
break; \
}
HANDLE_VAR(L2);
HANDLE_VAR(L1);
HANDLE_VAR(Linf);
HANDLE_VAR(Canberra);
HANDLE_VAR(BrayCurtis);
HANDLE_VAR(JensenShannon);
#undef HANDLE_VAR
case METRIC_Lp: {
VectorDistanceLp vd({(size_t)d, metric_arg});
knn_extra_metrics_template (vd, x, y, nx, ny, res);
break;
}
default:
FAISS_THROW_MSG ("metric type not implemented");
}
}
DistanceComputer *get_extra_distance_computer (
size_t d,
MetricType mt, float metric_arg,
size_t nb, const float *xb)
{
switch(mt) {
#define HANDLE_VAR(kw) \
case METRIC_ ## kw: { \
VectorDistance ## kw vd({(size_t)d}); \
return new ExtraDistanceComputer<VectorDistance ## kw>(vd, xb, nb); \
}
HANDLE_VAR(L2);
HANDLE_VAR(L1);
HANDLE_VAR(Linf);
HANDLE_VAR(Canberra);
HANDLE_VAR(BrayCurtis);
HANDLE_VAR(JensenShannon);
#undef HANDLE_VAR
case METRIC_Lp: {
VectorDistanceLp vd({(size_t)d, metric_arg});
return new ExtraDistanceComputer<VectorDistanceLp> (vd, xb, nb);
break;
}
default:
FAISS_THROW_MSG ("metric type not implemented");
}
}
} // namespace faiss