graclus.cpp 2.69 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
#include <torch/torch.h>

rusty1s's avatar
rusty1s committed
3
4
5
6
7
8
9
10
11
#include "utils.h"

#define ITERATE_NEIGHBORS(NODE, NAME, ROW, COL, ...)                           \
  {                                                                            \
    for (int64_t e = ROW[NODE]; e < ROW[NODE + 1]; e++) {                      \
      auto NAME = COL[e];                                                      \
      __VA_ARGS__;                                                             \
    }                                                                          \
  }
rusty1s's avatar
rusty1s committed
12
13

at::Tensor graclus(at::Tensor row, at::Tensor col, int64_t num_nodes) {
rusty1s's avatar
rusty1s committed
14
15
16
17
18
19
20
  std::tie(row, col) = remove_self_loops(row, col);
  std::tie(row, col) = rand(row, col);
  std::tie(row, col) = to_csr(row, col);
  auto row_data = row.data<int64_t>(), col_data = col.data<int64_t>();

  auto perm = randperm(num_nodes);
  auto perm_data = perm.data<int64_t>();
rusty1s's avatar
rusty1s committed
21
22

  auto cluster = at::full(num_nodes, -1, row.options());
rusty1s's avatar
rusty1s committed
23
24
25
26
27
28
29
30
31
  auto cluster_data = cluster.data<int64_t>();

  for (int64_t i = 0; i < num_nodes; i++) {
    auto u = perm_data[i];

    if (cluster_data[u] >= 0)
      continue;

    cluster_data[u] = u;
rusty1s's avatar
rusty1s committed
32

rusty1s's avatar
rusty1s committed
33
34
35
36
37
38
39
40
41
    ITERATE_NEIGHBORS(u, v, row_data, col_data, {
      if (cluster_data[v] >= 0)
        continue;

      cluster_data[u] = std::min(u, v);
      cluster_data[v] = std::min(u, v);
      break;
    });
  }
rusty1s's avatar
rusty1s committed
42
43
44
45
46
47

  return cluster;
}

at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor weight,
                            int64_t num_nodes) {
rusty1s's avatar
rusty1s committed
48
49
50
51
52
53
54
  std::tie(row, col) = remove_self_loops(row, col, weight);
  std::tie(row, col, weight) = to_csr(row, col, weight);
  auto row_data = row.data<int64_t>(), col_data = col.data<int64_t>();

  auto perm = randperm(num_nodes);
  auto perm_data = perm.data<int64_t>();

rusty1s's avatar
rusty1s committed
55
  auto cluster = at::full(num_nodes, -1, row.options());
rusty1s's avatar
rusty1s committed
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
  auto cluster_data = cluster.data<int64_t>();

  AT_DISPATCH_ALL_TYPES(weight.type(), "weighted_graclus", [&] {
    auto weight_data = weight.data<scalar_t>();
    auto weight_data = weight.data<scalar_t>();

    for (int64_t i = 0; i < num_nodes; i++) {
      auto u = perm_data[i];

      if (cluster_data[u] >= 0)
        continue;

      cluster_data[u] = u;

      int64_t v_max;
      scalar_t w_max = 0;

      ITERATE_NEIGHBORS(u, v, row_data, col_data, {
        if (cluster_data[v] >= 0)
          continue;

        auto w = weight_data[e];
        if (w >= w_max) {
          v_max = v;
          w_max = w;
        }
      });

      cluster_data[u] = std::min(u, v_max);
      cluster_data[v_max] = std::min(u, v_max);
    }
  });

rusty1s's avatar
rusty1s committed
89
90
91
92
93
94
95
  return cluster;
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("graclus", &graclus, "Graclus (CPU)");
  m.def("weighted_graclus", &weighted_graclus, "Weighted Graclus (CPU)");
}