Created
December 9, 2019 01:19
-
-
Save giacaglia/e4b16edda72ccc506dc788f7b76fe776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rank = args.nr * args.gpus + gpu | |
dist.init_process_group( | |
backend='nccl', | |
init_method='env://', | |
world_size=args.world_size, | |
rank=rank) | |
torch.manual_seed(0) | |
model = ConvNet() | |
torch.cuda.set_device(gpu) | |
model.cuda(gpu) | |
batch_size = 100 | |
# define loss function (criterion) and optimizer | |
criterion = nn.CrossEntropyLoss().cuda(gpu) | |
optimizer = torch.optim.SGD(model.parameters(), 1e-4) | |
# Wrap the model | |
############################################################## | |
model, optimizer = amp.initialize(model, optimizer, | |
opt_level='O1') | |
model = DDP(model) | |
############################################################## | |
# Data loading code | |
... | |
start = datetime.now() | |
total_step = len(train_loader) | |
for epoch in range(args.epochs): | |
for i, (images, labels) in enumerate(train_loader): | |
images = images.cuda(non_blocking=True) | |
labels = labels.cuda(non_blocking=True) | |
# Forward pass | |
outputs = model(images) | |
loss = criterion(outputs, labels) | |
# Backward and optimize | |
optimizer.zero_grad() | |
############################################################## | |
with amp.scale_loss(loss, optimizer) as scaled_loss: | |
scaled_loss.backward() | |
############################################################## | |
optimizer.step() | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment