nemo_automodel.components.models.glm_moe_dsa.optimized_kernels
nemo_automodel.components.models.glm_moe_dsa.optimized_kernels
Optional GLM-5.2 DSA sparse-kernel dispatch.
The dense torch path in layers.py (GlmMoeDsaIndexer / GlmMoeDsaMLA)
is the numerical reference and the default. This module provides optional
TileLang- and cuDNN-backed sparse paths, selected by backend.attn:
- the fused lighting indexer (logits + top-k), and
- the gather-top-k sparse MLA attention (reads only the selected KV; no
[T, T]mask is materialized).
The TileLang kernels are vendored from THUDM’s slime GLM-5.2 plugin under
nemo_automodel.components.models.glm_moe_dsa.kernels (see that package’s
__init__ for attribution); the cuDNN adapter lives in the same model-owned
kernel package. Optional entry points are imported with safe_import_from so
environments without their dependencies can still import the model and use
another attention backend.
Mirrors the structure of deepseek_v4/optimized_kernels.py.
Module Contents
Functions
Data
API
Build per-query key windows for padded THD layout.
cu_seqlens stores real document lengths compactly. cu_seqlens_padded
stores their offsets in the actual flattened token tensor, including CP
padding between documents. TileLang top-k indices refer to the flattened
token tensor, so starts/ends must be in the padded coordinate space while
still excluding CP padding keys for real query tokens.
Select fixed-width top-k indices with the cuDNN DSA indexer.
Parameters:
Contiguous CUDA bfloat16 packed-THD query tensor of shape
[tokens, index_heads, index_head_dim].
Contiguous CUDA bfloat16 packed-THD key tensor of shape
[tokens, index_head_dim].
Contiguous CUDA FP32 tensor of shape [tokens, index_heads],
scaled by index_heads**-0.5 * index_head_dim**-0.5. The adapter casts
it to the kernel dtype without applying any additional scaling.
CUDA int32 tensor of shape [sequences + 1] containing
cumulative lengths for the compact packed-token layout.
Fixed number of key indices emitted for every query token.
Optional CUDA integer tensor of shape [tokens] containing
global padded-storage query coordinates for context-parallel layouts.
Optional CUDA int32 tensor of shape [sequences + 1]
containing cumulative lengths for a padded packed-token layout.
Optional reusable segmented metadata prepared once for the current packed stage.
Returns: torch.Tensor
Contiguous CUDA int32 tensor of shape [tokens, 1, index_topk]. Invalid
Run cuDNN DSA attention on packed latent Q/KV tensors.
Parameters:
Contiguous CUDA bfloat16 packed-query tensor of shape
[tokens, heads, kv_lora_rank + rope_head_dim].
Contiguous CUDA bfloat16 packed latent-KV tensor of shape
[tokens, 1, kv_lora_rank + rope_head_dim].
Contiguous CUDA int32 tensor of shape [tokens, 1, index_topk].
MLA attention scale applied to query-key scores.
Optional int32 valid-prefix lengths of shape [tokens].
Whether every query has a positive valid-prefix length.
Optional cached int64 indices of nonempty query rows.
Returns: torch.Tensor
CUDA bfloat16 latent-attention tensor of shape [tokens, heads, kv_lora_rank].
Return whether all optional cuDNN DSA adapter entry points are available.
Return whether the optional TileLang kernel package for name is importable.
Prepare reusable local-query/global-key packed metadata once per stage.
Parameters:
CUDA int32 compact real-token offsets of shape
[sequences + 1].
Number of rows in the gathered padded-storage K/V tensor.
Optional maximum real sequence length as an integer or scalar integer tensor.
Optional CUDA integer tensor of shape [local_tokens]
containing each local query’s global padded-storage coordinate.
Optional CUDA int32 padded-storage boundaries of shape
[sequences + 1].
Optional local boolean tensor of shape [local_tokens];
True marks a padded query row.
Returns: CudnnDsaPackedMetadata
Segmented cuDNN metadata whose query and key-source fields use global
Decide whether to run the TileLang kernel; raise if forced but unavailable.
backend="tilelang" forces the kernel (and raises a clear error if it cannot run);
backend="auto" silently falls back to torch when the kernel is unavailable;
backend="torch" always uses the torch reference.
Fused lighting-indexer top-k selection (THD/varlen).
Parameters:
[T, index_n_heads, index_head_dim] bf16 (rope already applied).
[T, index_head_dim] bf16 (k_norm + rope already applied).
[T, index_n_heads] fp32; the caller must fold the index
softmax_scale into the weight (the kernel computes relu(q·k) * w
with no internal scale), i.e. weights_proj(x) * index_n_heads**-0.5 * index_head_dim**-0.5.
[num_seq + 1] cumulative sequence lengths of the packed batch.
number of keys to keep (e.g. 2048).
Optional global THD token indices for the local query
rows. Used by context parallelism when index_q is sharded but
index_k has been all-gathered in global token order.
Optional cumulative lengths in the padded THD token
layout. CP-packed datasets pad each document to a CP multiple, so
local query indices address this padded layout rather than the
compact cu_seqlens layout.
Returns: torch.Tensor
topk_indices [T, 1, index_topk] int32 (-1 for invalid/causal-masked),
Gather-top-k sparse MLA attention on the absorbed latent representation.
Parameters:
[T, n_heads, kv_lora_rank + qk_rope_head_dim] bf16 — the absorbed query
cat([q_nope @ w_kc, q_pe], -1) (e.g. 512 + 64 = 576).
[T, 1, kv_lora_rank + qk_rope_head_dim] bf16 — the latent KV
cat([kv_compressed, k_pe], -1).
[T, 1, index_topk] int32 (-1 sentinel).
[n_heads, v_head_dim, kv_lora_rank] — the value up-projection used to
map the latent attention output back to v_head_dim.
MLA attention scale mscale**2 / sqrt(qk_head_dim) (NOT the
kernel’s 1/sqrt(dim+tail) default).
Returns: torch.Tensor
attn_out [T, n_heads, v_head_dim] bf16.