forked from YukeWang96/TC-GNN_ATC23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_tcgnn.py
executable file
·181 lines (153 loc) · 6.63 KB
/
main_tcgnn.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os.path as osp
import argparse
import os
import sys
import time
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from tqdm import *
import torch.cuda as cuda
import TCGNN
from dataset import *
from gnn_conv import *
from config import *
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, default='amazon0601', help="dataset")
parser.add_argument("--dim", type=int, default=96, help="input embedding dimension")
parser.add_argument("--num_layers", type=int, default=2, help="num layers")
parser.add_argument("--hidden", type=int, default=16, help="hidden dimension")
parser.add_argument("--classes", type=int, default=22, help="number of output classes")
parser.add_argument("--epochs", type=int, default=200, help="number of epoches")
parser.add_argument("--model", type=str, default='gcn', help='GNN model', choices=['gcn', 'gin', 'agnn'])
parser.add_argument("--single_kernel", action='store_true', help="whether to profile a single SAG kernel")
args = parser.parse_args()
print(args)
#########################################
## Load Graph from files.
#########################################
dataset = args.dataset
path = osp.join("tcgnn-ae-graphs/", dataset + ".npz")
dataset = TCGNN_dataset(path, args.dim, args.classes, load_from_txt=False)
num_nodes = dataset.num_nodes
num_edges = dataset.num_edges
column_index = dataset.column_index
row_pointers = dataset.row_pointers
#########################################
## Compute TC-GNN related graph MetaData.
#########################################
num_row_windows = (num_nodes + BLK_H - 1) // BLK_H
edgeToColumn = torch.zeros(num_edges, dtype=torch.int)
edgeToRow = torch.zeros(num_edges, dtype=torch.int)
blockPartition = torch.zeros(num_row_windows, dtype=torch.int)
# preprocessing for generating meta-information
start = time.perf_counter()
TCGNN.preprocess(column_index, row_pointers, num_nodes, \
BLK_H, BLK_W, blockPartition, edgeToColumn, edgeToRow)
build_neighbor_parts = time.perf_counter() - start
print("Prep. (ms):\t{:.3f}".format(build_neighbor_parts*1e3))
column_index = column_index.cuda()
row_pointers = row_pointers.cuda()
blockPartition = blockPartition.cuda()
edgeToColumn = edgeToColumn.cuda()
edgeToRow = edgeToRow.cuda()
#########################################
## Single Satter-And-Gather (SAG) Profiling.
#########################################
if args.single_kernel:
SAG_obj = SAG(row_pointers, column_index,\
blockPartition, edgeToColumn, edgeToRow)
X = dataset.x
SAG_obj.profile(X)
exit(0)
#########################################
## Build GCN and AGNN Model
#########################################
if args.model == "gcn":
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = GCNConv(dataset.num_features, args.hidden)
self.hidden_layers = nn.ModuleList()
for _ in range(args.num_layers - 2):
self.hidden_layers.append(GCNConv(args.hidden, args.hidden))
self.conv2 = GCNConv(args.hidden, dataset.num_classes)
self.relu = nn.ReLU()
def forward(self):
x = dataset.x
x = self.relu(self.conv1(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow))
x = F.dropout(x, training=self.training)
for Gconv in self.hidden_layers:
x = Gconv(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
x = self.relu(x)
x = self.conv2(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
return F.log_softmax(x, dim=1)
elif args.model == "gin":
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = GINConv(dataset.num_features, args.hidden)
self.hidden_layers = nn.ModuleList()
for i in range(args.num_layers - 2):
self.hidden_layers.append(GINConv(args.hidden, args.hidden))
self.conv2 = GINConv(args.hidden, dataset.num_classes)
self.relu = nn.ReLU()
def forward(self):
x = dataset.x
x = self.relu(self.conv1(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow))
x = F.dropout(x, training=self.training)
for Gconv in self.hidden_layers:
x = Gconv(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
x = self.relu(x)
x = self.conv2(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
return F.log_softmax(x, dim=1)
elif args.model == "agnn":
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = AGNNConv(dataset.num_features, args.hidden)
self.hidden_layers = nn.ModuleList()
for _ in range(args.num_layers - 2):
self.hidden_layers.append(AGNNConv(args.hidden, args.hidden))
self.conv2 = AGNNConv(args.hidden, dataset.num_classes)
self.relu = nn.ReLU()
def forward(self):
x = dataset.x
x = self.relu(self.conv1(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow))
x = F.dropout(x, training=self.training)
for Gconv in self.hidden_layers:
x = Gconv(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
x = self.relu(x)
x = self.conv2(x, row_pointers, column_index, blockPartition, edgeToColumn, edgeToRow)
return F.log_softmax(x, dim=1)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model, dataset = Net().to(device), dataset.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, capturable=True)
# Training
def train():
model.train()
optimizer.zero_grad()
loss = F.nll_loss(model()[:], dataset.y[:])
loss.backward()
optimizer.step()
if __name__ == "__main__":
# s = torch.cuda.Stream()
# s.wait_stream(torch.cuda.current_stream())
# train()
# torch.cuda.current_stream().wait_stream(s)
# g = torch.cuda.CUDAGraph()
# with torch.cuda.graph(g):
# train()
# dry run.
for epoch in range(1, 10):
train()
# g.replay()
torch.cuda.synchronize()
start_train = time.perf_counter()
for _ in tqdm(range(1, args.epochs + 1)):
train()
# g.replay()
torch.cuda.synchronize()
train_time = time.perf_counter() - start_train
print("Train (ms):\t{:6.3f}".format(train_time*1e3/args.epochs))