-
Notifications
You must be signed in to change notification settings - Fork 52
/
bench.py
140 lines (130 loc) · 4.56 KB
/
bench.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
import sys
import numpy as np
import torch
import marlin
import time
def benchmark(f, warmup=1, iter=10):
for i in range(warmup + iter):
f()
# We do not synchronize here in order to hide the kernel launch overhead during benchmarkining as this will also
# happen during realistic model inference as many launches are submitted to the kernel queue.
if i == warmup - 1:
torch.cuda.synchronize()
tick = time.time()
torch.cuda.synchronize()
res = (time.time() - tick) / iter
# Make sure there is enough to "cool down" the GPU in between benchmarks to avoid throttling for later runs when
# we execute many benchmarks consecutively
time.sleep(1.)
return res
def get_problem(m, n, k, groupsize=-1):
if groupsize == -1:
groupsize = k
dev = torch.device('cuda:0')
A = torch.randn((m, k), dtype=torch.half, device=dev)
B = torch.randint(low=-2**31, high=2**31, size=(k * n // 8,), device=dev)
B_ref = torch.randn((k, n), dtype=torch.half, device=dev)
C = torch.zeros((m, n), dtype=torch.half, device=dev)
s = torch.zeros((k // groupsize, n), dtype=torch.half, device=dev)
torch.cuda.synchronize()
return A, B, C, B_ref, s
def benchmark_dense(A, B, C):
res = benchmark(lambda: torch.matmul(A, B, out=C))
return {
's': res,
'TFLOP/s': 2 * A.numel() * C.shape[1] / res / 10 ** 12,
'GB/s': (2 * A.numel() + 2 * B.numel() + 2 * C.numel()) / res / 10 ** 9
}
def benchmark_quant(A, B, C, s, thread_k, thread_n, sms):
workspace = torch.zeros(C.shape[1] // 128 * 16, device=torch.device('cuda:0'))
res = benchmark(lambda: marlin.mul(A, B, C, s, workspace, thread_k, thread_n, sms))
return {
's': res,
'TFLOP/s': 2 * A.numel() * C.shape[1] / res / 10 ** 12,
'GB/s': (2 * A.numel() + 4 * B.numel() + 2 * C.numel() + 2 * s.numel()) / res / 10 ** 9
}
# Pass the SM count for known GPUs to avoid the kernel having to query this information (this is very minor)
gpu = torch.cuda.get_device_name(0)
if 'A100' in gpu:
SMS = 108
elif 'A10' in gpu:
SMS = 72
elif '3090' in gpu:
SMS = 82
elif 'A6000' in gpu:
SMS = 84
else:
SMS = -1
MODELS = {
'ideal': [
(4 * 256 * SMS, 256 * SMS)
],
'Llama7B': [
(4096, 3 * 4096),
(4096, 4096),
(4096, 2 * 10752),
(10752, 4096)
],
'Llama13B': [
(5120, 3 * 5120),
(5120, 5120),
(5120, 2 * 13568),
(13568, 5120)
],
'Llama33B': [
(6656, 3 * 6656),
(6656, 6656),
(6656, 2 * 17664),
(17664, 6656)
],
'Llama65B': [
(8192, 3 * 8192),
(8192, 8192),
(8192, 2 * 21760),
(21760, 8192)
],
'Falcon180B': [
# Note that parallel attention and FC allows layer fusions
(14848, 14848 * 5 + 1024),
(14848 * 5, 14848)
]
}
# Set to true in order to run a more complete benchmark sweep; the default is reproduce README experiments
ALL = False
for groupsize in [-1, 128] if ALL else [128]:
print('groupsize=%d' % groupsize)
print()
for model, layers in MODELS.items():
print(model)
if ALL:
batchsizes = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
else:
batchsizes = [1, 2, 4, 8, 16, 32, 64, 128]
for batch in batchsizes:
if not ALL and model != 'ideal' and batch != 16:
continue
tot_q = {'s': 0, 'TFLOP/s': 0, 'GB/s': 0, 'speedup': 0}
for layer in layers:
A, B, C, B_ref, s = get_problem(batch, layer[1], layer[0], groupsize)
res_d = benchmark_dense(A, B_ref, C)
if model == 'ideal' and batch == 16:
# This is a special case constructed to be optimal for a thread-shape different than the default one
res_q = benchmark_quant(A, B, C, s, 64, 256, SMS)
else:
res_q = benchmark_quant(A, B, C, s, -1, -1, SMS)
res_q['speedup'] = res_d['s'] / res_q['s']
tot_q['s'] += res_q['s']
for k in tot_q:
if k != 's':
tot_q[k] += res_q[k] * res_q['s']
for k in tot_q:
if k != 's':
tot_q[k] /= tot_q['s']
print('batch=%04d: s=%.5f, TFLOP/s=%07.3f, GB/s=%08.3f, speedup=%.2f' % (
batch,
tot_q['s'],
tot_q['TFLOP/s'],
tot_q['GB/s'],
tot_q['speedup']
))
print()