-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_paramserver.py
More file actions
343 lines (287 loc) · 13.1 KB
/
Copy pathmain_paramserver.py
File metadata and controls
343 lines (287 loc) · 13.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import argparse
import os
import random
import time
import numpy as np
import shutil
import csv
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.datasets as datasets
from torchvision.models import resnet50
import torchvision.transforms as transforms
from torch.optim.lr_scheduler import StepLR
from torch.utils.data import Subset
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument('data', metavar='DIR', nargs='?', default='/inspire/hdd/ws-f4d69b29-e0a5-44e6-bd92-acf4de9990f0/public-project/public/public_datas/ILSVRC/Data/CLS-LOC',
help='path to dataset (default: imagenet)')
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=5, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('-b', '--batch-size', default=64, type=int,
metavar='N',
help='mini-batch size (default: 256), this is the total '
'batch size of all GPUs on the current node when '
'using Data Parallel or Distributed Data Parallel')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--wd', '--weight-decay', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)',
dest='weight_decay')
parser.add_argument('-p', '--print-freq', default=10, type=int,
metavar='N', help='print frequency (default: 10)')
parser.add_argument('-n', '--name', default='paramserver',
help='experiment name')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('--pretrained', dest='pretrained', action='store_true',
help='use pre-trained model')
parser.add_argument('--seed', default=212, type=int,
help='seed for initializing training. ')
def save_checkpoint(state, is_best, workpath, filename='checkpoint.pth.tar'):
if not os.path.exists(workpath):
os.makedirs(workpath)
torch.save(state, workpath+'/'+filename)
if is_best:
shutil.copyfile(workpath+'/'+filename, workpath+'/'+'model_best.pth.tar')
def train(train_loader, model, criterion, optimizer, epoch, device, rank, size, args):
batch_time = []
losses = []
top1 = []
top5 = []
throughputs = []
# switch to train mode
model.train()
end = time.time()
for i, (images, target) in enumerate(train_loader):
# move data to the same device as model, asynchronous
images = images.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
# throughputs
batch_size = images.size(0)
throughput = batch_size / (time.time() - end)
throughputs.append(throughput)
# compute output
output = model(images)
loss = criterion(output, target)
print(f'== debug before: rank {rank} has loss {loss.item()}')
req = None
if rank == 0:
tmp_loss = torch.zeros_like(loss).to(device)
for irank in range(1, size):
req = dist.irecv(tensor=tmp_loss, src=irank)
req.wait()
loss += tmp_loss
loss /= size
else:
req = dist.isend(tensor=loss, dst=0)
req.wait()
print(f'== debug after : rank {rank} has loss {loss.item()}')
# update loss to other rank
if rank == 0:
for irank in range(1, size):
req = dist.isend(tensor=loss, dst=irank)
req.wait()
else:
req = dist.irecv(tensor=loss, src=0)
req.wait()
print(f'== debug final : rank {rank} has loss {loss.item()}')
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
batch_time.append(time.time() - end)
losses.append(loss.item())
top1.append(acc1[0])
top5.append(acc5[0])
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.append(time.time() - end)
end = time.time()
# measure gpu situation
gpu_utilization = torch.cuda.utilization(device)
memory_allocated = torch.cuda.memory_allocated(device) / 1e9
memory_reserved = torch.cuda.memory_reserved(device) / 1e9
with open(args.csv_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([
epoch, i, batch_time[-1], losses[-1], top1[-1].item(), top5[-1].item(), throughput,
gpu_utilization, memory_allocated, memory_reserved
])
if rank == 0 and i % args.print_freq == 0:
print(f'round {i}({epoch}), batch_time: {batch_time[-1]:6.3f}, loss: {losses[-1]:.4e}, top1: {top1[-1]:6.2f}, top5: {top5[-1]:6.2f}, throughput: {throughputs[-1]:6.2f} samples/s')
with open(args.logpath, 'a') as file:
file.write(f'round {i}({epoch}), batch_time: {batch_time[-1]:6.3f}, loss: {losses[-1]:.4e}, top1: {top1[-1]:6.2f}, top5: {top5[-1]:6.2f}, throughput: {throughputs[-1]:6.2f} samples/s\n')
batch_time = torch.Tensor(batch_time)
losses = torch.Tensor(losses)
top1 = torch.Tensor(top1)
top5 = torch.Tensor(top5)
if rank == 0:
print(f'=== epoch {epoch} average: batch_time: {batch_time.mean():6.3f}, loss: {losses.mean():.4e}, top1: {top1.mean():6.2f}, top5: {top5.mean():6.2f}')
with open(args.logpath, 'a') as file:
file.write(f'=== epoch {epoch} average: batch_time: {batch_time.mean():6.3f}, loss: {losses.mean():.4e}, top1: {top1.mean():6.2f}, top5: {top5.mean():6.2f}\n')
def validate(val_loader, model, criterion, device, rank, size, args):
model.eval()
batch_time = []
losses = []
top1 = []
top5 = []
end = time.time()
with torch.no_grad():
for i, (images, target) in enumerate(val_loader):
if torch.cuda.is_available():
images = images.cuda(device, non_blocking=True)
target = target.cuda(device, non_blocking=True)
# compute output
output = model(images)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
batch_time.append(time.time() - end)
end = time.time()
losses.append(loss.item())
top1.append(acc1[0])
top5.append(acc5[0])
if rank == 0 and i % args.print_freq == 0:
print(f'\t\tround {i}, batch_time: {batch_time[-1]:6.3f}, loss: {losses[-1]:.4e}, top1: {top1[-1]:6.2f}, top5: {top5[-1]:6.2f}')
with open(args.logpath, 'a') as file:
file.write(f'\t\tround {i}, batch_time: {batch_time[-1]:6.3f}, loss: {losses[-1]:.4e}, top1: {top1[-1]:6.2f}, top5: {top5[-1]:6.2f}\n')
batch_time = torch.Tensor(batch_time)
losses = torch.Tensor(losses)
top1 = torch.Tensor(top1)
top5 = torch.Tensor(top5)
if rank == 0:
print(f'final average: batch_time: {batch_time.mean():6.3f}, loss: {losses.mean():.4e}, top1: {top1.mean():6.2f}, top5: {top5.mean():6.2f}')
with open(args.logpath, 'a') as file:
file.write(f'final average: batch_time: {batch_time.mean():6.3f}, loss: {losses.mean():.4e}, top1: {top1.mean():6.2f}, top5: {top5.mean():6.2f}\n')
return top1.mean()
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def run(rank, size, args):
print(f'=== start rank {rank}')
# logs
workpath = '/inspire/hdd/ws-f4d69b29-e0a5-44e6-bd92-acf4de9990f0/public-project/chenxinyan-240108120066/chenxinyan/engineering_hw3/outputs/' + args.name
if rank == 0:
if not os.path.exists(workpath):
os.makedirs(workpath)
logpath = workpath + '/log.txt'
if rank == 0:
with open(logpath, 'a') as file:
file.write(f"Experiment: {args.name}\n")
args.logpath = logpath
csv_path = workpath + f'/metrics_{rank}.csv'
with open(csv_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Epoch', 'Batch', 'Batch Time', 'Loss', 'Top1 Accuracy', 'Top5 Accuracy', 'Throughput (samples/s)', 'GPU Utilization (%)', 'Memory Allocated (GB)', 'Memory Reserved (GB)'])
args.csv_path = csv_path
# seed
random.seed(args.seed)
torch.manual_seed(args.seed)
# set device
device = torch.device('cuda:{}'.format(rank))
torch.cuda.set_device(device)
# create model
print(f"=> creating model resnet50, pretrained: {args.pretrained}")
model = resnet50(pretrained=args.pretrained)
model = model.to(device)
# define loss func, optim, scheduler
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
# # optionally resume from a checkpoint
best_acc1 = 0
# Data loading code
traindir = os.path.join(args.data, 'train')
valdir = os.path.join(args.data, 'val_label')
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
train_dataset = datasets.ImageFolder(
traindir,
transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]))
val_dataset = datasets.ImageFolder(
valdir,
transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, sampler=train_sampler,
num_workers=args.workers, pin_memory=True)
val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, sampler=val_sampler,
num_workers=args.workers, pin_memory=True)
if args.evaluate:
validate(val_loader, model, criterion, device, rank, size, args)
return
for epoch in range(args.epochs):
train_loader.sampler.set_epoch(epoch)
val_loader.sampler.set_epoch(epoch)
# train for one epoch
train(train_loader, model, criterion, optimizer, epoch, device, rank, size, args)
# evaluate on validation set
acc1 = validate(val_loader, model, criterion, device, rank, size, args)
scheduler.step()
# remember best acc@1 and save checkpoint
is_best = acc1 > best_acc1
best_acc1 = max(acc1, best_acc1)
if rank == 0:
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_acc1': best_acc1,
'optimizer' : optimizer.state_dict(),
'scheduler' : scheduler.state_dict()
}, is_best, workpath)
def init_process(rank, size, fn, args, backend='nccl'):
""" Initialize the distributed environment. """
os.environ['MASTER_ADDR'] = 'localhost' # '127.0.0.1'
os.environ['MASTER_PORT'] = '8077' # '8090'
os.environ['NCCL_DEBUG'] = 'INFO'
# os.environ['NCCL_ALGO'] = 'Ring' # 'Tree'
dist.init_process_group(backend, rank=rank, world_size=size)
fn(rank, size, args)
if __name__ == "__main__":
args = parser.parse_args()
size = 4
processes = []
mp.set_start_method("spawn")
for rank in range(size):
p = mp.Process(target=init_process, args=(rank, size, run, args))
p.start()
processes.append(p)
for p in processes:
p.join()