From 8e19db19b011e269643f297795b173c7c429a23a Mon Sep 17 00:00:00 2001 From: Khoa Pham Date: Thu, 8 Oct 2026 19:46:28 -0700 Subject: [PATCH] [KDA] Default safe-gate Blackwell prefill to FlashInfer (#43246) --- .../layers/attention/attention_registry.py | 8 +- .../layers/attention/linear/kda_backend.py | 25 +++++ .../srt/layers/attention/linear/utils.py | 6 +- .../attention/unittests/kda/README.md | 8 +- .../attention/test_linear_attn_config.py | 91 ++++++++++++++++++- 5 files changed, 131 insertions(+), 7 deletions(-) diff --git a/python/sglang/srt/layers/attention/attention_registry.py b/python/sglang/srt/layers/attention/attention_registry.py index 6acd9ce7fd..0e07022876 100644 --- a/python/sglang/srt/layers/attention/attention_registry.py +++ b/python/sglang/srt/layers/attention/attention_registry.py @@ -9,6 +9,7 @@ from sglang.srt.arg_groups.overrides import ( from sglang.srt.configs.hybrid_arch import ( glm5_next_config, hybrid_gdn_config, + hybrid_kda_config, hybrid_lightning_config, kimi_linear_config, mamba2_config, @@ -388,7 +389,10 @@ def attn_backend_wrapper(runner: "ModelRunner", full_attn_backend: "AttentionBac ) from sglang.kernels.ops.attention.fla.utils import check_environments - from sglang.srt.layers.attention.linear.kda_backend import KDAAttnBackend + from sglang.srt.layers.attention.linear.kda_backend import ( + KDAAttnBackend, + flashinfer_kda_prefill_default, + ) from sglang.srt.layers.attention.linear.lightning_backend import ( LightningAttentionBackend, ) @@ -430,6 +434,8 @@ def attn_backend_wrapper(runner: "ModelRunner", full_attn_backend: "AttentionBac prefill_default = None if hybrid_gdn_config(runner.model_config) is not None and not is_npu(): prefill_default = flashinfer_gdn_prefill_default(runner) + elif hybrid_kda_config(runner.model_config) is not None and not is_npu(): + prefill_default = flashinfer_kda_prefill_default(runner) runner.linear_attn_backends = resolve_linear_attn_backends( prefill_default=prefill_default ) diff --git a/python/sglang/srt/layers/attention/linear/kda_backend.py b/python/sglang/srt/layers/attention/linear/kda_backend.py index cf37a8093f..c14c199233 100644 --- a/python/sglang/srt/layers/attention/linear/kda_backend.py +++ b/python/sglang/srt/layers/attention/linear/kda_backend.py @@ -64,6 +64,31 @@ def _validate_flashinfer_kda_prefill( ) +def flashinfer_kda_prefill_default(model_runner: ModelRunner) -> Optional[str]: + """Default safe-gate KDA prefill to FlashInfer on SM100/SM103.""" + from sglang.srt.configs.bailing_hybrid import BailingHybridConfig + from sglang.srt.configs.hybrid_arch import hybrid_kda_config + + config = hybrid_kda_config(model_runner.model_config) + lower_bound = ( + config.kda_lower_bound + if isinstance(config, BailingHybridConfig) + else config.linear_attn_config.get("gate_lower_bound") + ) + execution = get_exec() + if ( + lower_bound is not None + and execution.mamba.linear_attn_backend == "triton" + and not execution.deterministic.enable_deterministic_inference + and not execution.overlap.enable_two_batch_overlap + and execution.graph.cuda_graph_config.prefill.backend != CudaGraphBackend.FULL + and is_cuda() + and torch.cuda.get_device_capability() in ((10, 0), (10, 3)) + ): + return "flashinfer" + return None + + class KDAKernelDispatcher: """Dispatches KDA kernel calls to the appropriate backend per mode.""" diff --git a/python/sglang/srt/layers/attention/linear/utils.py b/python/sglang/srt/layers/attention/linear/utils.py index 5b0af9e510..81fc535b32 100644 --- a/python/sglang/srt/layers/attention/linear/utils.py +++ b/python/sglang/srt/layers/attention/linear/utils.py @@ -68,7 +68,7 @@ class LinearAttnBackends(msgspec.Struct, frozen=True): """One runner's linear-attn kernel choice, per phase. Per runner, not per process: a target and its draft coexist and can want - different kernels (only the runner whose model is GDN gets the SM100 + different kernels (only eligible GDN/KDA runners get the FlashInfer prefill default, and an explicit flag applies to whichever runner was launched with it). """ @@ -83,8 +83,8 @@ def resolve_linear_attn_backends( ) -> LinearAttnBackends: """This runner's kernel choice from the published leaves. - ``prefill_default`` is the caller's own auto-default (the SM100 GDN - domain); an explicitly configured ``--linear-attn-prefill-backend`` wins. + ``prefill_default`` is the caller's own auto-default; + an explicitly configured ``--linear-attn-prefill-backend`` wins. """ mamba = get_exec().mamba base = mamba.linear_attn_backend diff --git a/test/registered/attention/unittests/kda/README.md b/test/registered/attention/unittests/kda/README.md index 9c18d78983..1adc6e8e36 100644 --- a/test/registered/attention/unittests/kda/README.md +++ b/test/registered/attention/unittests/kda/README.md @@ -33,14 +33,18 @@ Columns are runner modes; rows are the linear-attention kernel backend. Cells us ## FlashInfer Prefill Contract +Safe-gate KDA models default to FlashInfer prefill on SM100/SM103. Explicit +`--linear-attn-prefill-backend` choices take precedence. Other hardware, unbounded +gates, deterministic inference, TBO, and full prefill CUDA graphs retain Triton. + FlashInfer prefill execution requires SM100/SM103, BF16 model activations, BF16 or FP32 SSM state, equal Q/K/V head counts with 128-D heads, and a finite negative safe-gate lower bound. FlashInfer validates this tensor contract when the kernel runs, including during server warmup. Radix-cache checkpoints require a positive mamba chunk size divisible by 32. Incompatible intervals are rejected at backend -initialization; FlashInfer rejects unbounded gates during warmup, so original -Kimi Linear should use Triton prefill. +initialization; explicit FlashInfer selection rejects unbounded gates during +warmup. Original Kimi Linear retains Triton prefill by default. The backend maps radix-cache checkpoint destinations once per tracked batch, before layer execution. The adapter consumes raw gate/beta projections and diff --git a/test/registered/unit/layers/attention/test_linear_attn_config.py b/test/registered/unit/layers/attention/test_linear_attn_config.py index c46aea918f..38178a5222 100644 --- a/test/registered/unit/layers/attention/test_linear_attn_config.py +++ b/test/registered/unit/layers/attention/test_linear_attn_config.py @@ -1,7 +1,7 @@ """Each runner carries its own linear-attn kernel choice. A target and its draft coexist in one process and can want different kernels: -only the runner whose model is GDN gets the SM100 FlashInfer prefill default, +only eligible GDN/KDA runners get the FlashInfer prefill default, and the operator's explicit flag applies to the launch. So the choice is a per-runner stamp read from the runner, the way the full-attention pair already works (`prefill_attention_backend_str` / `decode_attention_backend_str`), rather @@ -17,6 +17,7 @@ backend). import unittest from types import SimpleNamespace +from unittest.mock import patch from sglang.srt.layers.attention.linear.utils import ( LinearAttnKernelBackend, @@ -158,6 +159,94 @@ class TestLinearAttnBackends(CustomTestCase): class TestFlashInferKDAPrefillPolicy(CustomTestCase): + def test_default_preserves_model_hardware_and_operator_choices(self): + from sglang.srt.configs.bailing_hybrid import BailingHybridConfig + from sglang.srt.configs.glm5_next import Glm5NextConfig + from sglang.srt.configs.kimi_k3 import KimiK3Config + from sglang.srt.configs.kimi_linear import KimiLinearConfig + from sglang.srt.layers.attention.linear.kda_backend import ( + flashinfer_kda_prefill_default, + ) + from sglang.srt.model_executor.cuda_graph_config import ( + CudaGraphConfig, + PhaseConfig, + ) + from sglang.srt.runtime_context import get_context, get_exec + + linear_config = {"kda_layers": [0], "full_attn_layers": []} + k3 = KimiK3Config( + text_config={ + "linear_attn_config": {**linear_config, "gate_lower_bound": -5} + } + ) + safe_configs = [ + k3, + Glm5NextConfig(text_config={"gate_lower_bound": -5}), + BailingHybridConfig(use_kda=True, kda_safe_gate=True, kda_lower_bound=-5), + ] + cases = [(config, (10, 3), {}, "flashinfer") for config in safe_configs] + cases += [ + (k3, (10, 0), {}, "flashinfer"), + (k3, (9, 0), {}, "triton"), + (k3, (12, 0), {}, "triton"), + (k3, None, {}, "triton"), + (KimiLinearConfig(linear_attn_config=linear_config), (10, 3), {}, "triton"), + (BailingHybridConfig(use_kda=True), (10, 3), {}, "triton"), + ] + cases += [ + (k3, (10, 3), fields, expected) + for fields, expected in [ + ({"linear_attn_prefill_backend": "triton"}, "triton"), + ({"linear_attn_prefill_backend": "helion"}, "helion"), + ({"linear_attn_backend": "helion"}, "helion"), + ({"enable_deterministic_inference": True}, "triton"), + ({"enable_two_batch_overlap": True}, "triton"), + ( + { + "cuda_graph_config": CudaGraphConfig( + prefill=PhaseConfig(backend="full") + ) + }, + "triton", + ), + ] + ] + for config, capability, fields, expected in cases: + runner = SimpleNamespace( + model_config=SimpleNamespace(hf_config=config, is_draft_model=False) + ) + with ( + self.subTest( + model=config.model_type, capability=capability, fields=fields + ), + get_context().override_server_args( + **{ + "linear_attn_backend": "triton", + "cuda_graph_config": CudaGraphConfig( + prefill=PhaseConfig(backend="disabled") + ), + **fields, + } + ), + patch( + "sglang.srt.layers.attention.linear.kda_backend.is_cuda", + return_value=capability is not None, + ), + patch("torch.cuda.get_device_capability", return_value=capability), + ): + backends = resolve_linear_attn_backends( + prefill_default=flashinfer_kda_prefill_default(runner) + ) + self.assertEqual(backends.prefill.value, expected) + self.assertEqual( + backends.decode.value, fields.get("linear_attn_backend", "triton") + ) + self.assertEqual(backends.verify, LinearAttnKernelBackend.TRITON) + self.assertEqual( + get_exec().mamba.linear_attn_prefill_backend, + fields.get("linear_attn_prefill_backend"), + ) + def test_fixed_restrictions_are_rejected_before_forward(self): from sglang.srt.layers.attention.linear.kda_backend import ( _validate_flashinfer_kda_prefill,