Skip to content

Commit cba1cdb

Browse files
committed
rebuild to new extension api
1 parent c1df3bd commit cba1cdb

File tree

16 files changed

+283
-69
lines changed

16 files changed

+283
-69
lines changed

.travis.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ matrix:
66
- python: 2.7
77
- python: 3.5
88
- python: 3.6
9+
addons:
10+
apt:
11+
sources:
12+
- ubuntu-toolchain-r-test
13+
packages:
14+
- gcc-4.9
15+
- g++-4.9
16+
before_install:
17+
- export CC="gcc-4.9"
18+
- export CXX="g++-4.9"
919
install:
1020
- if [[ $TRAVIS_PYTHON_VERSION == 2.7 ]]; then pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp27-cp27mu-linux_x86_64.whl; fi
1121
- if [[ $TRAVIS_PYTHON_VERSION == 3.5 ]]; then pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp35-cp35m-linux_x86_64.whl; fi
1222
- if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-linux_x86_64.whl; fi
1323
- pip install pycodestyle
1424
- pip install flake8
1525
- pip install codecov
16-
- pip install cffi
1726
script:
1827
- pycodestyle .
1928
- flake8 .

MANIFEST.in

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
include LICENSE
2-
include build.py
3-
include build.sh
4-
5-
recursive-include aten *
6-
7-
recursive-exclude torch_cluster/_ext *
2+
recursive-include cpu *
3+
recursive-include cuda *

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ $ echo $CPATH
3939
Then run:
4040

4141
```
42-
pip install cffi torch-cluster
42+
pip install torch-cluster
4343
```
4444

4545
If you are running into any installation problems, please create an [issue](https://github.com/rusty1s/pytorch_cluster/issues).
46+
Be sure to import `torch` first before using this package to resolve symbols the dynamic linker must see.
4647

4748
## Graclus
4849

@@ -62,7 +63,7 @@ cluster = graclus_cluster(row, col, weight)
6263

6364
```
6465
print(cluster)
65-
tensor([ 0, 0, 1])
66+
tensor([0, 0, 1])
6667
```
6768

6869
## VoxelGrid
@@ -81,7 +82,7 @@ cluster = grid_cluster(pos, size)
8182

8283
```
8384
print(cluster)
84-
tensor([ 0, 5, 3, 0, 1])
85+
tensor([0, 5, 3, 0, 1])
8586
```
8687

8788
## Running tests

build.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

build.sh

Lines changed: 0 additions & 10 deletions
This file was deleted.

cpu/graclus.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <torch/torch.h>
2+
3+
// #include "../include/degree.cpp"
4+
// #include "../include/loop.cpp"
5+
// #include "../include/perm.cpp"
6+
7+
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());
11+
12+
auto cluster = at::full(num_nodes, -1, row.options());
13+
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+
// }
35+
36+
return cluster;
37+
}
38+
39+
at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor weight,
40+
int64_t num_nodes) {
41+
auto cluster = at::full(num_nodes, -1, row.options());
42+
return cluster;
43+
}
44+
45+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
46+
m.def("graclus", &graclus, "Graclus (CPU)");
47+
m.def("weighted_graclus", &weighted_graclus, "Weighted Graclus (CPU)");
48+
}

cpu/grid.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <torch/torch.h>
2+
3+
at::Tensor grid(at::Tensor pos, at::Tensor size, at::Tensor start,
4+
at::Tensor end) {
5+
size = size.toType(pos.type());
6+
start = start.toType(pos.type());
7+
end = end.toType(pos.type());
8+
9+
pos = pos - start.view({1, -1});
10+
auto num_voxels = ((end - start) / size).toType(at::kLong);
11+
num_voxels = (num_voxels + 1).cumsum(0);
12+
num_voxels -= num_voxels.data<int64_t>()[0];
13+
num_voxels.data<int64_t>()[0] = 1;
14+
15+
auto cluster = pos / size.view({1, -1});
16+
cluster = cluster.toType(at::kLong);
17+
cluster *= num_voxels.view({1, -1});
18+
cluster = cluster.sum(1);
19+
20+
return cluster;
21+
}
22+
23+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("grid", &grid, "Grid (CPU)"); }

cuda/cluster.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <torch/torch.h>
2+
3+
at::Tensor grid(at::Tensor pos, at::Tensor size, at::Tensor start,
4+
at::Tensor end);
5+
6+
at::Tensor graclus(at::Tensor row, at::Tensor col, int num_nodes);
7+
8+
at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor weight,
9+
int num_nodes);
10+
11+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
12+
m.def("grid", &grid, "Grid (CUDA)");
13+
m.def("graclus", &graclus, "Graclus (CUDA)");
14+
m.def("weighted_graclus", &weighted_graclus, "Weightes Graclus (CUDA)");
15+
}

cuda/graclus.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <torch/torch.h>
2+
3+
#define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be CUDA tensor")
4+
5+
at::Tensor graclus_cuda(at::Tensor row, at::Tensor col, int64_t num_nodes);
6+
7+
at::Tensor weighted_graclus_cuda(at::Tensor row, at::Tensor col,
8+
at::Tensor weight, int64_t num_nodes);
9+
10+
at::Tensor graclus(at::Tensor row, at::Tensor col, int64_t num_nodes) {
11+
CHECK_CUDA(row);
12+
CHECK_CUDA(col);
13+
return graclus_cuda(row, col, num_nodes);
14+
}
15+
16+
at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor weight,
17+
int64_t num_nodes) {
18+
CHECK_CUDA(row);
19+
CHECK_CUDA(col);
20+
CHECK_CUDA(weight);
21+
return graclus_cuda(row, col, num_nodes);
22+
}
23+
24+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
25+
m.def("graclus", &graclus, "Graclus (CUDA)");
26+
m.def("weighted_graclus", &weighted_graclus, "Weighted Graclus (CUDA)");
27+
}

cuda/graclus_kernel.cu

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <ATen/ATen.h>
2+
3+
at::Tensor graclus_cuda(at::Tensor row, at::Tensor col, int64_t num_nodes) {
4+
auto cluster = at::full(num_nodes, -1, row.options());
5+
return cluster;
6+
}
7+
8+
at::Tensor weighted_graclus_cuda(at::Tensor row, at::Tensor col,
9+
at::Tensor weight, int64_t num_nodes) {
10+
auto cluster = at::full(num_nodes, -1, row.options());
11+
return cluster;
12+
}
13+
14+
// #include "color.cuh"
15+
// #include "common.cuh"
16+
17+
// at::Tensor graclus(at::Tensor row, at::Tensor col, int num_nodes) {
18+
// // Remove self-loops.
19+
// auto mask = row != col;
20+
// row = row.masked_select(mask);
21+
// col.masked_select(mask);
22+
23+
// // Sort by row index.
24+
// at::Tensor perm;
25+
// std::tie(row, perm) = row.sort();
26+
// col = col.index_select(0, perm);
27+
28+
// // Generate helper vectors.
29+
// auto cluster = at::full(row.type(), {num_nodes}, -1);
30+
// auto prop = at::full(row.type(), {num_nodes}, -1);
31+
// auto deg = degree(row, num_nodes);
32+
// auto cum_deg = deg.cumsum(0);
33+
34+
// color(cluster);
35+
36+
// /* while (!color(cluster)) { */
37+
// /* propose(cluster, prop, row, col, weight, deg, cum_deg); */
38+
// /* response(cluster, prop, row, col, weight, deg, cum_deg); */
39+
// /* } */
40+
41+
// return cluster;
42+
// }
43+
44+
// at::Tensor weighted_graclus(at::Tensor row, at::Tensor col, at::Tensor
45+
// weight,
46+
// int num_nodes) {
47+
// // Remove self-loops.
48+
// auto mask = row != col;
49+
// row = row.masked_select(mask);
50+
// col = col.masked_select(mask);
51+
// weight = weight.masked_select(mask);
52+
53+
// // Sort by row index.
54+
// at::Tensor perm;
55+
// std::tie(row, perm) = row.sort();
56+
// col = col.index_select(0, perm);
57+
// weight = weight.index_select(0, perm);
58+
59+
// // Generate helper vectors.
60+
// auto cluster = at::full(row.type(), {num_nodes}, -1);
61+
// auto prop = at::full(row.type(), {num_nodes}, -1);
62+
// auto deg = degree(row, num_nodes);
63+
// auto cum_deg = deg.cumsum(0);
64+
65+
// color(cluster);
66+
67+
// /* while (!color(cluster)) { */
68+
// /* weighted_propose(cluster, prop, row, col, weight, deg, cum_deg); */
69+
// /* weighted_response(cluster, prop, row, col, weight, deg, cum_deg); */
70+
// /* } */
71+
72+
// return cluster;
73+
// }

0 commit comments

Comments
 (0)