-
Notifications
You must be signed in to change notification settings - Fork 9
/
weight.cuh
541 lines (469 loc) · 21.2 KB
/
weight.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#pragma once
#include <iostream>
#include "vendor/faiss/gpu/GpuIndexFlat.h"
#include "vendor/faiss/gpu/GpuIndexIVF.h"
#include "testing.hpp"
const float float_max = std::numeric_limits<float>::max();
__global__ void kernel_calc_W(int N, int K, float perplexity, const float *distances,
int distances_align, // K * distance_pitch
float *W_output, int W_align // K * distance_pitch
) {
const float logp = log(perplexity);
unsigned tid = threadIdx.x;
unsigned int idx = blockIdx.x * blockDim.x + tid;
for (; idx < N; idx += gridDim.x * blockDim.x) { // iter point
float beta = 1e-5;
float minf = -1;
float maxf = 1;
float sumf;
for (int iter = 0; iter < 200; iter++) {
float H = 0;
sumf = 0;
for (int i = 0; i < K; i++) {
float d = distances[i * distances_align + idx];
float t = exp(-beta * d);
sumf += t;
H += beta * (d * t);
}
H = (H / sumf) + log(sumf);
float Hdiff = H - logp;
if (fabs(Hdiff) < 1e-5) break;
if (Hdiff > 0) {
minf = beta;
if (maxf < 0)
beta *= 2;
else
beta = (beta + maxf) / 2;
} else {
maxf = beta;
if (minf < 0)
beta /= 2;
else
beta = (minf + beta) / 2;
}
if (beta > float_max) beta = float_max;
}
for (int i = 0; i < K; i++) {
W_output[i * W_align + idx] = exp(-beta * distances[i * distances_align + idx]) / sumf;
}
}
}
// @param X: data
// @param Centers:
// @param C: number of centers
// @param K: number of neighbors
void calc_gauss_perplexity_and_build_graph_gpu(const float *points, unsigned points_num, unsigned dim,
const float *Centers, const faiss::gpu::GpuIndexIVF *point_index,
const faiss::gpu::GpuIndexFlat *center_index, float perplexity,
int K_center, // number of neighborhood center
int K_point, // number of neighborhood points
qvis::MatrixPitched<float> & W_point,
qvis::MatrixPitched<float> & W_center,
qvis::Graph<unsigned, true> &point_graph,
qvis::Graph<unsigned, true> ¢er_graph,
size_t memory_limit = 2lu << 30) {
if (K_center == 0 && K_point == 0) {
return;
}
qvis::test::time_point last_timepoint = qvis::test::now();
// allocate memory for one batch of points
// GPU memory consumption:
// (K_point + K_center) * batch_size distances
// (K_point + K_center) * batch_size W
const size_t batch_line_memory = (K_point + K_center) * 2 * sizeof(float);
// so pitch should be batch_size * sizeof(float)
unsigned batch_size = (memory_limit / batch_line_memory) & (~((1 << 6) - 1));
if (batch_size > points_num) {
batch_size = ((points_num + 63) / 64) * 64;
}
printf("calc_gauss_perplexity_and_build_graph_gpu batch_size = %d\n", batch_size);
// following 6 array is batch sized
float *point_distances;
long * point_indicates; // FIXME: we perfer int
float *center_distances;
int * center_indicates;
float *distances_host, *distances_device;
float *W_device;
if (K_point > 0) {
HANDLE_ERROR(cudaMallocHost((void **)&point_indicates, batch_size * (K_point + 1) * sizeof(long)));
HANDLE_ERROR(cudaMallocHost((void **)&point_distances, batch_size * (K_point + 1) * sizeof(float)));
}
if (K_center > 0) {
HANDLE_ERROR(cudaMallocHost((void **)¢er_indicates, batch_size * K_center * sizeof(int)));
HANDLE_ERROR(cudaMallocHost((void **)¢er_distances, batch_size * K_center * sizeof(float)));
}
HANDLE_ERROR(cudaMallocHost((void **)&distances_host, batch_size * (K_point + K_center) * sizeof(float)));
HANDLE_ERROR(cudaMalloc((void **)&distances_device, batch_size * (K_point + K_center) * sizeof(float)));
HANDLE_ERROR(cudaMalloc((void **)&W_device, batch_size * (K_point + K_center) * sizeof(float)));
// use our patch
const faiss::gpu::qvis_patch::GpuIndexFlat *center_index_ =
(faiss::gpu::qvis_patch::GpuIndexFlat *)center_index; // qvis patch
for (unsigned batch = 0; batch < points_num; batch += batch_size) { // loop for batch
int this_batch_size = std::min(points_num, batch + batch_size) - batch;
last_timepoint = qvis::test::now();
// build point graph
if (K_point > 0) {
point_index->search(this_batch_size, points + batch * dim, K_point + 1, point_distances, point_indicates);
HANDLE_ERROR(
cudaDeviceSynchronize()); // FIXME: 2018-4-20, if we don't sync here, next search call may crash
}
printf("%s %7.4lf ms\n", "point_index->search",
qvis::test::getmilliseconds(last_timepoint, qvis::test::now()));
// build center graph
if (K_center > 0) {
center_index_->search_int_labels(this_batch_size, points + batch * dim, K_center, center_distances,
center_indicates);
}
HANDLE_ERROR(cudaDeviceSynchronize()); // FIXME: I don't know why we should wait there, but if not, we may get
// some zero in result
printf("%s %7.4lf ms\n", "center_index_->search_int_labels",
qvis::test::getmilliseconds(last_timepoint, qvis::test::now()));
// for each point in batch
#pragma omp parallel for
for (int p = 0; p < this_batch_size; p++) { // suffix '_p' means point
float *point_distances_p = point_distances + p * (K_point + 1);
long * point_indicates_p = point_indicates + p * (K_point + 1);
float *center_distances_p = center_distances + p * K_center;
int * center_indicates_p = center_indicates + p * K_center;
// remove the same point from point_graph
{
int offset = 0;
for (int i = 0; i < K_point; i++) {
if (point_indicates_p[offset] == batch + p) {
offset++; // skip the same point
}
point_distances_p[i] = point_distances_p[offset];
point_indicates_p[i] = point_indicates_p[offset];
offset++;
}
}
// fill distances
for (int i = 0; i < K_point; i++) {
distances_host[i * batch_size + p] = point_distances_p[i];
}
for (int i = 0; i < K_center; i++) {
distances_host[(i + K_point) * batch_size + p] = center_distances_p[i];
}
// output graph
for (int i = 0; i < K_point; i++) {
point_graph[i][batch + p] = point_indicates_p[i];
}
for (int i = 0; i < K_center; i++) {
center_graph[i][batch + p] = center_indicates_p[i];
}
} // end loop point in batch
printf("%s %7.4lf ms\n", "finish dedupe", qvis::test::getmilliseconds(last_timepoint, qvis::test::now()));
// run binary search kernel
HANDLE_ERROR(cudaMemcpy(distances_device, distances_host, sizeof(float) * batch_size * (K_point + K_center),
cudaMemcpyHostToDevice));
const int ThreadPerBlock = 128;
int num_block = min((batch_size + ThreadPerBlock - 1) / ThreadPerBlock, 50);
last_timepoint = qvis::test::now();
kernel_calc_W<<<num_block, ThreadPerBlock>>>(this_batch_size, K_point + K_center,
perplexity, // Note, N = this_batch_size
distances_device, batch_size, W_device, batch_size);
HANDLE_ERROR(cudaDeviceSynchronize());
printf("%s %7.4lf ms\n", "kernel_calc_W", qvis::test::getmilliseconds(last_timepoint, qvis::test::now()));
// Copy back W
for (int i = 0; i < K_point; i++) {
HANDLE_ERROR(cudaMemcpy(W_point.data() + i * W_point.pitch / sizeof(float) + batch,
W_device + i * batch_size, sizeof(float) * this_batch_size,
cudaMemcpyDeviceToHost));
}
for (int i = 0; i < K_center; i++) {
HANDLE_ERROR(cudaMemcpy(W_center.data() + i * W_center.pitch / sizeof(float) + batch,
W_device + (i + K_point) * batch_size, sizeof(float) * this_batch_size,
cudaMemcpyDeviceToHost));
}
} // end loop of batch
printf("finish perplexity build\n");
// clear buffer for batch
if (K_point > 0) {
HANDLE_ERROR(cudaFreeHost(point_indicates));
HANDLE_ERROR(cudaFreeHost(point_distances));
}
if (K_center > 0) {
HANDLE_ERROR(cudaFreeHost(center_indicates));
HANDLE_ERROR(cudaFreeHost(center_distances));
}
HANDLE_ERROR(cudaFreeHost(distances_host));
HANDLE_ERROR(cudaFree(distances_device));
HANDLE_ERROR(cudaFree(W_device));
// calc sumf
float *sumf_degree = new float[K_point + K_center]; // sumf for per degree
#pragma omp parallel for
for (int i = 0; i < K_point + K_center; i++) {
sumf_degree[i] = 0.0;
if (i < K_point) { // point
for (unsigned n = 0; n < points_num; n++) {
sumf_degree[i] += W_point[i][n];
}
} else { // center
for (unsigned n = 0; n < points_num; n++) {
sumf_degree[i] += W_center[i - K_point][n];
}
}
}
float sumf = 0;
for (int i = 0; i < K_point + K_center; i++) {
sumf += sumf_degree[i];
// printf("sumf_degree[%d] = %f\n", i, sumf_degree[i]);
}
delete[] sumf_degree;
printf("sumf = %f\n", sumf);
// symmetric
// FIXME: scan neighbor of point in degree first graph is extreme cache unfriendly, maybe transpose is need
printf("building symmetric weight\n");
#pragma omp parallel for
for (unsigned n = 0; n < points_num; n++) { // loop for points
for (int i = 0; i < K_point; i++) { // loop for neighbors
unsigned id = point_graph[i][n];
if (id == 0xffffffff) { // not enough point in IVF, we will get this
break;
}
int found_id = -1;
for (int j = 0; j < K_point; j++) {
if (point_graph[j][id] == n) {
found_id = j;
break;
}
}
if (found_id > 0) {
if (id > n) {
float avg = (W_point[i][n] + W_point[found_id][id]) / 2;
W_point[i][n] = avg;
W_point[found_id][id] = avg;
}
} else {
W_point[i][n] /= 2;
}
}
for (int i = 0; i < K_center; i++) {
W_center[i][n] /= 2;
}
}
printf("finish symmetric weight build\n");
// divide each weight by the sum of weights
// #pragma omp parallel for
// for (int i = 0; i < K_point + K_center; i++) {
// if (i < K_point) { // point
// for (unsigned n = 0; n < points_num; n++) {
// W_point[i][n] /= sumf;
// }
// } else { // center
// for (unsigned n = 0; n < points_num; n++) {
// W_center[i - K_point][n] /= sumf;
// }
// }
// }
}
// @param X: data
// @param Centers:
// @param C: number of centers
// @param K: number of neighbors
void calc_gauss_perplexity_and_build_graph(const float *points, unsigned points_num, unsigned dim,
const float *Centers, const faiss::gpu::GpuIndexIVF *point_index,
const faiss::gpu::GpuIndexFlat *center_index, float perplexity,
int K_center, // number of neighborhood center
int K_point, // number of neighborhood points
qvis::MatrixPitched<float> &W_point, qvis::MatrixPitched<float> &W_center,
qvis::Graph<unsigned, true> &point_graph,
qvis::Graph<unsigned, true> ¢er_graph, size_t memory_limit = 1 << 30) {
if (K_center == 0 && K_point == 0) {
return;
}
// allocate memory for one batch of points
const size_t batch_line_memory =
std::max((K_point + 1) * (sizeof(float) + sizeof(long)), K_center * (sizeof(float) + sizeof(int)));
int batch_size = memory_limit / batch_line_memory;
// following 6 array is batch sized
float *point_distances;
long * point_indicates; // FIXME: we perfer int
float *center_distances;
int * center_indicates;
float *point_w = new float[batch_size * K_point];
float *center_w = new float[batch_size * K_center];
HANDLE_ERROR(cudaMallocHost((void **)&point_indicates, batch_size * (K_point + 1) * sizeof(long)));
HANDLE_ERROR(cudaMallocHost((void **)&point_distances, batch_size * (K_point + 1) * sizeof(float)));
HANDLE_ERROR(cudaMallocHost((void **)¢er_indicates, batch_size * K_center * sizeof(int)));
HANDLE_ERROR(cudaMallocHost((void **)¢er_distances, batch_size * K_center * sizeof(float)));
// use our patch
const faiss::gpu::qvis_patch::GpuIndexFlat *center_index_ =
(faiss::gpu::qvis_patch::GpuIndexFlat *)center_index; // qvis patch
for (unsigned batch = 0; batch < points_num; batch += batch_size) { // loop for batch
int this_batch_size = std::min(points_num, batch + batch_size) - batch;
// build point graph
if (K_point > 0) {
point_index->search(this_batch_size, points + batch * dim, K_point + 1, point_distances, point_indicates);
HANDLE_ERROR(
cudaDeviceSynchronize()); // FIXME: 2018-4-20, if we don't sync here, next search call may crash
}
// build center graph
if (K_center > 0) {
center_index_->search_int_labels(this_batch_size, points + batch * dim, K_center, center_distances,
center_indicates);
HANDLE_ERROR(cudaDeviceSynchronize()); // FIXME: I don't know why we should wait there, but if not, we may
// get some zero in result
}
// for each point in batch
#pragma omp parallel for
for (int p = 0; p < this_batch_size; p++) { // suffix '_p' means point
float *point_distances_p = point_distances + p * (K_point + 1);
long * point_indicates_p = point_indicates + p * (K_point + 1);
float *center_distances_p = center_distances + p * K_center;
int * center_indicates_p = center_indicates + p * K_center;
float *w_point_p = point_w + p * K_point;
float *w_center_p = center_w + p * K_center;
// remove the same point from point_graph
{
int offset = 0;
for (int i = 0; i < K_point; i++) {
if (point_indicates_p[offset] == batch + p) {
offset++; // skip the same point
}
point_distances_p[i] = point_distances_p[offset];
point_indicates_p[i] = point_indicates_p[offset];
offset++;
}
}
// distance form faiss is not squared
for (int i = 0; i < K_point; i++) {
// point_distances_p[i] *= point_distances_p[i];
}
for (int i = 0; i < K_center; i++) {
// center_distances_p[i] *= center_distances_p[i];
}
// calculate W
float beta = 1e-5;
float minf = -1;
float maxf = 1;
unsigned iter = 0;
float sumf = 0;
// do binary search
while (iter++ < 200) {
// calculate Shannon entropy h
sumf = 0;
for (int i = 0; i < K_point; i++) {
w_point_p[i] = exp(-beta * point_distances_p[i]);
sumf += w_point_p[i];
}
for (int i = 0; i < K_center; i++) {
w_center_p[i] = exp(-beta * center_distances_p[i]);
sumf += w_center_p[i];
}
float H = 0;
for (int i = 0; i < K_point; i++) {
H += beta * (point_distances_p[i] * w_point_p[i]);
}
for (int i = 0; i < K_center; i++) {
H += beta * (center_distances_p[i] * w_center_p[i]);
}
H = (H / sumf) + log(sumf);
// update beta
float Hdiff = H - log(perplexity);
if (fabs(Hdiff) < 1e-5) break;
if (Hdiff > 0) {
minf = beta;
if (maxf < 0)
beta *= 2;
else
beta = (beta + maxf) / 2;
} else {
maxf = beta;
if (minf < 0)
beta /= 2;
else
beta = (minf + beta) / 2;
}
if (beta > std::numeric_limits<float>::max()) beta = std::numeric_limits<float>::max();
}
// FIXME: we need a cache friendly implementation
// output weight
for (int i = 0; i < K_point; i++) {
W_point[i][batch + p] = w_point_p[i] / sumf;
}
for (int i = 0; i < K_center; i++) {
W_center[i][batch + p] = w_center_p[i] / sumf;
}
// output graph
for (int i = 0; i < K_point; i++) {
point_graph[i][batch + p] = point_indicates_p[i];
}
for (int i = 0; i < K_center; i++) {
center_graph[i][batch + p] = center_indicates_p[i];
}
} // end loop point in batch
} // end loop of batch
// clear buffer for batch
if (K_point > 0) {
HANDLE_ERROR(cudaFreeHost(point_indicates));
HANDLE_ERROR(cudaFreeHost(point_distances));
}
if (K_center > 0) {
HANDLE_ERROR(cudaFreeHost(center_indicates));
HANDLE_ERROR(cudaFreeHost(center_distances));
}
// symmetric
// FIXME: scan neighbor of point in degree first graph is extreme cache unfriendly, maybe transpose is need
#pragma omp parallel for
for (unsigned n = 0; n < points_num; n++) { // loop for points
for (int i = 0; i < K_point; i++) { // loop for neighbors
unsigned id = point_graph[i][n];
if (id == 0xffffffff) { // not enough point in IVF, we will get this
break;
}
int found_id = -1;
for (int j = 0; j < K_point; j++) {
if (point_graph[j][id] == n) {
found_id = j;
break;
}
}
if (found_id > 0) {
if (id > n) {
float avg = (W_point[i][n] + W_point[found_id][id]) / 2;
W_point[i][n] = avg;
W_point[found_id][id] = avg;
}
} else {
W_point[i][n] /= 2;
}
}
for (int i = 0; i < K_center; i++) {
W_center[i][n] /= 2;
}
}
printf("finish symmetric perplexity build\n");
// divide each weight by the sum of weights
float *sumf_degree = new float[K_point + K_center]; // sumf for per degree
#pragma omp parallel for
for (int i = 0; i < K_point + K_center; i++) {
sumf_degree[i] = 0.0;
if (i < K_point) { // point
for (unsigned n = 0; n < points_num; n++) {
sumf_degree[i] += W_point[i][n];
}
} else { // center
for (unsigned n = 0; n < points_num; n++) {
sumf_degree[i] += W_center[i - K_point][n];
}
}
}
float sumf = 0;
for (int i = 0; i < K_point + K_center; i++) {
sumf += sumf_degree[i];
}
delete[] sumf_degree;
#pragma omp parallel for
for (int i = 0; i < K_point + K_center; i++) {
if (i < K_point) { // point
for (unsigned n = 0; n < points_num; n++) {
W_point[i][n] /= sumf;
}
} else { // center
for (unsigned n = 0; n < points_num; n++) {
W_center[i - K_point][n] /= sumf;
}
}
}
}