|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +import torch |
| 6 | +import torch.distributed as dist |
| 7 | + |
| 8 | +import torchvision |
| 9 | +import torchvision.transforms as transforms |
| 10 | +from torchvision.models import AlexNet |
| 11 | +from torchvision.models import vgg19 |
| 12 | + |
| 13 | +import deepspeed |
| 14 | +from deepspeed.pipe import PipelineModule |
| 15 | +from deepspeed.utils import RepeatingLoader |
| 16 | + |
| 17 | + |
| 18 | +def cifar_trainset(local_rank, dl_path='/tmp/cifar10-data'): |
| 19 | + transform = transforms.Compose([ |
| 20 | + transforms.Resize(256), |
| 21 | + transforms.CenterCrop(224), |
| 22 | + transforms.ToTensor(), |
| 23 | + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), |
| 24 | + ]) |
| 25 | + |
| 26 | + # Ensure only one rank downloads. |
| 27 | + # Note: if the download path is not on a shared filesytem, remove the semaphore |
| 28 | + # and switch to args.local_rank |
| 29 | + dist.barrier() |
| 30 | + if local_rank != 0: |
| 31 | + dist.barrier() |
| 32 | + trainset = torchvision.datasets.CIFAR10(root=dl_path, |
| 33 | + train=True, |
| 34 | + download=True, |
| 35 | + transform=transform) |
| 36 | + if local_rank == 0: |
| 37 | + dist.barrier() |
| 38 | + return trainset |
| 39 | + |
| 40 | + |
| 41 | +def get_args(): |
| 42 | + parser = argparse.ArgumentParser(description='CIFAR') |
| 43 | + parser.add_argument('--local_rank', |
| 44 | + type=int, |
| 45 | + default=-1, |
| 46 | + help='local rank passed from distributed launcher') |
| 47 | + parser.add_argument('-s', |
| 48 | + '--steps', |
| 49 | + type=int, |
| 50 | + default=100, |
| 51 | + help='quit after this many steps') |
| 52 | + parser.add_argument('-p', |
| 53 | + '--pipeline-parallel-size', |
| 54 | + type=int, |
| 55 | + default=2, |
| 56 | + help='pipeline parallelism') |
| 57 | + parser.add_argument('--backend', |
| 58 | + type=str, |
| 59 | + default='nccl', |
| 60 | + help='distributed backend') |
| 61 | + parser.add_argument('--seed', type=int, default=1138, help='PRNG seed') |
| 62 | + parser = deepspeed.add_config_arguments(parser) |
| 63 | + args = parser.parse_args() |
| 64 | + return args |
| 65 | + |
| 66 | + |
| 67 | +def train_base(args): |
| 68 | + torch.manual_seed(args.seed) |
| 69 | + |
| 70 | + # VGG also works :-) |
| 71 | + #net = vgg19(num_classes=10) |
| 72 | + net = AlexNet(num_classes=10) |
| 73 | + |
| 74 | + trainset = cifar_trainset(args.local_rank) |
| 75 | + |
| 76 | + engine, _, dataloader, __ = deepspeed.initialize( |
| 77 | + args=args, |
| 78 | + model=net, |
| 79 | + model_parameters=[p for p in net.parameters() if p.requires_grad], |
| 80 | + training_data=trainset) |
| 81 | + |
| 82 | + dataloader = RepeatingLoader(dataloader) |
| 83 | + data_iter = iter(dataloader) |
| 84 | + |
| 85 | + rank = dist.get_rank() |
| 86 | + gas = engine.gradient_accumulation_steps() |
| 87 | + |
| 88 | + criterion = torch.nn.CrossEntropyLoss() |
| 89 | + |
| 90 | + total_steps = args.steps * engine.gradient_accumulation_steps() |
| 91 | + step = 0 |
| 92 | + for micro_step in range(total_steps): |
| 93 | + batch = next(data_iter) |
| 94 | + inputs = batch[0].to(engine.device) |
| 95 | + labels = batch[1].to(engine.device) |
| 96 | + |
| 97 | + outputs = engine(inputs) |
| 98 | + loss = criterion(outputs, labels) |
| 99 | + engine.backward(loss) |
| 100 | + engine.step() |
| 101 | + |
| 102 | + if micro_step % engine.gradient_accumulation_steps() == 0: |
| 103 | + step += 1 |
| 104 | + if rank == 0 and (step % 10 == 0): |
| 105 | + print(f'step: {step:3d} / {args.steps:3d} loss: {loss}') |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +def join_layers(vision_model): |
| 110 | + layers = [ |
| 111 | + *vision_model.features, |
| 112 | + vision_model.avgpool, |
| 113 | + lambda x: torch.flatten(x, 1), |
| 114 | + *vision_model.classifier, |
| 115 | + ] |
| 116 | + return layers |
| 117 | + |
| 118 | + |
| 119 | +def train_pipe(args, part='parameters'): |
| 120 | + torch.manual_seed(args.seed) |
| 121 | + deepspeed.runtime.utils.set_random_seed(args.seed) |
| 122 | + |
| 123 | + # |
| 124 | + # Build the model |
| 125 | + # |
| 126 | + |
| 127 | + # VGG also works :-) |
| 128 | + #net = vgg19(num_classes=10) |
| 129 | + net = AlexNet(num_classes=10) |
| 130 | + net = PipelineModule(layers=join_layers(net), |
| 131 | + loss_fn=torch.nn.CrossEntropyLoss(), |
| 132 | + num_stages=args.pipeline_parallel_size, |
| 133 | + partition_method=part, |
| 134 | + activation_checkpoint_interval=0) |
| 135 | + |
| 136 | + trainset = cifar_trainset(args.local_rank) |
| 137 | + |
| 138 | + engine, _, _, _ = deepspeed.initialize( |
| 139 | + args=args, |
| 140 | + model=net, |
| 141 | + model_parameters=[p for p in net.parameters() if p.requires_grad], |
| 142 | + training_data=trainset) |
| 143 | + |
| 144 | + for step in range(args.steps): |
| 145 | + loss = engine.train_batch() |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == '__main__': |
| 149 | + args = get_args() |
| 150 | + |
| 151 | + torch.cuda.set_device(args.local_rank) |
| 152 | + dist.init_process_group(backend=args.backend) |
| 153 | + |
| 154 | + if args.pipeline_parallel_size == 0: |
| 155 | + train_base(args) |
| 156 | + else: |
| 157 | + train_pipe(args) |
0 commit comments