-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomScheduleGenerator.py
More file actions
353 lines (304 loc) · 13.3 KB
/
randomScheduleGenerator.py
File metadata and controls
353 lines (304 loc) · 13.3 KB
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
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torchvision.models as models
import torch.nn as nn
from torch.cuda.amp import autocast
from torch.cuda.amp import GradScaler
import os
import time
import copy
from multiprocessing import Process, log_to_stderr
import csv
from gossip_module.utils import flatten_tensors, flatten_tensors_grad, unflatten_tensors, unflatten_tensors_grad
from fsdp_custom import FullyShardedDataParallel as FSDP
#from fairscale.nn.data_parallel import FullyShardedDataParallel as FSDP
from dp_custom import DataParallel_Custom as DP
from auto_wrap_custom import enable_wrap, auto_wrap, wrap
from torchvision.models.resnet import BasicBlock, Bottleneck, ResNet
from torch_scheduler import ShardScheduler
import threading
import argparse
import timeit
import numpy as np
import random
from torchsummary import summary
def compareTensors(a_list, b_list, model_summary):
if len(a_list) != len(b_list) :
return False
for a in a_list :
equal_flag = False
for b in b_list :
a_summary = model_summary[a]
b_summary = model_summary[b]
equal_flag = equal_flag or (a_summary["name"] == b_summary["name"])
if not equal_flag:
return False
return True
def checkScheduledList(target, scheduled_list, model_summary):
for comm in scheduled_list :
if compareTensors(comm["params"], target["params"], model_summary):
if target["comm_type"] == comm["comm_type"] :
print("====================")
print(comm["type"])
print(comm["comm_type"])
for param in comm["params"] :
param_summary = model_summary[param]
print(param_summary["name"])#
print(param.shape)
print("--------------------")
print(target["type"])
print(target["comm_type"])
for param in target["params"] :
param_summary = model_summary[param]
print(param_summary["name"])#
print(param.shape)
print("====================")
return True
return False
def check_scheduled_comm_type(param_chains, p, check_scheduled, comm_type):
if(comm_type == "AG_FSDP"):
param_ends = False
current_tensor = p
current = param_chains[current_tensor]["current"]
while not param_ends :
hash_key = hash(current_tensor) + hash("AG")
if(check_scheduled.get(hash_key, None) == None):
return False
current = param_chains[current_tensor]["previous"]
current_tensor = param_chains[current_tensor]["previous_tensor"]
if(current == "start"):
param_ends = True
return True
elif(comm_type == "RS") :
param_ends = False
current_tensor = p
current = param_chains[current_tensor]["current"]
while not param_ends :
hash_key = hash(param_chains[current_tensor]["current_tensor"]) + hash("AG_FSDP")
if(check_scheduled.get(hash_key, None) == None):
return False
current = param_chains[current_tensor]["next"]
current_tensor = param_chains[current_tensor]["next_tensor"]
if(current == "end"):
param_ends = True
return True
def validComm(target, scheduled_list, check_scheduled, model_summary, comm, model, param_chains):
if checkScheduledList(target, scheduled_list, model_summary) :
#print("duplicated")
return False
else :
if target["comm_type"] == "AG" :
return True
elif target["comm_type"] == "AG_FSDP" :
for p in comm["params"]:
hash_key = hash(p) + hash("AG")
if not check_scheduled_comm_type(param_chains, p, check_scheduled, "AG_FSDP"):
return False
return True
elif target["comm_type"] == "RS" :
for p in comm["params"]:
hash_key = hash(p) + hash("AG_FSDP")
if not check_scheduled_comm_type(param_chains, p, check_scheduled, "RS"):
return False
return True
def randomSchedule(model):
#summary model parameters
model_summary = {}
tensor_threshold = 20000
for n, p in model.named_parameters():
layer_info = {}
layer_info["numel"] = p.numel()
layer_info["partitions"] = (p.numel() // tensor_threshold) + 1
layer_info["name"] = n
model_summary[p] = copy.deepcopy(layer_info)
#FSDP ==> ReduceScatter AllGather AllGather_FSDP,
keywords = ["AG", "AG_FSDP", "RS"]
scheduled_info_dict = {}
random.seed(50)
for keyword in keywords :
for n,p in model.named_parameters():
key = hash(p) + hash(keyword)
scheduled_info = copy.deepcopy(model_summary[p])
#unscheduled_info["param"] = p
#unscheduled_info["dones"] = 0
#unscheduled_info["fusion"] = False
#select scheduling
#scheduling_methods = ["TF", "TP", "layer"]
scheduling_methods = ["TF", "layer"]
scheduling_method = random.choice(scheduling_methods)
scheduled_info["scheduled"] = 0
if(scheduling_method == "TF"):
scheduled_info["TF"] = True
scheduled_info["TP"] = False
elif(scheduling_method == "TP"):
scheduled_info["TF"] = False
scheduled_info["TP"] = True
partition_range = []
while scheduled_info["dones"] < scheduled_info["partitions"]:
rand_max = scheduled_info["partitions"]+1 - scheduled_info["dones"]
rand_min = 1
partition_num = random.randrange(rand_min, rand_max)
partition_range.append(partition_num)
scheduled_info["dones"] += partition_num
scheduled_info["partitions"] = copy.deepcopy(partition_range)
else :
scheduled_info["TF"] = False
scheduled_info["TP"] = False
scheduled_info_dict[key] = copy.deepcopy(scheduled_info)
#build communication lists
communications = []
#AG, RS, AG_FSDP
#communication info
#scheduling type : fusion, partitioning, layer
#communicated parameters : parameter list
#if scheduling type is partitioning ==> start index and end index
communication_info = {}
for keyword in keywords :
communication_info = dict()
fusioned_parameter = []
fusioned_parameter_size = 0
scheduling_info = {}
for n,p in model.named_parameters():
key = hash(p) + hash(keyword)
scheduling_info = scheduled_info_dict[key]
if(scheduling_info["TF"] == True):
fusioned_parameter.append(p)
fusioned_parameter_size += p.numel()
#elif(scheduling_info["TP"] == True):
# if(fusioned_parameter_size > tensor_threshold )
# for() : #add tensor partition cases
# else :
# fusioned_parameter.append(p)
# fusioned_parameter_size += p.numel()
elif((scheduling_info["TF"] == False ) and (scheduling_info["TP"] == False)):
if(fusioned_parameter_size > 0):
#add tensor fusion cases
communication_info["type"] = "TF"
#fusioned_parameter.append(p)
#fusioned_parameter_size += p.numel()
else :
communication_info["type"] = "layer"
fusioned_parameter.append(p)
fusioned_parameter_size += p.numel()
communication_info["comm_type"] = keyword
communication_info["params"] = copy.copy(fusioned_parameter)
#print(model_summary[communication_info["params"][0]]["name"])
communication_info["numel"] = fusioned_parameter_size
#print(fusioned_parameter_size)
fusioned_parameter = []
fusioned_parameter_size = 0
communications.append(copy.copy(communication_info))
#print(model_summary[communications[0]["params"][0]]["name"])
#communications.append(communication_info)
#add direct communication cases
if(scheduling_info["TF"] == True):
if(len(fusioned_parameter) == 1):
communication_info["type"] = "layer"
else :
communication_info["type"] = "TF"
communication_info["comm_type"] = keyword
communication_info["params"] = copy.copy(fusioned_parameter)
#print(model_summary[communication_info["params"][0]]["name"])
communication_info["numel"] = fusioned_parameter_size
#print(fusioned_parameter_size)
fusioned_parameter = []
fusioned_parameter_size = 0
communications.append(copy.copy(communication_info))
#with open("foo.txt", "w") as f:
# for comm in communications :
# f.write("--------------------\n")
# f.write(comm["type"]+"\n")
# f.write(comm["comm_type"]+ "\n")
# for param in comm["params"] :
# param_summary = model_summary[param]
# f.write(param_summary["name"]+"\n")#
#scheduling communication lists
scheduled_comms = []
check_scheduled = {}
index_list = [x for x in range(0, len(communications))]
#print(communications)
#with open("foo.txt", "w") as f:
param_list = list(model.parameters())
param_chains = {}
previous = -1
count = 0
previous_access = "start"
previous_previous_access = 0
for p in model.parameters() :
if(count == 1):
chain = {}
chain["previous"] = previous_previous_access
chain["current"] = model_summary[previous_access]["name"]
chain["next"] = model_summary[p]["name"]
chain["previous_tensor"] = previous_previous_access
chain["current_tensor"] = previous_access
chain["next_tensor"] = p
param_chains[previous_access] = copy.copy(chain)
elif(count > 1):
chain = {}
chain["previous"] = model_summary[previous_previous_access]["name"]
chain["current"] = model_summary[previous_access]["name"]
chain["next"] = model_summary[p]["name"]
chain["previous_tensor"] = previous_previous_access
chain["current_tensor"] = previous_access
chain["next_tensor"] = p
param_chains[previous_access] = copy.copy(chain)
previous_previous_access = previous_access
previous_access = p
count += 1
chain = {}
chain["previous"] = model_summary[previous_previous_access]["name"]
chain["current"] = model_summary[previous_access]["name"]
chain["next"] = "end"
chain["previous_tensor"] = previous_previous_access
chain["current_tensor"] = previous_access
chain["next_tensor"] = "end"
param_chains[p] = copy.copy(chain)
while len(scheduled_comms) < len(communications):
comm_idx = random.choice(index_list)
comm = communications[comm_idx]
print(len(scheduled_comms))
if(validComm(comm, scheduled_comms, check_scheduled, model_summary, comm, model, param_chains)):
#f.write(str(comm_idx)+"\n")
index_list.remove(comm_idx)
scheduled_comms.append(copy.copy(comm))
for p in comm["params"] :
hash_key = hash(p) + hash(comm["comm_type"])
#print(comm["comm_type"])
#print(hash("AG"))
check_scheduled[hash_key] = 1
#print(len(scheduled_comms))
#print(len(communications))
#summary schedule result
#for p in model.parameters():
# #print(p)
# print(model_summary[p]["name"])
#with open("foo.txt", "w") as f:
# for comm in scheduled_comms :
# f.write("--------------------\n")
# f.write(comm["type"]+ "\n")
# f.write(comm["comm_type"] + "\n")
# for param in comm["params"] :
# param_summary = model_summary[param]
# f.write(param_summary["name"] + "\n")#
return scheduled_comms
if __name__ == '__main__':
model = ResNet(Bottleneck, [3, 8, 36, 3]) #it means "resnet18 model"
scheduled_comms = randomSchedule(model)
model_summary = {}
tensor_threshold = 20000
for n, p in model.named_parameters():
layer_info = {}
layer_info["numel"] = p.numel()
layer_info["partitions"] = (p.numel() // tensor_threshold) + 1
layer_info["name"] = n
model_summary[p] = copy.deepcopy(layer_info)
with open("foo.txt", "w") as f:
for comm in scheduled_comms :
f.write("--------------------\n")
f.write(comm["type"]+ "\n")
f.write(comm["comm_type"] + "\n")
for param in comm["params"] :
param_summary = model_summary[param]
f.write(param_summary["name"] + "\n")#