-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_mbm.py
More file actions
70 lines (61 loc) · 2.68 KB
/
Copy pathgpu_mbm.py
File metadata and controls
70 lines (61 loc) · 2.68 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
import torch
import argparse
# Argument parsing
parser = argparse.ArgumentParser(description="Matrix multiplication with optional settings.")
parser.add_argument('--bmm', action='store_true', help="Use batched matrix multiplication.")
parser.add_argument('--bsz', type=int, default=5040, help="Batch size (only used if --bmm is set).")
parser.add_argument('--m', type=int, default=2048, help="Number of rows of the first matrix.")
parser.add_argument('--n', type=int, default=2048, help="Number of columns of the first matrix and rows of the second matrix.")
parser.add_argument('--k', type=int, default=2048, help="Number of columns of the second matrix.")
parser.add_argument('--iter', type=int, default=5, help="Number of iterations to repeat the process.")
parser.add_argument('--warmup', type=int, default=2, help="Number of iterations to repeat the process.")
parser.add_argument('--dtype', type=str, default='bfloat16', help="Data type of the matrix multiplication.")
args = parser.parse_args()
# Set variables based on arguments
bmm = args.bmm
bsz = args.bsz if bmm else 1
m = args.m
n = args.n
k = args.k
iterations = args.iter
warmup = args.warmup
dtype = args.dtype
data_type = torch.bfloat16 if dtype == 'bfloat16' else torch.float16
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
# Repeat the time measurement process
total_duration = 0
count = 0
for i in range(iterations):
# Generate random tensors
if bmm:
a = torch.ones((bsz, m, n), dtype=data_type, device='cuda')
b = torch.ones((bsz, n, k), dtype=data_type, device='cuda')
else:
a = torch.rand((m, n), dtype=data_type, device='cuda')
b = torch.rand((n, k), dtype=data_type, device='cuda')
# Measure time
if bmm:
start_event.record()
c = torch.bmm(a, b)
end_event.record()
else:
start_event.record()
c = torch.matmul(a, b)
end_event.record()
torch.cuda.synchronize() # Wait for all operations to finish
duration = start_event.elapsed_time(end_event) # Time in milliseconds
# Accumulate duration
duration /= 1000
if i > warmup - 1:
total_duration += duration
print(f"Iteration {i - warmup}: output data type: {c.dtype}, Duration: {duration:.6f} seconds")
del a, b
c = c + 1
torch.cuda.empty_cache()
# Calculate average duration and GFLOPS
average_duration = total_duration / (iterations-warmup)
n_comp = 2 * bsz * m * n * k if bmm else 2 * m * n * k
gflops = n_comp / average_duration / 10**9
print(f"Average Duration: {average_duration:.6f} seconds")
print(f"Throughput: {gflops} (GFLOPS)")