FlashAttention3 Forward Compatibility¶
This document is a quick reference for the current FlashAttention-3-compatible forward coverage provided by MATE on MUSA.
Compatibility Overview¶
If you already use FlashAttention-3 style Python APIs, start here to see what works on MATE and how to try it.
Install
flash_attn_3.Import
flash_attn_interface.Call the usual forward APIs.
Check the matrix below for supported modes, dtypes, and limits.
This page covers how to get started with forward compatibility on MUSA. It does not cover kernel internals, backward, or autograd behavior.
Getting Started¶
Install the compatibility wrapper from the MUSA wheel source:
python -m pip install flash_attn_3 \
--index-url https://dl.mthreads.com/repo/api/pypi/pypi/simple
Minimal forward example:
import torch
from flash_attn_interface import flash_attn_varlen_func
device = "musa"
dtype = torch.bfloat16
q = torch.randn((96, 32, 128), device=device, dtype=dtype)
k = torch.randn((160, 8, 128), device=device, dtype=dtype)
v = torch.randn((160, 8, 128), device=device, dtype=dtype)
cu_seqlens_q = torch.tensor([0, 32, 96], device=device, dtype=torch.int32)
cu_seqlens_k = torch.tensor([0, 64, 160], device=device, dtype=torch.int32)
out = flash_attn_varlen_func(
q=q,
k=k,
v=v,
cu_seqlens_q=cu_seqlens_q,
cu_seqlens_k=cu_seqlens_k,
max_seqlen_q=64,
max_seqlen_k=96,
causal=False,
)
If your integration uses KV cache, paged KV, scheduler metadata, FP8, or
only_qv, check the compatibility matrix below before porting more code.
Minimal KV-cache example:
import torch
from flash_attn_interface import flash_attn_with_kvcache
device = "musa"
dtype = torch.bfloat16
q = torch.randn((1, 4, 32, 128), device=device, dtype=dtype)
k_cache = torch.randn((8, 16, 8, 128), device=device, dtype=dtype)
v_cache = torch.randn((8, 16, 8, 128), device=device, dtype=dtype)
cache_seqlens = torch.tensor([32], device=device, dtype=torch.int32)
block_table = torch.tensor([[0, 1]], device=device, dtype=torch.int32)
out, softmax_lse = flash_attn_with_kvcache(
q=q,
k_cache=k_cache,
v_cache=v_cache,
cache_seqlens=cache_seqlens,
block_table=block_table,
causal=True,
)
At a Glance¶
Area |
Status |
Notes |
|---|---|---|
Q Mode |
✅ Supported |
|
KV Mode |
✅ Supported |
|
Append New KV |
✅ Supported |
|
RoPE Input |
✅ Supported |
|
Cache Index Options |
✅ Supported |
|
Mask Mode |
✅ Supported |
|
Score Mode |
✅ Supported |
Standard softmax and |
Page Size |
✅ Supported |
|
Dtype |
✅ Supported |
|
QV Input |
✅ Supported |
The forward path supports an optional |
HeadDim |
✅ Supported |
Any |
Optimization |
✅ Supported |
|
Output |
✅ Supported |
|
MATE Extensions¶
Extension |
Status |
Notes |
|---|---|---|
Context Parallel |
✅ Supported |
|
Learnable Sink |
✅ Supported |
Supported on the local-attention path |
Notes¶
This page summarizes the compatibility surface, not every internal kernel detail.
The statement
Any headdim <= 512refers to the supported forward-path head-dimension range.FP8 forward support includes
torch.float8_e4m3fnandtorch.float8_e5m2; pass optionalq_descale,k_descale, andv_descaletensors with shape(batch_size, num_heads_kv)when scale factors are required.When both
qand the optionalqvinput are FP8,q_descaleapplies to both query tensors;k_descaleandv_descalestill apply to the KV inputs.RoPE is supported only when appending new KV through
k/v;rotary_dimmust be<= headdimand divisible by 16.Local + attention_chunkrequires MUSA SDK >= 5.1.0.FP8 attention works on the forward path today. For best performance, use MUSA SDK 5.2.0 or newer when available.
For wrapper-level usage, see the FlashAttention wrapper page.