nemo_automodel.components.models.glm_moe_dsa.kernels.cudnn_dsa
nemo_automodel.components.models.glm_moe_dsa.kernels.cudnn_dsa
cuDNN and FlashMLA kernels for the split GLM-5.2 DSA path.
Module Contents
Classes
Functions
Data
API
Reusable THD metadata for local-query/global-key cuDNN DSA.
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 [T_q, H, 512].
Returns:
Gradients for the eight forward inputs: query [T_q, H, 576],
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 [T_q, H, 576].
CUDA BF16 gathered K/V tensor of shape [T_k, 1, 576].
CUDA int32 tensor of shape [T_q, 1, K] containing
global padded-storage K/V coordinates and a -1 suffix.
Scale applied to query-key scores.
FlashMLA-compatible padded head count.
Optional int32 valid-prefix lengths of shape [T_q].
Whether every query has a positive valid prefix.
Optional int64 indices of nonempty queries with shape
[T_valid].
Returns: torch.Tensor
CUDA BF16 latent values of shape [T_q, H, 512].
Canonicalize global indices [T, K] and return valid lengths [T].
Pad query [T, H, 576] and sink [H] to padded_heads.
Return the FlashMLA-supported head count for one SM generation.
Raise when either optional runtime required by the split kernel is absent.
Validate that arbitrary-layout input tensors share one SM90+ CUDA device.
Select top-k for FP32 scores [T, S_max] using causal lengths [T].
Validate reusable packed metadata without a CUDA-to-host synchronization.
Parameters:
Metadata whose per-query fields have shape [T_q] and
whose key-source indices address the gathered padded-storage K/V tensor.
Expected local query row count T_q.
Expected gathered padded-storage K/V row count T_k.
CUDA device shared by the metadata and kernel inputs.
Returns: CudnnDsaPackedMetadata
The validated metadata object, unchanged.
Validate GLM-5.2’s fixed sparse-selection width.
Compute GLM-5.2 packed-THD indexer top-k with cuDNN Frontend.
Parameters:
Rank-local indexer query in THD layout, BF16 [T_q, H_index, 128].
Gathered global indexer key in TD layout, BF16 [T_k, 128].
Already-scaled per-head weights in TH layout, FP32 or BF16
[T, H_index]. The caller owns both H_index**-0.5 and
128**-0.5 scaling; this function does not rescale them.
Compact packed-sequence offsets, CUDA int32
[num_sequences + 1].
Fixed output width K in [1, 2048].
Optional contiguous global padded coordinates for local query
rows, [T_q]. Absent means CP=1 identity coordinates.
Optional global padded packed-layout offsets.
Optional metadata object returned by
:func:prepare_cudnn_dsa_packed_metadata. Supplying it avoids rebuilding
and synchronizing the same metadata in every full-indexer layer.
Returns: torch.Tensor
CUDA int32 top-k indices in global padded-storage THD coordinates,
Raises:
RuntimeError: If optional kernels, CUDA, or SM90+ are unavailable.TypeError: If tensor dtypes orindex_topkare invalid.ValueError: If tensor shapes or compact packed metadata are invalid.
Run split GLM-5.2 sparse MLA with FlashMLA forward and cuDNN backward.
Parameters:
Absorbed MLA query, CUDA BF16 THD [T_q, H, 576]. The final
dimension is kv_lora_rank + qk_rope_head_dim (512 + 64).
Shared latent key/value, CUDA BF16 THD [T_kv, 1, 576].
Global padded-storage K/V indices, CUDA int32
[T_q, 1, K] with -1 for invalid slots.
Already-computed MLA attention scale. It is forwarded unchanged to both FlashMLA and cuDNN backward.
Optional int32 valid-prefix lengths [T_q] prepared once
from packed causal metadata. When supplied, topk_indices must already
contain the indexer’s canonical compact, ascending prefix.
Whether every query has a positive valid-prefix length. The model supplies this cached metadata flag to keep unpadded inputs on the allocation-free backward path.
Optional cached int64 row indices whose valid-prefix length is positive. The model supplies these once per stage for padded inputs so every attention layer can compact without rescanning CUDA metadata.
Returns: torch.Tensor
Latent sparse-attention output, CUDA BF16 [T_q, H, 512]. The caller
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, top-k, or scale are invalid.
Return whether both optional libraries required by cuDNN DSA import.
Build segmented THD metadata for local queries and gathered global keys.
This validation intentionally performs one device-to-host synchronization. The model prepares the object once per pipeline stage and reuses it across every indexer and shared-attention layer.
Parameters:
Cumulative compact real-token lengths, int32 tensor of shape
[sequences + 1].
Number of tokens in the gathered, padded THD key tensor.
Optional precomputed maximum sequence length as a Python integer or
scalar integer tensor. Its value is checked against cu_seqlens.
Optional contiguous integer tensor of shape [T_q] with
global padded-storage coordinates for the local query rows. When absent,
queries cover all global key tokens (CP=1).
Optional cumulative padded-storage boundaries, int32 tensor
of shape [sequences + 1]. When absent, cu_seqlens also defines
storage coordinates.
Optional local boolean padding mask of shape [T_q]. This
remains authoritative when THD preprocessing has absorbed trailing pack
padding into cu_seqlens.
Returns: CudnnDsaPackedMetadata
Metadata with per-query global padded-storage starts and real causal lengths,