-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_spmm.py
139 lines (119 loc) · 4.01 KB
/
test_spmm.py
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
import argparse, time, math
import numpy as np
import networkx as nx
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import dgl
import dgl.function as fn
from dgl.data import register_data_args
from dgl.data import CoraGraphDataset, CiteseerGraphDataset, PubmedGraphDataset
from dgl.data import RedditDataset
from torch_sparse import SparseTensor
from torch_geometric.nn import MessagePassing
from torch_sparse import matmul
from typing import Optional
th.classes.load_library("build/libadjmatrix.so")
AdjMatrix = th.classes.DGL.AdjMatrix
def do_spmm(adj: AdjMatrix,
op: str,
reduce: str,
ufeat : Optional[th.Tensor],
efeat : Optional[th.Tensor]):
return th.ops.DGL.GSpMM(adj, op, reduce, ufeat, efeat)
scripted_spmm = th.jit.script(do_spmm)
class GCNConv_pyg(MessagePassing):
def __init__(self):
super(GCNConv_pyg, self).__init__(aggr="add")
def forward(self, x, edge_index):
out = self.propagate(edge_index, x=x)
return out
def message(self, x_j):
return x_j
def message_and_aggregate(self, adj_t, x):
return matmul(adj_t, x, reduce=self.aggr)
pyg_spmm = GCNConv_pyg()
def run_dgl(g, features):
g.ndata["h"] = features
g.update_all(fn.copy_src(src="h", out="m"), fn.sum(msg="m", out="h"))
return g.ndata['h']
def run_pyg(edge_index, features):
return pyg_spmm(features, edge_index)
def run_script(adj, features):
return scripted_spmm(adj, "copy_lhs", "sum", features, None)
def main(args):
# load and preprocess dataset
if args.dataset == 'cora':
data = CoraGraphDataset()
elif args.dataset == 'citeseer':
data = CiteseerGraphDataset()
elif args.dataset == 'pubmed':
data = PubmedGraphDataset()
elif args.dataset == 'reddit':
data = RedditDataset()
else:
raise ValueError('Unknown dataset: {}'.format(args.dataset))
g = data[0]
if args.gpu < 0:
cuda = False
else:
cuda = True
g = g.to(args.gpu)
features = g.ndata['feat']
in_feats = features.shape[1]
n_classes = data.num_classes
print("feature size: {}".format(in_feats))
# add self loop
g = dgl.remove_self_loop(g)
g = dgl.add_self_loop(g)
n_edges = g.number_of_edges()
src, dst = g.edges()
edge_index = SparseTensor(row=src,
col=dst,
sparse_sizes=(g.number_of_nodes(), g.number_of_nodes()))
adj = AdjMatrix(src, dst)
runtime = 0.0
n = 1
if args.impl == "dgl":
run_dgl(g, features)
if args.gpu >= 0:
th.cuda.synchronize()
th.cuda.nvtx.range_push("spmm start")
for _ in range(n):
start_run = time.perf_counter()
run_dgl(g, features)
if args.gpu >= 0:
th.cuda.synchronize()
runtime += time.perf_counter() - start_run
th.cuda.nvtx.range_pop()
elif args.impl == "pyg":
run_pyg(edge_index, features)
if args.gpu >= 0:
th.cuda.synchronize()
for _ in range(n):
start_run = time.perf_counter()
run_pyg(edge_index, features)
if args.gpu >= 0:
th.cuda.synchronize()
runtime += time.perf_counter() - start_run
else:
run_script(adj, features)
if args.gpu >= 0:
th.cuda.synchronize()
for _ in range(n):
start_run = time.perf_counter()
run_script(adj, features)
if args.gpu >= 0:
th.cuda.synchronize()
runtime += time.perf_counter() - start_run
#print('Time (ms): {:.3f}'.format(runtime*1e3/n))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='GCN')
register_data_args(parser)
parser.add_argument("--impl", type=str, default="dgl",
help="use torch script or not")
parser.add_argument("--gpu", type=int, default=-1,
help="gpu")
args = parser.parse_args()
print(args)
main(args)