-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy path20_mnist_ddp.py
313 lines (235 loc) · 8.97 KB
/
20_mnist_ddp.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- coding: utf-8 -*-
# (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved.
#
# Licensed under the MIT license. See LICENSE file in the project root for details.
"""aihwkit example 20: MNIST training with PyTorch Distributed Data Parallel (DDP).
MNIST training example based on the paper:
https://www.frontiersin.org/articles/10.3389/fnins.2016.00333/full
Uses learning rates of η = 0.01, 0.005, and 0.0025
for epochs 0–10, 11–20, and 21–30, respectively.
"""
# pylint: disable=invalid-name, too-many-locals, not-callable
import os
from time import time
# Imports from PyTorch.
from torch import (
device as torch_device,
cat,
zeros,
tensor,
float32,
int32,
max as torch_max,
load,
save,
)
from torch import nn
from torch.utils.data import DistributedSampler, DataLoader
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim.lr_scheduler import StepLR
from torchvision import datasets, transforms
# Imports from aihwkit.
from aihwkit.nn import AnalogLinear, AnalogLinearMapped, AnalogSequential
from aihwkit.optim import AnalogSGD
from aihwkit.simulator.configs import InferenceRPUConfig
from aihwkit.simulator.rpu_base import cuda
# Check device
DEVICE = torch_device("cuda" if cuda.is_compiled() else "cpu")
# Path where the datasets will be stored.
PATH_DATASET = os.path.join("data", "DATASET")
# Network definition.
INPUT_SIZE = 784
HIDDEN_SIZES = [256, 128]
OUTPUT_SIZE = 10
# Training parameters.
EPOCHS = 30
BATCH_SIZE = 64
# rpu config
RPU_CONFIG = InferenceRPUConfig()
# simple checkpointing
LOAD_FROM_CHECKPOINT = False
CHECKPOINT_FILE = "checkpoint.pt"
def init_process(rank, size, fn, backend="nccl"):
"""Initialize the distributed environment."""
print("init process: ", rank)
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29412"
dist.init_process_group(backend, rank=rank, world_size=size)
fn()
def cleanup():
"""Destroy distributed processes once they are complete."""
dist.destroy_process_group()
def load_images():
"""Load images for train from the torchvision datasets."""
rank = dist.get_rank()
size = dist.get_world_size()
transform = transforms.Compose([transforms.ToTensor()])
# Load the images.
train_set = datasets.MNIST(PATH_DATASET, download=True, train=True, transform=transform)
val_set = datasets.MNIST(PATH_DATASET, download=True, train=False, transform=transform)
train_sampler = DistributedSampler(
train_set, num_replicas=size, rank=rank, shuffle=True, seed=42
)
train_data = DataLoader(
train_set,
batch_size=BATCH_SIZE,
shuffle=False,
num_workers=size,
sampler=train_sampler,
pin_memory=True,
)
validation_data = DataLoader(
val_set, batch_size=BATCH_SIZE, shuffle=True, num_workers=size, pin_memory=True
)
return train_data, validation_data
def create_analog_network(input_size, hidden_sizes, output_size):
"""Create the neural network using analog and digital layers.
Args:
input_size (int): size of the Tensor at the input.
hidden_sizes (list): list of sizes of the hidden layers (2 layers).
output_size (int): size of the Tensor at the output.
Returns:
nn.Module: created analog model
"""
model = AnalogSequential(
AnalogLinear(input_size, hidden_sizes[0], True, rpu_config=RPU_CONFIG),
nn.Sigmoid(),
AnalogLinear(hidden_sizes[0], hidden_sizes[1], True, rpu_config=RPU_CONFIG),
nn.Sigmoid(),
AnalogLinearMapped(hidden_sizes[1], output_size, True, rpu_config=RPU_CONFIG),
nn.LogSoftmax(dim=1),
)
return model
def create_sgd_optimizer(model, opt_state_dict=None):
"""Create the analog-aware optimizer.
Args:
model (nn.Module): model to be trained
opt_state_dict (Dict): optimizer state dict from checkpoint
Returns:
nn.Module: optimizer
"""
optimizer = AnalogSGD(model.parameters(), lr=0.1, momentum=0.1)
if opt_state_dict is not None:
optimizer.load_state_dict(opt_state_dict)
return optimizer
def train(model, train_set, opt_state_dict=None):
"""Train the network.
Args:
model (nn.Module): model to be trained.
train_set (DataLoader): dataset of elements to use as input for training.
opt_state_dict (Dict): optimizer state dict from checkpoint
"""
rank = dist.get_rank()
size = dist.get_world_size()
device = torch_device("cuda", rank)
classifier = nn.NLLLoss()
optimizer = create_sgd_optimizer(model, opt_state_dict)
scheduler = StepLR(optimizer, step_size=10, gamma=0.5)
time_init = time()
total_time = [zeros(1, dtype=float32).to(device) for _ in range(size)]
for epoch_number in range(EPOCHS):
total_loss = zeros(1, dtype=float32).to(device)
total_images = zeros(1, dtype=int32).to(device)
for images, labels in train_set:
images = images.to(device)
labels = labels.to(device)
# Flatten MNIST images into a 784 vector.
images = images.view(images.shape[0], -1)
optimizer.zero_grad()
# Add training Tensor to the model (input).
output = model(images)
loss = classifier(output, labels)
# Run training (backward propagation).
loss.backward()
# Optimize weights.
optimizer.step()
total_images += labels.size(0)
total_loss += loss.item() * labels.size(0)
dist.all_reduce(total_loss, op=dist.ReduceOp.SUM)
dist.all_reduce(total_images, op=dist.ReduceOp.SUM)
if rank == 0:
train_loss = total_loss.item() / total_images.item()
print("Epoch {} - Training loss: {:.16f}".format(epoch_number, train_loss))
# save checkpoint
dist.barrier()
if rank == 0:
print(f"saving checkpoint to '{CHECKPOINT_FILE}'")
save(
{"model": model.module.state_dict(), "optimizer": optimizer.state_dict()},
CHECKPOINT_FILE,
)
# Decay learning rate if needed.
scheduler.step()
dist.all_gather(total_time, tensor(time() - time_init).to(device))
if rank == 0:
avg_train_time = cat(total_time, 0).mean()
print("\nAverage Training Time (s) = {}".format(avg_train_time))
def test_evaluation(model, val_set):
"""Test trained network
Args:
model (nn.Model): Trained model to be evaluated
val_set (DataLoader): Validation set to perform the evaluation
"""
rank = dist.get_rank()
size = dist.get_world_size()
device = torch_device("cuda", rank)
# Setup counter of images predicted to 0.
predicted_ok = 0
total_images = 0
# make list to collect test ccuracies for each gpu
acc_list = [zeros(1, dtype=float32).to(device) for _ in range(size)]
model.eval()
for images, labels in val_set:
# Predict image.
images = images.to(device)
labels = labels.to(device)
images = images.view(images.shape[0], -1)
pred = model(images)
_, predicted = torch_max(pred.data, 1)
total_images += labels.size(0)
predicted_ok += (predicted == labels).sum().item()
dist.all_gather(acc_list, tensor(predicted_ok / total_images).to(device))
if rank == 0:
acc = cat(acc_list, 0).mean()
print("\nNumber Of Images Tested = {}".format(total_images))
print("Model Accuracy = {}".format(acc))
def main():
"""Train a PyTorch analog model with the MNIST dataset."""
rank = dist.get_rank()
device = torch_device("cuda", rank)
# Load datasets.
train_dataset, validation_dataset = load_images()
# Prepare the model.
model = create_analog_network(INPUT_SIZE, HIDDEN_SIZES, OUTPUT_SIZE)
opt_state_dict = None
model.prepare_for_ddp()
model.to(device)
if LOAD_FROM_CHECKPOINT and os.path.exists(CHECKPOINT_FILE):
print(f"rank {rank}: load from checkpoint.")
checkpoint = load(CHECKPOINT_FILE, map_location=device)
model.load_state_dict(checkpoint["model"], load_rpu_config=True)
opt_state_dict = checkpoint["optimizer"]
if rank == 0:
print(model)
# enable parallel training
model = DDP(model, device_ids=[rank], output_device=rank)
# Train the model.
train(model, train_dataset, opt_state_dict)
# Evaluate the trained model.
test_evaluation(model, validation_dataset)
cleanup()
if __name__ == "__main__":
# Execute only if run as the entry point into the program
world_size = 2
print("Device count: ", world_size)
processes = []
ctx = mp.get_context("spawn")
for world_rank in range(world_size):
print("Process: ", world_rank)
p = ctx.Process(target=init_process, args=(world_rank, world_size, main))
p.start()
processes.append(p)
for p in processes:
p.join()