-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgl_distributed.py
154 lines (131 loc) · 4.8 KB
/
dgl_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import argparse
import os
import time
import dgl
from dgl.nn import AvgPooling, GINEConv
import torch
import torch.distributed as dist
import torch.nn.functional as F
from torch.nn import BatchNorm1d as BatchNorm
from torch.nn import Linear, ReLU, Sequential
from torch.nn.parallel import DistributedDataParallel
from dgl.data import AsGraphPredDataset
from dgl.dataloading import GraphDataLoader
from ogb.graphproppred import Evaluator
from ogb.graphproppred import DglGraphPropPredDataset as Dataset
from ogb.graphproppred.mol_encoder import AtomEncoder, BondEncoder
from tqdm import tqdm
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()
self.pool = AvgPooling()
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, learn_eps=True))
self.lin = Linear(hidden_channels, out_channels)
def forward(self, g, x, x_e):
x = self.atom_encoder(x)
xe = self.bond_encoder(x_e)
for conv in self.convs:
x = conv(g, x, xe)
x = F.dropout(x, p=self.dropout, training=self.training)
x = self.pool(g, x)
x = self.lin(x)
return x
@torch.no_grad()
def evaluate(dataloader, device, model, evaluator):
model.eval()
y_true = []
y_pred = []
for batched_graph, labels in tqdm(dataloader):
batched_graph, labels = batched_graph.to(device), labels.to(device)
node_feat, edge_feat = (
batched_graph.ndata["feat"],
batched_graph.edata["feat"],
)
y_hat = model(batched_graph, node_feat, edge_feat)
y_true.append(labels.view(y_hat.shape).detach().cpu())
y_pred.append(y_hat.detach().cpu())
y_true = torch.cat(y_true, dim=0).numpy()
y_pred = torch.cat(y_pred, dim=0).numpy()
input_dict = {"y_true": y_true, "y_pred": y_pred}
return evaluator.eval(input_dict)
def run(rank, world_size: int, dataset_name: str, root: str, batch_size: int, epoch_num: int):
dgl.utils.set_num_threads(11)
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12347'
dist.init_process_group('nccl', rank=rank, world_size=world_size)
torch.manual_seed(12345)
dataset = AsGraphPredDataset(Dataset(dataset_name, root))
train_loader = GraphDataLoader(
dataset[dataset.train_idx], batch_size=batch_size, use_ddp=True, shuffle=True
)
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()
print("start!")
s = time.perf_counter()
for epoch in range(epoch_num):
count = 0
model.train()
train_loader.set_epoch(epoch)
for g, y in train_loader:
g, y = g.to(rank), y.to(rank)
optimizer.zero_grad()
x, xe = (g.ndata["feat"], g.edata["feat"])
logits = model(g, x, xe)
loss = criterion(logits, 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)
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)
import torch.multiprocessing as mp
mp.spawn(run, args=args, nprocs=world_size, join=True)