Flash attention from
llama.cpp, as a torch op (
flash_attn) and
as a
transformers attention implementation (
flash_attn_forward). Grouped-query attention is native, so
k and v are passed unexpanded.
1import torch
2from kernels import get_kernel
3
4attn = get_kernel("marcsun13/ggml-attn", version=1)
5
6q = torch.randn(1, 16, 1, 128, device="mps") # (n_seqs, n_heads, n_q, head_dim)
7k = torch.randn(1, 4, 512, 128, device="mps") # 4 kv heads, left unexpanded
8v = torch.randn(1, 4, 512, 128, device="mps")
9
10out = attn.flash_attn(q, k, v) # (1, 1, 16, 128) — tokens before heads
1model = AutoModelForCausalLM.from_pretrained(
2 ..., attn_implementation="marcsun13/ggml-attn"
3)