-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathm8d.py
416 lines (335 loc) · 11.1 KB
/
m8d.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Copyright 2024 Cisco Systems, Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
"""
m8d.py: This script demonstrates how to run a ResNet model on multiple worlds using PyTorch.
Summary:
- The script initializes a ResNet model on every worker.
- The leader sends an image to a worker for inference.
- The worker processes the image and sends the predicted class back to the leader.
Sample usage:
Single host: python m8d.py --num_workers 1 --backend gloo
Multi host: python m8d.py --num_workers 2 --backend nccl --multihost --addr 10.20.1.50 --rank 0
"""
#!/usr/bin/env python
import argparse
import asyncio
import atexit
import time
import torch
import torch.multiprocessing as mp
import torchvision.transforms as transforms
from multiworld.manager import WorldManager
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
from transformers import AutoModelForImageClassification
# Constants
CIFAR10_INPUT_SIZE = (1, 3, 32, 32)
# CIFAR10 class names
CIFAR10_CLASS_NAMES = [
"plane",
"car",
"bird",
"cat",
"deer",
"dog",
"frog",
"horse",
"ship",
"truck",
]
# leader rank
LEADER_RANK = 0
# Worker rank in every world is going to be 1 because we are creating 2 processes in every world
WORKER_RANK = 1
STARTING_PORT = 29500
WORLD_SIZE = 2
def index_to_class_name(index):
"""
Get class name from index.
Args:
index: Index of the class.
"""
return CIFAR10_CLASS_NAMES[index]
def load_cifar10(batch_size=1):
"""
Load CIFAR10 dataset.
Args:
batch_size: Batch size for the DataLoader.
Returns:
DataLoader instance
"""
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
cifar10_dataset = CIFAR10(
root="./data", train=False, download=True, transform=transform
)
cifar10_loader = DataLoader(cifar10_dataset, batch_size=batch_size, shuffle=True)
return cifar10_loader
async def dummy(world_name, rank, size, backend, world_communicator):
"""
Dummy function to be implemented later.
Args:
world_name: Name of the world.
rank: Rank of the process.
size: Number of processes.
backend: Backend used for communication.
"""
print(f"dummy function: world: {world_name}, my rank: {rank}, world size: {size}")
await asyncio.sleep(0)
async def run(world_name, rank, size, backend, world_communicator):
"""
Distributed function to be implemented later.
Args:
world_name: Name of the world.
rank: Rank of the process.
size: Number of processes.
backend: Backend used for communication.
world_communicator: World communicator
"""
world_idx = int(world_name[5:])
# Initialize ResNet18 model
model = AutoModelForImageClassification.from_pretrained(
"edadaltocg/resnet18_cifar10"
)
model.eval()
if backend == "nccl":
model = model.cuda(world_idx)
while True:
image_tensor = torch.zeros(CIFAR10_INPUT_SIZE)
image_tensor = (
image_tensor.to(f"cuda:{world_idx}") if backend == "nccl" else image_tensor
)
try:
await world_communicator.recv(image_tensor, LEADER_RANK, world_name)
except Exception as e:
print("RECV FAILED for RANK", rank)
print(f"Caught an except while receiving predicted class: {e}")
break
# Inference
with torch.no_grad():
output = model(image_tensor)
# Get the predicted class
_, predicted = torch.max(output.logits, 1)
print(f"Predicted : {predicted}, {predicted.shape}, {type(predicted)}")
# Send the predicted class back to the leader
try:
await world_communicator.send(predicted, LEADER_RANK, world_name)
except Exception as e:
print(f"Caught an except while sending image: {e}")
break
print(f"Predicted class: {predicted}")
world_manager = None
async def init_world(
world_name, rank, size, fn, backend="gloo", addr="127.0.0.1", port=-1
):
"""
Initialize the distributed environment.
Args:
world_name: Name of the world.
rank: Rank of the process.
size: Number of processes.
fn: Function to be executed.
backend: Backend to be used.
addr: Address of the leader process.
port: Port to be used.
"""
global world_manager
if world_manager is None:
# TODO: make WorldManager as singleton
world_manager = WorldManager()
world_idx = int(world_name[5:])
dev_str = f"cuda:{world_idx}" if backend == "nccl" else "cpu"
await world_manager.initialize_world(
world_name,
rank,
size,
backend=backend,
addr=addr,
port=port,
device=torch.device(dev_str),
)
await fn(world_name, rank, size, backend, world_manager.communicator)
def run_init_world(
world_name, rank, size, fn, backend="gloo", addr="127.0.0.1", port=-1
):
"""
Run the init_world function in a separate process.
Args:
world_name: Name of the world.
rank: Rank of the process.
size: Number of processes.
fn (function): Function to be executed.
backend: Backend to be used.
addr: Address of the leader process.
port: Port to be used.
"""
asyncio.run(init_world(world_name, rank, size, fn, backend, addr, port))
processes = []
async def create_world(world_name, world_size, addr, port, backend, fn1, fn2):
"""
Create a world with the given port and world name.
Args:
world_name: Name of the world.
world_size: Number of processes in the world.
addr: Address of the leader process.
port: Port number.
backend: Backend to be used.
fn1: Function to be executed in the world.
fn2: Function to be executed in the world leader.
Returns:
list: List of processes.
"""
global processes
for rank in range(world_size):
if rank == LEADER_RANK:
continue
p = mp.Process(
target=run_init_world,
args=(world_name, rank, world_size, fn1, backend, addr, port),
)
p.start()
print(p.pid)
processes.append(p)
# run leader late
await init_world(world_name, LEADER_RANK, world_size, fn2, backend, addr, port)
return processes
def cleanup():
"""Cleanup spawned processes."""
print("Cleaning up spwaned processes")
for p in processes:
p.terminate()
print("Cleaning up done")
async def run_leader(world_communicator, world_size, backend):
"""
Leader function to send images to workers for processing.
Args:
world_communicator: World communicator
world_size: Number of workers in the world
backend: Backend to use for distributed communication
"""
# Load CIFAR10 dataset
cifar10_loader = load_cifar10()
worker_idx = 1
for _, (image_tensor, _) in enumerate(cifar10_loader):
image_tensor = (
image_tensor.to(f"cuda:{LEADER_RANK}")
if backend == "nccl"
else image_tensor
)
# Keep trying for different workers until the image is sent
while True:
worker_idx += 1
if worker_idx > world_size:
worker_idx = 1
# Send the image to the worker
try:
await world_communicator.send(
image_tensor, WORKER_RANK, f"world{worker_idx}"
)
except Exception as e:
print(f"Caught an except while sending image: {e}")
continue
print(f"Sent image to worker{worker_idx} for processing")
# Receive the predicted class from the worker
predicted_class_tensor = torch.zeros(size=(1,), dtype=torch.int64)
predicted_class_tensor = (
predicted_class_tensor.to(f"cuda:{LEADER_RANK}")
if backend == "nccl"
else predicted_class_tensor
)
try:
await world_communicator.recv(
predicted_class_tensor, WORKER_RANK, f"world{worker_idx}"
)
except Exception as e:
print(f"Caught an except while receiving predicted class: {e}")
continue
print(
f"Predicted class: {index_to_class_name(predicted_class_tensor.item())}\n"
)
break
time.sleep(1)
async def single_host(args):
"""
Run the script on a single host.
Args:
args: Command line arguments.
"""
global processes
mp.set_start_method("spawn")
for world_idx in range(1, args.num_workers + 1):
pset = await create_world(
f"world{world_idx}",
WORLD_SIZE,
args.addr,
STARTING_PORT + world_idx,
args.backend,
run,
dummy,
)
processes += pset
await run_leader(world_manager.communicator, args.num_workers, args.backend)
for p in processes:
p.join()
async def multi_host(args):
"""
Run the script on multiple hosts.
Args:
args: Command line arguments.
"""
size = int(args.num_workers)
if args.rank == LEADER_RANK:
for world_idx in range(1, size + 1):
await init_world(
f"world{world_idx}",
LEADER_RANK,
WORLD_SIZE,
dummy,
args.backend,
args.addr,
STARTING_PORT + world_idx,
)
await run_leader(world_manager.communicator, size, args.backend)
else:
await init_world(
f"world{args.rank}",
1,
WORLD_SIZE,
run,
args.backend,
args.addr,
STARTING_PORT + args.rank,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--backend", default="nccl")
parser.add_argument("--addr", default="127.0.0.1")
parser.add_argument("--rank", type=int)
parser.add_argument("--num_workers", default=2, type=int)
parser.add_argument(
"--multihost", action=argparse.BooleanOptionalAction, default=False
)
args = parser.parse_args()
atexit.register(cleanup)
loop = asyncio.get_event_loop()
if not args.multihost:
loop.run_until_complete(single_host(args))
else:
loop.run_until_complete(multi_host(args))