[NVIDIA] [GDN] Enable FlashInfer MTP verify on SM100+ (Blackwell) (#23273)

Co-authored-by: Yangmin Li <yangminl@nvidia.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Shu Wang
2026-06-01 18:56:42 -07:00
committed by GitHub
co-authored by Yangmin Li Claude Opus 4.7
parent 54143264bf
commit 0574d2b8a5
4 changed files with 163 additions and 86 deletions
@@ -123,9 +123,9 @@ class GDNKernelDispatcher:
else:
raise ValueError(f"Unsupported GDN prefill backend: {prefill_backend}")
# Verify kernel: use FlashInfer only when the selected FlashInfer kernel
# supports MTP verify. On SM100+ FlashInfer GDN decode is supported, but
# its MTP verify path is not, so keep Triton as the verify fallback.
# Verify kernel: use FlashInfer when the selected FlashInfer kernel
# supports MTP verify. SM90 uses the fp32-state path; SM100 uses the
# bf16-state adapter in FlashInferGDNKernel.
if (
decode_backend.is_flashinfer() or prefill_backend.is_flashinfer()
) and flashinfer_kernel.supports_target_verify:
@@ -1,11 +1,11 @@
"""FlashInfer-based kernels for GDN (Gated Delta Network) linear attention.
Both SM90 and SM100+ use the same pool layout: [pool, HV, V, K] (K-last).
Both SM90 and SM100 use the same pool layout: [pool, HV, V, K] (K-last).
SM90 (Hopper): full support — decode, prefill, MTP. State dtype: fp32.
SM100+ (Blackwell+): decode and prefill with bf16 state. MTP verify on the way.
SM100 (Blackwell): full support — decode, prefill, MTP.
Requires flashinfer >= 0.6.4 (SM90) or >= 0.6.5 (SM100+).
Requires flashinfer >= 0.6.7.
"""
import logging
@@ -27,14 +27,15 @@ _flashinfer_gdn_available: Optional[bool] = None
_flashinfer_chunk_gated_delta_rule = None
_flashinfer_gated_delta_rule_mtp = None
_flashinfer_gated_delta_rule_decode = None
_flashinfer_gated_delta_rule_mtp_bf16 = None
def _get_flashinfer_gdn_kernels():
"""Lazy import for FlashInfer GDN prefill, decode and verify (MTP) kernels.
Returns (available, prefill_fn, mtp_fn, decode_fn).
Returns (available, prefill_fn, mtp_fn, decode_fn, mtp_bf16_fn).
"""
global _flashinfer_gdn_available, _flashinfer_chunk_gated_delta_rule, _flashinfer_gated_delta_rule_mtp, _flashinfer_gated_delta_rule_decode
global _flashinfer_gdn_available, _flashinfer_chunk_gated_delta_rule, _flashinfer_gated_delta_rule_mtp, _flashinfer_gated_delta_rule_decode, _flashinfer_gated_delta_rule_mtp_bf16
if _flashinfer_gdn_available is None:
try:
os.environ.setdefault("FLASHINFER_DISABLE_VERSION_CHECK", "1")
@@ -43,10 +44,14 @@ def _get_flashinfer_gdn_kernels():
gated_delta_rule_decode_pretranspose,
gated_delta_rule_mtp,
)
from flashinfer.gdn_kernels.gdn_decode_bf16_state import (
gated_delta_rule_mtp as gated_delta_rule_mtp_bf16,
)
from flashinfer.gdn_prefill import chunk_gated_delta_rule
_flashinfer_chunk_gated_delta_rule = chunk_gated_delta_rule
_flashinfer_gated_delta_rule_mtp = gated_delta_rule_mtp
_flashinfer_gated_delta_rule_mtp_bf16 = gated_delta_rule_mtp_bf16
_flashinfer_gated_delta_rule_decode = gated_delta_rule_decode_pretranspose
_flashinfer_gdn_available = (
torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 9
@@ -62,6 +67,7 @@ def _get_flashinfer_gdn_kernels():
_flashinfer_chunk_gated_delta_rule,
_flashinfer_gated_delta_rule_mtp,
_flashinfer_gated_delta_rule_decode,
_flashinfer_gated_delta_rule_mtp_bf16,
)
@@ -74,9 +80,9 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
"""FlashInfer kernel for GDN with K-last SSM state layout.
SM90 (Hopper): decode uses gather/scatter; prefill and MTP verify supported.
SM100+ (Blackwell+): decode and prefill supported; MTP verify not yet supported.
SM100 (Blackwell): decode uses gather/scatter; prefill and MTP verify supported.
Requires flashinfer >= 0.6.4 (SM90) or >= 0.6.5 (SM100+).
Requires flashinfer >= 0.6.7.
"""
def __init__(self):
@@ -85,6 +91,7 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
self._prefill_fn,
self._mtp_fn,
self._decode_fn,
mtp_bf16_fn,
) = _get_flashinfer_gdn_kernels()
if not available:
@@ -97,13 +104,46 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
sm_major = torch.cuda.get_device_capability()[0]
self.use_state_pool = sm_major >= 10
self.supports_target_verify = sm_major == 9
self.supports_target_verify = sm_major in (9, 10)
if sm_major == 9:
if self._prefill_fn is None:
raise RuntimeError("FlashInfer GDN prefill kernel is unavailable.")
if self._mtp_fn is None:
raise RuntimeError("FlashInfer GDN MTP (verify) kernel is unavailable.")
if sm_major == 9 and self._prefill_fn is None:
raise RuntimeError("FlashInfer GDN prefill kernel is unavailable.")
if self._mtp_fn is None:
raise RuntimeError("FlashInfer GDN MTP (verify) kernel is unavailable.")
if self.use_state_pool and mtp_bf16_fn is not None:
# Adapt bf16 kernel to fp32 kernel interface so target_verify needs no branching.
def _mtp_bf16_adapted(
q,
k,
v,
initial_state,
initial_state_indices,
A_log,
a,
dt_bias,
b,
use_qk_l2norm=True,
**kw,
):
out = mtp_bf16_fn(
A_log=A_log.float(),
a=a,
dt_bias=dt_bias,
softplus_beta=1.0,
softplus_threshold=20.0,
q=q,
k=k,
v=v,
b=b,
initial_state_source=initial_state,
initial_state_indices=initial_state_indices,
use_qk_l2norm_in_kernel=use_qk_l2norm,
**kw,
)
return out, None
self._mtp_fn = _mtp_bf16_adapted
logger.info("Using FlashInfer GDN kernels")
@@ -280,12 +320,7 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
retrieve_parent_token: torch.Tensor,
**kwargs,
) -> torch.Tensor:
if self.use_state_pool:
raise NotImplementedError(
"FlashInfer GDN MTP verify is not yet supported on SM100+."
)
# SM90: MTP verify using FlashInfer gated_delta_rule_mtp kernel.
# MTP verify using FlashInfer gated_delta_rule_mtp kernel (SM90 + SM100+).
if retrieve_parent_token is not None:
raise RuntimeError(
"FlashInfer GDN verify kernel only supports topk=1 "
@@ -313,6 +348,13 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
a_mtp = a.view(batch_size, draft_token_num, num_v_heads)
b_mtp = b.view(batch_size, draft_token_num, num_v_heads)
intermediate_states_buffer_mtp = intermediate_states_buffer
if self.use_state_pool and intermediate_states_buffer is not None:
# The SM100 bf16 MTP kernel indexes this scratch buffer by the
# per-call batch id, while SGLang's speculative state cache is
# pool-scoped and may include an extra dummy slot.
intermediate_states_buffer_mtp = intermediate_states_buffer[:batch_size]
output_fi, _ = self._mtp_fn(
q=query_mtp,
k=key_mtp,
@@ -325,7 +367,7 @@ class FlashInferGDNKernel(LinearAttnKernelBase):
b=b_mtp,
scale=None,
output=None,
intermediate_states_buffer=intermediate_states_buffer,
intermediate_states_buffer=intermediate_states_buffer_mtp,
disable_state_update=True,
use_qk_l2norm=True,
)
+3 -6
View File
@@ -3141,17 +3141,14 @@ class ServerArgs:
def _handle_linear_attn_backend(self):
import torch
# SM100+: default to FlashInfer GDN decode when the user hasn't
# explicitly chosen a decode backend and mamba-ssm-dtype is bf16
# (required by FlashInfer GDN on SM100+).
# SM100+: default to FlashInfer GDN decode (and MTP verify, via pool API)
# when the user hasn't explicitly chosen a decode backend and
# mamba-ssm-dtype is bf16 (required by FlashInfer GDN on SM100+).
# Fixed in FlashInfer v0.6.7: flashinfer-ai/flashinfer#2810
# Excluded when MTP speculative decoding is enabled because
# FlashInfer GDN MTP verify is not yet supported on SM100+.
if (
self.linear_attn_decode_backend is None
and is_sm100_supported()
and self.mamba_ssm_dtype == "bfloat16"
and self.speculative_algorithm is None
):
self.linear_attn_decode_backend = "flashinfer"
logger.info(