nemo_automodel.components.models.common.cudnn_sparse_attention
nemo_automodel.components.models.common.cudnn_sparse_attention
Shared FlashMLA-forward/cuDNN-backward sparse latent attention.
Module Contents
Classes
Functions
Data
_SUPPORTED_ATTENTION_HEAD_DIMS
API
Bases: Function
Pair FlashMLA forward with cuDNN backward for latent THD attention.
Map output gradients to query and gathered latent-KV layouts.
Parameters:
Autograd context populated by :meth:forward.
CUDA BF16 output gradient of shape
[query_tokens, heads, 512].
Returns: torch.Tensor | None
Gradients for the eight forward inputs: query tensor of shape
Run FlashMLA forward and save tensors required by cuDNN backward.
Parameters:
Autograd context used to save forward tensors and scalar metadata.
CUDA BF16 query tensor of shape [query_tokens, heads, head_dim].
CUDA BF16 latent K/V tensor of shape [key_tokens, 1, head_dim].
CUDA int32 tensor of shape [query_tokens, 1, sparse_width]
containing global K/V coordinates and invalid entries marked -1.
Scale applied to query-key scores.
FlashMLA-compatible padded head count.
Optional int32 valid-prefix lengths of shape [query_tokens].
Whether every query has a positive valid prefix.
Optional int64 indices of nonempty queries with shape
[valid_query_tokens].
Returns: torch.Tensor
CUDA BF16 latent values of shape [query_tokens, heads, 512].
Canonicalize sparse indices into an ascending valid prefix.
Parameters:
Integer tensor of shape [query_tokens, sparse_width] with
global K/V coordinates and negative invalid entries.
Number of rows in the flattened K/V tensor.
Returns: torch.Tensor
A contiguous int32 index tensor of shape [query_tokens, sparse_width]
Pad query and attention-sink head axes for FlashMLA.
Parameters:
Query tensor of shape [query_tokens, heads, head_dim].
FP32 attention-sink tensor of shape [heads].
FlashMLA-compatible output head count.
Returns: torch.Tensor
Query tensor of shape [query_tokens, padded_heads, head_dim] and
Return the FlashMLA-supported query-head count for one SM generation.
Raise when either optional sparse-attention runtime is unavailable.
Validate that arbitrary-layout input tensors share one SM90+ CUDA device.
Parameters:
Name included in validation errors.
Tensors with arbitrary shapes that must share one CUDA device.
Returns: tuple[int, int]
CUDA compute capability as (major, minor).
Run sparse latent attention with FlashMLA forward and cuDNN backward.
Parameters:
Contiguous CUDA BF16 query tensor of shape
[query_tokens, heads, head_dim], where head_dim is 512 or 576.
Contiguous CUDA BF16 latent K/V tensor of shape
[key_tokens, 1, head_dim].
Contiguous CUDA int32 tensor of shape
[query_tokens, 1, sparse_width] with global K/V coordinates and
invalid entries marked -1.
Scale forwarded unchanged to FlashMLA and cuDNN backward.
Optional contiguous CUDA int32 valid-prefix lengths of shape
[query_tokens]. When supplied, topk_indices must already contain
a compact, ascending valid prefix.
Whether every query has a positive valid-prefix length.
Optional contiguous CUDA int64 indices of nonempty queries
with shape [valid_query_tokens].
Returns: torch.Tensor
Contiguous CUDA BF16 latent output tensor of shape
Raises:
RuntimeError: If optional kernels, CUDA, or SM90+ are unavailable.TypeError: If compute tensors are not BF16 or indices are not int32.ValueError: If tensor layouts, dimensions, sparse width, or scale are invalid.
Return whether the cuDNN backward and FlashMLA forward runtimes import.