Run this model with HF Jobs
Run replicate/flash-mla 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/replicate/flash-mla. 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/replicate/flash-mla 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 replicate/flash-mla with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/flash-mla 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("replicate/flash-mla", version=1) - Notebooks
- Google Colab
- Kaggle
Build uploaded using `kernels`.
Browse filesFiles changed (24)
- build/torch210-cxx11-cu128-x86_64-linux/_flash_mla_cuda_09f70ef.abi3.so → build/torch210-cxx11-cu128-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soRenamed+1-1
- build/torch210-cxx11-cu128-x86_64-linux/_ops.py+3-3
- build/torch210-cxx11-cu128-x86_64-linux/flash_mla/__init__.py+2-2
- build/torch210-cxx11-cu130-x86_64-linux/_flash_mla_cuda_09f70ef.abi3.so → build/torch210-cxx11-cu130-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soRenamed+1-1
- build/torch210-cxx11-cu130-x86_64-linux/_ops.py+3-3
- build/torch210-cxx11-cu130-x86_64-linux/flash_mla/__init__.py+2-2
- build/torch211-cxx11-cu128-x86_64-linux/__init__.pyAdded+163
- build/torch211-cxx11-cu128-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
- build/torch211-cxx11-cu128-x86_64-linux/_ops.pyAdded+9
- build/torch211-cxx11-cu128-x86_64-linux/flash_mla/__init__.pyAdded+26
- build/torch211-cxx11-cu128-x86_64-linux/flash_mla_interface.pyAdded+435
- build/torch211-cxx11-cu128-x86_64-linux/metadata.jsonAdded+11
- build/torch211-cxx11-cu130-x86_64-linux/__init__.pyAdded+163
- build/torch211-cxx11-cu130-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
- build/torch211-cxx11-cu130-x86_64-linux/_ops.pyAdded+9
- build/torch211-cxx11-cu130-x86_64-linux/flash_mla/__init__.pyAdded+26
- build/torch211-cxx11-cu130-x86_64-linux/flash_mla_interface.pyAdded+435
- build/torch211-cxx11-cu130-x86_64-linux/metadata.jsonAdded+12
- build/torch29-cxx11-cu129-x86_64-linux/__init__.pyAdded+163
- build/torch29-cxx11-cu129-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
- build/torch29-cxx11-cu129-x86_64-linux/_ops.pyAdded+9
- build/torch29-cxx11-cu129-x86_64-linux/flash_mla/__init__.pyAdded+26
- build/torch29-cxx11-cu129-x86_64-linux/flash_mla_interface.pyAdded+435
- build/torch29-cxx11-cu129-x86_64-linux/metadata.jsonAdded+12
1223version https://git-lfs.github.com/spec/v1oid sha256:159f6907fce13b98e5b9efd95c8c51f14087d5e4014d2b79fea6c9ad4453f372oid sha256:a5a848e71e943d30f41eea9b201cb51b7ebdf5e962652e836d46d4fc3f4769f6size 3534072123234567899import torchfrom . import _flash_mla_cuda_09f70efops = torch.ops._flash_mla_cuda_09f70effrom . import _flash_mla_cuda_89d7fc1ops = torch.ops._flash_mla_cuda_89d7fc1
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_flash_mla_cuda_09f70ef::{op_name}" return f"_flash_mla_cuda_89d7fc1::{op_name}"1233445678910import ctypesimport importlib.utilimport sys
import importlibfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that1223version https://git-lfs.github.com/spec/v1oid sha256:c1f914866fdfdcf91845a2d11c0218309b99020025fd35ed547230ae8e1ba519oid sha256:ebb93cd1323c0ade7e8f102af41d8c4b310722ceea363da2ceadbd831dbf723esize 9395216123234567899import torchfrom . import _flash_mla_cuda_09f70efops = torch.ops._flash_mla_cuda_09f70effrom . import _flash_mla_cuda_89d7fc1ops = torch.ops._flash_mla_cuda_89d7fc1
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_flash_mla_cuda_09f70ef::{op_name}" return f"_flash_mla_cuda_89d7fc1::{op_name}"1233445678910import ctypesimport importlib.utilimport sys
import importlibfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163from typing import Optional, Tupleimport torch
from .flash_mla_interface import FlashMLASchedMetafrom . import flash_mla_interface as _impl
def get_mla_metadata(*args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: return _impl.get_mla_metadata(*args, **kwargs)
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_mla_with_kvcache( q=q, k_cache=k_cache, block_table=block_table, cache_seqlens=cache_seqlens, head_dim_v=head_dim_v, tile_scheduler_metadata=tile_scheduler_metadata, num_splits=num_splits, softmax_scale=softmax_scale, causal=causal, is_fp8_kvcache=is_fp8_kvcache, indices=indices, attn_sink=attn_sink, extra_k_cache=extra_k_cache, extra_indices_in_kvcache=extra_indices_in_kvcache, topk_length=topk_length, extra_topk_length=extra_topk_length, )
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: return _impl.flash_mla_sparse_fwd( q=q, kv=kv, indices=indices, sm_scale=sm_scale, d_v=d_v, attn_sink=attn_sink, topk_length=topk_length, )
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_func( q=q, k=k, v=v, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_qkvpacked_func( qkv=qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_kvpacked_func( q=q, kv=kv, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
__all__ = [ "__version__", "FlashMLASchedMeta", "get_mla_metadata", "flash_mla_with_kvcache", "flash_attn_varlen_func", "flash_attn_varlen_qkvpacked_func", "flash_attn_varlen_kvpacked_func", "flash_mla_sparse_fwd",]123version https://git-lfs.github.com/spec/v1oid sha256:3a1b76510b5134347620c4874e7f3e272dcbe3360e522fd7ffaede1d7f850cf6size 3522920123456789import torchfrom . import _flash_mla_cuda_89d7fc1ops = torch.ops._flash_mla_cuda_89d7fc1
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_flash_mla_cuda_89d7fc1::{op_name}"1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435from typing import Optional, Tupleimport dataclasses
import torch
from ._ops import ops as flash_mla_cuda
@dataclasses.dataclassclass FlashMLASchedMeta: """ A class that stores the tile scheduler metadata of FlashMLA """
@dataclasses.dataclass class Config: b: int s_q: int h_q: int page_block_size: int h_k: int
causal: bool is_fp8_kvcache: bool topk: Optional[int]
extra_page_block_size: Optional[int] extra_topk: Optional[int]
have_initialized: bool = False
config: Optional[Config] = None
tile_scheduler_metadata: Optional[torch.Tensor] = None # (num_sm_parts, TileSchedulerMetaDataSize), dtype torch.int32. num_splits: Optional[torch.Tensor] = None # (1), dtype torch.int32.
def get_mla_metadata( *args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: """ Returns an empty instance of FlashMLASchedMeta. The actual scheduling metadata will be generated during the first invocation of flash_mla_with_kvcache.
Arguments: This function does not need any arguments, but we keep *args and **kwargs to be compatible with the old interface.
Return: A tuple. Due to historical reasons, we return a tuple of (FlashMLASchedMeta, None) now. Only the first element is useful. """ return FlashMLASchedMeta(), None
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]: """ Arguments: q: (batch_size, seq_len_q, num_heads_q, head_dim). k_cache: (num_blocks, page_block_size, num_heads_k, head_dim). Different modes (including fp8/bf16, and sparsity) has different KV cache layouts. See comments below for details. The KV cache must be contiguously valid for sparse attention on sm100. Here "contiguously valid" means that every byte, from the very beginning of the KV cache, till the last byte in the KV cache, is valid memory address to visit (i.e. won't IMA). In other words, the KV cache could be a slice of a larger array, but cannot be a list of disjoint memory blocks. block_table: (batch_size, max_num_blocks_per_seq), torch.int32. Can be None when sparse attention is used. cache_seqlens: (batch_size), torch.int32. Can be None when sparse attention is used. head_dim_v: Head_dim of v. Must be 512 sched_meta: FlashMLASchedMeta, return by get_mla_metadata. You may reuse the same sched_meta across different invocations, but only when the tensor shapes and the values of cache_seqlens, topk_length, and extra_topk_length remain the same. num_splits_placeholder: must be "None" (to be compatible with the old interface). softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim_k). causal: bool. Whether to apply causal attention mask. Only valid for dense attention is_fp8_kvcache: bool. indices: (batch_size, seq_len_q, topk). KV indices when sparse attention is enabled. Pay attention that indices_in_kvcache[i][j][k] = (the index of the page block where token t resides) * block_size + (the offset of token t among the page block), where t is the k-th token of the j-th q-sequence in the i-th batch. attn_sink: Optional[torch.Tensor], (num_heads_q, ), torch.float32. If presented, the final output will be scaled by exp(lse) / (exp(lse) + exp(attn_sink)). Have no affect on the returned softmax_lse. +inf will cause the result to become 0. extra_k_cache and extra_indices_in_kvcache: If provided, will attend to these extra tokens in addition to those in k_cache and indices_in_kvcache. Their format requirements are the same as k_cache and indices_in_kvcache respectively. topk_length/extra_topk_length: (batch_size, ), torch.int32. If provided, only the leftmost topk_length indices will be processed. Useful when the actual topk for different queries are different so that we can save some computation, compared to masking. For DeepSeek V3, DeepSeek V3.1, and DeepSeek V3.2: head_dim should be 576 while head_dim_v should be 512. In FP8+sparse mode, each token's KV cache is 656 Bytes, structured as: - The shape of the tensor `k_cache` is (num_blocks, page_block_size, num_heads_k, head_dim), and num_heads_k must be 1. - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3 values. - Next 16 bytes: Scale factors, containing 4 float32 values. The first float32 is the scale for the first 128 float8_e4m3 values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy.
Return: out: (batch_size, seq_len_q, num_heads_q, head_dim_v). softmax_lse: (batch_size, num_heads_q, seq_len_q), torch.float32. """ sched_meta = tile_scheduler_metadata indices_in_kvcache = indices assert isinstance(sched_meta, FlashMLASchedMeta), "tile_scheduler_metadata must be of type FlashMLASchedMeta" assert num_splits is None, "num_splits must be None"
topk = indices_in_kvcache.shape[-1] if indices_in_kvcache is not None else None extra_k_page_block_size = extra_k_cache.shape[1] if extra_k_cache is not None else None extra_topk = extra_indices_in_kvcache.shape[-1] if extra_indices_in_kvcache is not None else None if softmax_scale is None: softmax_scale = q.shape[-1] ** (-0.5)
if not sched_meta.have_initialized: # Sanity check. We only perform sanity check during the first invocation to save CPU time. if indices_in_kvcache is not None: assert not causal, "causal must be False when indices_in_kvcache is not None (i.e. sparse attention is enabled)" # Initialize the tile scheduler metadata during the first invocation. sched_meta.have_initialized = True sched_meta.config = FlashMLASchedMeta.Config( q.shape[0], q.shape[1], q.shape[2], k_cache.shape[1], k_cache.shape[2],
causal, is_fp8_kvcache, topk,
extra_k_page_block_size, extra_topk, ) else: # Check whether the input arguments are consistent with sched_meta helper_msg = " Your input arguments are inconsistent with sched_meta. Please make sure the input arguments are consistent across different invocations of flash_mla_with_kvcache on the same sched_meta." assert sched_meta.config is not None assert sched_meta.config.b == q.shape[0], "sched_meta.config.b must be equal to batch_size." + helper_msg assert sched_meta.config.s_q == q.shape[1], "sched_meta.config.s_q must be equal to seq_len_q." + helper_msg assert sched_meta.config.h_q == q.shape[2], "sched_meta.config.h_q must be equal to num_heads_q." + helper_msg assert sched_meta.config.page_block_size == k_cache.shape[1], "sched_meta.config.page_block_size must be equal to page_block_size." + helper_msg assert sched_meta.config.h_k == k_cache.shape[2], "sched_meta.config.h_k must be equal to num_heads_k." + helper_msg assert sched_meta.config.causal == causal, "sched_meta.config.causal must be equal to causal." + helper_msg assert sched_meta.config.is_fp8_kvcache == is_fp8_kvcache, "sched_meta.config.is_fp8_kvcache must be equal to is_fp8_kvcache." + helper_msg assert sched_meta.config.topk == topk, "sched_meta.config.topk must be equal to the last dim of indices_in_kvcache." + helper_msg assert sched_meta.config.extra_page_block_size == extra_k_page_block_size, "sched_meta.config.extra_page_block_size must be equal to the page_block_size of extra_k_cache." + helper_msg assert sched_meta.config.extra_topk == extra_topk, "sched_meta.config.extra_topk must be equal to the last dim of extra_indices_in_kvcache." + helper_msg
if topk is not None: # Sparse attention assert not causal, "causal must be False when sparse attention is enabled" assert is_fp8_kvcache, "is_fp8_kvcache must be True when sparse attention is enabled" out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.sparse_decode_fwd( q, k_cache, indices_in_kvcache, topk_length, attn_sink, sched_meta.tile_scheduler_metadata, sched_meta.num_splits, extra_k_cache, extra_indices_in_kvcache, extra_topk_length, head_dim_v, softmax_scale ) else: # Dense attention assert indices_in_kvcache is None and attn_sink is None and extra_k_cache is None and extra_indices_in_kvcache is None and topk_length is None and extra_topk_length is None, "indices_in_kvcache, attn_sink, extra_k_cache, extra_indices_in_kvcache, topk_length and extra_topk_length must be None when dense attention is used." assert block_table is not None and cache_seqlens is not None, "block_table and cache_seqlens must be provided when dense attention is used." out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.dense_decode_fwd( q, k_cache, head_dim_v, cache_seqlens, block_table, softmax_scale, causal, sched_meta.tile_scheduler_metadata, sched_meta.num_splits ) sched_meta.tile_scheduler_metadata = new_tile_scheduler_metadata sched_meta.num_splits = new_num_splits return (out, lse)
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Sparse attention prefill kernel
Args: q: [s_q, h_q, d_qk], bfloat16 kv: [s_kv, h_kv, d_qk], bfloat16 indices: [s_q, h_kv, topk], int32. Invalid indices should be set to -1 or numbers >= s_kv sm_scale: float d_v: The dimension of value vectors. Can only be 512 attn_sink: optional, [h_q], float32. If attn_sink is provided, when computing output, output will be additionally multiplied by exp(lse) / (exp(lse) + exp(attn_sink)). +-inf in attn_sink will be handled normally (i.e., -inf has no effect, +inf will make corresponding output all zeros). This argument has no effect on lse and max_logits. topk_length: optional, [s_q], int32. If provided, the i-th q token will only attend to k tokens specified by indices[i, :, :topk_length[i]], ignoring later k/v tokens (even if provided in indices). In extremely rare cases (topk_length provided, there is a valid topk index between topk_length[i] ~ s_kv, and that topk index points to a k token containing NaN), operator output will contain NaN, so please avoid this situation.
Returns: (output, max_logits, lse) Please refer to tests/ref.py for the precise definitions of these parameters. - output: [s_q, h_q, d_v], bfloat16 - max_logits: [s_q, h_q], float - lse: [s_q, h_q], float, log-sum-exp of attention scores """ results = flash_mla_cuda.sparse_prefill_fwd( q, kv, indices, sm_scale, d_v, attn_sink, topk_length ) return results
def _flash_attn_varlen_forward( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, out: Optional[torch.Tensor] = None, lse: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if out is None: out = torch.empty(qo_total_len, num_qo_heads, head_dim_vo, device=q.device, dtype=q.dtype) if lse is None: # Make lse contiguous on seqlen dim lse = torch.empty(num_qo_heads, qo_total_len, device=q.device, dtype=torch.float32).T
workspace_buffer = torch.empty(32 * 1024 * 1024, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_fwd( workspace_buffer, q, k, v, cu_seqlens_qo, cu_seqlens_kv, out, lse, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return out, lse
def _flash_attn_varlen_backward( do: torch.Tensor, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, out: torch.Tensor, lse: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dq: Optional[torch.Tensor] = None, dk: Optional[torch.Tensor] = None, dv: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
# TODO: fix bwd GQA if num_qo_heads != num_kv_heads: raise ValueError(f"SM100 bwd doesn't support GQA now. num_qo_heads: {num_qo_heads}, num_kv_heads: {num_kv_heads}.")
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if dq is None: dq = torch.empty(qo_total_len, num_qo_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dk is None: dk = torch.empty(kv_total_len, num_kv_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dv is None: dv = torch.empty(kv_total_len, num_kv_heads, head_dim_vo, device=q.device, dtype=q.dtype)
max_seqlen_qo_aligned = (max_seqlen_qo + 7) // 8 * 8 bs = cu_seqlens_qo.shape[0] - 1 workspace_bytes = 0 workspace_bytes += 4 * bs * max_seqlen_qo_aligned * num_qo_heads * head_dim_qk # dQ_acc workspace_bytes += 4 * max_seqlen_qo_aligned * bs * num_qo_heads * 2 # sum_OdO and scaled_lse if num_qo_heads != num_kv_heads: workspace_bytes += 2 * kv_total_len * num_qo_heads * (head_dim_qk + head_dim_vo) # dKV_acc workspace_buffer = torch.empty(workspace_bytes, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_bwd( workspace_buffer, do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, dq, dk, dv, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return dq, dk, dv
class FlashAttnVarlenFunc(torch.autograd.Function): def forward( ctx, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True, ) -> Tuple[torch.Tensor, torch.Tensor]: out, lse = _flash_attn_varlen_forward( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal=causal, softmax_scale=softmax_scale, is_varlen=is_varlen, ) ctx.save_for_backward(q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv) ctx.max_seqlen_qo = max_seqlen_qo ctx.max_seqlen_kv = max_seqlen_kv ctx.causal = causal ctx.softmax_scale = softmax_scale ctx.is_varlen = is_varlen return out, lse
def backward( ctx, do: torch.Tensor, dlse: torch.Tensor, ): del dlse # LSE doesn't support backward currently q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv = ctx.saved_tensors dq, dk, dv = _flash_attn_varlen_backward( do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, ctx.max_seqlen_qo, ctx.max_seqlen_kv, causal=ctx.causal, softmax_scale=ctx.softmax_scale, is_varlen=ctx.is_varlen, ) return dq, dk, dv, None, None, None, None, None, None, None
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( qkv[:, :, :head_dim_qk], qkv[:, :, head_dim_qk:head_dim_qk * 2], qkv[:, :, head_dim_qk * 2:], cu_seqlens, cu_seqlens, max_seqlen, max_seqlen, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, kv[:, :, :head_dim_qk], kv[:, :, head_dim_qk:], cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )1234567891011{ "version": 1, "license": "MIT", "python-depends": [], "backend": { "type": "cuda", "archs": [ "9.0a" ] }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163from typing import Optional, Tupleimport torch
from .flash_mla_interface import FlashMLASchedMetafrom . import flash_mla_interface as _impl
def get_mla_metadata(*args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: return _impl.get_mla_metadata(*args, **kwargs)
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_mla_with_kvcache( q=q, k_cache=k_cache, block_table=block_table, cache_seqlens=cache_seqlens, head_dim_v=head_dim_v, tile_scheduler_metadata=tile_scheduler_metadata, num_splits=num_splits, softmax_scale=softmax_scale, causal=causal, is_fp8_kvcache=is_fp8_kvcache, indices=indices, attn_sink=attn_sink, extra_k_cache=extra_k_cache, extra_indices_in_kvcache=extra_indices_in_kvcache, topk_length=topk_length, extra_topk_length=extra_topk_length, )
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: return _impl.flash_mla_sparse_fwd( q=q, kv=kv, indices=indices, sm_scale=sm_scale, d_v=d_v, attn_sink=attn_sink, topk_length=topk_length, )
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_func( q=q, k=k, v=v, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_qkvpacked_func( qkv=qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_kvpacked_func( q=q, kv=kv, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
__all__ = [ "__version__", "FlashMLASchedMeta", "get_mla_metadata", "flash_mla_with_kvcache", "flash_attn_varlen_func", "flash_attn_varlen_qkvpacked_func", "flash_attn_varlen_kvpacked_func", "flash_mla_sparse_fwd",]123version https://git-lfs.github.com/spec/v1oid sha256:7e585a4092d2aaab0ba89ddf1e266034c0e574fd9388e5a6d2cb21948e80cce0size 9383872123456789import torchfrom . import _flash_mla_cuda_89d7fc1ops = torch.ops._flash_mla_cuda_89d7fc1
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_flash_mla_cuda_89d7fc1::{op_name}"1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435from typing import Optional, Tupleimport dataclasses
import torch
from ._ops import ops as flash_mla_cuda
@dataclasses.dataclassclass FlashMLASchedMeta: """ A class that stores the tile scheduler metadata of FlashMLA """
@dataclasses.dataclass class Config: b: int s_q: int h_q: int page_block_size: int h_k: int
causal: bool is_fp8_kvcache: bool topk: Optional[int]
extra_page_block_size: Optional[int] extra_topk: Optional[int]
have_initialized: bool = False
config: Optional[Config] = None
tile_scheduler_metadata: Optional[torch.Tensor] = None # (num_sm_parts, TileSchedulerMetaDataSize), dtype torch.int32. num_splits: Optional[torch.Tensor] = None # (1), dtype torch.int32.
def get_mla_metadata( *args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: """ Returns an empty instance of FlashMLASchedMeta. The actual scheduling metadata will be generated during the first invocation of flash_mla_with_kvcache.
Arguments: This function does not need any arguments, but we keep *args and **kwargs to be compatible with the old interface.
Return: A tuple. Due to historical reasons, we return a tuple of (FlashMLASchedMeta, None) now. Only the first element is useful. """ return FlashMLASchedMeta(), None
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]: """ Arguments: q: (batch_size, seq_len_q, num_heads_q, head_dim). k_cache: (num_blocks, page_block_size, num_heads_k, head_dim). Different modes (including fp8/bf16, and sparsity) has different KV cache layouts. See comments below for details. The KV cache must be contiguously valid for sparse attention on sm100. Here "contiguously valid" means that every byte, from the very beginning of the KV cache, till the last byte in the KV cache, is valid memory address to visit (i.e. won't IMA). In other words, the KV cache could be a slice of a larger array, but cannot be a list of disjoint memory blocks. block_table: (batch_size, max_num_blocks_per_seq), torch.int32. Can be None when sparse attention is used. cache_seqlens: (batch_size), torch.int32. Can be None when sparse attention is used. head_dim_v: Head_dim of v. Must be 512 sched_meta: FlashMLASchedMeta, return by get_mla_metadata. You may reuse the same sched_meta across different invocations, but only when the tensor shapes and the values of cache_seqlens, topk_length, and extra_topk_length remain the same. num_splits_placeholder: must be "None" (to be compatible with the old interface). softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim_k). causal: bool. Whether to apply causal attention mask. Only valid for dense attention is_fp8_kvcache: bool. indices: (batch_size, seq_len_q, topk). KV indices when sparse attention is enabled. Pay attention that indices_in_kvcache[i][j][k] = (the index of the page block where token t resides) * block_size + (the offset of token t among the page block), where t is the k-th token of the j-th q-sequence in the i-th batch. attn_sink: Optional[torch.Tensor], (num_heads_q, ), torch.float32. If presented, the final output will be scaled by exp(lse) / (exp(lse) + exp(attn_sink)). Have no affect on the returned softmax_lse. +inf will cause the result to become 0. extra_k_cache and extra_indices_in_kvcache: If provided, will attend to these extra tokens in addition to those in k_cache and indices_in_kvcache. Their format requirements are the same as k_cache and indices_in_kvcache respectively. topk_length/extra_topk_length: (batch_size, ), torch.int32. If provided, only the leftmost topk_length indices will be processed. Useful when the actual topk for different queries are different so that we can save some computation, compared to masking. For DeepSeek V3, DeepSeek V3.1, and DeepSeek V3.2: head_dim should be 576 while head_dim_v should be 512. In FP8+sparse mode, each token's KV cache is 656 Bytes, structured as: - The shape of the tensor `k_cache` is (num_blocks, page_block_size, num_heads_k, head_dim), and num_heads_k must be 1. - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3 values. - Next 16 bytes: Scale factors, containing 4 float32 values. The first float32 is the scale for the first 128 float8_e4m3 values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy.
Return: out: (batch_size, seq_len_q, num_heads_q, head_dim_v). softmax_lse: (batch_size, num_heads_q, seq_len_q), torch.float32. """ sched_meta = tile_scheduler_metadata indices_in_kvcache = indices assert isinstance(sched_meta, FlashMLASchedMeta), "tile_scheduler_metadata must be of type FlashMLASchedMeta" assert num_splits is None, "num_splits must be None"
topk = indices_in_kvcache.shape[-1] if indices_in_kvcache is not None else None extra_k_page_block_size = extra_k_cache.shape[1] if extra_k_cache is not None else None extra_topk = extra_indices_in_kvcache.shape[-1] if extra_indices_in_kvcache is not None else None if softmax_scale is None: softmax_scale = q.shape[-1] ** (-0.5)
if not sched_meta.have_initialized: # Sanity check. We only perform sanity check during the first invocation to save CPU time. if indices_in_kvcache is not None: assert not causal, "causal must be False when indices_in_kvcache is not None (i.e. sparse attention is enabled)" # Initialize the tile scheduler metadata during the first invocation. sched_meta.have_initialized = True sched_meta.config = FlashMLASchedMeta.Config( q.shape[0], q.shape[1], q.shape[2], k_cache.shape[1], k_cache.shape[2],
causal, is_fp8_kvcache, topk,
extra_k_page_block_size, extra_topk, ) else: # Check whether the input arguments are consistent with sched_meta helper_msg = " Your input arguments are inconsistent with sched_meta. Please make sure the input arguments are consistent across different invocations of flash_mla_with_kvcache on the same sched_meta." assert sched_meta.config is not None assert sched_meta.config.b == q.shape[0], "sched_meta.config.b must be equal to batch_size." + helper_msg assert sched_meta.config.s_q == q.shape[1], "sched_meta.config.s_q must be equal to seq_len_q." + helper_msg assert sched_meta.config.h_q == q.shape[2], "sched_meta.config.h_q must be equal to num_heads_q." + helper_msg assert sched_meta.config.page_block_size == k_cache.shape[1], "sched_meta.config.page_block_size must be equal to page_block_size." + helper_msg assert sched_meta.config.h_k == k_cache.shape[2], "sched_meta.config.h_k must be equal to num_heads_k." + helper_msg assert sched_meta.config.causal == causal, "sched_meta.config.causal must be equal to causal." + helper_msg assert sched_meta.config.is_fp8_kvcache == is_fp8_kvcache, "sched_meta.config.is_fp8_kvcache must be equal to is_fp8_kvcache." + helper_msg assert sched_meta.config.topk == topk, "sched_meta.config.topk must be equal to the last dim of indices_in_kvcache." + helper_msg assert sched_meta.config.extra_page_block_size == extra_k_page_block_size, "sched_meta.config.extra_page_block_size must be equal to the page_block_size of extra_k_cache." + helper_msg assert sched_meta.config.extra_topk == extra_topk, "sched_meta.config.extra_topk must be equal to the last dim of extra_indices_in_kvcache." + helper_msg
if topk is not None: # Sparse attention assert not causal, "causal must be False when sparse attention is enabled" assert is_fp8_kvcache, "is_fp8_kvcache must be True when sparse attention is enabled" out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.sparse_decode_fwd( q, k_cache, indices_in_kvcache, topk_length, attn_sink, sched_meta.tile_scheduler_metadata, sched_meta.num_splits, extra_k_cache, extra_indices_in_kvcache, extra_topk_length, head_dim_v, softmax_scale ) else: # Dense attention assert indices_in_kvcache is None and attn_sink is None and extra_k_cache is None and extra_indices_in_kvcache is None and topk_length is None and extra_topk_length is None, "indices_in_kvcache, attn_sink, extra_k_cache, extra_indices_in_kvcache, topk_length and extra_topk_length must be None when dense attention is used." assert block_table is not None and cache_seqlens is not None, "block_table and cache_seqlens must be provided when dense attention is used." out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.dense_decode_fwd( q, k_cache, head_dim_v, cache_seqlens, block_table, softmax_scale, causal, sched_meta.tile_scheduler_metadata, sched_meta.num_splits ) sched_meta.tile_scheduler_metadata = new_tile_scheduler_metadata sched_meta.num_splits = new_num_splits return (out, lse)
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Sparse attention prefill kernel
Args: q: [s_q, h_q, d_qk], bfloat16 kv: [s_kv, h_kv, d_qk], bfloat16 indices: [s_q, h_kv, topk], int32. Invalid indices should be set to -1 or numbers >= s_kv sm_scale: float d_v: The dimension of value vectors. Can only be 512 attn_sink: optional, [h_q], float32. If attn_sink is provided, when computing output, output will be additionally multiplied by exp(lse) / (exp(lse) + exp(attn_sink)). +-inf in attn_sink will be handled normally (i.e., -inf has no effect, +inf will make corresponding output all zeros). This argument has no effect on lse and max_logits. topk_length: optional, [s_q], int32. If provided, the i-th q token will only attend to k tokens specified by indices[i, :, :topk_length[i]], ignoring later k/v tokens (even if provided in indices). In extremely rare cases (topk_length provided, there is a valid topk index between topk_length[i] ~ s_kv, and that topk index points to a k token containing NaN), operator output will contain NaN, so please avoid this situation.
Returns: (output, max_logits, lse) Please refer to tests/ref.py for the precise definitions of these parameters. - output: [s_q, h_q, d_v], bfloat16 - max_logits: [s_q, h_q], float - lse: [s_q, h_q], float, log-sum-exp of attention scores """ results = flash_mla_cuda.sparse_prefill_fwd( q, kv, indices, sm_scale, d_v, attn_sink, topk_length ) return results
def _flash_attn_varlen_forward( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, out: Optional[torch.Tensor] = None, lse: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if out is None: out = torch.empty(qo_total_len, num_qo_heads, head_dim_vo, device=q.device, dtype=q.dtype) if lse is None: # Make lse contiguous on seqlen dim lse = torch.empty(num_qo_heads, qo_total_len, device=q.device, dtype=torch.float32).T
workspace_buffer = torch.empty(32 * 1024 * 1024, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_fwd( workspace_buffer, q, k, v, cu_seqlens_qo, cu_seqlens_kv, out, lse, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return out, lse
def _flash_attn_varlen_backward( do: torch.Tensor, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, out: torch.Tensor, lse: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dq: Optional[torch.Tensor] = None, dk: Optional[torch.Tensor] = None, dv: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
# TODO: fix bwd GQA if num_qo_heads != num_kv_heads: raise ValueError(f"SM100 bwd doesn't support GQA now. num_qo_heads: {num_qo_heads}, num_kv_heads: {num_kv_heads}.")
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if dq is None: dq = torch.empty(qo_total_len, num_qo_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dk is None: dk = torch.empty(kv_total_len, num_kv_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dv is None: dv = torch.empty(kv_total_len, num_kv_heads, head_dim_vo, device=q.device, dtype=q.dtype)
max_seqlen_qo_aligned = (max_seqlen_qo + 7) // 8 * 8 bs = cu_seqlens_qo.shape[0] - 1 workspace_bytes = 0 workspace_bytes += 4 * bs * max_seqlen_qo_aligned * num_qo_heads * head_dim_qk # dQ_acc workspace_bytes += 4 * max_seqlen_qo_aligned * bs * num_qo_heads * 2 # sum_OdO and scaled_lse if num_qo_heads != num_kv_heads: workspace_bytes += 2 * kv_total_len * num_qo_heads * (head_dim_qk + head_dim_vo) # dKV_acc workspace_buffer = torch.empty(workspace_bytes, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_bwd( workspace_buffer, do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, dq, dk, dv, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return dq, dk, dv
class FlashAttnVarlenFunc(torch.autograd.Function): def forward( ctx, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True, ) -> Tuple[torch.Tensor, torch.Tensor]: out, lse = _flash_attn_varlen_forward( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal=causal, softmax_scale=softmax_scale, is_varlen=is_varlen, ) ctx.save_for_backward(q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv) ctx.max_seqlen_qo = max_seqlen_qo ctx.max_seqlen_kv = max_seqlen_kv ctx.causal = causal ctx.softmax_scale = softmax_scale ctx.is_varlen = is_varlen return out, lse
def backward( ctx, do: torch.Tensor, dlse: torch.Tensor, ): del dlse # LSE doesn't support backward currently q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv = ctx.saved_tensors dq, dk, dv = _flash_attn_varlen_backward( do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, ctx.max_seqlen_qo, ctx.max_seqlen_kv, causal=ctx.causal, softmax_scale=ctx.softmax_scale, is_varlen=ctx.is_varlen, ) return dq, dk, dv, None, None, None, None, None, None, None
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( qkv[:, :, :head_dim_qk], qkv[:, :, head_dim_qk:head_dim_qk * 2], qkv[:, :, head_dim_qk * 2:], cu_seqlens, cu_seqlens, max_seqlen, max_seqlen, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, kv[:, :, :head_dim_qk], kv[:, :, head_dim_qk:], cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )123456789101112{ "version": 1, "license": "MIT", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0f", "9.0a" ] }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163from typing import Optional, Tupleimport torch
from .flash_mla_interface import FlashMLASchedMetafrom . import flash_mla_interface as _impl
def get_mla_metadata(*args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: return _impl.get_mla_metadata(*args, **kwargs)
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_mla_with_kvcache( q=q, k_cache=k_cache, block_table=block_table, cache_seqlens=cache_seqlens, head_dim_v=head_dim_v, tile_scheduler_metadata=tile_scheduler_metadata, num_splits=num_splits, softmax_scale=softmax_scale, causal=causal, is_fp8_kvcache=is_fp8_kvcache, indices=indices, attn_sink=attn_sink, extra_k_cache=extra_k_cache, extra_indices_in_kvcache=extra_indices_in_kvcache, topk_length=topk_length, extra_topk_length=extra_topk_length, )
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: return _impl.flash_mla_sparse_fwd( q=q, kv=kv, indices=indices, sm_scale=sm_scale, d_v=d_v, attn_sink=attn_sink, topk_length=topk_length, )
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_func( q=q, k=k, v=v, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_qkvpacked_func( qkv=qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: return _impl.flash_attn_varlen_kvpacked_func( q=q, kv=kv, cu_seqlens_qo=cu_seqlens_qo, cu_seqlens_kv=cu_seqlens_kv, max_seqlen_qo=max_seqlen_qo, max_seqlen_kv=max_seqlen_kv, head_dim_qk=head_dim_qk, dropout_p=dropout_p, softmax_scale=softmax_scale, causal=causal, deterministic=deterministic, is_varlen=is_varlen, )
__all__ = [ "__version__", "FlashMLASchedMeta", "get_mla_metadata", "flash_mla_with_kvcache", "flash_attn_varlen_func", "flash_attn_varlen_qkvpacked_func", "flash_attn_varlen_kvpacked_func", "flash_mla_sparse_fwd",]123version https://git-lfs.github.com/spec/v1oid sha256:dbee04ad485babe4202c1c634e2914fddb814f7225dc8b3ee556260d6b03557esize 9283880123456789import torchfrom . import _flash_mla_cuda_89d7fc1ops = torch.ops._flash_mla_cuda_89d7fc1
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_flash_mla_cuda_89d7fc1::{op_name}"1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435from typing import Optional, Tupleimport dataclasses
import torch
from ._ops import ops as flash_mla_cuda
@dataclasses.dataclassclass FlashMLASchedMeta: """ A class that stores the tile scheduler metadata of FlashMLA """
@dataclasses.dataclass class Config: b: int s_q: int h_q: int page_block_size: int h_k: int
causal: bool is_fp8_kvcache: bool topk: Optional[int]
extra_page_block_size: Optional[int] extra_topk: Optional[int]
have_initialized: bool = False
config: Optional[Config] = None
tile_scheduler_metadata: Optional[torch.Tensor] = None # (num_sm_parts, TileSchedulerMetaDataSize), dtype torch.int32. num_splits: Optional[torch.Tensor] = None # (1), dtype torch.int32.
def get_mla_metadata( *args, **kwargs) -> Tuple[FlashMLASchedMeta, None]: """ Returns an empty instance of FlashMLASchedMeta. The actual scheduling metadata will be generated during the first invocation of flash_mla_with_kvcache.
Arguments: This function does not need any arguments, but we keep *args and **kwargs to be compatible with the old interface.
Return: A tuple. Due to historical reasons, we return a tuple of (FlashMLASchedMeta, None) now. Only the first element is useful. """ return FlashMLASchedMeta(), None
def flash_mla_with_kvcache( q: torch.Tensor, k_cache: torch.Tensor, block_table: Optional[torch.Tensor], cache_seqlens: Optional[torch.Tensor], head_dim_v: int, tile_scheduler_metadata: FlashMLASchedMeta, num_splits: None = None, softmax_scale: Optional[float] = None, causal: bool = False, is_fp8_kvcache: bool = False, indices: Optional[torch.Tensor] = None, attn_sink: Optional[torch.Tensor] = None, extra_k_cache: Optional[torch.Tensor] = None, extra_indices_in_kvcache: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None, extra_topk_length: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, torch.Tensor]: """ Arguments: q: (batch_size, seq_len_q, num_heads_q, head_dim). k_cache: (num_blocks, page_block_size, num_heads_k, head_dim). Different modes (including fp8/bf16, and sparsity) has different KV cache layouts. See comments below for details. The KV cache must be contiguously valid for sparse attention on sm100. Here "contiguously valid" means that every byte, from the very beginning of the KV cache, till the last byte in the KV cache, is valid memory address to visit (i.e. won't IMA). In other words, the KV cache could be a slice of a larger array, but cannot be a list of disjoint memory blocks. block_table: (batch_size, max_num_blocks_per_seq), torch.int32. Can be None when sparse attention is used. cache_seqlens: (batch_size), torch.int32. Can be None when sparse attention is used. head_dim_v: Head_dim of v. Must be 512 sched_meta: FlashMLASchedMeta, return by get_mla_metadata. You may reuse the same sched_meta across different invocations, but only when the tensor shapes and the values of cache_seqlens, topk_length, and extra_topk_length remain the same. num_splits_placeholder: must be "None" (to be compatible with the old interface). softmax_scale: float. The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim_k). causal: bool. Whether to apply causal attention mask. Only valid for dense attention is_fp8_kvcache: bool. indices: (batch_size, seq_len_q, topk). KV indices when sparse attention is enabled. Pay attention that indices_in_kvcache[i][j][k] = (the index of the page block where token t resides) * block_size + (the offset of token t among the page block), where t is the k-th token of the j-th q-sequence in the i-th batch. attn_sink: Optional[torch.Tensor], (num_heads_q, ), torch.float32. If presented, the final output will be scaled by exp(lse) / (exp(lse) + exp(attn_sink)). Have no affect on the returned softmax_lse. +inf will cause the result to become 0. extra_k_cache and extra_indices_in_kvcache: If provided, will attend to these extra tokens in addition to those in k_cache and indices_in_kvcache. Their format requirements are the same as k_cache and indices_in_kvcache respectively. topk_length/extra_topk_length: (batch_size, ), torch.int32. If provided, only the leftmost topk_length indices will be processed. Useful when the actual topk for different queries are different so that we can save some computation, compared to masking. For DeepSeek V3, DeepSeek V3.1, and DeepSeek V3.2: head_dim should be 576 while head_dim_v should be 512. In FP8+sparse mode, each token's KV cache is 656 Bytes, structured as: - The shape of the tensor `k_cache` is (num_blocks, page_block_size, num_heads_k, head_dim), and num_heads_k must be 1. - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3 values. - Next 16 bytes: Scale factors, containing 4 float32 values. The first float32 is the scale for the first 128 float8_e4m3 values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy.
Return: out: (batch_size, seq_len_q, num_heads_q, head_dim_v). softmax_lse: (batch_size, num_heads_q, seq_len_q), torch.float32. """ sched_meta = tile_scheduler_metadata indices_in_kvcache = indices assert isinstance(sched_meta, FlashMLASchedMeta), "tile_scheduler_metadata must be of type FlashMLASchedMeta" assert num_splits is None, "num_splits must be None"
topk = indices_in_kvcache.shape[-1] if indices_in_kvcache is not None else None extra_k_page_block_size = extra_k_cache.shape[1] if extra_k_cache is not None else None extra_topk = extra_indices_in_kvcache.shape[-1] if extra_indices_in_kvcache is not None else None if softmax_scale is None: softmax_scale = q.shape[-1] ** (-0.5)
if not sched_meta.have_initialized: # Sanity check. We only perform sanity check during the first invocation to save CPU time. if indices_in_kvcache is not None: assert not causal, "causal must be False when indices_in_kvcache is not None (i.e. sparse attention is enabled)" # Initialize the tile scheduler metadata during the first invocation. sched_meta.have_initialized = True sched_meta.config = FlashMLASchedMeta.Config( q.shape[0], q.shape[1], q.shape[2], k_cache.shape[1], k_cache.shape[2],
causal, is_fp8_kvcache, topk,
extra_k_page_block_size, extra_topk, ) else: # Check whether the input arguments are consistent with sched_meta helper_msg = " Your input arguments are inconsistent with sched_meta. Please make sure the input arguments are consistent across different invocations of flash_mla_with_kvcache on the same sched_meta." assert sched_meta.config is not None assert sched_meta.config.b == q.shape[0], "sched_meta.config.b must be equal to batch_size." + helper_msg assert sched_meta.config.s_q == q.shape[1], "sched_meta.config.s_q must be equal to seq_len_q." + helper_msg assert sched_meta.config.h_q == q.shape[2], "sched_meta.config.h_q must be equal to num_heads_q." + helper_msg assert sched_meta.config.page_block_size == k_cache.shape[1], "sched_meta.config.page_block_size must be equal to page_block_size." + helper_msg assert sched_meta.config.h_k == k_cache.shape[2], "sched_meta.config.h_k must be equal to num_heads_k." + helper_msg assert sched_meta.config.causal == causal, "sched_meta.config.causal must be equal to causal." + helper_msg assert sched_meta.config.is_fp8_kvcache == is_fp8_kvcache, "sched_meta.config.is_fp8_kvcache must be equal to is_fp8_kvcache." + helper_msg assert sched_meta.config.topk == topk, "sched_meta.config.topk must be equal to the last dim of indices_in_kvcache." + helper_msg assert sched_meta.config.extra_page_block_size == extra_k_page_block_size, "sched_meta.config.extra_page_block_size must be equal to the page_block_size of extra_k_cache." + helper_msg assert sched_meta.config.extra_topk == extra_topk, "sched_meta.config.extra_topk must be equal to the last dim of extra_indices_in_kvcache." + helper_msg
if topk is not None: # Sparse attention assert not causal, "causal must be False when sparse attention is enabled" assert is_fp8_kvcache, "is_fp8_kvcache must be True when sparse attention is enabled" out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.sparse_decode_fwd( q, k_cache, indices_in_kvcache, topk_length, attn_sink, sched_meta.tile_scheduler_metadata, sched_meta.num_splits, extra_k_cache, extra_indices_in_kvcache, extra_topk_length, head_dim_v, softmax_scale ) else: # Dense attention assert indices_in_kvcache is None and attn_sink is None and extra_k_cache is None and extra_indices_in_kvcache is None and topk_length is None and extra_topk_length is None, "indices_in_kvcache, attn_sink, extra_k_cache, extra_indices_in_kvcache, topk_length and extra_topk_length must be None when dense attention is used." assert block_table is not None and cache_seqlens is not None, "block_table and cache_seqlens must be provided when dense attention is used." out, lse, new_tile_scheduler_metadata, new_num_splits = flash_mla_cuda.dense_decode_fwd( q, k_cache, head_dim_v, cache_seqlens, block_table, softmax_scale, causal, sched_meta.tile_scheduler_metadata, sched_meta.num_splits ) sched_meta.tile_scheduler_metadata = new_tile_scheduler_metadata sched_meta.num_splits = new_num_splits return (out, lse)
def flash_mla_sparse_fwd( q: torch.Tensor, kv: torch.Tensor, indices: torch.Tensor, sm_scale: float, d_v: int = 512, attn_sink: Optional[torch.Tensor] = None, topk_length: Optional[torch.Tensor] = None,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Sparse attention prefill kernel
Args: q: [s_q, h_q, d_qk], bfloat16 kv: [s_kv, h_kv, d_qk], bfloat16 indices: [s_q, h_kv, topk], int32. Invalid indices should be set to -1 or numbers >= s_kv sm_scale: float d_v: The dimension of value vectors. Can only be 512 attn_sink: optional, [h_q], float32. If attn_sink is provided, when computing output, output will be additionally multiplied by exp(lse) / (exp(lse) + exp(attn_sink)). +-inf in attn_sink will be handled normally (i.e., -inf has no effect, +inf will make corresponding output all zeros). This argument has no effect on lse and max_logits. topk_length: optional, [s_q], int32. If provided, the i-th q token will only attend to k tokens specified by indices[i, :, :topk_length[i]], ignoring later k/v tokens (even if provided in indices). In extremely rare cases (topk_length provided, there is a valid topk index between topk_length[i] ~ s_kv, and that topk index points to a k token containing NaN), operator output will contain NaN, so please avoid this situation.
Returns: (output, max_logits, lse) Please refer to tests/ref.py for the precise definitions of these parameters. - output: [s_q, h_q, d_v], bfloat16 - max_logits: [s_q, h_q], float - lse: [s_q, h_q], float, log-sum-exp of attention scores """ results = flash_mla_cuda.sparse_prefill_fwd( q, kv, indices, sm_scale, d_v, attn_sink, topk_length ) return results
def _flash_attn_varlen_forward( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, out: Optional[torch.Tensor] = None, lse: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if out is None: out = torch.empty(qo_total_len, num_qo_heads, head_dim_vo, device=q.device, dtype=q.dtype) if lse is None: # Make lse contiguous on seqlen dim lse = torch.empty(num_qo_heads, qo_total_len, device=q.device, dtype=torch.float32).T
workspace_buffer = torch.empty(32 * 1024 * 1024, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_fwd( workspace_buffer, q, k, v, cu_seqlens_qo, cu_seqlens_kv, out, lse, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return out, lse
def _flash_attn_varlen_backward( do: torch.Tensor, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, out: torch.Tensor, lse: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dq: Optional[torch.Tensor] = None, dk: Optional[torch.Tensor] = None, dv: Optional[torch.Tensor] = None, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: qo_total_len, num_qo_heads, head_dim_qk = q.shape kv_total_len, num_kv_heads, head_dim_vo = v.shape
# TODO: fix bwd GQA if num_qo_heads != num_kv_heads: raise ValueError(f"SM100 bwd doesn't support GQA now. num_qo_heads: {num_qo_heads}, num_kv_heads: {num_kv_heads}.")
mask_mode_code = 1 if causal else 0 if softmax_scale is None: softmax_scale = head_dim_qk ** (-0.5)
if dq is None: dq = torch.empty(qo_total_len, num_qo_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dk is None: dk = torch.empty(kv_total_len, num_kv_heads, head_dim_qk, device=q.device, dtype=q.dtype) if dv is None: dv = torch.empty(kv_total_len, num_kv_heads, head_dim_vo, device=q.device, dtype=q.dtype)
max_seqlen_qo_aligned = (max_seqlen_qo + 7) // 8 * 8 bs = cu_seqlens_qo.shape[0] - 1 workspace_bytes = 0 workspace_bytes += 4 * bs * max_seqlen_qo_aligned * num_qo_heads * head_dim_qk # dQ_acc workspace_bytes += 4 * max_seqlen_qo_aligned * bs * num_qo_heads * 2 # sum_OdO and scaled_lse if num_qo_heads != num_kv_heads: workspace_bytes += 2 * kv_total_len * num_qo_heads * (head_dim_qk + head_dim_vo) # dKV_acc workspace_buffer = torch.empty(workspace_bytes, dtype=torch.uint8, device=q.device) flash_mla_cuda.dense_prefill_bwd( workspace_buffer, do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, dq, dk, dv, mask_mode_code, softmax_scale, max_seqlen_qo, max_seqlen_kv, is_varlen, )
return dq, dk, dv
class FlashAttnVarlenFunc(torch.autograd.Function): def forward( ctx, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, causal: bool = False, softmax_scale: Optional[float] = None, is_varlen: bool = True, ) -> Tuple[torch.Tensor, torch.Tensor]: out, lse = _flash_attn_varlen_forward( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal=causal, softmax_scale=softmax_scale, is_varlen=is_varlen, ) ctx.save_for_backward(q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv) ctx.max_seqlen_qo = max_seqlen_qo ctx.max_seqlen_kv = max_seqlen_kv ctx.causal = causal ctx.softmax_scale = softmax_scale ctx.is_varlen = is_varlen return out, lse
def backward( ctx, do: torch.Tensor, dlse: torch.Tensor, ): del dlse # LSE doesn't support backward currently q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv = ctx.saved_tensors dq, dk, dv = _flash_attn_varlen_backward( do, q, k, v, out, lse, cu_seqlens_qo, cu_seqlens_kv, ctx.max_seqlen_qo, ctx.max_seqlen_kv, causal=ctx.causal, softmax_scale=ctx.softmax_scale, is_varlen=ctx.is_varlen, ) return dq, dk, dv, None, None, None, None, None, None, None
def flash_attn_varlen_func( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, k, v, cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_qkvpacked_func( qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( qkv[:, :, :head_dim_qk], qkv[:, :, head_dim_qk:head_dim_qk * 2], qkv[:, :, head_dim_qk * 2:], cu_seqlens, cu_seqlens, max_seqlen, max_seqlen, causal, softmax_scale, is_varlen, )
def flash_attn_varlen_kvpacked_func( q: torch.Tensor, kv: torch.Tensor, cu_seqlens_qo: torch.Tensor, cu_seqlens_kv: torch.Tensor, max_seqlen_qo: int, max_seqlen_kv: int, head_dim_qk: int, dropout_p: float = 0.0, softmax_scale: Optional[float] = None, causal: bool = False, deterministic: bool = False, is_varlen: bool = True,) -> Tuple[torch.Tensor, torch.Tensor]: assert dropout_p == 0.0 assert not deterministic return FlashAttnVarlenFunc.apply( q, kv[:, :, :head_dim_qk], kv[:, :, head_dim_qk:], cu_seqlens_qo, cu_seqlens_kv, max_seqlen_qo, max_seqlen_kv, causal, softmax_scale, is_varlen, )123456789101112{ "version": 1, "license": "MIT", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0f", "9.0a" ] }}build/torch210-cxx11-cu128-x86_64-linux/_flash_mla_cuda_09f70ef.abi3.so → build/torch210-cxx11-cu128-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soRenamed+1-1
build/torch210-cxx11-cu128-x86_64-linux/_ops.py+3-3
build/torch210-cxx11-cu128-x86_64-linux/flash_mla/__init__.py+2-2
build/torch210-cxx11-cu130-x86_64-linux/_flash_mla_cuda_09f70ef.abi3.so → build/torch210-cxx11-cu130-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soRenamed+1-1
build/torch210-cxx11-cu130-x86_64-linux/_ops.py+3-3
build/torch210-cxx11-cu130-x86_64-linux/flash_mla/__init__.py+2-2
build/torch211-cxx11-cu128-x86_64-linux/__init__.pyAdded+163
build/torch211-cxx11-cu128-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
build/torch211-cxx11-cu128-x86_64-linux/_ops.pyAdded+9
build/torch211-cxx11-cu128-x86_64-linux/flash_mla/__init__.pyAdded+26
build/torch211-cxx11-cu128-x86_64-linux/flash_mla_interface.pyAdded+435
build/torch211-cxx11-cu128-x86_64-linux/metadata.jsonAdded+11
build/torch211-cxx11-cu130-x86_64-linux/__init__.pyAdded+163
build/torch211-cxx11-cu130-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
build/torch211-cxx11-cu130-x86_64-linux/_ops.pyAdded+9
build/torch211-cxx11-cu130-x86_64-linux/flash_mla/__init__.pyAdded+26
build/torch211-cxx11-cu130-x86_64-linux/flash_mla_interface.pyAdded+435
build/torch211-cxx11-cu130-x86_64-linux/metadata.jsonAdded+12
build/torch29-cxx11-cu129-x86_64-linux/__init__.pyAdded+163
build/torch29-cxx11-cu129-x86_64-linux/_flash_mla_cuda_89d7fc1.abi3.soAdded+3
build/torch29-cxx11-cu129-x86_64-linux/_ops.pyAdded+9
build/torch29-cxx11-cu129-x86_64-linux/flash_mla/__init__.pyAdded+26
build/torch29-cxx11-cu129-x86_64-linux/flash_mla_interface.pyAdded+435
build/torch29-cxx11-cu129-x86_64-linux/metadata.jsonAdded+12