-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_kernel.py
71 lines (59 loc) · 2.49 KB
/
get_kernel.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
import torch
from triton.testing import do_bench
def get_flops_mm(M,N,K,b=None, get_kernels=False, file= None):
A = torch.randn((b, M, N), device='cuda', dtype=torch.float16)
B = torch.randn(K, N, device='cuda', dtype=torch.float16)
C = torch.randn((b, M, K), device='cuda', dtype=torch.float16)
def f():
return torch.nn.functional.linear(A, B, out=C)
if get_kernels:
with torch.profiler.profile() as prof:
f()
for e in prof.events():
#print(e.name)
if "gemm" in e.name or "triton" in e.name or "gemv" in e.name:
if file:
print(f"{N}: {e.name}", file = open(file, 'a'))
else:
print(f"{N}: {e.name}")
timer = e.cuda_time/1e3
timer = do_bench(f)[0]
iters_per_second = 1e3/timer
flops = A.shape[0] * A.shape[1] * A.shape[2] * B.shape[0] * 2
flops_achieved = iters_per_second * flops/1e12
if file:
print(f"{M}x{N}x{K}, B={b}: {flops_achieved:.2f}TF/s", file = open(file, 'a'))
else:
print(f"{M}x{N}x{K}, B={b}: {flops_achieved:.2f}TF/s")
def get_flops_bmm(M,N,K,b=None, get_kernels=False):
A = torch.randn(b, M, N, device='cuda', dtype=torch.float16)
B = torch.randn(b, N, K, device='cuda', dtype=torch.float16)
C = torch.randn(b, M, K, device='cuda', dtype=torch.float16)
def f():
return torch.bmm(A, B, out=C)
if get_kernels:
with torch.profiler.profile() as prof:
f()
#for e in prof.events():
# print(e.name)
#if "gemm" in e.name or "triton" in e.name or "gemv" in e.name:
# print(f"{N}: {e.name}")
# timer = e.cuda_time/1e3
#timer = do_bench(f)[0]
#iters_per_second = 1e3/timer
#flops = 2* M * N * K * b
#flops_achieved = iters_per_second * flops/1e12
#print(f"{M}x{N}x{K}, B={b}: {flops_achieved:.2f}TF/s")
print("-" * 40)
def mm_for_profiles(M,N,K,b=None, get_kernels=False, file= None):
A = torch.randn((b, M, N), device='cuda', dtype=torch.float16)
B = torch.randn(K, N, device='cuda', dtype=torch.float16)
C = torch.randn((b, M, K), device='cuda', dtype=torch.float16)
def f():
return torch.nn.functional.linear(A, B, out=C)
f()
file = "attention_kvq_transform_kernels.txt"
M = 4
for N in range(9472-64, 9472+64, 64):
#get_flops_mm(M, N, N*3, b=2048, get_kernels=True, file = file)
mm_for_profiles(M, N, N*3, b=2048, get_kernels=True, file = file)