nemo_automodel.components.models.deepseek_v4.kernels.sparse_attention
nemo_automodel.components.models.deepseek_v4.kernels.sparse_attention
Autograd wrapper for vendored Miles DeepSeek V4 sparse-attention kernels.
Attribution:
- Upstream project: Miles, https://github.com/yueming-yuan/miles
- Upstream revision: e561465d0b9bbf06188b7a5e2020dc7fd691f732, deepseek-v4 branch
- Upstream license: Apache-2.0, copyright 2025 Zhipu AI
- Original source: https://github.com/yueming-yuan/miles/blob/e561465d0b9bbf06188b7a5e2020dc7fd691f732/miles_plugins/models/deepseek_v4/ops/attention_core.py
Module Contents
Classes
Functions
API
Bases: Function
TileLang sparse MQA attention with custom backward.
Run the existing backward using the saved base-two LSE.
Parameters:
Saved forward tensors and score multiplier.
CUDA output gradient [batch, sequence, heads, head_dim]. Non-contiguous gradients are copied before the kernel call.
Returns: torch.Tensor | None
Query gradient [batch, sequence, heads, head_dim], shared-KV gradient
Run the vendored sparse attention forward kernel.
Parameters:
Autograd context retaining inputs, output and base-two LSE.
Contiguous CUDA BF16 queries [batch, sequence, heads, head_dim].
Contiguous CUDA BF16 shared keys/values [batch, kv_sequence, head_dim].
CUDA FP32 softmax denominator biases [heads].
Contiguous CUDA integer indices [batch, sequence, slots], with -1 for masked slots.
Score multiplier, defaulting to head_dim**-0.5.
Use scaled-logit exponent arithmetic matching the released inference kernel.
Returns: torch.Tensor
Independent CUDA BF16 output [batch, sequence, heads, head_dim].
Bases: Function
TileLang sparse attention with smaller head groups and fp32 KV-grad accumulation.
Run chunked backward and accumulate shared KV gradients in FP32.
Parameters:
Saved forward tensors, score multiplier and backward chunk size.
CUDA output gradient [batch, sequence, heads, head_dim]. Non-contiguous gradients are copied before the kernel call.
Returns: torch.Tensor | None
Query gradient [batch, sequence, heads, head_dim], shared-KV gradient
Run forward over head chunks while preserving backward chunk size.
Parameters:
Autograd context retaining tensors and backward chunk size.
CUDA BF16 queries [batch, sequence, heads, head_dim].
Contiguous CUDA BF16 shared keys/values [batch, kv_sequence, head_dim].
CUDA FP32 denominator biases [heads].
Contiguous CUDA integer indices [batch, sequence, slots], with -1 for masked slots.
Maximum heads per backward kernel. Forward uses the same count unless reference mode requires at least 64.
Score multiplier, defaulting to head_dim**-0.5.
Preserve the original 64-head forward layout and scaled-logit exponent arithmetic.
Returns: torch.Tensor
Independent CUDA BF16 output [batch, sequence, heads, head_dim].
Run vendored Miles DeepSeek V4 TileLang sparse attention.
Parameters:
Contiguous CUDA BF16 queries [batch, sequence, heads, head_dim].
Contiguous CUDA BF16 shared keys/values [batch, kv_sequence, head_dim].
CUDA FP32 denominator biases [heads].
Contiguous CUDA integer indices [batch, sequence, slots], with -1 for masked slots.
Score multiplier, defaulting to head_dim**-0.5.
Select released-inference forward exponent arithmetic.
Returns: torch.Tensor
Independent BF16 tensor [batch, sequence, heads, head_dim], with the
Run vendored sparse attention with bounded backward head chunks.
Parameters:
CUDA BF16 queries [batch, sequence, heads, head_dim].
Contiguous CUDA BF16 shared keys/values [batch, kv_sequence, head_dim].
CUDA FP32 denominator biases [heads].
Contiguous CUDA integer indices [batch, sequence, slots], with -1 for masked slots.
Backward head chunk size; forward uses at least 64 heads per chunk when reference_rounding is enabled.
Score multiplier, defaulting to head_dim**-0.5.
Select original-inference forward rounding while retaining smaller backward chunks and FP32 shared-KV accumulation.
Returns: torch.Tensor
Independent CUDA BF16 tensor [batch, sequence, heads, head_dim].