-
Notifications
You must be signed in to change notification settings - Fork 3
/
cluster_gcn_dgl.py
executable file
·108 lines (90 loc) · 3.77 KB
/
cluster_gcn_dgl.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
import argparse
import time
import random
import os.path as osp
import numpy as np
import torch
from ogb.nodeproppred import DglNodePropPredDataset
from dgl.data import register_data_args
from modules import *
from sampler import ClusterIter
from utils import load_data
from dataset import *
from tqdm import *
import QGTC
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser()
register_data_args(parser)
parser.add_argument("--gpu", type=int, default=0, help="gpu")
parser.add_argument("--n-epochs", type=int, default=20, help="number of training epochs")
parser.add_argument("--batch-size", type=int, default=20, help="batch size")
parser.add_argument("--psize", type=int, default=1500, help="number of partitions")
parser.add_argument("--dim", type=int, default=10, help="input dimension of each dataset")
parser.add_argument("--n-hidden", type=int, default=16, help="number of hidden gcn units")
parser.add_argument("--n-classes", type=int, default=10, help="number of classes")
parser.add_argument("--n-layers", type=int, default=1, help="number of hidden gcn layers")
parser.add_argument("--use-pp", action='store_true',help="whether to use precomputation")
parser.add_argument("--regular", action='store_true',help="whether to use DGL")
parser.add_argument("--run_GIN", action='store_true',help="whether to run GIN model")
parser.add_argument("--use_QGTC", action='store_true',help="whether to use QGTC")
parser.add_argument("--zerotile_jump", action='store_true',help="whether to profile zero-tile jumping")
args = parser.parse_args()
print(args)
def main(args):
torch.manual_seed(3)
np.random.seed(2)
random.seed(2)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# load and preprocess dataset
if args.dataset in ['ppi', 'reddit']:
data = load_data(args)
g = data.g
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
labels = g.ndata['label']
elif args.dataset in ['ogbn-arxiv', 'ogbn-products']:
data = DglNodePropPredDataset(name=args.dataset) #'ogbn-proteins'
split_idx = data.get_idx_split()
g, labels = data[0]
train_mask = split_idx['train']
val_mask = split_idx['valid']
test_mask = split_idx['test']
else:
path = osp.join("./qgtc_graphs", args.dataset+".npz")
data = QGTC_dataset(path, args.dim, args.n_classes)
g = data.g
train_mask = data.train_mask
val_mask = data.val_mask
test_mask = data.test_mask
train_nid = np.nonzero(train_mask.data.numpy())[0].astype(np.int64)
in_feats = g.ndata['feat'].shape[1]
n_classes = data.num_classes
# metis only support int64 graph
g = g.long()
# get the subgraph based on the partitioning nodes list.
cluster_iterator = ClusterIter(args.dataset, g, args.psize, args.batch_size, train_nid, use_pp=False, regular=args.regular)
torch.cuda.set_device(args.gpu)
val_mask = val_mask.cuda()
test_mask = test_mask.cuda()
g = g.int().to(args.gpu)
feat_size = g.ndata['feat'].shape[1]
if args.run_GIN:
model = GIN(in_feats, args.n_hidden, n_classes)
else:
model = GraphSAGE(in_feats, args.n_hidden, n_classes, args.n_layers)
model.cuda()
train_nid = torch.from_numpy(train_nid).cuda()
start_time = time.time()
for epoch in tqdm(range(args.n_epochs)):
for j, cluster in enumerate(cluster_iterator):
cluster = cluster.to(torch.cuda.current_device())
model(cluster)
cluster = cluster.cpu()
torch.cuda.synchronize()
end_time = time.time()
print("Avg. Epoch: {:.3f} ms".format((end_time - start_time)*1000/args.n_epochs))
if __name__ == '__main__':
main(args)