-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassert_flash.py
46 lines (31 loc) · 985 Bytes
/
assert_flash.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
import torch
from ring_attention_pytorch import (
default_attention,
ring_flash_attn
)
# variables
causal = True
seq_len = 62
bucket_size = 4
# base qkv
q = torch.randn(2, seq_len, 2, 16)
k = torch.randn(2, seq_len, 2, 16)
v = torch.randn(2, seq_len, 2, 16)
# flash and regular qkv's
fq = q.clone().requires_grad_()
fk = k.clone().requires_grad_()
fv = v.clone().requires_grad_()
rq = q.clone().requires_grad_()
rk = k.clone().requires_grad_()
rv = v.clone().requires_grad_()
# forward
o = default_attention(rq, rk, rv, causal = causal)
fo = ring_flash_attn(fq, fk, fv, bucket_size = bucket_size, causal = causal)
assert torch.allclose(o, fo, atol = 1e-6)
# backwards
o.sum().backward()
fo.sum().backward()
assert torch.allclose(rq.grad, fq.grad, atol = 1e-6)
assert torch.allclose(rk.grad, fk.grad, atol = 1e-6)
assert torch.allclose(rv.grad, fv.grad, atol = 1e-6)
print('✅ outputs and gradients are same between regular attention and naive flash attention')