Run this model with HF Jobs
Run kernels-community/flash-attn2 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/kernels-community/flash-attn2. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/kernels-community/flash-attn2 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use kernels-community/flash-attn2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use kernels-community/flash-attn2 with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("kernels-community/flash-attn2", version=1) - Notebooks
- Google Colab
- Kaggle
drbh commited on
Commit Β·
56449c1
1
Parent(s): 09eec95
feat: bump api on readme
Browse filesFiles changed (2)
- README.md+8-14
- scripts/readme_example.py+3-8
29 unmodified lines30313233343536378 unmodified lines4647484950515253545556573 unmodified lines6162636465666768697071721 unmodified line7475767778798030 unmodified lines1111121131141151161171 unmodified line1191201211221231241257 unmodified lines13313413513613713813914014114214314414514614714814915015115215329 unmodified linesflash_attn = get_kernel("kernels-community/flash-attn")device = torch.device("cuda")print("Flash Attention functions:", [i for i in dir(flash_attn) if i.startswith("mha")])# Create test tensorsB, S, H, D = 2, 5, 4, 8 # batch, seq_len, heads, head_dimq = k = v = torch.randn(B, S, H, D, device=device, dtype=torch.float16)8 unmodified lines# 1. Standard attentionprint("\n1. Standard attention:")out_ref = reference_attention(q, k, v)out_flash = flash_attn.mha_fwd(q=q,k=k,v=v,is_causal=False,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Reference output: {out_ref.shape}")print(f"Flash output: {out_flash.shape}")3 unmodified linesprint("\n2. Causal attention:")out_ref_causal = reference_attention(q, k, v, causal=True)out_causal = flash_attn.mha_fwd(q=q,k=k,v=v,is_causal=True,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Reference causal output: {out_ref_causal.shape}")print(f"Flash causal output: {out_causal.shape}")1 unmodified linedef var_reference_attention(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, causal=False):batch_size = cu_seqlens_q.shape[0] - 1# Return output in packed formattotal_tokens_q = q.shape[0]out = torch.zeros((total_tokens_q, q.shape[1], q.shape[2]), device=q.device, dtype=q.dtype)30 unmodified linesout_var_ref = var_reference_attention(q_var, k_var, v_var, cu_q, cu_k, max_seqlen_q=4, max_seqlen_k=5, causal=False)# Custom function to handle variableout_var = flash_attn.mha_varlen_fwd(q=q_var,k=k_var,v=v_var,1 unmodified linecu_seqlens_k=cu_k,max_seqlen_q=4,max_seqlen_k=5,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Variable length output: {out_var.shape}")print(f"Reference variable length output: {out_var_ref.shape}")7 unmodified lines``````txtReading inline script metadata from `flash-attn/scripts/readme_example.py`Fetching 4 files: 100%|βββββββββββββββββββββββββββββββββββββββββ| 4/4 [00:00<00:00, 33354.31it/s]Flash Attention functions: ['mha_bwd', 'mha_fwd', 'mha_fwd_kvcache', 'mha_varlen_bwd', 'mha_varlen_fwd']1. Standard attention:Reference output: torch.Size([2, 5, 4, 8])Flash output: torch.Size([2, 5, 4, 8])Outputs close: True1. Causal attention:Reference causal output: torch.Size([2, 5, 4, 8])Flash causal output: torch.Size([2, 5, 4, 8])Outputs close: True1. Variable length sequences:Variable length output: torch.Size([10, 4, 8])Reference variable length output: torch.Size([10, 4, 8])Outputs close: True29 unmodified lines3031323334358 unmodified lines44454647484950515253543 unmodified lines58596061626364656667681 unmodified line7071727374757630 unmodified lines1071081091101111121131 unmodified line1151161171181191207 unmodified lines12812913013113213313413513613713813914014114214314414514614729 unmodified linesflash_attn = get_kernel("kernels-community/flash-attn")device = torch.device("cuda")# Create test tensorsB, S, H, D = 2, 5, 4, 8 # batch, seq_len, heads, head_dimq = k = v = torch.randn(B, S, H, D, device=device, dtype=torch.float16)8 unmodified lines# 1. Standard attentionprint("\n1. Standard attention:")out_ref = reference_attention(q, k, v)out_flash = flash_attn.fwd(q=q,k=k,v=v,is_causal=False,)[0]print(f"Reference output: {out_ref.shape}")print(f"Flash output: {out_flash.shape}")3 unmodified linesprint("\n2. Causal attention:")out_ref_causal = reference_attention(q, k, v, causal=True)out_causal = flash_attn.fwd(q=q,k=k,v=v,is_causal=True,)[0]print(f"Reference causal output: {out_ref_causal.shape}")print(f"Flash causal output: {out_causal.shape}")1 unmodified linedef var_reference_attention(q, k, v, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, causal=False):batch_size = cu_seqlens_q.shape[0] - 1# Return output in packed format (same as flash attention)total_tokens_q = q.shape[0]out = torch.zeros((total_tokens_q, q.shape[1], q.shape[2]), device=q.device, dtype=q.dtype)30 unmodified linesout_var_ref = var_reference_attention(q_var, k_var, v_var, cu_q, cu_k, max_seqlen_q=4, max_seqlen_k=5, causal=False)# Custom function to handle variableout_var = flash_attn.varlen_fwd(q=q_var,k=k_var,v=v_var,1 unmodified linecu_seqlens_k=cu_k,max_seqlen_q=4,max_seqlen_k=5,)[0]print(f"Variable length output: {out_var.shape}")print(f"Reference variable length output: {out_var_ref.shape}")7 unmodified lines``````txtReading inline script metadata from `scripts/readme_example.py`Fetching 20 files: 100%|ββββββββββββββββββββββββββββββββββββββββββββββββββ| 20/20 [00:00<00:00, 16371.21it/s]1. Standard attention:Reference output: torch.Size([2, 5, 4, 8])Flash output: torch.Size([2, 5, 4, 8])Outputs close: True2. Causal attention:Reference causal output: torch.Size([2, 5, 4, 8])Flash causal output: torch.Size([2, 5, 4, 8])Outputs close: True3. Variable length sequences:Variable length output: torch.Size([10, 4, 8])Reference variable length output: torch.Size([10, 4, 8])Outputs close: True
12 unmodified lines13141516171819208 unmodified lines2930313233343536373839403 unmodified lines44454647484950515253545538 unmodified lines9495969798991001 unmodified line10210310410510610710812 unmodified linesflash_attn = get_kernel("kernels-community/flash-attn")device = torch.device("cuda")print("Flash Attention functions:", [i for i in dir(flash_attn) if i.startswith("mha")])# Create test tensorsB, S, H, D = 2, 5, 4, 8 # batch, seq_len, heads, head_dimq = k = v = torch.randn(B, S, H, D, device=device, dtype=torch.float16)8 unmodified lines# 1. Standard attentionprint("\n1. Standard attention:")out_ref = reference_attention(q, k, v)out_flash = flash_attn.mha_fwd(q=q,k=k,v=v,is_causal=False,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Reference output: {out_ref.shape}")print(f"Flash output: {out_flash.shape}")3 unmodified linesprint("\n2. Causal attention:")out_ref_causal = reference_attention(q, k, v, causal=True)out_causal = flash_attn.mha_fwd(q=q,k=k,v=v,is_causal=True,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Reference causal output: {out_ref_causal.shape}")print(f"Flash causal output: {out_causal.shape}")38 unmodified linesout_var_ref = var_reference_attention(q_var, k_var, v_var, cu_q, cu_k, max_seqlen_q=4, max_seqlen_k=5, causal=False)# Custom function to handle variableout_var = flash_attn.mha_varlen_fwd(q=q_var,k=k_var,v=v_var,1 unmodified linecu_seqlens_k=cu_k,max_seqlen_q=4,max_seqlen_k=5,softmax_scale=1.0 / (D ** 0.5), # scale factor)[0]print(f"Variable length output: {out_var.shape}")print(f"Reference variable length output: {out_var_ref.shape}")12 unmodified lines1314151617188 unmodified lines27282930313233343536373 unmodified lines414243444546474849505138 unmodified lines909192939495961 unmodified line989910010110210312 unmodified linesflash_attn = get_kernel("kernels-community/flash-attn")device = torch.device("cuda")# Create test tensorsB, S, H, D = 2, 5, 4, 8 # batch, seq_len, heads, head_dimq = k = v = torch.randn(B, S, H, D, device=device, dtype=torch.float16)8 unmodified lines# 1. Standard attentionprint("\n1. Standard attention:")out_ref = reference_attention(q, k, v)out_flash = flash_attn.fwd(q=q,k=k,v=v,is_causal=False,)[0]print(f"Reference output: {out_ref.shape}")print(f"Flash output: {out_flash.shape}")3 unmodified linesprint("\n2. Causal attention:")out_ref_causal = reference_attention(q, k, v, causal=True)out_causal = flash_attn.fwd(q=q,k=k,v=v,is_causal=True,)[0]print(f"Reference causal output: {out_ref_causal.shape}")print(f"Flash causal output: {out_causal.shape}")38 unmodified linesout_var_ref = var_reference_attention(q_var, k_var, v_var, cu_q, cu_k, max_seqlen_q=4, max_seqlen_k=5, causal=False)# Custom function to handle variableout_var = flash_attn.varlen_fwd(q=q_var,k=k_var,v=v_var,1 unmodified linecu_seqlens_k=cu_k,max_seqlen_q=4,max_seqlen_k=5,)[0]print(f"Variable length output: {out_var.shape}")print(f"Reference variable length output: {out_var_ref.shape}")
README.md+8-14
scripts/readme_example.py+3-8