-
Notifications
You must be signed in to change notification settings - Fork 2
/
torch_transformer_flops_copy.py
502 lines (445 loc) · 20.5 KB
/
torch_transformer_flops_copy.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
import time
import torch
import os
import numpy as np
import megatron_wrapper
import megatron
from megatron.model import LayerNorm
from megatron.model.fused_softmax import FusedScaleMaskSoftmax, SoftmaxFusionTypes
from megatron.model.transformer import ParallelSelfAttention, ParallelMLP, ParallelTransformerLayer
from megatron.model.transformer import bias_dropout_add_fused_train
from megatron.model.activations import bias_gelu_impl
from megatron.model.gpt2_model import gpt2_attention_mask_func as attention_mask_func
from megatron.model.word_embeddings import Embedding
print(torch.__version__, "\n")
dtype = torch.float16
def display(shape):
return "x".join([str(dim) for dim in shape])
def initialize_mm_b(dtype, M, N, K, b):
torch.cuda.empty_cache()
sizes = [(b, M, K), (K, N), (b, M, N), (b, M, N)]
return [torch.randint(-3, 3, size, device='cuda').to(dtype) for size in sizes]
def initialize_mm(dtype, M, N, K, b):
torch.cuda.empty_cache()
sizes = [(M, K), (K, N), (M, N), (M, N)]
return [torch.randint(-3, 3, size, device='cuda').to(dtype) for size in sizes]
def initialize_bmm(dtype, M, N, K, b):
torch.cuda.empty_cache()
sizes = [(b, M, K), (b, K, N), (b, M, N), (b, M, N)]
return [torch.randint(-3, 3, size, device='cuda').to(dtype) for size in sizes]
def benchmark_mm(m, n, k, label, b=None, num_iterations=200):
B = torch.randn((k, n)).half().to("cuda:0")
if b is None:
A = torch.randn((m, n)).half().to("cuda:0")
C = torch.empty((m, k)).half().to("cuda:0")
b = 1
else:
A = torch.randn((b, m, n)).half().to("cuda:0")
C = torch.empty((b, m, k)).half().to("cuda:0")
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
# torch.mm(A, B, out=C)
torch.nn.functional.linear(A, B, out=C)
torch.cuda.synchronize()
times[i] = time.time()
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
print(f"Elapsed time for {label} ({m}x{n}x{k}, b={b}): {median_time:.4f}")
print(f"Throughput (in TFLOP/s) for {label} ({m}x{n}x{k}, b={b}): "
f"{(2 * b * m * n * k) / (median_time * 10**12):.3f}")
return median_time
def benchmark_mm_cutlass(m,n,k,label,b=None, num_iterations=100):
plan = cutlass.op.Gemm(element=dtype, layout=cutlass.LayoutType.RowMajor)
if b is None:
As, Bs, Cs, Ds, = initialize_mm(dtype, m, n, k, b)
b=0
else:
As, Bs, Cs, Ds, = initialize_mm_b(dtype, m, n, k, b)
torch.cuda.empty_cache()
#print(torch.cuda.memory_summary(device='cuda'))
num_warm = 50
times = np.zeros(num_iterations+num_warm)
start_time = time.time()
for i in range( num_warm + num_iterations):
plan.run(As, Bs, Cs, Ds, sync=True)
torch.cuda.synchronize()
times[i] = time.time()
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
del As, Bs, Cs, Ds
print(f"Elapsed time for {label} ({m}x{n}x{k}, b={b}): {median_time:.4f}")
print(f"Throughput (in TFLOP/s) for {label} ({m}x{n}x{k}, b={b}): "
f"{(2 * b * m * n * k) / (median_time * 10**12):.3f}")
return median_time
def benchmark_bmm(b, m, n, k, label, num_iterations=200):
A = torch.randn((b, m, n)).half().to("cuda:0")
B = torch.randn((b, n, k)).half().to("cuda:0")
C = torch.empty((b, m, k)).half().to("cuda:0")
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
torch.bmm(A, B, out=C)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
print(f"Elapsed time for {label} ({b}x{m}x{n}x{k}): {median_time:.4f}")
print(f"Throughput (in TFLOP/s) for {label} ({b}x{m}x{n}x{k}): "
f"{(2 * b * m * n * k) / (median_time * 10**12):.3f}")
return median_time
def benchmark_bmm_cutlass(b, m, n, k, label, num_iterations=100):
print(f"b: {b}, m: {m}, n: {n}, k: {k},")
As, Bs, Cs, Ds, = initialize_bmm(dtype, m, n, k, b)
plan = cutlass.op.Gemm(element=dtype, layout=cutlass.LayoutType.RowMajor)
plan.compile()
torch.cuda.empty_cache()
num_warm = 50
times = np.zeros(num_iterations+num_warm)
start_time = time.time()
for i in range( num_warm + num_iterations):
plan.run(As, Bs, Cs, Ds, sync=True)
torch.cuda.synchronize()
times[i] = time.time()
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
del As, Bs, Cs, Ds
torch.cuda.empty_cache()
print(f"Elapsed time for {label} ({b}x{m}x{n}x{k}): {median_time:.4f}")
print(f"Throughput (in TFLOP/s) for {label} ({b}x{m}x{n}x{k}): "
f"{(2 * b * m * n * k) / (median_time * 10**12):.3f}")
return median_time
def benchmark_dropout(A_dim, label, num_iterations=100):
A = torch.randn(A_dim).half().to("cuda:0")
dropout = torch.nn.Dropout(0.5).to("cuda:0")
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
dropout(A)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
'''for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
dropout(A)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations'''
print(f"Elapsed time for {label} ({display(A_dim)}): {median_time:.4f}")
return median_time
def benchmark_softmax(scores_shape, seq_length, label, num_iterations=100):
scores = torch.randn(scores_shape).half().to("cuda:0")
attention_mask = torch.tril(torch.ones(
(1, seq_length, seq_length), device="cuda:0")).view(
1, 1, seq_length, seq_length)
attention_mask = attention_mask < 0.5
softmax = FusedScaleMaskSoftmax(
True, False,
SoftmaxFusionTypes.none, #attentionmasktype.padding=1,True
attention_mask_func, True, 1)
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
softmax(scores, attention_mask)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
'''for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
softmax(scores, attention_mask)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations'''
print(f"Elapsed time for {label} ({display(scores_shape)}): {median_time:.4f}")
return median_time
def benchmark_fused_gelu(A_dim, b_dim, label, num_iterations=100):
A = torch.randn(A_dim).half().to("cuda:0")
b = torch.randn(b_dim).half().to("cuda:0")
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
bias_gelu_impl(A, b)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
'''for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
bias_gelu_impl(A, b)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations'''
print(f"Elapsed time for {label} ({display(A_dim)}): {median_time:.4f}")
return median_time
def benchmark_layer_norm(A_dim, normalized_shape, label, num_iterations=100):
A = torch.randn(A_dim).half().to("cuda:0")
layer_norm = LayerNorm(normalized_shape).half().to("cuda:0")
num_warmup_iterations = 50
for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
layer_norm(A)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations
print(f"Elapsed time for {label} ({display(A_dim)}): {elapsed_time:.4f}")
return elapsed_time
def benchmark_add_bias_dropout(shape, label, num_iterations=100):
A = torch.randn(shape).half().to("cuda:0")
bias = torch.randn(shape).half().to("cuda:0")
residue = torch.randn(shape).half().to("cuda:0")
num_warmup_iterations = 50
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
bias_dropout_add_fused_train(A, bias, residue, 0.0)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.amax(times)
'''for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
bias_dropout_add_fused_train(A, bias, residue, 0.0)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations'''
print(f"Elapsed time for {label} ({display(shape)}): {median_time:.4f}")
return median_time
def benchmark_transformer_from_mm_and_bmm(configuration, seq_length, global_batch_size, num_iterations=200):
(microbatch_size, hidden_size, (tensor_mp_size, pipeline_mp_size, dp_size), num_attention_heads,vocab_size) = configuration
print("\n\nEstimate")
print("--------")
elapsed_attention_time = 0.0
elapsed_mlp_time = 0.0
elapsed_add_bias_dropout_time = 0.0
elapsed_layer_norm_time = 0.0
'''elapsed_attention_time += benchmark_mm(
microbatch_size, hidden_size,
3 * hidden_size // tensor_mp_size,
'attention_key_value_query_transform',
b=seq_length, num_iterations=num_iterations)'''
elapsed_attention_time += benchmark_bmm(
microbatch_size * num_attention_heads // tensor_mp_size,
seq_length, hidden_size // num_attention_heads,
seq_length, 'attention_key_query_prob',
num_iterations=num_iterations)
elapsed_attention_time += benchmark_bmm(
microbatch_size * num_attention_heads // tensor_mp_size,
seq_length, seq_length, hidden_size // num_attention_heads,
'attention_prob_times_values',
num_iterations=num_iterations)
'''
elapsed_attention_time += benchmark_dropout(
(microbatch_size, num_attention_heads // tensor_mp_size, seq_length, seq_length),
'attention_dropout',
num_iterations=num_iterations)
elapsed_attention_time += benchmark_softmax(
(microbatch_size, num_attention_heads // tensor_mp_size, seq_length, seq_length),
seq_length, 'attention_softmax',
num_iterations=num_iterations)
'''
'''
elapsed_attention_time += benchmark_mm(
microbatch_size, hidden_size // tensor_mp_size,
hidden_size, 'attention_linear_projection',
b=seq_length,
num_iterations=num_iterations)
elapsed_mlp_time += benchmark_mm(
microbatch_size, hidden_size,
4 * hidden_size // tensor_mp_size, 'mlp_h_to_4h',
b=seq_length,
num_iterations=num_iterations)
'''
'''
elapsed_mlp_time += benchmark_fused_gelu(
(seq_length, microbatch_size, 4 * hidden_size // tensor_mp_size),
(4 * hidden_size // tensor_mp_size,),
'mlp_fused_gelu', num_iterations=num_iterations)
'''
'''
elapsed_mlp_time += benchmark_mm(
microbatch_size, 4 * hidden_size // tensor_mp_size,
hidden_size, 'mlp_4h_to_h',
b=seq_length,
num_iterations=num_iterations)
'''
'''
elapsed_add_bias_dropout_time = 2 * benchmark_add_bias_dropout(
(seq_length, microbatch_size, hidden_size),
'transformer_add_bias_dropout',
num_iterations=num_iterations)
elapsed_layer_norm_time = 2 * benchmark_layer_norm(
(seq_length, microbatch_size, hidden_size),
hidden_size,
'transformer_layer_norm',
num_iterations=num_iterations)'''
elapsed_total_time = elapsed_attention_time + elapsed_mlp_time + elapsed_add_bias_dropout_time + \
elapsed_layer_norm_time
num_attention_floating_point_operations = \
(4 * microbatch_size * seq_length * hidden_size / tensor_mp_size) * (
2 * hidden_size + seq_length)
num_mlp_floating_point_operations = \
16 * microbatch_size * seq_length * hidden_size * hidden_size / tensor_mp_size
num_total_floating_point_operations = num_attention_floating_point_operations + \
num_mlp_floating_point_operations
attention_throughput = num_attention_floating_point_operations / (elapsed_attention_time * 10**12)
mlp_throughput = 1# num_mlp_floating_point_operations / (elapsed_mlp_time * 10**12)
total_throughput = 1# num_total_floating_point_operations / (elapsed_total_time * 10**12)
print()
for (elapsed_time, throughput, label) in \
zip([elapsed_attention_time, elapsed_mlp_time, elapsed_total_time],
[attention_throughput, mlp_throughput, total_throughput],
["Attention", "MLP", "Transformer"]):
print(f"{label} duration (in seconds): {elapsed_time:.4f}")
print(f"{label} throughput (in TFLOP/s): {throughput:.3f}")
print("Transformer - MLP - Attention (in seconds): "
f"{(elapsed_total_time - elapsed_attention_time - elapsed_mlp_time):.4f}")
num_microbatches_in_pipeline = global_batch_size // (microbatch_size * dp_size)
pipeline_bubble_fraction = (pipeline_mp_size - 1) / num_microbatches_in_pipeline
elapsed_time *= (1 + pipeline_bubble_fraction)
# Throughput if considering pipeline bubble.
throughput = num_total_floating_point_operations / (elapsed_time * 10**12)
def benchmark_transformer(configuration, seq_length, global_batch_size, num_iterations=100):
(microbatch_size, hidden_size,
(tensor_mp_size, pipeline_mp_size, dp_size), num_attention_heads,vocab_size) = configuration
print("\n\nActual")
print("------")
args = megatron_wrapper.get_megatron_args(configuration)
fn_args = [megatron.model.init_functions.init_method_normal(args.init_method_std),
megatron.model.init_functions.init_method_normal(args.init_method_std)]
init_method = megatron.model.init_functions.init_method_normal(args.init_method_std)
#embedding_layer = Embedding(args,hidden_size,vocab_size,seq_length,0.0,init_method=init_method,use_pos_emb=False)
attention_layer = ParallelSelfAttention(args,attention_mask_func=attention_mask_func, init_method=init_method,output_layer_init_method=init_method, layer_number=0).half().to("cuda:0")
mlp_layer = ParallelMLP(args,init_method=init_method,output_layer_init_method=init_method).half().to("cuda:0")
transformer_layer = ParallelTransformerLayer(args,attention_mask_func=attention_mask_func,init_method=init_method,output_layer_init_method=init_method,layer_number=0).half().to("cuda:0")
inp = torch.randn((args.seq_length, args.batch_size, args.hidden_size)).half().to("cuda:0")
attention_mask = torch.tril(torch.ones(
(1, args.seq_length, args.seq_length), device="cuda:0")).view(
1, 1, args.seq_length, args.seq_length)
attention_mask = attention_mask < 0.5
num_embedding_floating_point_operations = \
(2*vocab_size -1) * seq_length * microbatch_size * hidden_size
num_attention_floating_point_operations = \
(4 * microbatch_size * seq_length * hidden_size / tensor_mp_size) * (
2 * hidden_size + seq_length)
num_mlp_floating_point_operations = \
16 * microbatch_size * seq_length * hidden_size * hidden_size / tensor_mp_size
num_total_floating_point_operations = num_attention_floating_point_operations + \
num_mlp_floating_point_operations
num_warmup_iterations = 50
allTimes = []
"""
for layer, label, need_attention_mask, num_floating_point_operations in \
zip([ attention_layer, mlp_layer, transformer_layer],
[ "Attention", "MLP", "Transformer"],
[ True, False, True],
[num_attention_floating_point_operations, num_mlp_floating_point_operations,
num_total_floating_point_operations]):
"""
for layer, label, need_attention_mask, num_floating_point_operations in \
zip([ transformer_layer],
[ "Transformer"],
[ True],
[num_total_floating_point_operations]):
layer.train()
times = np.zeros(num_iterations+num_warmup_iterations)
start_time = time.time()
for i in range(num_warmup_iterations + num_iterations):
with torch.no_grad():
if need_attention_mask:
out = layer(inp, attention_mask)
torch.cuda.empty_cache()
else:
if label == "Embedding":
out = layer(inp, None)
else:
out = layer(inp)
torch.cuda.synchronize()
times[i] = time.time()
#elapsed_time = (time.time() - start_time) / num_iterations
times -= start_time
times = np.diff(times)
times = times[50:]
median_time = np.median(times)
'''for i in range(num_warmup_iterations + num_iterations):
if i == num_warmup_iterations:
start_time = time.time()
with torch.no_grad():
if need_attention_mask:
out = layer(inp, attention_mask)
else:
out = layer(inp)
torch.cuda.synchronize()
elapsed_time = (time.time() - start_time) / num_iterations'''
allTimes.append(median_time)
throughput = num_floating_point_operations / (median_time * 10**12)
print(f"{label} duration (in seconds): {median_time:.4f}")
print(f"{label} throughput (in TFLOP/s): {throughput:.3f}")
print("Transformer - MLP - Attention (in seconds): "
f"{(allTimes[-1] - allTimes[0] - allTimes[1]):.4f}")
if __name__ == '__main__':
torch.cuda.set_device("cuda:0")
print("here")
seq_length = 2048
train_batch_size = 2048
configurations = []
for tensor_mp_size in [1]:
for num_attention_heads in [20]:# [32,128]: #[32, 64, 96, 128]:
for hidden_size in range(num_attention_heads,2**15 + num_attention_heads,num_attention_heads): #[32768]: #range(8192,2**15, num_attention_heads):
for microbatch_size in [4]:
for vocab_size in [51200]:
configurations.append((microbatch_size, hidden_size,
(tensor_mp_size, 1, 1), num_attention_heads,vocab_size))
#megatron_wrapper.initialize_megatron(configurations[0])
for configuration in configurations:
(microbatch_size, hidden_size,
(tensor_mp_size, pipeline_mp_size, dp_size), num_attention_heads,vocab_size) = configuration
label = {'num_attention_heads': num_attention_heads,
'hidden_size': hidden_size,
'train_micro_batch_size_per_gpu': microbatch_size,
'tensor_mp_size': tensor_mp_size,
'pipeline_mp_size': pipeline_mp_size,
'dp_size': dp_size}
label_str = ", ".join([f"{k}: {v}" for (k, v) in label.items()])
print(label_str)
benchmark_transformer_from_mm_and_bmm(configuration, seq_length, train_batch_size)
#benchmark_transformer(configuration, seq_length, train_batch_size)
print("=" * 120)