|
| 1 | +def train_func(): |
| 2 | + import os |
| 3 | + import torch |
| 4 | + import torch.distributed as dist |
| 5 | + import torch.nn as nn |
| 6 | + import torch.optim as optim |
| 7 | + from torchvision import datasets, transforms |
| 8 | + from torch.utils.data import DataLoader, DistributedSampler |
| 9 | + |
| 10 | + # Initialize distributed process group |
| 11 | + dist.init_process_group(backend="nccl" if torch.cuda.is_available() else "gloo") |
| 12 | + rank = dist.get_rank() |
| 13 | + world_size = dist.get_world_size() |
| 14 | + local_rank = int(os.getenv("LOCAL_RANK", 0)) |
| 15 | + torch.cuda.set_device(local_rank) |
| 16 | + |
| 17 | + # Configuration |
| 18 | + batch_size = 64 |
| 19 | + epochs = 5 |
| 20 | + learning_rate = 0.01 |
| 21 | + |
| 22 | + # Dataset and DataLoader |
| 23 | + transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) |
| 24 | + train_dataset = datasets.MNIST(root="/tmp/datasets/mnist", train=True, download=True, transform=transform) |
| 25 | + train_sampler = DistributedSampler(train_dataset, num_replicas=world_size, rank=rank) |
| 26 | + train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, sampler=train_sampler) |
| 27 | + |
| 28 | + # Model, Loss, and Optimizer |
| 29 | + model = nn.Sequential( |
| 30 | + nn.Flatten(), |
| 31 | + nn.Linear(28 * 28, 128), |
| 32 | + nn.ReLU(), |
| 33 | + nn.Linear(128, 10) |
| 34 | + ).cuda(local_rank) |
| 35 | + |
| 36 | + model = nn.parallel.DistributedDataParallel(model, device_ids=[local_rank]) |
| 37 | + criterion = nn.CrossEntropyLoss().cuda(local_rank) |
| 38 | + optimizer = optim.SGD(model.parameters(), lr=learning_rate) |
| 39 | + |
| 40 | + # Training loop |
| 41 | + for epoch in range(epochs): |
| 42 | + model.train() |
| 43 | + epoch_loss = 0 |
| 44 | + for batch_idx, (data, target) in enumerate(train_loader): |
| 45 | + data, target = data.cuda(local_rank, non_blocking=True), target.cuda(local_rank, non_blocking=True) |
| 46 | + |
| 47 | + optimizer.zero_grad() |
| 48 | + output = model(data) |
| 49 | + loss = criterion(output, target) |
| 50 | + loss.backward() |
| 51 | + optimizer.step() |
| 52 | + |
| 53 | + epoch_loss += loss.item() |
| 54 | + |
| 55 | + # Log epoch stats |
| 56 | + print(f"Rank {rank} | Epoch {epoch + 1}/{epochs} | Loss: {epoch_loss / len(train_loader)}") |
| 57 | + |
| 58 | + # Cleanup |
| 59 | + dist.destroy_process_group() |
| 60 | + |
| 61 | +def train_func_2(): |
| 62 | + import os |
| 63 | + import torch |
| 64 | + import torch.nn.functional as F |
| 65 | + from torch.utils.data import DistributedSampler |
| 66 | + from torchvision import datasets, transforms |
| 67 | + import torch.distributed as dist |
| 68 | + |
| 69 | + # [1] Setup PyTorch DDP. Distributed environment will be set automatically by Training Operator. |
| 70 | + dist.init_process_group(backend="nccl" if torch.cuda.is_available() else "gloo") |
| 71 | + Distributor = torch.nn.parallel.DistributedDataParallel |
| 72 | + local_rank = int(os.getenv("LOCAL_RANK", 0)) |
| 73 | + print( |
| 74 | + "Distributed Training for WORLD_SIZE: {}, RANK: {}, LOCAL_RANK: {}".format( |
| 75 | + dist.get_world_size(), |
| 76 | + dist.get_rank(), |
| 77 | + local_rank, |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + # [2] Create PyTorch CNN Model. |
| 82 | + class Net(torch.nn.Module): |
| 83 | + def __init__(self): |
| 84 | + super(Net, self).__init__() |
| 85 | + self.conv1 = torch.nn.Conv2d(1, 20, 5, 1) |
| 86 | + self.conv2 = torch.nn.Conv2d(20, 50, 5, 1) |
| 87 | + self.fc1 = torch.nn.Linear(4 * 4 * 50, 500) |
| 88 | + self.fc2 = torch.nn.Linear(500, 10) |
| 89 | + |
| 90 | + def forward(self, x): |
| 91 | + x = F.relu(self.conv1(x)) |
| 92 | + x = F.max_pool2d(x, 2, 2) |
| 93 | + x = F.relu(self.conv2(x)) |
| 94 | + x = F.max_pool2d(x, 2, 2) |
| 95 | + x = x.view(-1, 4 * 4 * 50) |
| 96 | + x = F.relu(self.fc1(x)) |
| 97 | + x = self.fc2(x) |
| 98 | + return F.log_softmax(x, dim=1) |
| 99 | + |
| 100 | + # [3] Attach model to the correct GPU device and distributor. |
| 101 | + device = torch.device(f"cuda:{local_rank}") |
| 102 | + model = Net().to(device) |
| 103 | + model = Distributor(model) |
| 104 | + optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.5) |
| 105 | + |
| 106 | + # [4] Setup FashionMNIST dataloader and distribute data across PyTorchJob workers. |
| 107 | + dataset = datasets.FashionMNIST( |
| 108 | + "./data", |
| 109 | + download=True, |
| 110 | + train=True, |
| 111 | + transform=transforms.Compose([transforms.ToTensor()]), |
| 112 | + ) |
| 113 | + train_loader = torch.utils.data.DataLoader( |
| 114 | + dataset=dataset, |
| 115 | + batch_size=128, |
| 116 | + sampler=DistributedSampler(dataset), |
| 117 | + ) |
| 118 | + |
| 119 | + # [5] Start model Training. |
| 120 | + for epoch in range(3): |
| 121 | + for batch_idx, (data, target) in enumerate(train_loader): |
| 122 | + # Attach Tensors to the device. |
| 123 | + data = data.to(device) |
| 124 | + target = target.to(device) |
| 125 | + |
| 126 | + optimizer.zero_grad() |
| 127 | + output = model(data) |
| 128 | + loss = F.nll_loss(output, target) |
| 129 | + loss.backward() |
| 130 | + optimizer.step() |
| 131 | + if batch_idx % 10 == 0 and dist.get_rank() == 0: |
| 132 | + print( |
| 133 | + "Train Epoch: {} [{}/{} ({:.0f}%)]\tloss={:.4f}".format( |
| 134 | + epoch, |
| 135 | + batch_idx * len(data), |
| 136 | + len(train_loader.dataset), |
| 137 | + 100.0 * batch_idx / len(train_loader), |
| 138 | + loss.item(), |
| 139 | + ) |
| 140 | + ) |
0 commit comments