Created
April 8, 2020 10:27
-
-
Save ptrblck/52736ec7a8656e7ad4914ccd6d4eb35a 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
import torch | |
import torch.nn as nn | |
from torch.nn.parallel import DistributedDataParallel as DDP | |
from apex.parallel import SyncBatchNorm as ApexSyncBatchNorm | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--local_rank', type=int, default=0) | |
parser.add_argument('--apex', action='store_true') | |
args = parser.parse_args() | |
torch.manual_seed(2809) | |
# Setup DDP | |
torch.cuda.set_device(args.local_rank) | |
device = torch.device('cuda:{}'.format(args.local_rank)) | |
torch.distributed.init_process_group( | |
'nccl', | |
init_method='env://', | |
rank=args.local_rank, | |
) | |
# Setup model | |
if args.apex: | |
model = nn.Sequential( | |
nn.Conv2d(3, 6, 3, 1, 1), | |
ApexSyncBatchNorm(6) | |
) | |
else: | |
model = nn.Sequential( | |
nn.Conv2d(3, 6, 3, 1, 1), | |
nn.SyncBatchNorm(6) | |
) | |
# Setup reference model | |
model_reference = nn.Sequential( | |
nn.Conv2d(3, 6, 3, 1, 1), | |
nn.BatchNorm2d(6) | |
) | |
with torch.no_grad(): | |
model_reference[0].weight.copy_(model[0].weight) | |
model_reference[0].bias.copy_(model[0].bias) | |
model_reference.to(device) | |
# Setup SyncBN | |
#if not args.apex: | |
# model = nn.SyncBatchNorm.convert_sync_batchnorm(model) | |
model = model.to(device) | |
model = DDP(model, device_ids=[args.local_rank], output_device=args.local_rank) | |
# Create random data | |
if args.local_rank == 0: | |
data = torch.randn(16, 3, 24, 24, device=device) * 100 | |
else: | |
data = torch.randn(8, 3, 24, 24, device=device) | |
print('Input.sum() {}, .mean() {}, .std() {}, .min() {}, .max() {}, device {}'.format( | |
data.sum(), data.mean(), data.std(), data.min(), data.max(), data.device)) | |
# DDP forward/backward | |
output = model(data) | |
print('DDP output.sum() {}, .mean() {}, .std() {}, .min() {}, .max() {}, device {}'.format( | |
output.sum(), output.mean(), output.std(), output.min(), output.max(), output.device)) | |
output.sum().backward() | |
# Reference forward/backward | |
output_reference = model_reference(data) | |
print('Reference output.sum() {}, .mean() {}, .std() {}, .min() {}, .max() {}, device {}'.format( | |
output_reference.sum(), output_reference.mean(), output_reference.std(), output_reference.min(), | |
output_reference.max(), output_reference.device)) | |
output_reference.sum().backward() | |
# Print stats | |
print('DDP stats ', model.module[1].running_mean, model.module[1].running_var) | |
print('Reference stats ', model_reference[1].running_mean, model_reference[1].running_var) | |
print('DDP grads ', model.module[0].weight.grad.abs().sum()) | |
print('Reference grads ', model_reference[0].weight.grad.abs().sum()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Vanilla output
Apex output