Repository navigation
FA4 consumer Blackwell (sm_120) integration — squashed history #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 35 commits
ead67d3
bc67a9c
c0250ca
e595c95
a6b9896
bf10b6d
b7be538
3cd8cb6
59cf537
6c4f74f
59f01d6
0bbb25a
c200609
eaf806d
f82d0dc
6dba037
c184200
b02b07e
fefa96a
d80a771
22a7223
766ed20
12f0ce1
98688fd
bc58abc
fbf4f9d
8163761
71d5f97
89d78ff
3190fa4
4279d5c
08129f8
cb23d13
5933648
fc1f20c
b1dbdfb
6121d6a
7bc5df5
e87110e
fb02fc8
c410ecf
1944c3a
6c3e045
3406adb
bda31e1
e2b84bd
5c02393
ca456ab
44918b5
fc8cbad
98ed878
96ca9c0
7052e21
cbbab83
d16e381
bdda748
940cd96
6a94f8b
82d6441
c56ba0f
ddfec5d
6f644f9
0046957
5ed0361
890f238
46b2ae3
e79261e
73c992c
002cce0
1f7ce2f
6e646e0
5835c73
af04967
2ee8023
0816ef1
2402cb0
77aacb6
2c18385
467749e
2b32b86
2409214
b54df16
00756db
14c3779
849f660
c75d019
c46b814
df61ab6
4a948e9
c68c592
4460ebb
5579b12
7a08d7a
d7e4dba
3fa8105
1cc7ff6
69e1bcb
a369df7
4cabd92
baabf5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Copyright (c) 2025, Johnsonms. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # We recommend locking GPU clocks before running the benchmark to ensure consistent results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # This can be done using the following commands (2619 MHz is the max clock for B200): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # sudo nvidia-smi -i 0 -pm 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # sudo nvidia-smi -i 0 --lock-gpu-clocks 2619,2619 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # See more here: https://github.com/triton-lang/triton/blob/d9f10ebdc5da53f73eb852fde73d8d7d80b679d1/python/triton/testing.py#L487 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from triton.testing import do_bench | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from flash_attn.cute.interface import flash_attn_varlen_func | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| device = "cuda" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dtype = torch.bfloat16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seqlen_q = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nheads_q = 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nheads_kv = 1 # MQA-128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headdim = 64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headdim_v = 512 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| causal = True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| batch_size = 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page_sizes = [None, 16, 64, 128] # None = non-paged baseline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| torch.manual_seed(0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"\nMLA paged KV, nheads_q = {nheads_q}, nheads_kv = {nheads_kv}, headdim = {headdim}, headdim_v = {headdim_v}, causal = {causal}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for seqlen in [s * 1024 for s in [1, 2, 4, 8, 16, 32, 64]]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Varlen format: (total_tokens, nheads, hdim) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_q = batch_size * seqlen_q | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_k = batch_size * seqlen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q = torch.randn(total_q, nheads_q, headdim, dtype=dtype, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| k = torch.randn(total_k, nheads_kv, headdim, dtype=dtype, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v = torch.randn(total_k, nheads_kv, headdim_v, dtype=dtype, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qv = torch.randn(total_q, nheads_q, headdim_v, dtype=dtype, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except torch.OutOfMemoryError: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cu_seqlens_q = torch.arange(0, total_q + seqlen_q, seqlen_q, dtype=torch.int32, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cu_seqlens_k = torch.arange(0, total_k + seqlen, seqlen, dtype=torch.int32, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Mem I/O: KV read + Q/QV read + O write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_seqlen = seqlen * batch_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mem_io = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_seqlen * nheads_kv * (headdim + headdim_v) * 2 # K + V read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + q.numel() * 2 + qv.numel() * 2 # Q + QV read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + total_q * nheads_q * headdim_v * 2 # O write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # FLOPs: QK^T + PV (with qv, PV uses headdim_v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flops = seqlen_q * total_seqlen * nheads_q * (headdim + headdim_v * 2) * 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for page_size in page_sizes: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if page_size is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Non-paged baseline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn = lambda: flash_attn_varlen_func( # noqa: E731 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q, k, v, qv=qv, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=cu_seqlens_k, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_seqlen_q=seqlen_q, max_seqlen_k=seqlen, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| causal=causal, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = "non-paged" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create paged KV | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| num_pages_per_seq = (seqlen + page_size - 1) // page_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_pages = num_pages_per_seq * batch_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| k_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim, device=device, dtype=dtype) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim_v, device=device, dtype=dtype) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page_table = torch.zeros(batch_size, num_pages_per_seq, dtype=torch.int32, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for b in range(batch_size): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for p in range(num_pages_per_seq): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page_idx = b * num_pages_per_seq + p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| start = p * page_size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end = min(start + page_size, seqlen) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| k_offset = b * seqlen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if start < seqlen: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| k_paged[page_idx, :end - start] = k[k_offset + start:k_offset + end] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| v_paged[page_idx, :end - start] = v[k_offset + start:k_offset + end] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page_table[b, p] = page_idx | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seqused_k = torch.full((batch_size,), seqlen, dtype=torch.int32, device=device) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle OOM in the paged-KV allocation path as well. Lines 73-86 can still OOM and terminate the whole run, while the non-paged allocations already skip gracefully. 🛠️ Proposed fix else:
# Create paged KV
- num_pages_per_seq = (seqlen + page_size - 1) // page_size
- total_pages = num_pages_per_seq * batch_size
- k_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim, device=device, dtype=dtype)
- v_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim_v, device=device, dtype=dtype)
- page_table = torch.zeros(batch_size, num_pages_per_seq, dtype=torch.int32, device=device)
+ try:
+ num_pages_per_seq = (seqlen + page_size - 1) // page_size
+ total_pages = num_pages_per_seq * batch_size
+ k_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim, device=device, dtype=dtype)
+ v_paged = torch.zeros(total_pages, page_size, nheads_kv, headdim_v, device=device, dtype=dtype)
+ page_table = torch.zeros(batch_size, num_pages_per_seq, dtype=torch.int32, device=device)
+ except torch.OutOfMemoryError:
+ print(f"Skipping paged-{page_size} at seqlen={seqlen}: OOM")
+ continue📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn = lambda kp=k_paged, vp=v_paged, pt=page_table, su=seqused_k: flash_attn_varlen_func( # noqa: E731 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| q, kp, vp, qv=qv, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cu_seqlens_q=cu_seqlens_q, cu_seqlens_k=None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max_seqlen_q=seqlen_q, max_seqlen_k=None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| seqused_k=su, page_table=pt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| causal=causal, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| path = "TMA" if page_size == 128 else "cp.async" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label = f"paged-{page_size} ({path})" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn() # warmup / compile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| time.sleep(1) # avoid power throttling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t = do_bench(fn, warmup=5, rep=20) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"Seqlen = {seqlen}, {label}: {t * 1e3:.1f} us, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"{mem_io * 1e-9 / (t * 1e-3):.0f} GB/s, " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"{flops * 1e-12 / (t * 1e-3):.0f} TFLOPS/s" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"Arithmetic intensity: {flops / mem_io:.1f}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 110
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 2336
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 1799
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 961
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 961
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 961
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 961
🏁 Script executed:
Repository: thad0ctor/flash-attention
Length of output: 961
Pin
Jimver/cuda-toolkitto an immutable commit SHA..github/workflows/_build.ymluses the mutable tagJimver/cuda-toolkit@v0.2.35; resolve it to its commit SHA3d45d157f327c09c04b50ee6ccdea2d9d017ec76to prevent tag retargeting.🔒 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 zizmor (1.25.2)
[error] 80-80: unpinned action reference (unpinned-uses): action is not pinned to a hash (required by blanket policy)
(unpinned-uses)
🤖 Prompt for AI Agents
Source: Linters/SAST tools