-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofiler.py
529 lines (449 loc) · 21.3 KB
/
profiler.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
from utils import port_pretrained_models
import tensorflow as tf
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from tensorboard_plugin_profile.protobuf import tf_stats_pb2 #, kernel_stats_pb2
from tensorboard_plugin_profile.convert.tf_stats_proto_to_gviz import generate_chart_table
# from tensorboard_plugin_profile.convert.kernel_stats_proto_to_gviz import generate_kernel_reports_table
from tqdm import tqdm
import argparse
import time
import csv
import os
def profile_backpropagation(
model,
input_shape,
batch_size,
num_iterations,
logdir):
"""
This function profiles NN ops in backward pass.
Args:
model (tf.keras.Model): NN model to profile
input_shape (tuple): input shape of NN model
batch_size (int): batch size for backward pass
num_iterations (int): number of backward passes to run
logdir (str): path to where profile is recorded
"""
if os.path.exists(logdir):
print(f"Profile '{logdir}' already exists")
return
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
@tf.function
def train_step(x, y):
with tf.GradientTape() as tape:
y_pred = model(x, training=True)
loss = loss_fn(y, y_pred)
gradients = tape.gradient(loss, model.trainable_weights)
return gradients
# dummy training data
x = tf.random.normal((batch_size, input_shape[0], input_shape[1], input_shape[2]))
y = tf.ones((batch_size,))
print("Warmup...")
for k in tqdm(range(2)):
train_step(x, y)
t0 = time.time()
print("Profiling the model...")
tf.profiler.experimental.start(logdir)
for k in range(num_iterations):
with tf.profiler.experimental.Trace('train', step_num=k, _r=1):
train_step(x, y)
tf.profiler.experimental.stop()
t1 = time.time()
print("Finished profiling!")
print("Elasped time (s):", t1 - t0)
def convert_pb_to_csv(logdir, outdir):
"""
This function extracts timing-related info from the recorded profile.
Args:
logdir (str): path to where profile is recorded
outdir (str): path to where the extracted timing info is stored
"""
if os.path.exists(outdir):
print(f"Extracted timing info '{outdir}' already exists")
return
tf_profile_path = logdir + '/plugins/profile/'
tf_stats_path = ''
for root, subdirs, files in os.walk(logdir):
if tf_profile_path in root:
fn = ''
for f in files:
if 'tensorflow_stats.pb' in f:
fn = f
tf_stats_path = root + '/' + fn
with tf.io.gfile.GFile(tf_stats_path, 'rb') as f:
tf_stats_db = tf_stats_pb2.TfStatsDatabase()
tf_stats_db.ParseFromString(f.read())
csv_table = generate_chart_table(tf_stats_db.with_idle,
tf_stats_db.device_type).ToCsv()
with open(outdir, 'w') as f:
f.write(csv_table)
def profile_parser(
model,
model_type,
num_iterations,
filedir,
draw_figure=False):
"""
This function constructs tensor timings from the profiled op timings.
Args:
model (tf.keras.Model): NN model
model_type (str): type of NN model
num_iterations (int): number of iterations of backward passes in profiling
filedir (str): where the timing-related info is stored
draw_figure (bool, optional): whether to plot the tensor timings. Defaults to False.
Returns:
tensor timings t_dw and t_dy
"""
if model_type in ('resnet50', 'vgg16'):
all_stats = []
with open(filedir) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_reader.__next__()
for r in csv_reader:
all_stats.append(r)
op_total_time = []
op_names = []
# extract gradient related ops
for op_stat in all_stats:
if 'gradient_tape' in op_stat[3]:
op_total_time.append(float(op_stat[5]))
op_names.append(op_stat[3])
base_layers = model.layers[4].layers
custom_layers = model.layers[5:]
model_layers = [*base_layers, *custom_layers]
t_dw = [0.0 for k in range(len(model.trainable_weights))]
t_dy = [0.0 for k in range(len(model.trainable_weights))]
weight_count = 0
for l in model_layers:
if '_conv' in l.name:
if l.use_bias:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'Conv2DBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'Conv2DBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
elif l.name in op and 'BiasAddGrad' in op:
t_dw[weight_count + 1] = t
t_dy[weight_count + 1] = 0
weight_count += 2
else:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'Conv2DBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'Conv2DBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
weight_count += 1
elif '_bn' in l.name:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'FusedBatchNormGrad' in op:
# for gamma
t_dw[weight_count] = 0
t_dy[weight_count] = 0
# for beta
t_dw[weight_count + 1] = 0
t_dy[weight_count + 1] = t
weight_count += 2
elif 'dense' in l.name:
if l.use_bias:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif l.name in op and 'MatMul/MatMul' in op:
t_dy[weight_count] = t
elif l.name in op and 'BiasAddGrad' in op:
t_dw[weight_count + 1] = t
t_dy[weight_count + 1] = 0
weight_count += 2
else:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif l.name in op and 'MatMul/MatMul' in op:
t_dy[weight_count] = t
weight_count += 1
else:
# fuse backprop time of non-trainables to the previous trainable layer
for op, t in zip(op_names, op_total_time):
if l.name in op and weight_count > 0:
t_dy[weight_count - 1] += t
# the first layer never propagates input grads, just remove t_dy[0]
# t_dy = t_dy[1:] # 1~N-1
t_dw = np.array(t_dw) / num_iterations # (us)
t_dy = np.array(t_dy) / num_iterations # (us)
print(f'# model trainbles: {len(model.trainable_weights)}')
print(f'# t_dw: {weight_count}, # t_dy: {weight_count}')
if draw_figure:
fig = plt.figure()
plt.barh(np.arange(t_dw.shape[0]), t_dw, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dw (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
fig = plt.figure()
plt.barh(np.arange(t_dy.shape[0]), t_dy, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dy (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
return tf.convert_to_tensor(t_dw/1000.0, tf.float32), tf.convert_to_tensor(t_dy/1000.0, tf.float32) # (ms)
elif model_type == 'mobilenetv2':
all_stats = []
with open(filedir) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_reader.__next__()
for r in csv_reader:
all_stats.append(r)
op_total_time = []
op_names = []
# extract gradient related ops
for op_stat in all_stats:
if 'gradient_tape' in op_stat[3]:
op_total_time.append(float(op_stat[5]))
op_names.append(op_stat[3])
base_layers = model.layers[4].layers
custom_layers = model.layers[5:]
model_layers = [*base_layers, *custom_layers]
t_dw = [0.0 for k in range(len(model.trainable_weights))]
t_dy = [0.0 for k in range(len(model.trainable_weights))]
weight_count = 0
for l in model_layers:
# take care of the standard conv
if ('Conv1' == l.name) or ('Conv_1' == l.name) or ((l.name).endswith('_project')) or ((l.name).endswith('_expand')):
if l.use_bias:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'Conv2DBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'Conv2DBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
elif l.name in op and 'BiasAddGrad' in op:
t_dw[weight_count + 1] = t
t_dy[weight_count + 1] = 0
weight_count += 2
else:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'Conv2DBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'Conv2DBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
weight_count += 1
# take care of the lightweight conv
elif ((l.name).endswith('_depthwise')):
if l.use_bias:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'DepthwiseConv2dNativeBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'DepthwiseConv2dNativeBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
elif l.name in op and 'BiasAddGrad' in op:
t_dw[weight_count + 1] = t
t_dy[weight_count + 1] = 0
weight_count += 2
else:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'DepthwiseConv2dNativeBackpropFilter' in op:
t_dw[weight_count] = t
elif l.name in op and 'DepthwiseConv2dNativeBackpropInput' in op:
t_dy[weight_count] += t # include TransposeNCHWToNHWC
weight_count += 1
elif ('bn' in l.name) or ('BN' in l.name):
for op, t in zip(op_names, op_total_time):
if l.name in op and 'FusedBatchNormGrad' in op:
# for gamma
t_dw[weight_count] = 0
t_dy[weight_count] = 0
# for beta
t_dw[weight_count + 1] = 0
t_dy[weight_count + 1] = t
weight_count += 2
elif 'dense' in l.name:
if l.use_bias:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif l.name in op and 'MatMul/MatMul' in op:
t_dy[weight_count] = t
elif l.name in op and 'BiasAddGrad' in op:
t_dw[weight_count + 1] = t
t_dy[weight_count + 1] = 0
weight_count += 2
else:
for op, t in zip(op_names, op_total_time):
if l.name in op and 'MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif l.name in op and 'MatMul/MatMul' in op:
t_dy[weight_count] = t
weight_count += 1
else:
# fuse backprop time of non-trainables to the previous trainable layer
for op, t in zip(op_names, op_total_time):
if l.name in op and weight_count > 0:
t_dy[weight_count - 1] += t
# the first layer never propagates input grads, just remove t_dy[0]
# t_dy = t_dy[1:] # 1~N-1
t_dw = np.array(t_dw) / num_iterations # (us)
t_dy = np.array(t_dy) / num_iterations # (us)
print(f'# model trainbles: {len(model.trainable_weights)}')
print(f'# t_dw: {weight_count}, # t_dy: {weight_count}')
if draw_figure:
fig = plt.figure()
plt.barh(np.arange(t_dw.shape[0]), t_dw, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dw (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
fig = plt.figure()
plt.barh(np.arange(t_dy.shape[0]), t_dy, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dy (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
return tf.convert_to_tensor(t_dw/1000.0, tf.float32), tf.convert_to_tensor(t_dy/1000.0, tf.float32) # (ms)
elif model_type == 'vit':
all_stats = []
with open(filedir) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_reader.__next__()
for r in csv_reader:
all_stats.append(r)
op_total_time = []
op_names = []
# extract gradient related ops
for op_stat in all_stats:
if 'gradient_tape' in op_stat[3]:
op_total_time.append(float(op_stat[5]))
op_names.append(op_stat[3])
t_dw = [0.0 for k in range(len(model.trainable_weights))]
t_dy = [0.0 for k in range(len(model.trainable_weights))]
for weight_count, w in enumerate(model.trainable_weights):
if 'embedding/kernel' in w.name:
for op, t in zip(op_names, op_total_time):
if 'embedding' in op and 'Conv2DBackpropFilter' in op:
t_dw[weight_count] = t
elif 'query/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/query/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/MultiHeadDotProductAttention_1/query/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
elif 'key/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/key/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/MultiHeadDotProductAttention_1/key/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
elif 'key/bias' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/truediv/RealDiv' in op:
t_dy[weight_count] += t # c = 1/sqrt(d)
elif s + '/MultiHeadDotProductAttention_1/mul' in op:
t_dy[weight_count] += t # *c
elif s + '/MultiHeadDotProductAttention_1/MatMul/' in op:
t_dy[weight_count] += t # query*key
elif 'value/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/value/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/MultiHeadDotProductAttention_1/value/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
elif 'value/bias' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/MatMul_1/' in op:
t_dy[weight_count] += t # att*value
elif 'out/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/MultiHeadDotProductAttention_1/out/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/MultiHeadDotProductAttention_1/out/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
elif 'Dense_0/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/Dense_0/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/Dense_0/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
elif 'Dense_1/kernel' in w.name:
s = w.name.split('/')[1]
for op, t in zip(op_names, op_total_time):
if s + '/Dense_1/Tensordot/MatMul/MatMul_1' in op:
t_dw[weight_count] = t
elif s + '/Dense_1/Tensordot/MatMul/MatMul' in op:
t_dy[weight_count] = t
t_dw = np.array(t_dw) / num_iterations # (us)
t_dy = np.array(t_dy) / num_iterations # (us)
print(f'# model trainbles: {len(model.trainable_weights)}')
print(f'# t_dw: {weight_count+1}, # t_dy: {weight_count+1}')
if draw_figure:
fig = plt.figure()
plt.barh(np.arange(t_dw.shape[0]), t_dw, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dw (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
fig = plt.figure()
plt.barh(np.arange(t_dy.shape[0]), t_dy, color ='navy')
#plt.xticks(rotation=45)
plt.xlabel('t_dy (us)', fontsize=20)
plt.xticks(fontsize=20)
plt.ylabel('Layer ID', fontsize=20)
plt.yticks(fontsize=20)
plt.show()
return tf.convert_to_tensor(t_dw/1000.0, tf.float32), tf.convert_to_tensor(t_dy/1000.0, tf.float32) # (ms)
else:
raise NotImplementedError("This model has not been implemented yet")
def main():
parser = argparse.ArgumentParser(description='Tensor timing profiling')
parser.add_argument('--model_name', type=str, default='resnet50', help='valid model names are resnet50, vgg16, mobilenetv2, vit')
parser.add_argument('--input_size', type=int, default=224, help='input resolution, e.g., 224 stands for 224x224')
parser.add_argument('--batch_size', type=int, default=4, help='batch size used to run during profiling')
parser.add_argument('--num_classes', type=int, default=200, help='number of categories model can classify')
# parser.add_argument('--num_iterations', type=int, default=5, help='number of backward passes to run during profiling')
args = parser.parse_args()
model_name = args.model_name
input_size = args.input_size
batch_size = args.batch_size
num_classes = args.num_classes
model = port_pretrained_models(
model_type=model_name,
input_shape=(input_size, input_size, 3),
num_classes=num_classes,
)
run_name = model_name + '_' + str(input_size) + '_' + str(num_classes) + '_' + str(batch_size) + '_' + 'profile'
profile_backpropagation(
model,
(input_size, input_size, 3),
batch_size,
5,
'logs/' + run_name,
)
convert_pb_to_csv('logs/' + run_name, 'profile_extracted/' + run_name)
t_dw, t_dy = profile_parser(
model,
model_name,
5,
'profile_extracted/' + run_name,
draw_figure=False,
)
# for w, t1, t2 in zip(model.trainable_weights, t_dw, t_dy):
# print(w.name, t1.numpy(), t2.numpy())
if __name__ == '__main__':
main()