forked from DeepPoolML/DeepPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobDescription.py
439 lines (371 loc) · 17.3 KB
/
jobDescription.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# Copyright (c) 2021 MIT
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import json
import torch
import io
import os
from os.path import exists
from typing import Optional, IO, List, Any
from collections import defaultdict
import copy
import sys
class TensorProperties:
def __init__(self, tensor: torch.Tensor = None):
self.props = {}
if tensor is not None:
self.props["tensor_shape"] = tuple(tensor.shape)
self.props["dtype"] = str(tensor.dtype)
def __repr__(self):
return json.dumps(self.props, sort_keys=True)
def __str__(self):
return self.__repr__()
def fromStr(self, jstr):
self.props = json.loads(jstr)
def tensor_shape(self):
return tuple(self.props["tensor_shape"])
def dtype(self):
return eval(self.props["dtype"])
def genRand(self, batchsize, device):
iSize = (batchsize,) + self.tensor_shape()
return torch.zeros(iSize, dtype=self.dtype()).to(device)
class Layer:
def __init__(self, module, name: str, params: tuple, prevLayers: list):
self.id = None # Assigned later by calling printAllLayers.
self.name = name
self.params = params
self.prevLayers = prevLayers
for prevLayer in prevLayers or []:
prevLayer.nextLayers.append(self)
self.nextLayers = []
self.module = module
self.jit_module = None
self.moduleSavedLocation = None
self.losslayer = ""
# self.inputDim = (0, 0, 0) # (Channel, Width, Height) for 2d convolution
# self.outputDim = (0, 0, 0) # (Channel, Width, Height)
self.must_trace = False
def setInputShapes(self, list_of_tensors: List[torch.Tensor]):
self.initial_inputs = []
assert not self.prevLayers
for t in list_of_tensors:
self.initial_inputs.append(TensorProperties(t))
def getInputShapes(self) -> List[TensorProperties]:
if not self.prevLayers:
return self.initial_inputs
shapes = []
for layer in self.prevLayers:
shapes.append(layer.getOutputShape())
return shapes
def getOutputShape(self) -> TensorProperties:
if hasattr(self, "outputShape"):
return self.outputShape
output = self.scriptModule()(*self.getRandomInputs(1))
self.outputShape = TensorProperties(output[0])
# set inputDim and outputDim for backwards compatibility
self.inputDim = self.getInputShapes()[0].tensor_shape()
if len(self.inputDim) == 1:
self.inputDim = self.inputDim[0]
self.outputDim = self.outputShape.tensor_shape()
if len(self.outputDim) == 1:
self.outputDim = self.outputDim[0]
return self.outputShape
def getParameters(self):
pass
def getRandomInputs(self, batchsize, device="cuda"):
fakeInputs = []
for shape in self.getInputShapes():
fakeInputs.append(shape.genRand(batchsize, device))
return fakeInputs
def getModuleId(self):
import hashlib
m = hashlib.sha256()
m.update(json.dumps([str(a) for a in self.getInputShapes()], separators=('_', '-')).encode("utf-8"))
return self.name +\
json.dumps(self.params, sort_keys=True, separators=('_', '-')) +\
m.hexdigest()
def scriptModule(self):
if not self.moduleSavedLocation:
moduleId = self.getModuleId()
saveLocation = os.getcwd() + f"/modules/scriptmodule_{moduleId}.pt"
self.moduleSavedLocation = saveLocation
if exists(self.moduleSavedLocation): # Skip if module file is already there.
if not self.jit_module:
return torch.jit.load(self.moduleSavedLocation).to("cuda")
return self.jit_module
fakeInput = self.getRandomInputs(1, "cpu")
if self.must_trace:
print("jit tracing...", self.name)
traced = torch.jit.trace(self.module, fakeInput)
else:
print("jit scripting...", self.name)
traced = torch.jit.script(self.module, fakeInput)
# saveLocation = "modules/scriptmodule_%d.pt"%self.id
torch.jit.save(traced, self.moduleSavedLocation)
self.jit_module = torch.jit.load(self.moduleSavedLocation).to("cuda")
return self.jit_module
def getInitialConfig(self, globalBatch: int):
inputDim = self.getInputShapes()[0].tensor_shape()
outputDim = self.getOutputShape().tensor_shape()
if self.name in ["conv2d"]:
initCfg = (globalBatch, inputDim[1], inputDim[2], inputDim[0], outputDim[2]) # (batch, width, height, channel, filter)
elif self.name in ["linear", "ReLU1d"]:
initCfg = (globalBatch, *inputDim, *outputDim)
elif self.name in ["flatten", "maxPool2d", "avgPool2d", "adAvgPool2d", "ReLU2d", "concat"]:
initCfg = (globalBatch, inputDim[1], inputDim[2], inputDim[0]) # (batch, width, height, channel, filter)
else:
initCfg = (globalBatch, *inputDim) # (batch, width, height, channel)
return initCfg
def dumpForJSON(self):
prop = {}
if self.id == None:
raise Exception("layer is not yet initialized.")
prop["id"] = self.id
prop["name"] = self.name
prop["params"] = self.params
prop["gpuTime"] = self.gpuTime
prop["prevLayers"] = []
if self.prevLayers != None:
for prevLayer in self.prevLayers:
prop["prevLayers"].append(prevLayer.id)
prop["prevLayers"] = sorted(prop["prevLayers"])
prop["nextLayers"] = []
if self.nextLayers != None:
for nextLayer in self.nextLayers:
prop["nextLayers"].append(nextLayer.id)
prop["nextLayers"] = sorted(prop["nextLayers"])
prop["inputDim"] = self.inputDim
prop["outputDim"] = self.outputDim
if hasattr(self, 'gpuAssignment'):
prop["gpuAssignment"] = self.gpuAssignment
if self.moduleSavedLocation:
prop["moduleSavedLocation"] = self.moduleSavedLocation
elif self.module != None:
self.scriptModule()
prop["moduleSavedLocation"] = saveLocation
if not self.prevLayers:
prop["initial_inputs"] = [str(a) for a in self.initial_inputs]
return prop
class TrainingJob:
def __init__(self, name: str, layers: List[Layer], layerConfigs: List[tuple], globalBatchSize: int, maxGpusUsed: int, datasetDir: str):
self.name = name
self.layers = layers
self.layerConfigs = layerConfigs
self.globalBatchSize = globalBatchSize
self.maxGpusUsed = maxGpusUsed
self.datasetDir = datasetDir
self.bytesPerParam = 4
self.initialBatchSizes = None
self.sampleIndicesList = None
self.perRankConfigCache = []
def loadJSON(self, jobInJson: str):
job = json.loads(jobInJson)
self.globalBatchSize = job["globalBatchSize"]
self.maxGpusUsed = job["maxGpusUsed"]
self.layers = []
self.layerConfigs = []
for ldsc in job["layers"]:
# print(ldsc)
prevLayers = [self.layers[prevLayerId] for prevLayerId in ldsc["prevLayers"]]
l = Layer(None, ldsc["name"], ldsc["params"], prevLayers)
if 'gpuTime' in ldsc:
l.gpuTime = ldsc["gpuTime"]
l.id = ldsc["id"]
# l.nextLayers = ldsc["nextLayers"]
l.inputDim = ldsc["inputDim"]
l.outputDim = ldsc["outputDim"]
if 'gpuAssignment' in ldsc:
l.gpuAssignment = ldsc["gpuAssignment"]
l.bestCfg = ldsc["config"]
if 'moduleSavedLocation' in ldsc:
l.moduleSavedLocation = ldsc["moduleSavedLocation"]
for inputdesc in ldsc.get('initial_inputs', []):
if not hasattr(l, "initial_inputs"): l.initial_inputs = []
prop = TensorProperties()
prop.fromStr(inputdesc)
l.initial_inputs.append(prop)
config = ldsc["config"]
self.layers.append(l)
self.layerConfigs.append(config)
def getGpusUsed(self):
return self.maxGpusUsed
# maxGpusUsed = 0
# for l, config in zip(self.layers, self.layerConfigs):
# destGpus = self.calcGpusNeeded(l, config, self.globalBatchSize)
# maxGpusUsed = max(maxGpusUsed, destGpus)
# # print("[getGpusUsed] layer: %d, destGpus: %d, maxGpusUsed: %d, config: %s" % (l.id, destGpus, maxGpusUsed, str(config)))
# return maxGpusUsed
def dumpInJSON(self, layers: List[Layer] = None, layerConfigs: list = None):
if layers is None:
layers = self.layers
if layerConfigs is None:
layerConfigs = self.layerConfigs
allProps = []
for l, config in zip(layers, layerConfigs):
prop = l.dumpForJSON()
prop["config"] = config
allProps.append(prop)
fullDesc = {"globalBatchSize": self.globalBatchSize, "maxGpusUsed": self.maxGpusUsed, "layers": allProps}
# return json.dumps(fullDesc, indent=1, sort_keys=False)
return json.dumps(fullDesc, sort_keys=False)
def dumpSingleRunnableModule(self, targetRank: int) -> str: # Only supports DP now.
if len(self.perRankConfigCache) == 0:
self.perRankConfigCache = [self.dumpSingleRunnableModuleHelper(rank) for rank in range(self.getGpusUsed())]
fullDesc = self.perRankConfigCache[targetRank]
fullDesc["initialBatchSizes"] = self.initialBatchSizes
fullDesc["sampleIndices"] = self.sampleIndicesList[targetRank]
dumpedStr = json.dumps(fullDesc, sort_keys=False)
return dumpedStr
def computeXfers(self):
if getattr(self, "xferSamplesDone", False):
return
self.xferSamplesDone = True
initBSize = self.layers[0].bestCfg[0]
initAssigned = self.layers[0].gpuAssignment
self.initialBatchSizes = [initBSize if g in initAssigned else 0 for g in range(self.getGpusUsed())]
totalSamples = 0
xferCounter = 0
self.all_xfers = []
for l in self.layers:
l.byGpu = defaultdict(list)
l.bySample = {}
if not l.prevLayers: # 1st layer.
for i in range(self.getGpusUsed()):
if i not in l.gpuAssignment: continue
for _ in range(l.bestCfg[0]):
l.byGpu[i].append(totalSamples)
l.bySample[totalSamples] = i
totalSamples += 1
continue
# find a prevLayer with a matching config for matching sample assignment
found = False
for prevLayer in l.prevLayers:
if set(prevLayer.gpuAssignment) == set(l.gpuAssignment):
assert prevLayer.bestCfg[0] == l.bestCfg[0]
l.byGpu = copy.deepcopy(prevLayer.byGpu)
l.bySample = copy.deepcopy(prevLayer.bySample)
found = True
break
# if no matching prevlayer, try to reassign samples, minimizing bandwidth
if not found:
assert(len(l.byGpu) == 0 and len(l.bySample) == 0)
lastGpuAssigned = 0
def tryAssign(idx, gpu):
nonlocal lastGpuAssigned
assert idx not in l.bySample
if gpu in l.gpuAssignment and len(l.byGpu[gpu]) < l.bestCfg[0]:
l.bySample[idx] = gpu
l.byGpu[gpu].append(idx)
lastGpuAssigned = gpu
return True
return False
for i in range(totalSamples):
if tryAssign(i, prevLayer.bySample[i]):
continue
if tryAssign(i, lastGpuAssigned):
continue
for gpu in range(self.getGpusUsed()):
if tryAssign(i, gpu):
break
assert i in l.bySample
for prevLayer in l.prevLayers:
if l.bySample == prevLayer.bySample:
continue
# TODO: find a way to make sure that we dont have to shuffle samples when GPU assignments dont change
# assert set(prevLayer.gpuAssignment) != set(l.gpuAssignment)
xfer_to_from_pairs = defaultdict(lambda: defaultdict(list))
# Arrange by RX
lastSrcG, lastDstG = prevLayer.bySample[0], l.bySample[0]
rangeStart = 0
for i in range(1, totalSamples):
curSrcG = prevLayer.bySample[i]
curDstG = l.bySample[i]
if curSrcG == lastSrcG and curDstG == lastDstG: continue
xfer_to_from_pairs[lastDstG][lastSrcG].append((rangeStart, i - rangeStart))
lastSrcG = curSrcG
lastDstG = curDstG
rangeStart = i
xfer_to_from_pairs[lastDstG][lastSrcG].append((rangeStart, totalSamples - rangeStart))
# TODO: pickout special scatters/gathers someday?
for receiver, srcs in xfer_to_from_pairs.items():
for src, samples in srcs.items():
for sample_start, nr_sample in samples:
assert sorted(prevLayer.byGpu[src]) == prevLayer.byGpu[src]
assert sorted(l.byGpu[src]) == l.byGpu[src]
rxOffset = l.byGpu[receiver].index(sample_start)
txOffset = prevLayer.byGpu[src].index(sample_start)
xfer = {
"name": f"{l.id}_from_{prevLayer.id}_{receiver}:{rxOffset}_{src}:{txOffset}_sample_{xferCounter}",
"prop": {
"rxSampleOffset": rxOffset,
"txSampleOffset": txOffset,
"xferSamples": nr_sample,
"prevLayerId": prevLayer.id,
"nextLayerId": l.id,
},
"dest": receiver,
"src": src,
"bytes": 1 # fix
}
self.all_xfers.append(xfer)
xferCounter += 1
for i in range(self.getGpusUsed()):
for j in range(self.getGpusUsed()):
if not xfer_to_from_pairs[i][j]: continue
self.sampleIndicesList = []
for i in range(self.getGpusUsed()):
self.sampleIndicesList.append(sorted(self.layers[-1].byGpu[i]))
def dumpSingleRunnableModuleHelper(self, targetRank: int) -> str:
self.computeXfers()
allProps = []
for l in self.layers:
if not self.isConfigDataParallelOnly(l, l.bestCfg, self.globalBatchSize):
assert False, "WHAT IS THIS?"
print("[dumpSingleRunnableModule] config was not DP-only.")
return None
prop = l.dumpForJSON()
cfg = list(l.bestCfg)
if targetRank not in l.gpuAssignment:
cfg[0] = 0
prop["config"] = tuple(cfg)
allProps.append(prop)
for xfer in self.all_xfers:
if xfer["src"] == targetRank or xfer["dest"] == targetRank:
d = allProps[xfer["prop"]["nextLayerId"]]
if not d.get("xfers"):
d["xfers"] = []
d["xfers"].append(xfer)
fullDesc = {"rank": targetRank,
"maxGpusUsed": self.maxGpusUsed,
"globalBatchSize": self.globalBatchSize,
"layers": allProps}
return fullDesc
def calcGpusNeeded(self, layer: Layer, config: tuple, globalBatch: int):
initCfg = layer.getInitialConfig(globalBatch)
gpuCount = 1
# if len(config) != len(initCfg):
# print("[calcGpusNeeded] dimension of configs doesn't match!! %20s layer len(config):%d != len(initCfg):%d" % (layer.name, len(config), len(initCfg)))
for i in range(len(initCfg)):
gpuCount *= int(initCfg[i] / config[i])
return gpuCount
def isConfigDataParallelOnly(self, layer: Layer, config: tuple, globalBatch: int):
initCfg = layer.getInitialConfig(globalBatch)
dpOnly = True
for i in range(1, len(config)):
if config[i] != initCfg[i]:
dpOnly = True
return dpOnly
def test():
return
if __name__ == "__main__":
test()