-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
144 lines (123 loc) · 5.67 KB
/
main.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
import os
import builtins
import argparse
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
import random
import torch.distributed as dist
from torch import nn, optim
from torch.utils.data import DataLoader, DistributedSampler
from net import Net
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--net', default='resnet18', type=str)
parser.add_argument('--lr', default=1e-3, type=float, help='learning rate')
parser.add_argument('--batch_size', default=4, type=int, help='batch size per GPU')
parser.add_argument('--gpu', default=None, type=int)
parser.add_argument('--start_epoch', default=0, type=int,
help='start epoch number (useful on restarts)')
parser.add_argument('--epochs', default=10, type=int, help='number of total epochs to run')
# DDP configs:
parser.add_argument('--world-size', default=-1, type=int,
help='number of nodes for distributed training')
parser.add_argument('--rank', default=-1, type=int,
help='node rank for distributed training')
parser.add_argument('--dist-url', default='env://', type=str,
help='url used to set up distributed training')
parser.add_argument('--dist-backend', default='nccl', type=str,
help='distributed backend')
parser.add_argument('--local_rank', default=-1, type=int,
help='local rank for distributed training')
args = parser.parse_args()
return args
def main(args):
# DDP setting
if "WORLD_SIZE" in os.environ:
args.world_size = int(os.environ["WORLD_SIZE"])
args.distributed = args.world_size > 1
ngpus_per_node = torch.cuda.device_count()
if args.distributed:
if args.local_rank != -1: # for torch.distributed.launch
args.rank = args.local_rank
args.gpu = args.local_rank
elif 'SLURM_PROCID' in os.environ: # for slurm scheduler
args.rank = int(os.environ['SLURM_PROCID'])
args.gpu = args.rank % torch.cuda.device_count()
dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
print(f"starting rank {dist.get_rank()}")
# suppress printing if not on master gpu
# if args.rank!=0:
# def print_pass(*args):
# pass
# builtins.print = print_pass
### model ###
model = Net()
if args.distributed:
# For multiprocessing distributed, DistributedDataParallel constructor
# should always set the single device scope, otherwise,
# DistributedDataParallel will use all available devices.
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model.cuda(args.gpu)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
model_without_ddp = model.module
else:
model.cuda()
model = torch.nn.parallel.DistributedDataParallel(model)
model_without_ddp = model.module
else:
raise NotImplementedError("Only DistributedDataParallel is supported.")
### optimizer ###
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=1e-5)
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
### data ###
train_sampler = DistributedSampler(trainset, shuffle=True)
train_loader = DataLoader(trainset, batch_size=args.batch_size,
num_workers=2, pin_memory=True, sampler=train_sampler)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
test_loader = DataLoader(testset, batch_size=args.batch_size,
shuffle=False, num_workers=2)
torch.backends.cudnn.benchmark = True
### loss ###
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9)
### main loop ###
for epoch in range(args.start_epoch, args.epochs):
np.random.seed(epoch)
random.seed(epoch)
# fix sampling seed such that each gpu gets different part of dataset
if args.distributed:
train_loader.sampler.set_epoch(epoch)
train_one_epoch(train_loader, model, criterion, optimizer, epoch, args)
def train_one_epoch(train_loader, model, criterion, optimizer, epoch, args):
# only one gpu is visible here, so you can send cpu data to gpu by
# input_data = input_data.cuda() as normal
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs = data[0].cuda()
labels = data[1].cuda()
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print(f'[epoch = {epoch + 1}, iteration = {i + 1:5d}, rank = {dist.get_rank()}] loss: {running_loss / 2000:.3f}')
running_loss = 0.0
if __name__ == '__main__':
args = parse_args()
main(args)