-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathmnist_distributed.py
230 lines (165 loc) · 6.28 KB
/
mnist_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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""A deep MNIST classifier using convolutional layers.
This example was adapted from
https://pytorch.org/docs/master/distributed.html
https://pytorch.org/tutorials/intermediate/dist_tuto.html
https://github.com/narumiruna/pytorch-distributed-example/blob/master/mnist/main.py
Each worker reads the full MNIST dataset and asynchronously trains a CNN with dropout and using the Adam optimizer,
updating the model parameters on shared parameter servers.
The current training accuracy is printed out after every 100 steps.
"""
from __future__ import division, print_function
import argparse
import os
import torch
import torch.nn.functional as F
from torch import distributed, nn
from torch.utils import data
from torch.utils.data.distributed import DistributedSampler
from torchvision import datasets, transforms
class AverageMeter(object):
def __init__(self):
self.sum = 0
self.count = 0
def update(self, value, number):
self.sum += value * number
self.count += number
@property
def average(self):
return self.sum / self.count
class AccuracyMeter(object):
def __init__(self):
self.correct = 0
self.count = 0
def update(self, output, label):
predictions = output.data.argmax(dim=1)
correct = predictions.eq(label.data).sum().item()
self.correct += correct
self.count += output.size(0)
@property
def accuracy(self):
return self.correct / self.count
class Trainer(object):
def __init__(self, net, optimizer, train_loader, test_loader, device):
self.net = net
self.optimizer = optimizer
self.train_loader = train_loader
self.test_loader = test_loader
self.device = device
def train(self):
train_loss = AverageMeter()
train_acc = AccuracyMeter()
self.net.train()
for data, label in self.train_loader:
data = data.to(self.device)
label = label.to(self.device)
output = self.net(data)
loss = F.cross_entropy(output, label)
self.optimizer.zero_grad()
loss.backward()
# average the gradients
self.average_gradients()
self.optimizer.step()
train_loss.update(loss.item(), data.size(0))
train_acc.update(output, label)
return train_loss.average, train_acc.accuracy
def evaluate(self):
test_loss = AverageMeter()
test_acc = AccuracyMeter()
self.net.eval()
with torch.no_grad():
for data, label in self.test_loader:
data = data.to(self.device)
label = label.to(self.device)
output = self.net(data)
loss = F.cross_entropy(output, label)
test_loss.update(loss.item(), data.size(0))
test_acc.update(output, label)
return test_loss.average, test_acc.accuracy
def average_gradients(self):
world_size = distributed.get_world_size()
for p in self.net.parameters():
group = distributed.new_group(ranks=list(range(world_size)))
tensor = p.grad.data.cpu()
distributed.all_reduce(
tensor, op=distributed.reduce_op.SUM, group=group)
tensor /= float(world_size)
p.grad.data = tensor.to(self.device)
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc = nn.Linear(784, 10)
def forward(self, x):
return self.fc(x.view(x.size(0), -1))
def get_dataloader(root, batch_size):
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.13066047740239478,), (0.3081078087569972,))
])
train_set = datasets.MNIST(
root, train=True, transform=transform, download=True)
sampler = DistributedSampler(train_set)
train_loader = data.DataLoader(
train_set,
batch_size=batch_size,
shuffle=(sampler is None),
sampler=sampler)
test_loader = data.DataLoader(
datasets.MNIST(root, train=False, transform=transform, download=True),
batch_size=batch_size,
shuffle=False)
return train_loader, test_loader
def solve(args):
device = torch.device('cuda' if args.cuda else 'cpu')
net = Net().to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=args.learning_rate)
train_loader, test_loader = get_dataloader(args.root, args.batch_size)
trainer = Trainer(net, optimizer, train_loader, test_loader, device)
for epoch in range(1, args.epochs + 1):
train_loss, train_acc = trainer.train()
test_loss, test_acc = trainer.evaluate()
print(
'Epoch: {}/{},'.format(epoch, args.epochs),
'train loss: {:.6f}, train acc: {:.6f}, test loss: {:.6f}, test acc: {:.6f}.'.
format(train_loss, train_acc, test_loss, test_acc))
def init_process(args):
distributed.init_process_group(
backend=args.backend,
init_method=args.init_method,
rank=args.rank,
world_size=args.world_size)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--backend',
type=str,
default='tcp',
help='Name of the backend to use.')
parser.add_argument(
'--init-method',
'-i',
type=str,
default=os.environ.get('INIT_METHOD', 'tcp://127.0.0.1:23456'),
help='URL specifying how to initialize the package.')
parser.add_argument(
'--rank', '-r',
type=int,
default=int(os.environ.get('RANK')),
help='Rank of the current process.')
parser.add_argument(
'--world-size',
'-s',
type=int,
default=int(os.environ.get('WORLD')),
help='Number of processes participating in the job.')
parser.add_argument('--epochs', type=int, default=20)
parser.add_argument('--no-cuda', action='store_true')
parser.add_argument('--learning-rate', '-lr', type=float, default=1e-3)
parser.add_argument('--root', type=str, default='data')
parser.add_argument('--batch-size', type=int, default=128)
args = parser.parse_args()
args.cuda = torch.cuda.is_available() and not args.no_cuda
print(args)
init_process(args)
solve(args)
if __name__ == '__main__':
main()