-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbench_flash_attn.py
More file actions
63 lines (52 loc) · 1.78 KB
/
Copy pathbench_flash_attn.py
File metadata and controls
63 lines (52 loc) · 1.78 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
#!/usr/bin/env python3
import time
import torch
BATCH = 1
SEQLENS = [1024, 2048, 4096, 8192, 16384, 32768]
NHEADS = 32
NHEADS_KV = 8
HEAD_DIM = 128
CAUSAL = True
DTYPE = torch.bfloat16
WARMUP = 5
ITERS = 20
major, _ = torch.cuda.get_device_capability()
if major == 9:
from flash_attn_interface import flash_attn_func
BACKEND = "fa3"
elif major == 10:
from flash_attn.cute import flash_attn_func
BACKEND = "fa4"
else:
raise RuntimeError(f"unsupported compute capability {major}.x")
torch.manual_seed(0)
dev = "cuda"
print(f"# gpu={torch.cuda.get_device_name()} backend={BACKEND} causal={CAUSAL} "
f"nheads={NHEADS} nheads_kv={NHEADS_KV} head_dim={HEAD_DIM}")
def bench(fn):
for _ in range(WARMUP):
fn()
torch.cuda.synchronize()
t0 = time.perf_counter()
for _ in range(ITERS):
fn()
torch.cuda.synchronize()
return (time.perf_counter() - t0) / ITERS
for seqlen in SEQLENS:
q = torch.randn(BATCH, seqlen, NHEADS, HEAD_DIM, device=dev, dtype=DTYPE, requires_grad=True)
k = torch.randn(BATCH, seqlen, NHEADS_KV, HEAD_DIM, device=dev, dtype=DTYPE, requires_grad=True)
v = torch.randn(BATCH, seqlen, NHEADS_KV, HEAD_DIM, device=dev, dtype=DTYPE, requires_grad=True)
dout = torch.randn(BATCH, seqlen, NHEADS, HEAD_DIM, device=dev, dtype=DTYPE)
def fwd():
o = flash_attn_func(q, k, v, causal=CAUSAL)
return o[0] if isinstance(o, tuple) else o
def fwd_bwd():
fwd().backward(dout)
f = 4 * BATCH * seqlen**2 * NHEADS * HEAD_DIM
if CAUSAL:
f //= 2
t_fwd = bench(fwd)
t_fb = bench(fwd_bwd)
print(f'{{"seqlen": {seqlen}, '
f'"fwd_ms": {t_fwd*1000:.2f}, "fwd_tflops": {f/t_fwd/1e12:.2f}, '
f'"fwd_bwd_ms": {t_fb*1000:.2f}, "fwd_bwd_tflops": {3.5*f/t_fb/1e12:.2f}}}')