benchmark.py 1.56 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
import time
rusty1s's avatar
rusty1s committed
2
import sys
rusty1s's avatar
rusty1s committed
3
4

import torch
rusty1s's avatar
rusty1s committed
5
6
from torch.autograd import Variable
from torch_spline_conv import spline_conv
rusty1s's avatar
rusty1s committed
7

rusty1s's avatar
rusty1s committed
8
9
10
11
12
13
14
15
16
17
18
19
20
sys.path.insert(0, '../../pytorch_geometric')

from torch_geometric.nn.modules import SplineConv  # noqa

n = 600
m_in = 64
m_out = 64
d = 4
x = torch.FloatTensor(n, m_in).uniform_(-1, 1)
row = torch.arange(0, n).view(-1, 1).repeat(1, n).view(-1).long()
col = torch.arange(0, n).repeat(n).long()
edge_index = torch.stack([row, col], dim=0)
pseudo = torch.FloatTensor(n * n, d).uniform_(0, 1)
rusty1s's avatar
rusty1s committed
21
22
23
kernel_size = torch.LongTensor(d).fill_(5)
is_open_spline = torch.ByteTensor(d).fill_(1)
K = kernel_size.prod()
rusty1s's avatar
rusty1s committed
24
weight = torch.FloatTensor(K, m_in, m_out).uniform_(-1, 1)
rusty1s's avatar
rusty1s committed
25

rusty1s's avatar
rusty1s committed
26
27
28
29
# t = time.perf_counter()
# out = spline_conv(x, edge_index, pseudo, weight, kernel_size, is_open_spline)
# t = time.perf_counter() - t
# print('CPU:', t)
rusty1s's avatar
rusty1s committed
30

rusty1s's avatar
rusty1s committed
31
32
x = x.cuda()
edge_index = edge_index.cuda()
rusty1s's avatar
rusty1s committed
33
pseudo = pseudo.cuda()
rusty1s's avatar
rusty1s committed
34
weight = weight.cuda()
rusty1s's avatar
rusty1s committed
35
36
37
kernel_size = kernel_size.cuda()
is_open_spline = is_open_spline.cuda()

rusty1s's avatar
rusty1s committed
38
out = spline_conv(x, edge_index, pseudo, weight, kernel_size, is_open_spline)
rusty1s's avatar
rusty1s committed
39
40
torch.cuda.synchronize()
t = time.perf_counter()
rusty1s's avatar
rusty1s committed
41
out = spline_conv(x, edge_index, pseudo, weight, kernel_size, is_open_spline)
rusty1s's avatar
rusty1s committed
42
43
44
torch.cuda.synchronize()
t = time.perf_counter() - t
print('GPU:', t)
rusty1s's avatar
rusty1s committed
45
46
47
48
49
50
51
52
53
54
55

conv = SplineConv(m_in, m_out, d, kernel_size, is_open_spline.long()).cuda()
adj = {'indices': edge_index, 'values': pseudo, 'size': torch.Size([n, n, d])}
x = Variable(x)
conv(adj, x)
torch.cuda.synchronize()
t = time.perf_counter()
conv(adj, x)
torch.cuda.synchronize()
t = time.perf_counter() - t
print('GPU old:', t)