|
1 | 1 | #include <torch/torch.h> |
2 | 2 |
|
3 | | -// #include "../include/degree.cpp" |
4 | | -// #include "../include/loop.cpp" |
5 | | -// #include "../include/perm.cpp" |
| 3 | +#include "utils.h" |
| 4 | + |
| 5 | +#define ITERATE_NEIGHBORS(NODE, NAME, ROW, COL, ...) \ |
| 6 | + { \ |
| 7 | + for (int64_t e = ROW[NODE]; e < ROW[NODE + 1]; e++) { \ |
| 8 | + auto NAME = COL[e]; \ |
| 9 | + __VA_ARGS__; \ |
| 10 | + } \ |
| 11 | + } |
6 | 12 |
|
7 | 13 | at::Tensor graclus(at::Tensor row, at::Tensor col, int64_t num_nodes) { |
8 | | - // std::tie(row, col) = remove_self_loops(row, col); |
9 | | - // std::tie(row, col) = randperm(row, col, num_nodes); |
10 | | - // auto deg = degree(row, num_nodes, row.type().scalarType()); |
| 14 | + std::tie(row, col) = remove_self_loops(row, col); |
| 15 | + std::tie(row, col) = rand(row, col); |
| 16 | + std::tie(row, col) = to_csr(row, col); |
| 17 | + auto row_data = row.data<int64_t>(), col_data = col.data<int64_t>(); |
| 18 | + |
| 19 | + auto perm = randperm(num_nodes); |
| 20 | + auto perm_data = perm.data<int64_t>(); |
11 | 21 |
|
12 | 22 | auto cluster = at::full(num_nodes, -1, row.options()); |
| 23 | + auto cluster_data = cluster.data<int64_t>(); |
| 24 | + |
| 25 | + for (int64_t i = 0; i < num_nodes; i++) { |
| 26 | + auto u = perm_data[i]; |
| 27 | + |
| 28 | + if (cluster_data[u] >= 0) |
| 29 | + continue; |
| 30 | + |
| 31 | + cluster_data[u] = u; |
13 | 32 |
|
14 | | - // auto *row_data = row.data<int64_t>(); |
15 | | - // auto *col_data = col.data<int64_t>(); |
16 | | - // auto *deg_data = deg.data<int64_t>(); |
17 | | - // auto *cluster_data = cluster.data<int64_t>(); |
18 | | - |
19 | | - // int64_t e_idx = 0, d_idx, r, c; |
20 | | - // while (e_idx < row.size(0)) { |
21 | | - // r = row_data[e_idx]; |
22 | | - // if (cluster_data[r] < 0) { |
23 | | - // cluster_data[r] = r; |
24 | | - // for (d_idx = 0; d_idx < deg_data[r]; d_idx++) { |
25 | | - // c = col_data[e_idx + d_idx]; |
26 | | - // if (cluster_data[c] < 0) { |
27 | | - // cluster_data[r] = std::min(r, c); |
28 | | - // cluster_data[c] = std::min(r, c); |
29 | | - // break; |
30 | | - // } |
31 | | - // } |
32 | | - // } |
33 | | - // e_idx += deg_data[r]; |
34 | | - // } |
| 33 | + ITERATE_NEIGHBORS(u, v, row_data, col_data, { |
| 34 | + if (cluster_data[v] >= 0) |
| 35 | + continue; |
| 36 | + |
| 37 | + cluster_data[u] = std::min(u, v); |
| 38 | + cluster_data[v] = std::min(u, v); |
| 39 | + break; |
| 40 | + }); |
| 41 | + } |
35 | 42 |
|
36 | 43 | return cluster; |
37 | 44 | } |
38 | 45 |
|
39 | 46 | at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor weight, |
40 | 47 | int64_t num_nodes) { |
| 48 | + std::tie(row, col) = remove_self_loops(row, col, weight); |
| 49 | + std::tie(row, col, weight) = to_csr(row, col, weight); |
| 50 | + auto row_data = row.data<int64_t>(), col_data = col.data<int64_t>(); |
| 51 | + |
| 52 | + auto perm = randperm(num_nodes); |
| 53 | + auto perm_data = perm.data<int64_t>(); |
| 54 | + |
41 | 55 | auto cluster = at::full(num_nodes, -1, row.options()); |
| 56 | + auto cluster_data = cluster.data<int64_t>(); |
| 57 | + |
| 58 | + AT_DISPATCH_ALL_TYPES(weight.type(), "weighted_graclus", [&] { |
| 59 | + auto weight_data = weight.data<scalar_t>(); |
| 60 | + auto weight_data = weight.data<scalar_t>(); |
| 61 | + |
| 62 | + for (int64_t i = 0; i < num_nodes; i++) { |
| 63 | + auto u = perm_data[i]; |
| 64 | + |
| 65 | + if (cluster_data[u] >= 0) |
| 66 | + continue; |
| 67 | + |
| 68 | + cluster_data[u] = u; |
| 69 | + |
| 70 | + int64_t v_max; |
| 71 | + scalar_t w_max = 0; |
| 72 | + |
| 73 | + ITERATE_NEIGHBORS(u, v, row_data, col_data, { |
| 74 | + if (cluster_data[v] >= 0) |
| 75 | + continue; |
| 76 | + |
| 77 | + auto w = weight_data[e]; |
| 78 | + if (w >= w_max) { |
| 79 | + v_max = v; |
| 80 | + w_max = w; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + cluster_data[u] = std::min(u, v_max); |
| 85 | + cluster_data[v_max] = std::min(u, v_max); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
42 | 89 | return cluster; |
43 | 90 | } |
44 | 91 |
|
|
0 commit comments