-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyg_distributed.py
136 lines (112 loc) · 4.36 KB
/
pyg_distributed.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
import argparse
import os
import time
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn.functional as F
from ogb.graphproppred import Evaluator
from ogb.graphproppred import PygGraphPropPredDataset as Dataset
from ogb.graphproppred.mol_encoder import AtomEncoder, BondEncoder
from torch.nn import BatchNorm1d as BatchNorm
from torch.nn import Linear, ReLU, Sequential
from torch.nn.parallel import DistributedDataParallel
from torch.utils.data.distributed import DistributedSampler
import torch_geometric.transforms as T
from torch_geometric.loader import DataLoader
from torch_geometric.nn import GINEConv, global_mean_pool
class GIN(torch.nn.Module):
def __init__(self, hidden_channels, out_channels, num_layers=3,
dropout=0.5):
super().__init__()
self.dropout = dropout
self.atom_encoder = AtomEncoder(hidden_channels)
self.bond_encoder = BondEncoder(hidden_channels)
self.convs = torch.nn.ModuleList()
for _ in range(num_layers):
nn = Sequential(
Linear(hidden_channels, 2 * hidden_channels),
BatchNorm(2 * hidden_channels),
ReLU(),
Linear(2 * hidden_channels, hidden_channels),
BatchNorm(hidden_channels),
ReLU(),
)
self.convs.append(GINEConv(nn, train_eps=True))
self.lin = Linear(hidden_channels, out_channels)
def forward(self, x, adj_t, batch):
x = self.atom_encoder(x)
edge_attr = adj_t.coo()[2]
adj_t = adj_t.set_value(self.bond_encoder(edge_attr), layout='coo')
for conv in self.convs:
x = conv(x, adj_t)
x = F.dropout(x, p=self.dropout, training=self.training)
x = global_mean_pool(x, batch)
x = self.lin(x)
return x
def run(rank, world_size: int, dataset_name: str, root: str, batch_size: int, epoch_num: int):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355'
dist.init_process_group('nccl', rank=rank, world_size=world_size)
torch.manual_seed(12345)
dataset = Dataset(dataset_name, root,
pre_transform=T.ToSparseTensor(attr='edge_attr'))
split_idx = dataset.get_idx_split()
train_dataset = dataset[split_idx['train']]
train_sampler = DistributedSampler(train_dataset, rank=rank, shuffle=True)
train_loader = DataLoader(train_dataset, batch_size=batch_size,
sampler=train_sampler)
model = GIN(300, dataset.num_tasks, num_layers=3, dropout=0.5).to(rank)
model = DistributedDataParallel(model, device_ids=[rank])
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
criterion = torch.nn.BCEWithLogitsLoss()
s = time.perf_counter()
for epoch in range(epoch_num):
model.train()
train_sampler.set_epoch(epoch)
for data in train_loader:
data = data.to(rank)
optimizer.zero_grad()
logits = model(data.x, data.adj_t, data.batch)
loss = criterion(logits, data.y.to(torch.float))
loss.backward()
optimizer.step()
if rank == 0:
print(f"elpased time: {(time.perf_counter() - s) / float(epoch_num)} for {world_size} gpus for each epoch ")
dist.barrier()
dist.destroy_process_group()
if __name__ == '__main__':
print(os.getpid())
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type=str,
default="ogbg-molhiv",
choices=["ogbg-molhiv", "ogbg-molpcba"]
)
parser.add_argument(
"--world_size",
type=int,
default=torch.cuda.device_count()
)
parser.add_argument(
"--batch_size",
type=int,
default=128
)
parser.add_argument(
"--epoch",
type=int,
default=10
)
dataset_name = parser.parse_args().dataset
root = '.'
# Download and process the dataset on main process.
Dataset(dataset_name, root,
pre_transform=T.ToSparseTensor(attr='edge_attr'))
world_size = parser.parse_args().world_size
batch_size = parser.parse_args().batch_size
epoch = parser.parse_args().epoch
print('Let\'s use', world_size, 'GPUs!')
args = (world_size, dataset_name, root, batch_size, epoch)
mp.spawn(run, args=args, nprocs=world_size, join=True)