-
Notifications
You must be signed in to change notification settings - Fork 174
/
l2_distance.cuh
495 lines (423 loc) · 17.7 KB
/
l2_distance.cuh
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright (c) 2021-2022, NVIDIA CORPORATION.
*
* 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.
*/
#pragma once
#include <raft/spatial/knn/knn.cuh>
#include <raft/cuda_utils.cuh>
#include <raft/cudart_utils.h>
#include <raft/distance/distance_type.hpp>
#include <raft/linalg/unary_op.cuh>
#include <raft/sparse/csr.hpp>
#include <raft/sparse/detail/cusparse_wrappers.h>
#include <raft/sparse/detail/utils.h>
#include <raft/sparse/distance/common.h>
#include <raft/sparse/distance/detail/ip_distance.cuh>
#include <rmm/device_uvector.hpp>
#include <nvfunctional>
#include <thrust/for_each.h>
#include <thrust/iterator/counting_iterator.h>
#include <algorithm>
namespace raft {
namespace sparse {
namespace distance {
namespace detail {
// @TODO: Move this into sparse prims (coo_norm)
template <typename value_idx, typename value_t>
__global__ void compute_row_norm_kernel(value_t* out,
const value_idx* __restrict__ coo_rows,
const value_t* __restrict__ data,
value_idx nnz)
{
value_idx i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < nnz) { atomicAdd(&out[coo_rows[i]], data[i] * data[i]); }
}
template <typename value_idx, typename value_t>
__global__ void compute_row_sum_kernel(value_t* out,
const value_idx* __restrict__ coo_rows,
const value_t* __restrict__ data,
value_idx nnz)
{
value_idx i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < nnz) { atomicAdd(&out[coo_rows[i]], data[i]); }
}
template <typename value_idx, typename value_t, typename expansion_f>
__global__ void compute_euclidean_warp_kernel(value_t* __restrict__ C,
const value_t* __restrict__ Q_sq_norms,
const value_t* __restrict__ R_sq_norms,
value_idx n_rows,
value_idx n_cols,
expansion_f expansion_func)
{
std::size_t tid = blockDim.x * blockIdx.x + threadIdx.x;
value_idx i = tid / n_cols;
value_idx j = tid % n_cols;
if (i >= n_rows || j >= n_cols) return;
value_t dot = C[(size_t)i * n_cols + j];
// e.g. Euclidean expansion func = -2.0 * dot + q_norm + r_norm
value_t val = expansion_func(dot, Q_sq_norms[i], R_sq_norms[j]);
// correct for small instabilities
C[(size_t)i * n_cols + j] = val * (fabs(val) >= 0.0001);
}
template <typename value_idx, typename value_t>
__global__ void compute_correlation_warp_kernel(value_t* __restrict__ C,
const value_t* __restrict__ Q_sq_norms,
const value_t* __restrict__ R_sq_norms,
const value_t* __restrict__ Q_norms,
const value_t* __restrict__ R_norms,
value_idx n_rows,
value_idx n_cols,
value_idx n)
{
std::size_t tid = blockDim.x * blockIdx.x + threadIdx.x;
value_idx i = tid / n_cols;
value_idx j = tid % n_cols;
if (i >= n_rows || j >= n_cols) return;
value_t dot = C[(size_t)i * n_cols + j];
value_t Q_l1 = Q_norms[i];
value_t R_l1 = R_norms[j];
value_t Q_l2 = Q_sq_norms[i];
value_t R_l2 = R_sq_norms[j];
value_t numer = n * dot - (Q_l1 * R_l1);
value_t Q_denom = n * Q_l2 - (Q_l1 * Q_l1);
value_t R_denom = n * R_l2 - (R_l1 * R_l1);
value_t val = 1 - (numer / sqrt(Q_denom * R_denom));
// correct for small instabilities
C[(size_t)i * n_cols + j] = val * (fabs(val) >= 0.0001);
}
template <typename value_idx, typename value_t, int tpb = 256, typename expansion_f>
void compute_euclidean(value_t* C,
const value_t* Q_sq_norms,
const value_t* R_sq_norms,
value_idx n_rows,
value_idx n_cols,
cudaStream_t stream,
expansion_f expansion_func)
{
int blocks = raft::ceildiv<size_t>((size_t)n_rows * n_cols, tpb);
compute_euclidean_warp_kernel<<<blocks, tpb, 0, stream>>>(
C, Q_sq_norms, R_sq_norms, n_rows, n_cols, expansion_func);
}
template <typename value_idx, typename value_t, int tpb = 256, typename expansion_f>
void compute_l2(value_t* out,
const value_idx* Q_coo_rows,
const value_t* Q_data,
value_idx Q_nnz,
const value_idx* R_coo_rows,
const value_t* R_data,
value_idx R_nnz,
value_idx m,
value_idx n,
cudaStream_t stream,
expansion_f expansion_func)
{
rmm::device_uvector<value_t> Q_sq_norms(m, stream);
rmm::device_uvector<value_t> R_sq_norms(n, stream);
RAFT_CUDA_TRY(cudaMemsetAsync(Q_sq_norms.data(), 0, Q_sq_norms.size() * sizeof(value_t)));
RAFT_CUDA_TRY(cudaMemsetAsync(R_sq_norms.data(), 0, R_sq_norms.size() * sizeof(value_t)));
compute_row_norm_kernel<<<raft::ceildiv(Q_nnz, tpb), tpb, 0, stream>>>(
Q_sq_norms.data(), Q_coo_rows, Q_data, Q_nnz);
compute_row_norm_kernel<<<raft::ceildiv(R_nnz, tpb), tpb, 0, stream>>>(
R_sq_norms.data(), R_coo_rows, R_data, R_nnz);
compute_euclidean(out, Q_sq_norms.data(), R_sq_norms.data(), m, n, stream, expansion_func);
}
template <typename value_idx, typename value_t, int tpb = 256>
void compute_correlation(value_t* C,
const value_t* Q_sq_norms,
const value_t* R_sq_norms,
const value_t* Q_norms,
const value_t* R_norms,
value_idx n_rows,
value_idx n_cols,
value_idx n,
cudaStream_t stream)
{
int blocks = raft::ceildiv<size_t>((size_t)n_rows * n_cols, tpb);
compute_correlation_warp_kernel<<<blocks, tpb, 0, stream>>>(
C, Q_sq_norms, R_sq_norms, Q_norms, R_norms, n_rows, n_cols, n);
}
template <typename value_idx, typename value_t, int tpb = 256>
void compute_corr(value_t* out,
const value_idx* Q_coo_rows,
const value_t* Q_data,
value_idx Q_nnz,
const value_idx* R_coo_rows,
const value_t* R_data,
value_idx R_nnz,
value_idx m,
value_idx n,
value_idx n_cols,
cudaStream_t stream)
{
// sum_sq for std dev
rmm::device_uvector<value_t> Q_sq_norms(m, stream);
rmm::device_uvector<value_t> R_sq_norms(n, stream);
// sum for mean
rmm::device_uvector<value_t> Q_norms(m, stream);
rmm::device_uvector<value_t> R_norms(n, stream);
RAFT_CUDA_TRY(cudaMemsetAsync(Q_sq_norms.data(), 0, Q_sq_norms.size() * sizeof(value_t)));
RAFT_CUDA_TRY(cudaMemsetAsync(R_sq_norms.data(), 0, R_sq_norms.size() * sizeof(value_t)));
RAFT_CUDA_TRY(cudaMemsetAsync(Q_norms.data(), 0, Q_norms.size() * sizeof(value_t)));
RAFT_CUDA_TRY(cudaMemsetAsync(R_norms.data(), 0, R_norms.size() * sizeof(value_t)));
compute_row_norm_kernel<<<raft::ceildiv(Q_nnz, tpb), tpb, 0, stream>>>(
Q_sq_norms.data(), Q_coo_rows, Q_data, Q_nnz);
compute_row_norm_kernel<<<raft::ceildiv(R_nnz, tpb), tpb, 0, stream>>>(
R_sq_norms.data(), R_coo_rows, R_data, R_nnz);
compute_row_sum_kernel<<<raft::ceildiv(Q_nnz, tpb), tpb, 0, stream>>>(
Q_norms.data(), Q_coo_rows, Q_data, Q_nnz);
compute_row_sum_kernel<<<raft::ceildiv(R_nnz, tpb), tpb, 0, stream>>>(
R_norms.data(), R_coo_rows, R_data, R_nnz);
compute_correlation(out,
Q_sq_norms.data(),
R_sq_norms.data(),
Q_norms.data(),
R_norms.data(),
m,
n,
n_cols,
stream);
}
/**
* L2 distance using the expanded form: sum(x_k)^2 + sum(y_k)^2 - 2 * sum(x_k * y_k)
* The expanded form is more efficient for sparse data.
*/
template <typename value_idx = int, typename value_t = float>
class l2_expanded_distances_t : public distances_t<value_t> {
public:
explicit l2_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: config_(&config), ip_dists(config)
{
}
void compute(value_t* out_dists)
{
ip_dists.compute(out_dists);
value_idx* b_indices = ip_dists.b_rows_coo();
value_t* b_data = ip_dists.b_data_coo();
rmm::device_uvector<value_idx> search_coo_rows(config_->a_nnz, config_->handle.get_stream());
raft::sparse::convert::csr_to_coo(config_->a_indptr,
config_->a_nrows,
search_coo_rows.data(),
config_->a_nnz,
config_->handle.get_stream());
compute_l2(out_dists,
search_coo_rows.data(),
config_->a_data,
config_->a_nnz,
b_indices,
b_data,
config_->b_nnz,
config_->a_nrows,
config_->b_nrows,
config_->handle.get_stream(),
[] __device__ __host__(value_t dot, value_t q_norm, value_t r_norm) {
return -2 * dot + q_norm + r_norm;
});
}
~l2_expanded_distances_t() = default;
protected:
const distances_config_t<value_idx, value_t>* config_;
ip_distances_t<value_idx, value_t> ip_dists;
};
/**
* L2 sqrt distance performing the sqrt operation after the distance computation
* The expanded form is more efficient for sparse data.
*/
template <typename value_idx = int, typename value_t = float>
class l2_sqrt_expanded_distances_t : public l2_expanded_distances_t<value_idx, value_t> {
public:
explicit l2_sqrt_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: l2_expanded_distances_t<value_idx, value_t>(config)
{
}
void compute(value_t* out_dists) override
{
l2_expanded_distances_t<value_idx, value_t>::compute(out_dists);
// Sqrt Post-processing
raft::linalg::unaryOp<value_t>(
out_dists,
out_dists,
this->config_->a_nrows * this->config_->b_nrows,
[] __device__(value_t input) {
int neg = input < 0 ? -1 : 1;
return sqrt(abs(input) * neg);
},
this->config_->handle.get_stream());
}
~l2_sqrt_expanded_distances_t() = default;
};
template <typename value_idx, typename value_t>
class correlation_expanded_distances_t : public distances_t<value_t> {
public:
explicit correlation_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: config_(&config), ip_dists(config)
{
}
void compute(value_t* out_dists)
{
ip_dists.compute(out_dists);
value_idx* b_indices = ip_dists.b_rows_coo();
value_t* b_data = ip_dists.b_data_coo();
rmm::device_uvector<value_idx> search_coo_rows(config_->a_nnz, config_->handle.get_stream());
raft::sparse::convert::csr_to_coo(config_->a_indptr,
config_->a_nrows,
search_coo_rows.data(),
config_->a_nnz,
config_->handle.get_stream());
compute_corr(out_dists,
search_coo_rows.data(),
config_->a_data,
config_->a_nnz,
b_indices,
b_data,
config_->b_nnz,
config_->a_nrows,
config_->b_nrows,
config_->b_ncols,
config_->handle.get_stream());
}
~correlation_expanded_distances_t() = default;
protected:
const distances_config_t<value_idx, value_t>* config_;
ip_distances_t<value_idx, value_t> ip_dists;
};
/**
* Cosine distance using the expanded form: 1 - ( sum(x_k * y_k) / (sqrt(sum(x_k)^2) *
* sqrt(sum(y_k)^2))) The expanded form is more efficient for sparse data.
*/
template <typename value_idx = int, typename value_t = float>
class cosine_expanded_distances_t : public distances_t<value_t> {
public:
explicit cosine_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: config_(&config), workspace(0, config.handle.get_stream()), ip_dists(config)
{
}
void compute(value_t* out_dists)
{
ip_dists.compute(out_dists);
value_idx* b_indices = ip_dists.b_rows_coo();
value_t* b_data = ip_dists.b_data_coo();
rmm::device_uvector<value_idx> search_coo_rows(config_->a_nnz, config_->handle.get_stream());
raft::sparse::convert::csr_to_coo(config_->a_indptr,
config_->a_nrows,
search_coo_rows.data(),
config_->a_nnz,
config_->handle.get_stream());
compute_l2(out_dists,
search_coo_rows.data(),
config_->a_data,
config_->a_nnz,
b_indices,
b_data,
config_->b_nnz,
config_->a_nrows,
config_->b_nrows,
config_->handle.get_stream(),
[] __device__ __host__(value_t dot, value_t q_norm, value_t r_norm) {
value_t norms = sqrt(q_norm) * sqrt(r_norm);
// deal with potential for 0 in denominator by forcing 0/1 instead
value_t cos = ((norms != 0) * dot) / ((norms == 0) + norms);
// flip the similarity when both rows are 0
bool both_empty = (q_norm == 0) && (r_norm == 0);
return 1 - ((!both_empty * cos) + both_empty);
});
}
~cosine_expanded_distances_t() = default;
private:
const distances_config_t<value_idx, value_t>* config_;
rmm::device_uvector<char> workspace;
ip_distances_t<value_idx, value_t> ip_dists;
};
/**
* Hellinger distance using the expanded form: sqrt(1 - sum(sqrt(x_k) * sqrt(y_k)))
* The expanded form is more efficient for sparse data.
*
* This distance computation modifies A and B by computing a sqrt
* and then performing a `pow(x, 2)` to convert it back. Because of this,
* it is possible that the values in A and B might differ slightly
* after this is invoked.
*/
template <typename value_idx = int, typename value_t = float>
class hellinger_expanded_distances_t : public distances_t<value_t> {
public:
explicit hellinger_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: config_(&config), workspace(0, config.handle.get_stream())
{
}
void compute(value_t* out_dists)
{
rmm::device_uvector<value_idx> coo_rows(std::max(config_->b_nnz, config_->a_nnz),
config_->handle.get_stream());
raft::sparse::convert::csr_to_coo(config_->b_indptr,
config_->b_nrows,
coo_rows.data(),
config_->b_nnz,
config_->handle.get_stream());
balanced_coo_pairwise_generalized_spmv<value_idx, value_t>(
out_dists,
*config_,
coo_rows.data(),
[] __device__(value_t a, value_t b) { return sqrt(a) * sqrt(b); },
Sum(),
AtomicAdd());
raft::linalg::unaryOp<value_t>(
out_dists,
out_dists,
config_->a_nrows * config_->b_nrows,
[=] __device__(value_t input) {
// Adjust to replace NaN in sqrt with 0 if input to sqrt is negative
bool rectifier = (1 - input) > 0;
return sqrt(rectifier * (1 - input));
},
config_->handle.get_stream());
}
~hellinger_expanded_distances_t() = default;
private:
const distances_config_t<value_idx, value_t>* config_;
rmm::device_uvector<char> workspace;
};
template <typename value_idx = int, typename value_t = float>
class russelrao_expanded_distances_t : public distances_t<value_t> {
public:
explicit russelrao_expanded_distances_t(const distances_config_t<value_idx, value_t>& config)
: config_(&config), workspace(0, config.handle.get_stream()), ip_dists(config)
{
}
void compute(value_t* out_dists)
{
ip_dists.compute(out_dists);
value_t n_cols = config_->a_ncols;
value_t n_cols_inv = 1.0 / n_cols;
raft::linalg::unaryOp<value_t>(
out_dists,
out_dists,
config_->a_nrows * config_->b_nrows,
[=] __device__(value_t input) { return (n_cols - input) * n_cols_inv; },
config_->handle.get_stream());
auto exec_policy = rmm::exec_policy(config_->handle.get_stream());
auto diags = thrust::counting_iterator<value_idx>(0);
value_idx b_nrows = config_->b_nrows;
thrust::for_each(exec_policy, diags, diags + config_->a_nrows, [=] __device__(value_idx input) {
out_dists[input * b_nrows + input] = 0.0;
});
}
~russelrao_expanded_distances_t() = default;
private:
const distances_config_t<value_idx, value_t>* config_;
rmm::device_uvector<char> workspace;
ip_distances_t<value_idx, value_t> ip_dists;
};
}; // END namespace detail
}; // END namespace distance
}; // END namespace sparse
}; // END namespace raft