chore: import upstream snapshot with attribution
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# AMD-specific DeepSeek common model helpers.
|
||||
@@ -0,0 +1,158 @@
|
||||
import logging
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import torch
|
||||
import triton
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_FUSED_HC_POST_PRE_M_THRESHOLD = 64
|
||||
_FUSED_HC_POST_PRE_CACHE: dict[tuple, dict[str, torch.Tensor]] = {}
|
||||
_TRITON_MHC_POST_PRE_OPS = None
|
||||
_TRITON_MHC_POST_PRE_RUNTIME_DISABLED = False
|
||||
|
||||
|
||||
def _get_triton_mhc_post_pre_ops():
|
||||
global _TRITON_MHC_POST_PRE_OPS
|
||||
|
||||
if _TRITON_MHC_POST_PRE_OPS is not None:
|
||||
return _TRITON_MHC_POST_PRE_OPS
|
||||
|
||||
try:
|
||||
from aiter.ops.triton.fusions.mhc import mhc_post_pre
|
||||
from aiter.ops.triton.utils.mhc_config_utils import get_mhc_config
|
||||
except Exception as err:
|
||||
logger.warning(
|
||||
"Triton fused mHC (mhc_post_pre) is unavailable, falling back: %s", err
|
||||
)
|
||||
return None
|
||||
|
||||
_TRITON_MHC_POST_PRE_OPS = (mhc_post_pre, get_mhc_config)
|
||||
return _TRITON_MHC_POST_PRE_OPS
|
||||
|
||||
|
||||
def _get_fused_hc_post_pre_buffers(
|
||||
num_tokens: int,
|
||||
hidden_size: int,
|
||||
hc_mult: int,
|
||||
dtype: torch.dtype,
|
||||
device: torch.device,
|
||||
) -> Optional[dict[str, torch.Tensor]]:
|
||||
ops = _get_triton_mhc_post_pre_ops()
|
||||
if ops is None:
|
||||
return None
|
||||
_, get_mhc_config = ops
|
||||
|
||||
key = (num_tokens, hidden_size, hc_mult, dtype, device.type, device.index)
|
||||
bufs = _FUSED_HC_POST_PRE_CACHE.get(key)
|
||||
if bufs is not None:
|
||||
return bufs
|
||||
|
||||
try:
|
||||
cfg, _ = get_mhc_config("MHC_FUSED", num_tokens, hidden_size, mode="sinkhorn")
|
||||
except Exception as err:
|
||||
logger.warning("Failed to initialize fused mHC config, falling back: %s", err)
|
||||
return None
|
||||
|
||||
n_total = 2 * hc_mult + hc_mult * hc_mult
|
||||
k_dim = hc_mult * hidden_size
|
||||
block_k = cfg.get("BLOCK_K", min(512, triton.next_power_of_2(k_dim)))
|
||||
block_k = min(block_k, triton.next_power_of_2(k_dim))
|
||||
block_c_split = max(block_k // hc_mult, 1)
|
||||
num_ksplit = triton.cdiv(hidden_size, block_c_split)
|
||||
|
||||
bufs = {
|
||||
"residual_out": torch.empty(
|
||||
num_tokens, hc_mult, hidden_size, dtype=dtype, device=device
|
||||
),
|
||||
"layer_input_out": torch.empty(
|
||||
num_tokens, hidden_size, dtype=dtype, device=device
|
||||
),
|
||||
"h_post": torch.empty(num_tokens, hc_mult, dtype=torch.float32, device=device),
|
||||
"h_res": torch.empty(
|
||||
num_tokens, hc_mult, hc_mult, dtype=torch.float32, device=device
|
||||
),
|
||||
"acc_partial": torch.empty(
|
||||
num_ksplit, num_tokens, n_total, dtype=torch.float32, device=device
|
||||
),
|
||||
"acc_sq_partial": torch.empty(
|
||||
num_ksplit, num_tokens, dtype=torch.float32, device=device
|
||||
),
|
||||
}
|
||||
_FUSED_HC_POST_PRE_CACHE[key] = bufs
|
||||
return bufs
|
||||
|
||||
|
||||
def try_fused_hc_post_pre(
|
||||
x: torch.Tensor,
|
||||
residual: torch.Tensor,
|
||||
post: torch.Tensor,
|
||||
comb: torch.Tensor,
|
||||
hc_fn_t: torch.Tensor,
|
||||
hc_scale: torch.Tensor,
|
||||
hc_base: torch.Tensor,
|
||||
hc_mult: int,
|
||||
norm_eps: float,
|
||||
hc_eps: float,
|
||||
hc_post_mult: float,
|
||||
sinkhorn_iters: int,
|
||||
is_gfx95_supported: bool,
|
||||
) -> Optional[Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, bool]]:
|
||||
global _TRITON_MHC_POST_PRE_RUNTIME_DISABLED
|
||||
|
||||
if (
|
||||
_TRITON_MHC_POST_PRE_RUNTIME_DISABLED
|
||||
or not envs.SGLANG_OPT_USE_TRITON_FUSED_MHC.get()
|
||||
or not is_gfx95_supported
|
||||
or x.shape[0] == 0
|
||||
or x.shape[0] > _FUSED_HC_POST_PRE_M_THRESHOLD
|
||||
or x.dim() != 2
|
||||
or residual.dim() != 3
|
||||
):
|
||||
return None
|
||||
|
||||
ops = _get_triton_mhc_post_pre_ops()
|
||||
if ops is None:
|
||||
return None
|
||||
mhc_post_pre, _ = ops
|
||||
|
||||
bufs = _get_fused_hc_post_pre_buffers(
|
||||
x.shape[0], x.shape[1], hc_mult, residual.dtype, x.device
|
||||
)
|
||||
if bufs is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
_, _, layer_input_out, new_residual = mhc_post_pre(
|
||||
x,
|
||||
residual,
|
||||
post,
|
||||
comb,
|
||||
hc_fn_t,
|
||||
hc_scale,
|
||||
hc_base,
|
||||
hc_mult,
|
||||
norm_eps,
|
||||
hc_eps,
|
||||
hc_post_mult,
|
||||
sinkhorn_iters,
|
||||
# Match sglang's exp-domain asymmetric Sinkhorn used in hc_pre.
|
||||
asymmetric_exp_domain=True,
|
||||
hc_sinkhorn_eps=hc_eps,
|
||||
residual_out=bufs["residual_out"],
|
||||
h_post=bufs["h_post"],
|
||||
h_res=bufs["h_res"],
|
||||
layer_input_out=bufs["layer_input_out"],
|
||||
acc_partial=bufs["acc_partial"],
|
||||
acc_sq_partial=bufs["acc_sq_partial"],
|
||||
)
|
||||
except Exception as err:
|
||||
logger.warning(
|
||||
"Triton fused mHC kernel failed, disabling fallback path: %s", err
|
||||
)
|
||||
_TRITON_MHC_POST_PRE_RUNTIME_DISABLED = True
|
||||
return None
|
||||
|
||||
return new_residual, layer_input_out, bufs["h_post"], bufs["h_res"], False
|
||||
@@ -0,0 +1,216 @@
|
||||
from sglang.srt.layers.attention.tbo_backend import TboAttnBackend
|
||||
from sglang.srt.layers.utils.cp_utils import mla_use_prefill_cp
|
||||
from sglang.srt.model_executor.forward_context import get_attn_backend
|
||||
from sglang.srt.model_executor.runner_backend_utils.breakable_cuda_graph import (
|
||||
is_in_breakable_cuda_graph,
|
||||
)
|
||||
from sglang.srt.model_executor.runner_backend_utils.tc_piecewise_cuda_graph import (
|
||||
is_in_tc_piecewise_cuda_graph,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.attention_forward_methods.forward_methods import (
|
||||
AttnForwardMethod,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.utils import _is_hip
|
||||
from sglang.srt.runtime_context import get_server_args
|
||||
from sglang.srt.utils import use_intel_amx_backend
|
||||
|
||||
MHA_ONE_SHOT_SUPPORTED_BACKENDS = ["fa3", "flashinfer", "flashmla"]
|
||||
|
||||
|
||||
class AttentionBackendRegistry:
|
||||
_handlers = {}
|
||||
|
||||
@classmethod
|
||||
def register(cls, backend_name, handler_func):
|
||||
cls._handlers[backend_name] = handler_func
|
||||
|
||||
@classmethod
|
||||
def get_handler(cls, backend_name):
|
||||
return cls._handlers.get(backend_name, cls._handlers.get("triton"))
|
||||
|
||||
|
||||
def _dispatch_mla_subtype(attn, forward_batch):
|
||||
if _is_hip:
|
||||
if attn.rocm_fused_decode_mla and forward_batch.forward_mode.is_decode():
|
||||
return AttnForwardMethod.MLA_FUSED_ROPE_ROCM
|
||||
else:
|
||||
return AttnForwardMethod.MLA
|
||||
else:
|
||||
if hasattr(attn, "fused_qkv_a_proj_with_mqa") and use_intel_amx_backend(attn):
|
||||
return AttnForwardMethod.MLA_FUSED_ROPE_CPU
|
||||
else:
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
|
||||
def handle_attention_ascend(attn, forward_batch):
|
||||
if (
|
||||
forward_batch.forward_mode.is_extend()
|
||||
and not forward_batch.forward_mode.is_target_verify()
|
||||
and not forward_batch.forward_mode.is_draft_extend_v2()
|
||||
):
|
||||
if hasattr(attn, "use_dsa") and attn.use_dsa:
|
||||
return AttnForwardMethod.DSA_NPU
|
||||
else:
|
||||
return AttnForwardMethod.MHA_NPU
|
||||
else:
|
||||
if hasattr(attn, "use_dsa") and attn.use_dsa:
|
||||
return AttnForwardMethod.DSA_NPU
|
||||
else:
|
||||
return AttnForwardMethod.MLA_NPU
|
||||
|
||||
|
||||
def _get_sum_extend_prefix_lens(forward_batch):
|
||||
return (
|
||||
sum(forward_batch.extend_prefix_lens_cpu)
|
||||
if forward_batch.extend_prefix_lens_cpu is not None
|
||||
else 0
|
||||
)
|
||||
|
||||
|
||||
def _support_mha_one_shot(attn, forward_batch, backend_name):
|
||||
attn_supported = backend_name in MHA_ONE_SHOT_SUPPORTED_BACKENDS
|
||||
sum_seq_lens = (
|
||||
sum(forward_batch.seq_lens_cpu) if forward_batch.seq_lens_cpu is not None else 0
|
||||
)
|
||||
return attn_supported and sum_seq_lens <= forward_batch.get_max_chunk_capacity()
|
||||
|
||||
|
||||
def _handle_attention_backend(attn, forward_batch, backend_name):
|
||||
if is_in_tc_piecewise_cuda_graph():
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
# MLA prefill CP forces absorbed MLA regardless of prefix length: the
|
||||
# CP path gathers latent KV via rebuild_cp_kv_cache and feeds the
|
||||
# backend's absorbed-MLA kernel.
|
||||
if mla_use_prefill_cp(forward_batch):
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
|
||||
sum_extend_prefix_lens = _get_sum_extend_prefix_lens(forward_batch)
|
||||
disable_ragged = (
|
||||
backend_name in ["flashinfer", "flashmla"]
|
||||
) and attn.flashinfer_mla_disable_ragged
|
||||
|
||||
if (
|
||||
not disable_ragged
|
||||
and forward_batch.forward_mode.is_extend_without_speculative()
|
||||
and (
|
||||
(
|
||||
sum_extend_prefix_lens >= attn.chunked_prefix_cache_threshold
|
||||
and not attn.disable_chunked_prefix_cache
|
||||
)
|
||||
or sum_extend_prefix_lens == 0
|
||||
)
|
||||
):
|
||||
if _support_mha_one_shot(attn, forward_batch, backend_name):
|
||||
return AttnForwardMethod.MHA_ONE_SHOT
|
||||
return AttnForwardMethod.MHA_CHUNKED_KV
|
||||
else:
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
|
||||
|
||||
def handle_attention_flashinfer(attn, forward_batch):
|
||||
return _handle_attention_backend(attn, forward_batch, "flashinfer")
|
||||
|
||||
|
||||
def handle_attention_fa3(attn, forward_batch):
|
||||
# when deterministic inference is enabled, use MLA
|
||||
if get_server_args().enable_deterministic_inference:
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
else:
|
||||
return _handle_attention_backend(attn, forward_batch, "fa3")
|
||||
|
||||
|
||||
def handle_attention_flashmla(attn, forward_batch):
|
||||
return _handle_attention_backend(attn, forward_batch, "flashmla")
|
||||
|
||||
|
||||
def handle_attention_cutlass_mla(attn, forward_batch):
|
||||
return _handle_attention_backend(attn, forward_batch, "cutlass_mla")
|
||||
|
||||
|
||||
def handle_attention_fa4(attn, forward_batch):
|
||||
# TODO(cicirori): use FA4 MHA for DeepSeekV3 for now
|
||||
return AttnForwardMethod.MHA_CHUNKED_KV
|
||||
|
||||
|
||||
def handle_attention_trtllm_mla(attn, forward_batch):
|
||||
if is_in_tc_piecewise_cuda_graph():
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
sum_extend_prefix_lens = _get_sum_extend_prefix_lens(forward_batch)
|
||||
if forward_batch.forward_mode.is_extend_without_speculative() and (
|
||||
not attn.disable_chunked_prefix_cache or sum_extend_prefix_lens == 0
|
||||
):
|
||||
return AttnForwardMethod.MHA_CHUNKED_KV
|
||||
else:
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
|
||||
|
||||
def handle_attention_tokenspeed_mla(attn, forward_batch):
|
||||
# tokenspeed_mla shares the trtllm_mla dispatch pattern: pure prefill goes
|
||||
# via MHA chunked KV (TRT-LLM ragged), spec decode / decode goes via MLA.
|
||||
return handle_attention_trtllm_mla(attn, forward_batch)
|
||||
|
||||
|
||||
def handle_attention_aiter(attn, forward_batch):
|
||||
# During PCG/BCG capture on ROCm, aiter fp8 MLA prefill has no capture
|
||||
# kernels; route through the MHA path (radix_attention swaps attn_mqa for
|
||||
# its attn_mha companion) so capture/replay use valid head/dim metadata.
|
||||
if is_in_tc_piecewise_cuda_graph() or is_in_breakable_cuda_graph():
|
||||
return AttnForwardMethod.MHA
|
||||
if forward_batch.forward_mode.is_extend_without_speculative():
|
||||
return AttnForwardMethod.MHA
|
||||
else:
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
|
||||
def handle_attention_dsa(attn, forward_batch):
|
||||
"""
|
||||
Dispatch logic is centralized in DeepseekSparseAttnBackend.set_dsa_prefill_impl and executed
|
||||
in init_forward_metadata. Read the decision from backend.use_mha.
|
||||
"""
|
||||
|
||||
backend = get_attn_backend()
|
||||
if isinstance(backend, TboAttnBackend): # if enable tbo, get primary backend
|
||||
backend = backend.primary
|
||||
if hasattr(backend, "use_mha") and backend.use_mha:
|
||||
return AttnForwardMethod.MHA_ONE_SHOT
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
|
||||
def handle_attention_triton(attn, forward_batch):
|
||||
if is_in_tc_piecewise_cuda_graph():
|
||||
return AttnForwardMethod.MLA
|
||||
|
||||
# when deterministic inference is enabled, use MLA
|
||||
if get_server_args().enable_deterministic_inference:
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
|
||||
if (
|
||||
forward_batch.forward_mode.is_extend_without_speculative()
|
||||
and sum(forward_batch.extend_prefix_lens_cpu) == 0
|
||||
):
|
||||
return AttnForwardMethod.MHA
|
||||
else:
|
||||
return _dispatch_mla_subtype(attn, forward_batch)
|
||||
|
||||
|
||||
def handle_attention_intel_xpu(attn, forward_batch):
|
||||
return _handle_attention_backend(attn, forward_batch, "intel_xpu")
|
||||
|
||||
|
||||
AttentionBackendRegistry.register("ascend", handle_attention_ascend)
|
||||
AttentionBackendRegistry.register("flashinfer", handle_attention_flashinfer)
|
||||
AttentionBackendRegistry.register("fa3", handle_attention_fa3)
|
||||
AttentionBackendRegistry.register("flashmla", handle_attention_flashmla)
|
||||
AttentionBackendRegistry.register("cutlass_mla", handle_attention_cutlass_mla)
|
||||
AttentionBackendRegistry.register("fa4", handle_attention_fa4)
|
||||
AttentionBackendRegistry.register("trtllm_mla", handle_attention_trtllm_mla)
|
||||
AttentionBackendRegistry.register("tokenspeed_mla", handle_attention_tokenspeed_mla)
|
||||
AttentionBackendRegistry.register("aiter", handle_attention_aiter)
|
||||
AttentionBackendRegistry.register("dsa", handle_attention_dsa)
|
||||
AttentionBackendRegistry.register(
|
||||
"nsa", handle_attention_dsa
|
||||
) # Deprecated alias; use "dsa"
|
||||
AttentionBackendRegistry.register("triton", handle_attention_triton)
|
||||
AttentionBackendRegistry.register("intel_xpu", handle_attention_intel_xpu)
|
||||
@@ -0,0 +1,13 @@
|
||||
from .forward_methods import AttnForwardMethod
|
||||
from .forward_mha import DeepseekMHAForwardMixin
|
||||
from .forward_mla import DeepseekMLAForwardMixin
|
||||
from .forward_mla_fused_rope_cpu import DeepseekMLACpuForwardMixin
|
||||
from .forward_mla_fused_rope_rocm import DeepseekMLARocmForwardMixin
|
||||
|
||||
__all__ = [
|
||||
"AttnForwardMethod",
|
||||
"DeepseekMHAForwardMixin",
|
||||
"DeepseekMLACpuForwardMixin",
|
||||
"DeepseekMLAForwardMixin",
|
||||
"DeepseekMLARocmForwardMixin",
|
||||
]
|
||||
@@ -0,0 +1,32 @@
|
||||
from enum import IntEnum, auto
|
||||
|
||||
|
||||
class AttnForwardMethod(IntEnum):
|
||||
# Use multi-head attention
|
||||
MHA = auto()
|
||||
|
||||
# Use absorbed multi-latent attention
|
||||
MLA = auto()
|
||||
|
||||
# Use multi-head attention, but with KV cache chunked.
|
||||
# This method can avoid OOM when prefix lengths are long.
|
||||
MHA_CHUNKED_KV = auto()
|
||||
|
||||
# Use multi-head attention, execute the MHA for prefix and extended kv in a single kernel
|
||||
# when the sequence lengths are below the threshold.
|
||||
MHA_ONE_SHOT = auto()
|
||||
|
||||
# Use MLA but with fused RoPE
|
||||
MLA_FUSED_ROPE_ROCM = auto()
|
||||
|
||||
# Use MLA with fused RoPE kernel for CPU
|
||||
MLA_FUSED_ROPE_CPU = auto()
|
||||
|
||||
# Use multi-head attention for NPU
|
||||
MHA_NPU = auto()
|
||||
|
||||
# Use absorbed multi-latent attention for NPU
|
||||
MLA_NPU = auto()
|
||||
|
||||
# Use Deepseek V3.2 sparse multi-latent attention for NPU
|
||||
DSA_NPU = auto()
|
||||
@@ -0,0 +1,604 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
from sglang.srt.layers.attention.dsa.dequant_k_cache import dequantize_k_cache_paged
|
||||
from sglang.srt.layers.attention.tbo_backend import TboAttnBackend
|
||||
from sglang.srt.layers.attention.utils import concat_and_cast_mha_k_triton
|
||||
from sglang.srt.layers.communicator import get_attn_tp_context
|
||||
from sglang.srt.layers.dcp import (
|
||||
all_gather_kv_cache_for_mha_chunk_extend,
|
||||
all_gather_kv_cache_for_mha_extend,
|
||||
filter_dcp_local_kv_indices,
|
||||
)
|
||||
from sglang.srt.layers.quantization.fp8_utils import (
|
||||
materialize_bpreshuffle_fp8_scale_tuple,
|
||||
)
|
||||
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
||||
from sglang.srt.model_executor.forward_context import (
|
||||
get_attn_backend,
|
||||
get_token_to_kv_pool,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
_is_cuda,
|
||||
_is_hip,
|
||||
_is_musa,
|
||||
_is_npu,
|
||||
_use_aiter_bpreshuffle_gfx95,
|
||||
_use_aiter_gfx95,
|
||||
)
|
||||
from sglang.srt.runtime_context import get_parallel, get_server_args
|
||||
from sglang.srt.utils import BumpAllocator, get_bool_env_var, next_power_of_2
|
||||
|
||||
_use_fp8_prefill_attn = (
|
||||
get_bool_env_var("SGLANG_AITER_FP8_PREFILL_ATTN", "True") and _use_aiter_gfx95
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sglang.srt.models.deepseek_v2 import DeepseekV2AttentionMLA
|
||||
|
||||
if _is_cuda:
|
||||
from sgl_kernel import merge_state_v2
|
||||
|
||||
from sglang.jit_kernel.concat_mla import concat_mla_k
|
||||
elif _is_musa:
|
||||
from sgl_kernel import concat_mla_k
|
||||
|
||||
if _use_aiter_gfx95:
|
||||
from aiter.ops.triton.fused_fp8_quant import fused_rms_fp8_group_quant
|
||||
|
||||
from sglang.srt.layers.quantization.fp8_kernel import fp8_dtype
|
||||
from sglang.srt.layers.quantization.rocm_mxfp4_utils import fused_rms_mxfp4_quant
|
||||
|
||||
|
||||
def _resolve_attn_backend(forward_batch: ForwardBatch):
|
||||
backend = get_attn_backend()
|
||||
if isinstance(backend, TboAttnBackend):
|
||||
backend = backend.primary
|
||||
return backend
|
||||
|
||||
|
||||
# Configs for DeepSeek-V3:
|
||||
# num_local_heads = 128
|
||||
# qk_nope_head_dim = 128
|
||||
# qk_rope_head_dim = 64
|
||||
# qk_head_dim = qk_nope_head_dim + qk_rope_head_dim = 192
|
||||
# v_head_dim = 128
|
||||
|
||||
# Configs for kv chunking strategy:
|
||||
# sum_prefix_length:
|
||||
# Total number of tokens to be fetched from kv cache for current batch.
|
||||
# e.g: For batch with 2 sequences, seq_lens_kv = [1024, 2048], seq_lens_q = [512, 1024], then sum_prefix_length = (1024 - 512) + (2048 - 1024) = 1536
|
||||
# sum_extended_length:
|
||||
# Total number of tokens in the extended part of the current batch. (=sum(seq_lens_q))
|
||||
# chunked_prefix_cache_threshold:
|
||||
# The minimum sum_prefix_length to enable mha with kv chunking, 8192 by default (can be changed with SGLANG_CHUNKED_PREFIX_CACHE_THRESHOLD)
|
||||
# For batches with smaller sum_prefix_length > 0, MLA kernel with absorption will be used instead.
|
||||
# max_kv_chunk_capacity:
|
||||
# The maximum number of tokens in each kv chunk, 128 * 1024 by default (can be changed with SGLANG_MAX_KV_CHUNK_CAPACITY, or get with forward_batch.get_max_chunk_capacity())
|
||||
|
||||
# The forward methods for MHA in DeepSeek models:
|
||||
#
|
||||
# 1. forward_normal: AttnForwardMethod.MHA
|
||||
# use multi-head attention with empty kv cache (the first batch of chunked prefill, prefix lens = 0)
|
||||
# q: [sum_extended_length, num_local_heads, qk_head_dim]
|
||||
# k: [sum_extended_length, num_local_heads, qk_head_dim]
|
||||
# v: [sum_extended_length, num_local_heads, v_head_dim]
|
||||
#
|
||||
# 2. forward_normal_one_shot: AttnForwardMethod.MHA_ONE_SHOT
|
||||
# use multi-head attention with short kv prefix length (chunked_prefix_cache_threshold <= sum_prefix_lens <= max_kv_chunk_capacity)
|
||||
# the kv latent vectors are fetched from memory pool, with combined kv_indices of prefix part and extended part
|
||||
# q: [batch_size, num_local_heads, qk_head_dim]
|
||||
# k: [sum_extended_length + sum_prefix_length, num_local_heads, qk_head_dim]
|
||||
# v: [sum_extended_length + sum_prefix_length, num_local_heads, v_head_dim]
|
||||
#
|
||||
# 3. forward_normal_chunked_kv: AttnForwardMethod.MHA_CHUNKED_KV
|
||||
# multiple phases of multi-head attention with chunked kv cache (sum_prefix_length > max_kv_chunk_capacity)
|
||||
# For the first phase, it will execute normal forward method, and returns output o_1 and lse_1,
|
||||
# q_1: [sum_extended_length, num_local_heads, qk_head_dim],
|
||||
# k_1: [sum_extended_length, num_local_heads, qk_head_dim],
|
||||
# v_1: [sum_extended_length, num_local_heads, qk_head_dim],
|
||||
# acc_o_1, acc_lse_1 = o_1, lse_1
|
||||
# For i in range(2, n), (n-1 is the number of prefix chunks), kv latent vectors are fetched from memory pool with prefix kv indices
|
||||
# q_i: [sum_extended_length, num_local_heads, qk_head_dim],
|
||||
# k_i: [chunk_size, num_local_heads, qk_head_dim],
|
||||
# v_i: [chunk_size, num_local_heads, v_head_dim],
|
||||
# acc_o_i, acc_lse_i = merge_state(acc_o_{i-1}, acc_lse_{i-1}, o_i, lse_i)
|
||||
# The final output is the accumulated output acc_o_n
|
||||
|
||||
|
||||
class DeepseekMHAForwardMixin:
|
||||
|
||||
def init_mha_forward(self: DeepseekV2AttentionMLA):
|
||||
self.disable_chunked_prefix_cache = (
|
||||
get_server_args().disable_chunked_prefix_cache
|
||||
)
|
||||
|
||||
# TODO: Design a finer way to determine the threshold
|
||||
self.chunked_prefix_cache_threshold = (
|
||||
envs.SGLANG_CHUNKED_PREFIX_CACHE_THRESHOLD.get()
|
||||
)
|
||||
|
||||
def forward_normal_prepare(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
zero_allocator: BumpAllocator,
|
||||
):
|
||||
if self.q_lora_rank is not None:
|
||||
q, latent_cache = (
|
||||
get_attn_tp_context()
|
||||
.fetch_qkv_latent()
|
||||
.split(
|
||||
[self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
|
||||
dim=-1,
|
||||
)
|
||||
)
|
||||
|
||||
# DSA Indexer: cache quantized keys, auto-skip topk for sequences <= dsa_index_topk
|
||||
|
||||
if self.use_dsa:
|
||||
# DSA requires unquantized q_lora for the indexer. When q_b_proj is FP8
|
||||
# on gfx95, we can still use fused RMSNorm+FP8 quant, but MUST request
|
||||
# the unquantized output for q_lora; otherwise q_lora becomes the (fp8,scale)
|
||||
# tuple.
|
||||
if (
|
||||
_use_aiter_gfx95
|
||||
and self.q_b_proj.weight.dtype == torch.float8_e4m3fn
|
||||
):
|
||||
q_quanted, q_lora, _, _ = fused_rms_fp8_group_quant(
|
||||
q,
|
||||
self.q_a_layernorm.weight,
|
||||
self.q_a_layernorm.variance_epsilon,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
group_size=128,
|
||||
dtype_quant=torch.float8_e4m3fn,
|
||||
res1=None,
|
||||
output_unquantized_inp1=True,
|
||||
transpose_scale=False,
|
||||
)
|
||||
if _use_aiter_bpreshuffle_gfx95:
|
||||
q_quanted = materialize_bpreshuffle_fp8_scale_tuple(q_quanted)
|
||||
q = self.q_b_proj(q_quanted)[0].view(
|
||||
-1, self.num_local_heads, self.qk_head_dim
|
||||
)
|
||||
else:
|
||||
q_lora = self.q_a_layernorm(q)
|
||||
q = self.q_b_proj(q_lora)[0].view(
|
||||
-1, self.num_local_heads, self.qk_head_dim
|
||||
)
|
||||
if self.should_run_indexer():
|
||||
_ = self.indexer(
|
||||
x=hidden_states,
|
||||
q_lora=q_lora,
|
||||
positions=positions,
|
||||
forward_batch=forward_batch,
|
||||
layer_id=self.layer_id,
|
||||
return_indices=False,
|
||||
)
|
||||
elif _use_aiter_gfx95 and self.q_b_proj.weight.dtype == torch.uint8:
|
||||
# MXFP4: fused RMSNorm + quant
|
||||
q, _, _, _ = fused_rms_mxfp4_quant(
|
||||
q,
|
||||
self.q_a_layernorm.weight,
|
||||
self.q_a_layernorm.variance_epsilon,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
q = self.q_b_proj(q)[0].view(-1, self.num_local_heads, self.qk_head_dim)
|
||||
elif _use_aiter_gfx95 and self.q_b_proj.weight.dtype == torch.float8_e4m3fn:
|
||||
|
||||
q, _, _, _ = fused_rms_fp8_group_quant(
|
||||
q,
|
||||
self.q_a_layernorm.weight,
|
||||
self.q_a_layernorm.variance_epsilon,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
group_size=128,
|
||||
dtype_quant=torch.float8_e4m3fn,
|
||||
res1=None,
|
||||
output_unquantized_inp1=False,
|
||||
transpose_scale=False,
|
||||
)
|
||||
if _use_aiter_bpreshuffle_gfx95:
|
||||
q = materialize_bpreshuffle_fp8_scale_tuple(q)
|
||||
q = self.q_b_proj(q)[0].view(-1, self.num_local_heads, self.qk_head_dim)
|
||||
else:
|
||||
q = self.q_a_layernorm(q)
|
||||
q = self.q_b_proj(q)[0].view(-1, self.num_local_heads, self.qk_head_dim)
|
||||
|
||||
else:
|
||||
q = self.q_proj(hidden_states)[0].view(
|
||||
-1, self.num_local_heads, self.qk_head_dim
|
||||
)
|
||||
latent_cache = self.kv_a_proj_with_mqa(hidden_states)[0]
|
||||
|
||||
_, q_pe = q.split([self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1)
|
||||
kv_a, _ = latent_cache.split([self.kv_lora_rank, self.qk_rope_head_dim], dim=-1)
|
||||
latent_cache = latent_cache.unsqueeze(1)
|
||||
|
||||
if _use_aiter_gfx95 and self.kv_b_proj.weight.dtype == torch.float8_e4m3fn:
|
||||
|
||||
kv_a_quanted, kv_a, _, _ = fused_rms_fp8_group_quant(
|
||||
kv_a,
|
||||
self.kv_a_layernorm.weight,
|
||||
self.kv_a_layernorm.variance_epsilon,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
group_size=128,
|
||||
dtype_quant=torch.float8_e4m3fn,
|
||||
res1=None,
|
||||
output_unquantized_inp1=True, # return unqaunt kv_a
|
||||
transpose_scale=False,
|
||||
)
|
||||
if _use_aiter_bpreshuffle_gfx95:
|
||||
kv_a_quanted = materialize_bpreshuffle_fp8_scale_tuple(kv_a_quanted)
|
||||
|
||||
else:
|
||||
kv_a = self.kv_a_layernorm(kv_a)
|
||||
|
||||
k_pe = latent_cache[:, :, self.kv_lora_rank :]
|
||||
|
||||
# Backend prefill hook: the backend owns the BF16->FP8 transition
|
||||
# (fused RoPE + quantize for Q/K, direct FP8 KV-cache write) and
|
||||
# returns FP8 tensors ready for its kernel. Backends without the
|
||||
# hook fall through to the BF16 path below.
|
||||
backend = _resolve_attn_backend(forward_batch)
|
||||
if hasattr(backend, "prepare_prefill_qkv"):
|
||||
q_out, k_out, v_out = backend.prepare_prefill_qkv(
|
||||
q=q,
|
||||
q_pe=q_pe,
|
||||
kv_a=kv_a,
|
||||
k_pe=k_pe,
|
||||
positions=positions,
|
||||
layer=self,
|
||||
forward_batch=forward_batch,
|
||||
)
|
||||
return q_out, k_out, v_out, forward_batch
|
||||
|
||||
if self.rotary_emb is not None:
|
||||
q_pe, k_pe = self.rotary_emb(positions, q_pe, k_pe)
|
||||
q[..., self.qk_nope_head_dim :] = q_pe
|
||||
|
||||
self._set_mla_kv_buffer(latent_cache, kv_a, k_pe, forward_batch)
|
||||
if (
|
||||
forward_batch.mha_one_shot
|
||||
and sum(forward_batch.extend_prefix_lens_cpu) != 0
|
||||
):
|
||||
if (
|
||||
self.use_dsa
|
||||
and self.kv_cache_dtype == "fp8_e4m3"
|
||||
and (
|
||||
not get_server_args().dsa_decode_backend == "trtllm"
|
||||
or not get_server_args().dsa_prefill_backend == "trtllm"
|
||||
)
|
||||
):
|
||||
# FP8 path: dequantize DSA-specific FP8 format to BF16
|
||||
kv_a, k_pe = self._get_mla_kv_buffer_from_fp8_for_dsa(forward_batch)
|
||||
else:
|
||||
# BF16/FP16 path: directly fetch from cache
|
||||
if get_parallel().dcp_enabled:
|
||||
kv_a, k_pe = all_gather_kv_cache_for_mha_extend(
|
||||
get_token_to_kv_pool(),
|
||||
self.attn_mha,
|
||||
forward_batch.attn_dcp_metadata.dcp_local_prefix_kv_indices,
|
||||
forward_batch.seq_lens,
|
||||
forward_batch.extend_prefix_lens,
|
||||
forward_batch.extend_prefix_lens_cpu,
|
||||
forward_batch.extend_seq_lens,
|
||||
kv_a,
|
||||
k_pe,
|
||||
)
|
||||
else:
|
||||
kv_a, k_pe = self._get_mla_kv_buffer(
|
||||
forward_batch.fetch_mha_one_shot_kv_indices(),
|
||||
q.dtype,
|
||||
forward_batch,
|
||||
)
|
||||
if _use_fp8_prefill_attn and self.kv_b_proj.weight.dtype == torch.uint8:
|
||||
# MXFP4 weights + FP8 prefill: fuse GEMM, nope/v split, and k_pe cat
|
||||
# into a single kernel (fused_gemm_afp4wfp4_split_cat) that writes k and v
|
||||
# directly in FP8, avoiding a separate elementwise cast
|
||||
k, v = self.kv_b_proj(
|
||||
(
|
||||
kv_a,
|
||||
k_pe.expand(-1, self.num_local_heads, -1),
|
||||
self.qk_nope_head_dim,
|
||||
self.v_head_dim,
|
||||
fp8_dtype,
|
||||
)
|
||||
)[0]
|
||||
else:
|
||||
if _use_aiter_gfx95 and self.kv_b_proj.weight.dtype == torch.float8_e4m3fn:
|
||||
kv = self.kv_b_proj(kv_a_quanted)[0]
|
||||
else:
|
||||
kv = self.kv_b_proj(kv_a)[0]
|
||||
kv = kv.view(
|
||||
-1, self.num_local_heads, self.qk_nope_head_dim + self.v_head_dim
|
||||
)
|
||||
k_nope = kv[..., : self.qk_nope_head_dim]
|
||||
v = kv[..., self.qk_nope_head_dim :]
|
||||
|
||||
k = self._concat_and_cast_mha_k(k_nope, k_pe, forward_batch)
|
||||
return q, k, v, forward_batch
|
||||
|
||||
def forward_normal_core(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
) -> torch.Tensor:
|
||||
attn_output = self.attn_mha(q, k, v, forward_batch, save_kv_cache=False)
|
||||
attn_output = attn_output.reshape(-1, self.num_local_heads * self.v_head_dim)
|
||||
output, _ = self.o_proj(attn_output)
|
||||
return output
|
||||
|
||||
def forward_normal_chunked_kv_prepare(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
zero_allocator: BumpAllocator,
|
||||
):
|
||||
# In normal mha, the k and v tensors will become overly large when the prefix length is long.
|
||||
# To avoid this, we split the kv cache into chunks and process them one after another.
|
||||
# Since mha is compute friendly, the for loop induced here will not introduce significant overhead.
|
||||
# The top comments in https://github.com/vllm-project/vllm/blob/main/vllm/v1/attention/backends/mla/common.py
|
||||
# will be helpful for understanding the purpose of this function.
|
||||
|
||||
# First do normal mha forward to get output for extended part
|
||||
return self.forward_normal_prepare(
|
||||
positions, hidden_states, forward_batch, zero_allocator
|
||||
)
|
||||
|
||||
def forward_normal_chunked_kv_core(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
) -> torch.Tensor:
|
||||
has_extend_prefix = forward_batch.extend_prefix_lens_cpu is not None and any(
|
||||
forward_batch.extend_prefix_lens_cpu
|
||||
)
|
||||
# Only initialize the info once
|
||||
if has_extend_prefix and forward_batch.num_prefix_chunks is None:
|
||||
forward_batch.prepare_chunked_prefix_cache_info(q.device)
|
||||
if hasattr(get_attn_backend(), "init_mha_chunk_metadata"):
|
||||
get_attn_backend().init_mha_chunk_metadata(forward_batch)
|
||||
|
||||
forward_batch.mha_return_lse = has_extend_prefix
|
||||
# Do mha for extended part without prefix
|
||||
forward_batch.set_attn_attend_prefix_cache(False)
|
||||
attn_output = self.attn_mha(q, k, v, forward_batch, save_kv_cache=False)
|
||||
|
||||
# Do mha attention with chunked prefix cache if there are any sequence with prefix
|
||||
if has_extend_prefix:
|
||||
attn_output, lse = attn_output
|
||||
forward_batch.set_attn_attend_prefix_cache(True)
|
||||
attn_output = self._chunked_prefix_attn_mha(
|
||||
q=q,
|
||||
accum_output=attn_output,
|
||||
accum_lse=lse,
|
||||
forward_batch=forward_batch,
|
||||
)
|
||||
|
||||
attn_output = attn_output.reshape(-1, self.num_local_heads * self.v_head_dim)
|
||||
output, _ = self.o_proj(attn_output)
|
||||
return output
|
||||
|
||||
def forward_normal_one_shot_prepare(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
zero_allocator: BumpAllocator,
|
||||
):
|
||||
forward_batch.mha_one_shot = True
|
||||
return self.forward_normal_prepare(
|
||||
positions, hidden_states, forward_batch, zero_allocator
|
||||
)
|
||||
|
||||
def forward_normal_one_shot_core(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
) -> torch.Tensor:
|
||||
has_extend_prefix = any(forward_batch.extend_prefix_lens_cpu)
|
||||
# Only initialize the info once
|
||||
if has_extend_prefix and forward_batch.num_prefix_chunks is None:
|
||||
forward_batch.num_prefix_chunks = 0
|
||||
if hasattr(get_attn_backend(), "init_mha_chunk_metadata"):
|
||||
get_attn_backend().init_mha_chunk_metadata(forward_batch)
|
||||
forward_batch.mha_return_lse = False
|
||||
# Do mha for extended part without prefix
|
||||
forward_batch.set_attn_attend_prefix_cache(False)
|
||||
return self.forward_normal_core(q, k, v, forward_batch)
|
||||
|
||||
def _chunked_prefix_attn_mha(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q: torch.Tensor,
|
||||
accum_output: torch.Tensor,
|
||||
accum_lse: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
) -> torch.Tensor:
|
||||
|
||||
# kv_b_proj needs BF16 input, but legacy q.dtype was BF16 by accident.
|
||||
backend = _resolve_attn_backend(forward_batch)
|
||||
pack_fn = getattr(backend, "pack_prefix_chunk_kv", None)
|
||||
kv_a_dtype = torch.bfloat16 if pack_fn is not None else q.dtype
|
||||
|
||||
assert forward_batch.num_prefix_chunks is not None
|
||||
for i in range(forward_batch.num_prefix_chunks):
|
||||
forward_batch.set_prefix_chunk_idx(i)
|
||||
|
||||
kv_indices = forward_batch.prefix_chunk_kv_indices[i]
|
||||
# Fetch latent cache from memory pool with precomputed chunked kv indices
|
||||
kv_a_normed, k_pe = self._get_mla_kv_buffer(
|
||||
kv_indices, kv_a_dtype, forward_batch
|
||||
)
|
||||
kv_a_normed, k_pe = all_gather_kv_cache_for_mha_chunk_extend(
|
||||
kv_a_normed,
|
||||
k_pe,
|
||||
forward_batch.prefix_chunk_seq_lens_cpu[i],
|
||||
forward_batch.prefix_chunk_starts_cpu[i],
|
||||
)
|
||||
kv = self.kv_b_proj(kv_a_normed)[0]
|
||||
kv = kv.view(
|
||||
-1, self.num_local_heads, self.qk_nope_head_dim + self.v_head_dim
|
||||
)
|
||||
v = kv[..., self.qk_nope_head_dim :]
|
||||
k_nope = kv[..., : self.qk_nope_head_dim]
|
||||
|
||||
if pack_fn is not None:
|
||||
k, v = pack_fn(k_nope, k_pe, v)
|
||||
else:
|
||||
k = torch.empty(
|
||||
(
|
||||
k_nope.shape[0],
|
||||
self.num_local_heads,
|
||||
self.qk_nope_head_dim + self.qk_rope_head_dim,
|
||||
),
|
||||
dtype=v.dtype,
|
||||
device=v.device,
|
||||
)
|
||||
k[..., : self.qk_nope_head_dim] = k_nope
|
||||
k[..., self.qk_nope_head_dim :] = k_pe
|
||||
|
||||
output, lse = self.attn_mha(q, k, v, forward_batch, save_kv_cache=False)
|
||||
tmp_output = torch.empty_like(accum_output)
|
||||
tmp_lse = torch.empty_like(accum_lse)
|
||||
merge_state_v2(output, lse, accum_output, accum_lse, tmp_output, tmp_lse)
|
||||
accum_output, accum_lse = tmp_output, tmp_lse
|
||||
del kv, k, v, output, lse, tmp_output, tmp_lse
|
||||
|
||||
return accum_output
|
||||
|
||||
def _set_mla_kv_buffer(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
latent_cache: torch.Tensor,
|
||||
kv_a: torch.Tensor,
|
||||
k_pe: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
):
|
||||
if _is_cuda or _use_aiter_gfx95:
|
||||
# Save latent cache
|
||||
get_token_to_kv_pool().set_mla_kv_buffer(
|
||||
self.attn_mha, forward_batch.out_cache_loc, kv_a.unsqueeze(1), k_pe
|
||||
)
|
||||
elif _is_npu:
|
||||
# To reduce a time-costing split operation
|
||||
get_token_to_kv_pool().set_kv_buffer(
|
||||
self.attn_mha, forward_batch.out_cache_loc, kv_a.unsqueeze(1), k_pe
|
||||
)
|
||||
else:
|
||||
latent_cache[:, :, : self.kv_lora_rank] = kv_a.unsqueeze(1)
|
||||
latent_cache[:, :, self.kv_lora_rank :] = k_pe.clone()
|
||||
|
||||
# Save latent cache
|
||||
get_token_to_kv_pool().set_kv_buffer(
|
||||
self.attn_mha, forward_batch.out_cache_loc, latent_cache, None
|
||||
)
|
||||
|
||||
def _get_mla_kv_buffer(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
kv_indices: torch.Tensor,
|
||||
dst_dtype: torch.dtype,
|
||||
forward_batch: ForwardBatch,
|
||||
):
|
||||
if _is_cuda or _use_aiter_gfx95:
|
||||
kv_indices = filter_dcp_local_kv_indices(kv_indices=kv_indices)
|
||||
kv_a, k_pe = get_token_to_kv_pool().get_mla_kv_buffer(
|
||||
self.attn_mha, kv_indices, dst_dtype
|
||||
)
|
||||
kv_a = kv_a.squeeze(1)
|
||||
else:
|
||||
latent_cache_buf = get_token_to_kv_pool().get_key_buffer(
|
||||
self.attn_mha.layer_id
|
||||
)
|
||||
latent_cache = latent_cache_buf[kv_indices].contiguous().to(dst_dtype)
|
||||
|
||||
kv_a, k_pe = latent_cache.split(
|
||||
[self.kv_lora_rank, self.qk_rope_head_dim], dim=-1
|
||||
)
|
||||
kv_a = kv_a.squeeze(1).contiguous()
|
||||
return kv_a, k_pe
|
||||
|
||||
def _get_mla_kv_buffer_from_fp8_for_dsa(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
forward_batch: ForwardBatch,
|
||||
):
|
||||
"""
|
||||
Dequantize FP8 KV cache to BF16 for MLA attention (DSA-specific format).
|
||||
|
||||
Returns: (kv_a, k_pe) both in BF16
|
||||
"""
|
||||
backend = get_attn_backend()
|
||||
if isinstance(backend, TboAttnBackend): # if enable tbo, get primary backend
|
||||
backend = backend.primary
|
||||
kv_indices = backend.forward_metadata.page_table_1_flattened
|
||||
assert (
|
||||
kv_indices is not None
|
||||
), "page_table_1_flattened should have been generated for FP8 MHA path"
|
||||
|
||||
kv_cache_fp8 = get_token_to_kv_pool().get_key_buffer(self.attn_mha.layer_id)
|
||||
|
||||
kv_latent_bf16 = dequantize_k_cache_paged(kv_cache_fp8, kv_indices)
|
||||
|
||||
kv_a = kv_latent_bf16[:, :, : self.kv_lora_rank].squeeze(1).contiguous()
|
||||
k_pe = kv_latent_bf16[:, :, self.kv_lora_rank :]
|
||||
|
||||
return kv_a, k_pe
|
||||
|
||||
def _concat_and_cast_mha_k(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
k_nope: torch.Tensor,
|
||||
k_pe: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
):
|
||||
# Temporary for DeepSeek V3/R1 only, but can generalize if needed
|
||||
k_shape = (k_nope.shape[0], self.num_local_heads, self.qk_head_dim)
|
||||
if (
|
||||
(_is_cuda or _is_musa)
|
||||
and (self.num_local_heads == 128)
|
||||
and (self.qk_nope_head_dim == 128)
|
||||
and (self.qk_rope_head_dim == 64)
|
||||
):
|
||||
k = k_nope.new_empty(*k_shape)
|
||||
concat_mla_k(k=k, k_nope=k_nope, k_rope=k_pe)
|
||||
elif (
|
||||
_is_cuda
|
||||
and next_power_of_2(self.num_local_heads) == self.num_local_heads
|
||||
and next_power_of_2(self.qk_nope_head_dim) == self.qk_nope_head_dim
|
||||
and next_power_of_2(self.qk_rope_head_dim) == self.qk_rope_head_dim
|
||||
):
|
||||
# fa3 mha support fp8 inputs
|
||||
if (
|
||||
self.current_attention_backend == "fa3"
|
||||
and self.kv_cache_dtype != "auto"
|
||||
):
|
||||
attn_dtype = get_token_to_kv_pool().dtype
|
||||
else:
|
||||
attn_dtype = k_nope.dtype
|
||||
k = k_nope.new_empty(*k_shape, dtype=attn_dtype)
|
||||
concat_and_cast_mha_k_triton(k, k_nope, k_pe)
|
||||
elif _is_hip and self.current_attention_backend == "aiter":
|
||||
k = k_nope.new_empty(*k_shape)
|
||||
concat_and_cast_mha_k_triton(k, k_nope, k_pe)
|
||||
else:
|
||||
k = k_nope.new_empty(*k_shape)
|
||||
k[..., : self.qk_nope_head_dim] = k_nope
|
||||
k[..., self.qk_nope_head_dim :] = k_pe
|
||||
return k
|
||||
File diff suppressed because it is too large
Load Diff
+153
@@ -0,0 +1,153 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
|
||||
from sglang.srt.layers.amx_utils import PackWeightMethod
|
||||
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
_is_cpu,
|
||||
_is_cpu_amx_available,
|
||||
)
|
||||
from sglang.srt.utils import BumpAllocator, use_intel_amx_backend
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sglang.srt.models.deepseek_v2 import DeepseekV2AttentionMLA
|
||||
|
||||
|
||||
class DeepseekMLACpuForwardMixin:
|
||||
|
||||
def init_mla_fused_rope_cpu_forward(self: DeepseekV2AttentionMLA):
|
||||
assert hasattr(self, "has_fused_proj") and hasattr(self, "is_packed_weight")
|
||||
|
||||
# If we have self.fused_qkv_a_proj_with_mqa and we're running on CPU, we will choose the torch.ops.sgl_kernel.qkv_proj_with_rope_fused_weight kernel
|
||||
# which requires self.w_kc and self.w_vc to be packed.
|
||||
# If not, we will use torch.bmm and weight shouldn't be packed in this case
|
||||
if self.has_fused_proj and _is_cpu and _is_cpu_amx_available:
|
||||
self.quant_method = PackWeightMethod(
|
||||
weight_names=["w_kc", "w_vc"], transpose_dims=[[1, 2], [1, 2]]
|
||||
)
|
||||
|
||||
self.qkv_proj_with_rope_is_int8 = (
|
||||
self.has_fused_proj
|
||||
and not self.is_packed_weight
|
||||
and self.fused_qkv_a_proj_with_mqa.weight.dtype == torch.int8
|
||||
)
|
||||
self.qkv_proj_with_rope_is_fp8 = (
|
||||
self.has_fused_proj
|
||||
and not self.is_packed_weight
|
||||
and self.fused_qkv_a_proj_with_mqa.weight.dtype == torch.float8_e4m3fn
|
||||
)
|
||||
|
||||
self.weight_block_size = None
|
||||
if self.qkv_proj_with_rope_is_fp8 and _is_cpu and _is_cpu_amx_available:
|
||||
assert getattr(
|
||||
self.fused_qkv_a_proj_with_mqa.quant_method, "block_quant", False
|
||||
) == getattr(self.q_b_proj.quant_method, "block_quant", False)
|
||||
use_block_quant = getattr(
|
||||
self.fused_qkv_a_proj_with_mqa.quant_method, "block_quant", False
|
||||
)
|
||||
|
||||
if use_block_quant:
|
||||
assert (
|
||||
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
|
||||
== self.q_b_proj.quant_method.quant_config.weight_block_size
|
||||
)
|
||||
self.weight_block_size = (
|
||||
self.fused_qkv_a_proj_with_mqa.quant_method.quant_config.weight_block_size
|
||||
)
|
||||
|
||||
def forward_absorb_fused_mla_rope_cpu_prepare(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
zero_allocator: BumpAllocator,
|
||||
):
|
||||
assert self.q_lora_rank is not None and use_intel_amx_backend(
|
||||
self
|
||||
), "forward_absorb_fused_mla_rope_cpu_prepare requires q_lora_rank is not None and use_intel_amx_backend"
|
||||
|
||||
q_input, k_input, v_input = (
|
||||
torch.ops.sgl_kernel.qkv_proj_with_rope_fused_weight(
|
||||
hidden_states,
|
||||
self.fused_qkv_a_proj_with_mqa.weight,
|
||||
self.q_b_proj.weight,
|
||||
self.w_kc,
|
||||
self.q_a_layernorm.weight,
|
||||
self.kv_a_layernorm.weight,
|
||||
positions,
|
||||
self.rotary_emb.cos_sin_cache,
|
||||
self.kv_a_layernorm.variance_epsilon,
|
||||
self.qkv_proj_with_rope_is_int8,
|
||||
self.qkv_proj_with_rope_is_fp8,
|
||||
(
|
||||
self.fused_qkv_a_proj_with_mqa.weight_scale
|
||||
if self.qkv_proj_with_rope_is_int8
|
||||
else (
|
||||
self.fused_qkv_a_proj_with_mqa.weight_scale_inv
|
||||
if self.qkv_proj_with_rope_is_fp8
|
||||
else None
|
||||
)
|
||||
),
|
||||
(
|
||||
self.q_b_proj.weight_scale
|
||||
if self.qkv_proj_with_rope_is_int8
|
||||
else (
|
||||
self.q_b_proj.weight_scale_inv
|
||||
if self.qkv_proj_with_rope_is_fp8
|
||||
else None
|
||||
)
|
||||
),
|
||||
self.w_scale if self.qkv_proj_with_rope_is_fp8 else None,
|
||||
True, # is_vnni
|
||||
self.weight_block_size,
|
||||
self.q_lora_rank,
|
||||
self.kv_lora_rank,
|
||||
self.qk_rope_head_dim,
|
||||
)
|
||||
)
|
||||
return (q_input, k_input, v_input, forward_batch, zero_allocator)
|
||||
|
||||
def forward_absorb_fused_mla_rope_cpu_core(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q_input,
|
||||
k_input,
|
||||
v_input,
|
||||
forward_batch,
|
||||
zero_allocator,
|
||||
):
|
||||
assert self.q_lora_rank is not None and use_intel_amx_backend(
|
||||
self
|
||||
), "forward_absorb_fused_mla_rope_cpu_core requires q_lora_rank is not None and use_intel_amx_backend"
|
||||
|
||||
attn_output = self.attn_mqa(q_input, k_input, v_input, forward_batch)
|
||||
attn_output = attn_output.view(-1, self.num_local_heads, self.kv_lora_rank)
|
||||
|
||||
# [Note] Align shapes of bmm inputs.
|
||||
# Shapes of inputs:
|
||||
# q_nope: [M, B, K]
|
||||
# original self.w_kc: [B, K, N]
|
||||
# current self.w_kc (which has been converted in PackWeightMethod): [B, N, K]
|
||||
|
||||
# Shapes of inputs to sgl_kernel.cpu.bmm:
|
||||
# out: [B, M, N]
|
||||
# mat1: [B, M, K]
|
||||
# mat2: [B, N, K]
|
||||
B = self.w_vc.size(0)
|
||||
N = self.w_vc.size(1)
|
||||
M = attn_output.size(0)
|
||||
output = torch.empty([M, int(B * N)], dtype=attn_output.dtype)
|
||||
attn_bmm_output = output.view([M, B, N]).transpose_(0, 1)
|
||||
torch.ops.sgl_kernel.bmm_cpu(
|
||||
attn_bmm_output,
|
||||
attn_output.transpose(0, 1),
|
||||
self.w_vc,
|
||||
True, # is_vnni
|
||||
self.w_scale if self.qkv_proj_with_rope_is_fp8 else None, # scale
|
||||
)
|
||||
attn_output = output
|
||||
output, _ = self.o_proj(attn_output)
|
||||
|
||||
return output
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import torch
|
||||
|
||||
from sglang.srt.layers.quantization.fp8_kernel import per_tensor_quant_mla_fp8
|
||||
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
|
||||
from sglang.srt.model_executor.forward_context import (
|
||||
get_attn_backend,
|
||||
get_token_to_kv_pool,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
_is_cuda,
|
||||
_is_hip,
|
||||
)
|
||||
from sglang.srt.utils import BumpAllocator, get_bool_env_var
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sglang.srt.models.deepseek_v2 import DeepseekV2AttentionMLA
|
||||
|
||||
if _is_cuda:
|
||||
from sgl_kernel import bmm_fp8
|
||||
|
||||
if _is_hip:
|
||||
from sglang.kernels.ops.attention.rocm_mla_decode_rope import (
|
||||
decode_attention_fwd_grouped_rope,
|
||||
)
|
||||
|
||||
|
||||
class DeepseekMLARocmForwardMixin:
|
||||
|
||||
def init_mla_fused_rope_rocm_forward(self: DeepseekV2AttentionMLA):
|
||||
self.rocm_fused_decode_mla = get_bool_env_var(
|
||||
"SGLANG_ROCM_FUSED_DECODE_MLA", "false"
|
||||
)
|
||||
|
||||
def forward_absorb_fused_mla_rope_prepare(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
positions: torch.Tensor,
|
||||
hidden_states: torch.Tensor,
|
||||
forward_batch: ForwardBatch,
|
||||
zero_allocator: BumpAllocator,
|
||||
):
|
||||
enable_rope_fusion = (
|
||||
os.getenv("SGLANG_FUSED_MLA_ENABLE_ROPE_FUSION", "1") == "1"
|
||||
)
|
||||
# NOTE: hidden_states can be a tuple for some quantization paths.
|
||||
# For shape/device/dtype, use the first tensor; still pass the original
|
||||
# hidden_states through linear ops which may accept tuple inputs.
|
||||
hidden_states_tensor = (
|
||||
hidden_states[0] if isinstance(hidden_states, tuple) else hidden_states
|
||||
)
|
||||
|
||||
q_len = hidden_states_tensor.shape[0]
|
||||
q_input = hidden_states_tensor.new_empty(
|
||||
q_len, self.num_local_heads, self.kv_lora_rank + self.qk_rope_head_dim
|
||||
)
|
||||
if self.q_lora_rank is not None:
|
||||
q, latent_cache = self.fused_qkv_a_proj_with_mqa(hidden_states)[0].split(
|
||||
[self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim], dim=-1
|
||||
)
|
||||
q = self.q_a_layernorm(q)
|
||||
q = self.q_b_proj(q)[0].view(-1, self.num_local_heads, self.qk_head_dim)
|
||||
else:
|
||||
q = self.q_proj(hidden_states)[0].view(
|
||||
-1, self.num_local_heads, self.qk_head_dim
|
||||
)
|
||||
latent_cache = self.kv_a_proj_with_mqa(hidden_states)[0]
|
||||
q_nope, q_pe = q.split([self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1)
|
||||
|
||||
if _is_hip:
|
||||
# TODO(haishaw): add bmm_fp8 to ROCm
|
||||
q_nope_out = torch.bmm(
|
||||
q_nope.to(torch.bfloat16).transpose(0, 1),
|
||||
self.w_kc.to(torch.bfloat16) * self.w_scale,
|
||||
)
|
||||
elif self.w_kc.dtype == torch.float8_e4m3fn:
|
||||
q_nope_val, q_nope_scale = per_tensor_quant_mla_fp8(
|
||||
q_nope.transpose(0, 1),
|
||||
zero_allocator.allocate(1),
|
||||
dtype=torch.float8_e4m3fn,
|
||||
)
|
||||
q_nope_out = bmm_fp8(
|
||||
q_nope_val, self.w_kc, q_nope_scale, self.w_scale, torch.bfloat16
|
||||
)
|
||||
else:
|
||||
q_nope_out = torch.bmm(q_nope.transpose(0, 1), self.w_kc)
|
||||
q_input[..., : self.kv_lora_rank] = q_nope_out.transpose(0, 1)
|
||||
v_input = latent_cache[..., : self.kv_lora_rank]
|
||||
v_input = self.kv_a_layernorm(v_input.contiguous()).unsqueeze(1)
|
||||
k_input = latent_cache.unsqueeze(1)
|
||||
k_input[..., : self.kv_lora_rank] = v_input
|
||||
|
||||
if not enable_rope_fusion:
|
||||
k_pe = k_input[..., self.kv_lora_rank :]
|
||||
q_pe, k_pe = self.rotary_emb(positions, q_pe, k_pe)
|
||||
q_input[..., self.kv_lora_rank :] = q_pe
|
||||
k_input[..., self.kv_lora_rank :] = k_pe
|
||||
k_pe_output = None
|
||||
else:
|
||||
k_pe_output = torch.empty_like(k_input[..., self.kv_lora_rank :])
|
||||
|
||||
q_input[..., self.kv_lora_rank :] = q_pe
|
||||
|
||||
# attn_output = self.attn_mqa(q_input, k_input, v_input, forward_batch)
|
||||
# Use Fused ROPE with use_rope=OFF.
|
||||
attn_output = torch.empty(
|
||||
(q_len, self.num_local_heads, self.kv_lora_rank),
|
||||
dtype=q.dtype,
|
||||
device=q.device,
|
||||
)
|
||||
attn_logits, _, kv_indptr, kv_indices, _, _, _ = (
|
||||
get_attn_backend().forward_metadata
|
||||
)
|
||||
cos_sin_cache = self.rotary_emb.cos_sin_cache
|
||||
num_kv_split = get_attn_backend().num_kv_splits
|
||||
sm_scale = self.attn_mqa.scaling
|
||||
if attn_logits is None:
|
||||
attn_logits = torch.empty(
|
||||
(
|
||||
forward_batch.batch_size,
|
||||
self.num_local_heads,
|
||||
num_kv_split,
|
||||
self.kv_lora_rank + 1,
|
||||
),
|
||||
dtype=torch.float32,
|
||||
device=q.device,
|
||||
)
|
||||
|
||||
# save current latent cache.
|
||||
get_token_to_kv_pool().set_kv_buffer(
|
||||
self.attn_mqa, forward_batch.out_cache_loc, k_input, None
|
||||
)
|
||||
key_cache_buf = get_token_to_kv_pool().get_key_buffer(self.attn_mqa.layer_id)
|
||||
val_cache_buf = key_cache_buf[..., : self.kv_lora_rank]
|
||||
|
||||
return (
|
||||
q_input,
|
||||
key_cache_buf,
|
||||
val_cache_buf,
|
||||
attn_output,
|
||||
kv_indptr,
|
||||
kv_indices,
|
||||
k_pe_output,
|
||||
cos_sin_cache,
|
||||
positions,
|
||||
attn_logits,
|
||||
num_kv_split,
|
||||
sm_scale,
|
||||
enable_rope_fusion,
|
||||
k_input,
|
||||
forward_batch,
|
||||
zero_allocator,
|
||||
)
|
||||
|
||||
def forward_absorb_fused_mla_rope_core(
|
||||
self: DeepseekV2AttentionMLA,
|
||||
q_input,
|
||||
key_cache_buf,
|
||||
val_cache_buf,
|
||||
attn_output,
|
||||
kv_indptr,
|
||||
kv_indices,
|
||||
k_pe_output,
|
||||
cos_sin_cache,
|
||||
positions,
|
||||
attn_logits,
|
||||
num_kv_split,
|
||||
sm_scale,
|
||||
enable_rope_fusion,
|
||||
k_input,
|
||||
forward_batch,
|
||||
zero_allocator,
|
||||
):
|
||||
decode_attention_fwd_grouped_rope(
|
||||
q_input,
|
||||
key_cache_buf,
|
||||
val_cache_buf,
|
||||
attn_output,
|
||||
kv_indptr,
|
||||
kv_indices,
|
||||
k_pe_output,
|
||||
self.kv_lora_rank,
|
||||
self.rotary_emb.rotary_dim,
|
||||
cos_sin_cache,
|
||||
positions,
|
||||
attn_logits,
|
||||
num_kv_split,
|
||||
sm_scale,
|
||||
logit_cap=self.attn_mqa.logit_cap,
|
||||
use_rope=enable_rope_fusion,
|
||||
is_neox_style=self.rotary_emb.is_neox_style,
|
||||
)
|
||||
|
||||
if enable_rope_fusion:
|
||||
k_input[..., self.kv_lora_rank :] = k_pe_output
|
||||
get_token_to_kv_pool().set_kv_buffer(
|
||||
self.attn_mqa, forward_batch.out_cache_loc, k_input, None
|
||||
)
|
||||
|
||||
attn_output = attn_output.view(-1, self.num_local_heads, self.kv_lora_rank)
|
||||
|
||||
if _is_hip:
|
||||
# TODO(haishaw): add bmm_fp8 to ROCm
|
||||
attn_bmm_output = torch.bmm(
|
||||
attn_output.to(torch.bfloat16).transpose(0, 1),
|
||||
self.w_vc.to(torch.bfloat16) * self.w_scale,
|
||||
)
|
||||
elif self.w_vc.dtype == torch.float8_e4m3fn:
|
||||
attn_output_val, attn_output_scale = per_tensor_quant_mla_fp8(
|
||||
attn_output.transpose(0, 1),
|
||||
zero_allocator.allocate(1),
|
||||
dtype=torch.float8_e4m3fn,
|
||||
)
|
||||
attn_bmm_output = bmm_fp8(
|
||||
attn_output_val,
|
||||
self.w_vc,
|
||||
attn_output_scale,
|
||||
self.w_scale,
|
||||
torch.bfloat16,
|
||||
)
|
||||
else:
|
||||
attn_bmm_output = torch.bmm(attn_output.transpose(0, 1), self.w_vc)
|
||||
attn_output = attn_bmm_output.transpose(0, 1).flatten(1, 2)
|
||||
output, _ = self.o_proj(attn_output)
|
||||
|
||||
return output
|
||||
@@ -0,0 +1,798 @@
|
||||
# Copyright 2026 SGLang Team
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
|
||||
import concurrent.futures
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, Iterable, List, Optional, Tuple
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import tqdm
|
||||
from transformers import PretrainedConfig
|
||||
|
||||
from sglang.srt.distributed.parallel_state import GroupCoordinator
|
||||
from sglang.srt.environ import envs
|
||||
from sglang.srt.layers import deep_gemm_wrapper
|
||||
from sglang.srt.layers.moe.fused_moe_triton.layer import FusedMoE
|
||||
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
||||
from sglang.srt.layers.quantization.fp8_utils import (
|
||||
block_quant_dequant,
|
||||
block_quant_to_tensor_quant,
|
||||
channel_quant_to_tensor_quant,
|
||||
inverse_transform_scale_ue8m0,
|
||||
normalize_e4m3fn_to_e4m3fnuz,
|
||||
quant_weight_ue8m0,
|
||||
)
|
||||
from sglang.srt.layers.quantization.int8_utils import (
|
||||
block_dequant as int8_block_dequant,
|
||||
)
|
||||
from sglang.srt.layers.utils import get_layer_id
|
||||
from sglang.srt.model_loader.utils import (
|
||||
maybe_executor_submit,
|
||||
should_async_load,
|
||||
should_deepgemm_weight_requant_ue8m0,
|
||||
)
|
||||
from sglang.srt.model_loader.weight_utils import (
|
||||
RUNAI_STREAMER_TENSOR_ATTR,
|
||||
default_weight_loader,
|
||||
)
|
||||
from sglang.srt.models.deepseek_common.utils import (
|
||||
_is_cuda,
|
||||
_is_fp8_fnuz,
|
||||
_is_hip,
|
||||
_is_musa,
|
||||
_is_npu,
|
||||
_is_xpu,
|
||||
_use_aiter_gfx95,
|
||||
awq_dequantize_func,
|
||||
enable_nextn_moe_bf16_cast_to_fp8,
|
||||
)
|
||||
from sglang.srt.utils import bind_or_assign, get_bool_env_var, log_info_on_rank0
|
||||
|
||||
if _use_aiter_gfx95:
|
||||
from sglang.srt.layers.quantization.quark.utils import quark_post_load_weights
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Optional quantization for DeepSeek nvfp4 checkpoint
|
||||
NVFP4_CKPT_FP8_ATTN_QUANT_MODULES = ["q_b_proj"]
|
||||
|
||||
|
||||
def _clone_if_runai_streamed_tensor(tensor: torch.Tensor) -> torch.Tensor:
|
||||
if getattr(tensor, RUNAI_STREAMER_TENSOR_ATTR, False):
|
||||
return tensor.clone().detach()
|
||||
return tensor
|
||||
|
||||
|
||||
def _load_fused_indexer_wk(
|
||||
name: str,
|
||||
loaded_weight: torch.Tensor,
|
||||
params_dict: Dict[str, torch.Tensor],
|
||||
pending: Dict[str, Dict[str, torch.Tensor]],
|
||||
quant_config: Optional[QuantizationConfig],
|
||||
) -> bool:
|
||||
"""Load an indexer wk / weights_proj shard into the fused bf16 wk_weights_proj
|
||||
param: wk fills the top head_dim rows (dequantized from block-fp8 if needed),
|
||||
weights_proj the bottom n_heads rows.
|
||||
|
||||
Returns False when there is no fused param (non-CUDA, or CUDA with
|
||||
SGLANG_DISABLE_DSA_INDEXER_FUSION set, where wk and weights_proj are
|
||||
separate) so the caller falls through to per-tensor loading.
|
||||
"""
|
||||
fused_name = name.rsplit(".indexer.", 1)[0] + ".indexer.wk_weights_proj.weight"
|
||||
fused_param = params_dict.get(fused_name)
|
||||
if fused_param is None or fused_param.dtype != torch.bfloat16:
|
||||
return False
|
||||
|
||||
if ".indexer.weights_proj." in name:
|
||||
w = _clone_if_runai_streamed_tensor(loaded_weight)
|
||||
fused_param.data[-w.shape[0] :].copy_(w)
|
||||
return True
|
||||
|
||||
# wk: a bf16 checkpoint copies straight in; block-fp8 needs weight + scale.
|
||||
is_scale = name.endswith(".weight_scale_inv")
|
||||
if not is_scale and loaded_weight.dtype != torch.float8_e4m3fn:
|
||||
w = _clone_if_runai_streamed_tensor(loaded_weight)
|
||||
fused_param.data[: w.shape[0]].copy_(w)
|
||||
return True
|
||||
|
||||
entry = pending.setdefault(fused_name, {})
|
||||
entry["scale" if is_scale else "weight"] = _clone_if_runai_streamed_tensor(
|
||||
loaded_weight
|
||||
)
|
||||
if "weight" in entry and "scale" in entry:
|
||||
pending.pop(fused_name)
|
||||
block_size = getattr(quant_config, "weight_block_size", None) or [128, 128]
|
||||
wk_bf16 = block_quant_dequant(
|
||||
entry["weight"], entry["scale"], block_size, torch.bfloat16
|
||||
)
|
||||
fused_param.data[: wk_bf16.shape[0]].copy_(wk_bf16)
|
||||
return True
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NextNEnabledConfig:
|
||||
num_nextn_layers: int
|
||||
nextn_layer_id: int
|
||||
nextn_layer_prefix: str
|
||||
nextn_spec_weight_names: List[str]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NextNDisabledConfig:
|
||||
pass
|
||||
|
||||
|
||||
"""Union type for NextN configuration, including enabled and disabled configurations."""
|
||||
NextNConfig = NextNEnabledConfig | NextNDisabledConfig
|
||||
|
||||
|
||||
class DeepseekV2WeightLoaderMixin:
|
||||
"""Mixin for loading weights in DeepSeek V2/V3 models."""
|
||||
|
||||
model: nn.Module
|
||||
config: PretrainedConfig
|
||||
quant_config: Optional[QuantizationConfig]
|
||||
pp_group: GroupCoordinator
|
||||
num_fused_shared_experts: int
|
||||
|
||||
def do_load_weights(
|
||||
self,
|
||||
weights: Iterable[Tuple[str, torch.Tensor]],
|
||||
is_nextn: bool = False,
|
||||
):
|
||||
"""Load model weights from checkpoint.
|
||||
|
||||
Args:
|
||||
weights: Iterable of (weight_name, weight_tensor) pairs
|
||||
is_nextn: Whether loading NextN speculative decoding weights
|
||||
"""
|
||||
nextn_conf = self._initialize_nextn_conf(is_nextn)
|
||||
|
||||
weights = self._maybe_quant_weights_to_fp8_ue8m0(
|
||||
weights, NVFP4_CKPT_FP8_ATTN_QUANT_MODULES, nextn_conf
|
||||
)
|
||||
|
||||
stacked_params_mapping = [
|
||||
# (param_name, shard_name, shard_id)
|
||||
("gate_up_proj", "gate_proj", 0),
|
||||
("gate_up_proj", "up_proj", 1),
|
||||
]
|
||||
|
||||
# Params for weights, fp8 weight scales, fp8 activation scales
|
||||
# (param_name, weight_name, expert_id, shard_id)
|
||||
expert_params_mapping = FusedMoE.make_expert_params_mapping(
|
||||
ckpt_gate_proj_name="gate_proj",
|
||||
ckpt_down_proj_name="down_proj",
|
||||
ckpt_up_proj_name="up_proj",
|
||||
num_experts=self.config.n_routed_experts + self.num_fused_shared_experts,
|
||||
)
|
||||
# Params for special naming rules in mixed-precision models, for example:
|
||||
# model.layers.xx.mlp.experts.xx.w1.input_scale. For details,
|
||||
# see https://huggingface.co/Barrrrry/DeepSeek-R1-W4AFP8/blob/main.
|
||||
if self.quant_config and self.quant_config.get_name() == "w4afp8":
|
||||
expert_params_mapping += FusedMoE.make_expert_input_scale_params_mapping(
|
||||
num_experts=self.config.n_routed_experts
|
||||
)
|
||||
|
||||
# Fuse q_a_proj and kv_a_proj_with_mqa along output dimension when q_lora_rank is not None
|
||||
fuse_qkv_a_proj = hasattr(self.config, "q_lora_rank") and (
|
||||
self.config.q_lora_rank is not None
|
||||
)
|
||||
cached_a_proj = {} if fuse_qkv_a_proj else None
|
||||
|
||||
pending_indexer_wk: Dict[str, Dict[str, torch.Tensor]] = {}
|
||||
|
||||
if self.num_fused_shared_experts > 0:
|
||||
assert self.num_fused_shared_experts == 1
|
||||
log_info_on_rank0(logger, "Shared experts fusion optimization enabled.")
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
futures = []
|
||||
params_dict = dict(self.named_parameters())
|
||||
weight_names = []
|
||||
for name, loaded_weight in weights:
|
||||
use_async_loading = should_async_load(loaded_weight)
|
||||
layer_id = get_layer_id(name)
|
||||
if (
|
||||
layer_id is not None
|
||||
and hasattr(self.model, "start_layer")
|
||||
and (
|
||||
layer_id < self.model.start_layer
|
||||
or layer_id >= self.model.end_layer
|
||||
)
|
||||
):
|
||||
continue
|
||||
if self.num_fused_shared_experts > 0 and "mlp.shared_experts" in name:
|
||||
name = name.replace(
|
||||
"mlp.shared_experts",
|
||||
f"mlp.experts.{self.config.n_routed_experts}",
|
||||
)
|
||||
|
||||
weight_names.append(name)
|
||||
|
||||
match nextn_conf:
|
||||
case NextNEnabledConfig(
|
||||
nextn_layer_prefix=layer_prefix,
|
||||
nextn_spec_weight_names=spec_weight_names,
|
||||
):
|
||||
if not name.startswith(layer_prefix):
|
||||
continue
|
||||
|
||||
# Use shared head and embed weights from target model
|
||||
if "shared_head.head" in name or "embed_tokens" in name:
|
||||
continue
|
||||
|
||||
# Transform name: NextN-specific → "model.*", decoder → "model.decoder.*"
|
||||
if any(s in name for s in spec_weight_names):
|
||||
name = name.replace(layer_prefix, "model")
|
||||
else:
|
||||
name = name.replace(layer_prefix, "model.decoder")
|
||||
case NextNDisabledConfig():
|
||||
if hasattr(self.config, "num_nextn_predict_layers"):
|
||||
num_nextn_layers = self.config.num_nextn_predict_layers
|
||||
if num_nextn_layers > 0 and name.startswith("model.layers"):
|
||||
name_list = name.split(".")
|
||||
if (
|
||||
len(name_list) >= 3
|
||||
and int(name_list[2])
|
||||
>= self.config.num_hidden_layers
|
||||
):
|
||||
continue
|
||||
|
||||
if "rotary_emb.inv_freq" in name:
|
||||
continue
|
||||
|
||||
# CUDA fuses wk + weights_proj into one bf16 wk_weights_proj; the
|
||||
# helper returns True once it has consumed the shard.
|
||||
if (
|
||||
".indexer.wk." in name or ".indexer.weights_proj." in name
|
||||
) and _load_fused_indexer_wk(
|
||||
name,
|
||||
loaded_weight,
|
||||
params_dict,
|
||||
pending_indexer_wk,
|
||||
self.quant_config,
|
||||
):
|
||||
continue
|
||||
|
||||
for param_name, weight_name, shard_id in stacked_params_mapping:
|
||||
# Skip non-stacked layers and experts (experts handled below).
|
||||
if weight_name not in name:
|
||||
continue
|
||||
if _is_npu:
|
||||
name = name.replace("weight_packed", "weight")
|
||||
# We have mlp.experts[0].gate_proj in the checkpoint.
|
||||
# Since we handle the experts below in expert_params_mapping,
|
||||
# we need to skip here BEFORE we update the name, otherwise
|
||||
# name will be updated to mlp.experts[0].gate_up_proj, which
|
||||
# will then be updated below in expert_params_mapping
|
||||
# for mlp.experts[0].gate_gate_up_proj, which breaks load.
|
||||
if ("mlp.experts." in name) and name not in params_dict:
|
||||
continue
|
||||
name = name.replace(weight_name, param_name)
|
||||
# Skip loading extra bias for GPTQ models.
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
continue
|
||||
param = params_dict[name]
|
||||
weight_loader = param.weight_loader
|
||||
maybe_executor_submit(
|
||||
executor=executor,
|
||||
futures=futures,
|
||||
use_async=use_async_loading,
|
||||
func=weight_loader,
|
||||
func_args=(param, loaded_weight, shard_id),
|
||||
)
|
||||
break
|
||||
else:
|
||||
for mapping in expert_params_mapping:
|
||||
param_name, weight_name, expert_id, shard_id = mapping
|
||||
if weight_name not in name:
|
||||
continue
|
||||
if _is_npu:
|
||||
name = name.replace("weight_packed", "weight")
|
||||
name = name.replace(weight_name, param_name)
|
||||
if name not in params_dict:
|
||||
continue
|
||||
param = params_dict[name]
|
||||
weight_loader = param.weight_loader
|
||||
maybe_executor_submit(
|
||||
executor=executor,
|
||||
futures=futures,
|
||||
use_async=use_async_loading,
|
||||
func=weight_loader,
|
||||
func_args=(
|
||||
param,
|
||||
loaded_weight,
|
||||
name,
|
||||
),
|
||||
func_kwargs={
|
||||
"shard_id": shard_id,
|
||||
"expert_id": expert_id,
|
||||
},
|
||||
)
|
||||
break
|
||||
else:
|
||||
# Skip loading extra bias for GPTQ models.
|
||||
if name.endswith(".bias") and name not in params_dict:
|
||||
continue
|
||||
# Skip loading embed_tokens if not first rank in pipeline parallelism
|
||||
if ".embed_tokens." in name and not self.pp_group.is_first_rank:
|
||||
continue
|
||||
# Skip loading norm if not last rank in pipeline parallelism
|
||||
if ".norm." in name and not self.pp_group.is_last_rank:
|
||||
continue
|
||||
if fuse_qkv_a_proj and (
|
||||
"q_a_proj" in name or "kv_a_proj_with_mqa" in name
|
||||
):
|
||||
cached_a_proj[name] = _clone_if_runai_streamed_tensor(
|
||||
loaded_weight
|
||||
)
|
||||
q_a_proj_name = (
|
||||
name
|
||||
if "q_a_proj" in name
|
||||
else name.replace("kv_a_proj_with_mqa", "q_a_proj")
|
||||
)
|
||||
kv_a_proj_name = (
|
||||
name
|
||||
if "kv_a_proj_with_mqa" in name
|
||||
else name.replace("q_a_proj", "kv_a_proj_with_mqa")
|
||||
)
|
||||
|
||||
# When both q_a_proj and kv_a_proj_with_mqa has been cached, load the fused weight to parameter
|
||||
if (
|
||||
q_a_proj_name in cached_a_proj
|
||||
and kv_a_proj_name in cached_a_proj
|
||||
):
|
||||
q_a_proj_weight = cached_a_proj[q_a_proj_name]
|
||||
kv_a_proj_weight = cached_a_proj[kv_a_proj_name]
|
||||
|
||||
if q_a_proj_weight.shape == torch.Size(
|
||||
[]
|
||||
) and kv_a_proj_weight.shape == torch.Size([]):
|
||||
fused_weight = q_a_proj_weight
|
||||
else:
|
||||
cat_dim = 0
|
||||
if self.quant_config is not None and (
|
||||
self.quant_config.get_name() == "awq"
|
||||
or self.quant_config.get_name() == "awq_marlin"
|
||||
or self.quant_config.get_name() == "moe_wna16"
|
||||
):
|
||||
cat_dim = 1
|
||||
|
||||
fused_weight = torch.cat(
|
||||
[q_a_proj_weight, kv_a_proj_weight], dim=cat_dim
|
||||
)
|
||||
|
||||
param_name = (
|
||||
name.replace(
|
||||
"q_a_proj", "fused_qkv_a_proj_with_mqa"
|
||||
)
|
||||
if "q_a_proj" in name
|
||||
else name.replace(
|
||||
"kv_a_proj_with_mqa",
|
||||
"fused_qkv_a_proj_with_mqa",
|
||||
)
|
||||
)
|
||||
param = params_dict[param_name]
|
||||
|
||||
weight_loader = getattr(
|
||||
param, "weight_loader", default_weight_loader
|
||||
)
|
||||
maybe_executor_submit(
|
||||
executor=executor,
|
||||
futures=futures,
|
||||
use_async=use_async_loading,
|
||||
func=weight_loader,
|
||||
func_args=(param, fused_weight),
|
||||
)
|
||||
cached_a_proj.pop(q_a_proj_name)
|
||||
cached_a_proj.pop(kv_a_proj_name)
|
||||
else:
|
||||
if (
|
||||
"k_scale" in name or "v_scale" in name
|
||||
) and name not in params_dict:
|
||||
# modelopt attn kv scale is named differently
|
||||
for scale in ["k_scale", "v_scale"]:
|
||||
if scale in name:
|
||||
name = name.replace(
|
||||
f"{scale[0]}_proj", "attn_mqa"
|
||||
)
|
||||
break
|
||||
if name not in params_dict:
|
||||
# modelopt ckpt contains not needed weights for MTP module:
|
||||
# model.decoder.self_attn.attn_mqa.v_scale and
|
||||
# model.decoder.self_attn.attn_mqa.k_scale
|
||||
logger.warning(f"{name} not found in params_dict.")
|
||||
continue
|
||||
param = params_dict[name]
|
||||
weight_loader = getattr(
|
||||
param, "weight_loader", default_weight_loader
|
||||
)
|
||||
maybe_executor_submit(
|
||||
executor=executor,
|
||||
futures=futures,
|
||||
use_async=use_async_loading,
|
||||
func=weight_loader,
|
||||
func_args=(param, loaded_weight),
|
||||
)
|
||||
|
||||
# Wait for all tasks to complete and raise any exceptions.
|
||||
for future in concurrent.futures.as_completed(futures):
|
||||
future.result()
|
||||
|
||||
self.post_load_weights(is_nextn=is_nextn, weight_names=weight_names)
|
||||
|
||||
def _initialize_nextn_conf(self, is_nextn: bool) -> NextNConfig:
|
||||
"""
|
||||
Initialize the nextn configuration.
|
||||
|
||||
Raises:
|
||||
ValueError: If num_nextn_predict_layers is not in the config.
|
||||
AssertionError: If num_nextn_predict_layers is not equal to 1.
|
||||
"""
|
||||
if not is_nextn:
|
||||
return NextNDisabledConfig()
|
||||
|
||||
if not hasattr(self.config, "num_nextn_predict_layers"):
|
||||
raise ValueError("num_nextn_predict_layers is not in the config")
|
||||
|
||||
num_nextn_layers = self.config.num_nextn_predict_layers
|
||||
assert num_nextn_layers == 1, "Only 1 nextn layer is supported"
|
||||
|
||||
# compatible with old design
|
||||
nextn_layer_id = (
|
||||
0 if self.config.num_hidden_layers == 1 else self.config.num_hidden_layers
|
||||
)
|
||||
|
||||
return NextNEnabledConfig(
|
||||
num_nextn_layers=num_nextn_layers,
|
||||
nextn_layer_id=nextn_layer_id,
|
||||
nextn_layer_prefix=f"model.layers.{nextn_layer_id}",
|
||||
nextn_spec_weight_names=[
|
||||
"shared_head.norm",
|
||||
"eh_proj",
|
||||
"enorm",
|
||||
"hnorm",
|
||||
],
|
||||
)
|
||||
|
||||
def post_load_weights(
|
||||
self,
|
||||
is_nextn: bool = False,
|
||||
weight_names: Optional[Iterable[str]] = None,
|
||||
) -> None:
|
||||
"""Post-process weights after loading.
|
||||
|
||||
Handles kv_b_proj weight processing including:
|
||||
- AWQ dequantization
|
||||
- FP8/INT8 requantization and block-wise to tensor-wise conversion
|
||||
- Splitting weights into w_kc and w_vc components for MLA
|
||||
|
||||
Args:
|
||||
is_nextn: Whether processing NextN weights
|
||||
weight_names: Optional list of loaded weight names to determine which layers to process
|
||||
"""
|
||||
if is_nextn:
|
||||
layer_ids = [self.config.num_hidden_layers]
|
||||
else:
|
||||
if weight_names is None:
|
||||
layer_ids = range(self.model.start_layer, self.model.end_layer)
|
||||
else:
|
||||
layer_ids = set()
|
||||
for name in weight_names:
|
||||
if "kv_b_proj" in name:
|
||||
layer_id = int(name.split(".")[2])
|
||||
if layer_id < self.config.num_hidden_layers:
|
||||
layer_ids.add(layer_id)
|
||||
|
||||
for layer_id in layer_ids:
|
||||
self_attn = (
|
||||
self.model.layers[layer_id].self_attn
|
||||
if not is_nextn
|
||||
else self.model.decoder.self_attn
|
||||
)
|
||||
|
||||
if hasattr(self_attn.kv_b_proj, "qweight"):
|
||||
# awq compatible, dequantize the weight if supported
|
||||
awq_dequantize_f = awq_dequantize_func()
|
||||
if awq_dequantize_f is not None:
|
||||
w = awq_dequantize_f(
|
||||
self_attn.kv_b_proj.qweight,
|
||||
self_attn.kv_b_proj.scales,
|
||||
self_attn.kv_b_proj.qzeros,
|
||||
).T
|
||||
else:
|
||||
raise ValueError(
|
||||
"AWQ dequantize function is not supported for the current device"
|
||||
)
|
||||
else:
|
||||
w = self_attn.kv_b_proj.weight
|
||||
|
||||
# NOTE(HandH1998): Since `bmm_fp8` only supports per-tensor scale, we have to requantize `self_attn.kv_b_proj`.
|
||||
# This may affect the accuracy of fp8 model.
|
||||
# Fix deepseek v3 blockwise bmm by using deep_gemm
|
||||
use_deep_gemm_bmm = False
|
||||
|
||||
if w.dtype in (
|
||||
torch.float8_e4m3fn,
|
||||
torch.float8_e4m3fnuz,
|
||||
):
|
||||
# For mixed quantization (experts int4, linear fp8), use linear_fp8_config
|
||||
selected_quant_config = getattr(
|
||||
self.quant_config, "linear_fp8_config", None
|
||||
)
|
||||
if selected_quant_config is None:
|
||||
selected_quant_config = self.quant_config
|
||||
weight_block_size = getattr(
|
||||
selected_quant_config, "weight_block_size", None
|
||||
)
|
||||
if weight_block_size is not None:
|
||||
assert hasattr(self_attn.kv_b_proj, "weight_scale_inv") or hasattr(
|
||||
self_attn.kv_b_proj, "weight_scale"
|
||||
)
|
||||
weight_scale = (
|
||||
self_attn.kv_b_proj.weight_scale
|
||||
if hasattr(self_attn.kv_b_proj, "weight_scale")
|
||||
else self_attn.kv_b_proj.weight_scale_inv
|
||||
)
|
||||
if _is_fp8_fnuz:
|
||||
weight, weight_scale, _ = normalize_e4m3fn_to_e4m3fnuz(
|
||||
weight=w,
|
||||
weight_scale=weight_scale,
|
||||
input_scale=None,
|
||||
)
|
||||
else:
|
||||
weight = w
|
||||
|
||||
# In multiple weight loading scenarios (e.g. RL), we need to inverse the scale of the weights after the requantization happened at the first loading.
|
||||
if (
|
||||
should_deepgemm_weight_requant_ue8m0(
|
||||
weight_block_size=getattr(
|
||||
self.quant_config, "weight_block_size", None
|
||||
)
|
||||
)
|
||||
and weight_scale.format_ue8m0
|
||||
):
|
||||
weight_scale = inverse_transform_scale_ue8m0(
|
||||
weight_scale, mn=weight.shape[-2]
|
||||
)
|
||||
|
||||
if (
|
||||
(_is_cuda or _is_musa or _is_xpu)
|
||||
and weight_block_size[0] == 128
|
||||
and weight_block_size[1] == 128
|
||||
):
|
||||
if (
|
||||
deep_gemm_wrapper.ENABLE_JIT_DEEPGEMM
|
||||
and not deep_gemm_wrapper.DEEPGEMM_BLACKWELL
|
||||
and get_bool_env_var("SGL_USE_DEEPGEMM_BMM", "false")
|
||||
):
|
||||
block_scale = weight_scale
|
||||
use_deep_gemm_bmm = True
|
||||
else:
|
||||
w = block_quant_dequant(
|
||||
weight,
|
||||
weight_scale,
|
||||
weight_block_size,
|
||||
torch.bfloat16,
|
||||
)
|
||||
else:
|
||||
w, scale = block_quant_to_tensor_quant(
|
||||
weight, weight_scale, weight_block_size
|
||||
)
|
||||
self_attn.w_scale = scale
|
||||
else:
|
||||
if _is_fp8_fnuz:
|
||||
weight, weight_scale, _ = normalize_e4m3fn_to_e4m3fnuz(
|
||||
weight=w,
|
||||
weight_scale=self_attn.kv_b_proj.weight_scale,
|
||||
input_scale=None,
|
||||
)
|
||||
else:
|
||||
weight = w
|
||||
weight_scale = self_attn.kv_b_proj.weight_scale
|
||||
|
||||
w, scale = channel_quant_to_tensor_quant(weight, weight_scale)
|
||||
self_attn.w_scale = scale
|
||||
|
||||
if w.dtype == torch.int8:
|
||||
if hasattr(self.quant_config, "weight_block_size"):
|
||||
# block-wise int8 need it
|
||||
weight_block_size = self.quant_config.weight_block_size
|
||||
if weight_block_size is not None:
|
||||
assert hasattr(self_attn.kv_b_proj, "weight_scale_inv")
|
||||
weight = w
|
||||
weight_scale = self_attn.kv_b_proj.weight_scale_inv
|
||||
w = int8_block_dequant(
|
||||
weight, weight_scale, weight_block_size
|
||||
).to(torch.bfloat16)
|
||||
else:
|
||||
# channel-wise int8 need it
|
||||
w = w.to(torch.bfloat16) * self_attn.kv_b_proj.weight_scale.to(
|
||||
torch.bfloat16
|
||||
)
|
||||
|
||||
w_kc, w_vc = w.unflatten(
|
||||
0, (-1, self_attn.qk_nope_head_dim + self_attn.v_head_dim)
|
||||
).split([self_attn.qk_nope_head_dim, self_attn.v_head_dim], dim=1)
|
||||
|
||||
if (
|
||||
_use_aiter_gfx95
|
||||
and self.quant_config is not None
|
||||
and self.quant_config.get_name() == "quark"
|
||||
and self.config.architectures
|
||||
and self.config.architectures[0]
|
||||
== "DeepseekV3ForCausalLM" # Avoid processing other models like GlmMoeDsaForCausalLM
|
||||
):
|
||||
w_kc, self_attn.w_scale_k, w_vc, self_attn.w_scale_v = (
|
||||
quark_post_load_weights(self_attn, w, "mxfp4")
|
||||
)
|
||||
|
||||
if not use_deep_gemm_bmm:
|
||||
self_attn.w_kc = bind_or_assign(
|
||||
self_attn.w_kc, w_kc.transpose(1, 2).contiguous().transpose(1, 2)
|
||||
)
|
||||
w_vc = w_vc.contiguous().transpose(1, 2)
|
||||
if _is_npu:
|
||||
w_vc = w_vc.contiguous()
|
||||
self_attn.w_vc = bind_or_assign(self_attn.w_vc, w_vc)
|
||||
if (
|
||||
hasattr(self_attn.kv_b_proj, "weight_scale")
|
||||
and self_attn.w_scale is None
|
||||
):
|
||||
self_attn.w_scale = bind_or_assign(
|
||||
self_attn.w_scale, self_attn.kv_b_proj.weight_scale
|
||||
)
|
||||
if _is_hip:
|
||||
self_attn.w_scale *= 2.0
|
||||
# XXX (MUSA): Remove this after adding FP8 support in bmm kernel on MUSA
|
||||
if _is_musa and w.dtype == torch.float8_e4m3fn:
|
||||
self_attn.w_kc = (
|
||||
self_attn.w_kc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
self_attn.w_vc = (
|
||||
self_attn.w_vc.to(torch.bfloat16) * self_attn.w_scale
|
||||
)
|
||||
else:
|
||||
num_tiles_k = self_attn.qk_nope_head_dim // weight_block_size[1]
|
||||
num_tiles_n = self_attn.v_head_dim // weight_block_size[0]
|
||||
ws_kc, ws_vc = block_scale.unflatten(
|
||||
0, (-1, (num_tiles_k + num_tiles_n))
|
||||
).split([num_tiles_k, num_tiles_n], dim=1)
|
||||
self_attn.w_scale_k = bind_or_assign(
|
||||
self_attn.w_scale_k, ws_kc.transpose(1, 2).contiguous()
|
||||
)
|
||||
self_attn.w_scale_v = bind_or_assign(
|
||||
self_attn.w_scale_v, ws_vc.contiguous()
|
||||
)
|
||||
self_attn.w_kc = bind_or_assign(
|
||||
self_attn.w_kc, w_kc.transpose(1, 2).contiguous()
|
||||
)
|
||||
self_attn.w_vc = bind_or_assign(self_attn.w_vc, w_vc.contiguous())
|
||||
self_attn.use_deep_gemm_bmm = True
|
||||
|
||||
@classmethod
|
||||
def generate_weight_name_filter(cls, logical_experts_map: Dict[int, List[int]]):
|
||||
"""
|
||||
Generates a filter function that tests whether the (layer_id, expert_id)
|
||||
indicated by a param name lies in the `logical_experts` map
|
||||
Args:
|
||||
logical_experts_map: a map of layer_id to expert_ids, specifying a list of expert_ids by a specific layer_id.
|
||||
|
||||
Returns:
|
||||
A function (name: str) -> bool
|
||||
"""
|
||||
import re
|
||||
|
||||
# Regex pattern to extract layer_id and expert_id from weight name
|
||||
pattern = re.compile(r"layers\.(\d+)\.mlp\.experts\.(\d+)\.")
|
||||
|
||||
def weight_name_filter(name: str) -> bool:
|
||||
match = pattern.search(name)
|
||||
if match:
|
||||
layer_id, expert = int(match.group(1)), int(match.group(2))
|
||||
# First check if layer_id exists, then check if expert is in the list
|
||||
return (
|
||||
layer_id in logical_experts_map
|
||||
and expert in logical_experts_map[layer_id]
|
||||
)
|
||||
return False
|
||||
|
||||
return weight_name_filter
|
||||
|
||||
def _maybe_quant_weights_to_fp8_ue8m0(
|
||||
self,
|
||||
weights,
|
||||
attn_quant_modules,
|
||||
nextn_conf: NextNConfig,
|
||||
):
|
||||
"""Optionally quantize weights to FP8 UE8M0 format for DeepSeek nvfp4 checkpoints.
|
||||
|
||||
Args:
|
||||
weights: Iterable of (name, tensor) weight pairs
|
||||
attn_quant_modules: List of attention module names to quantize
|
||||
nextn_conf: NextN configuration
|
||||
|
||||
Returns:
|
||||
Original weights iterator if no quantization needed,
|
||||
otherwise list of (name, tensor) pairs with quantized weights
|
||||
"""
|
||||
weight_block_size = [128, 128]
|
||||
partial_names = []
|
||||
|
||||
match nextn_conf:
|
||||
case NextNEnabledConfig(nextn_layer_id=layer_id):
|
||||
if envs.SGLANG_NVFP4_CKPT_FP8_GEMM_IN_ATTN.get():
|
||||
for stem in attn_quant_modules:
|
||||
partial_names.append(
|
||||
f"model.layers.{layer_id}.self_attn.{stem}"
|
||||
)
|
||||
|
||||
if enable_nextn_moe_bf16_cast_to_fp8(self.quant_config):
|
||||
expert_sub_names = ["shared_experts"] + [
|
||||
f"experts.{i}" for i in range(self.config.n_routed_experts)
|
||||
]
|
||||
for expert_sub_name in expert_sub_names:
|
||||
for stem in ["gate_proj", "up_proj", "down_proj"]:
|
||||
partial_names.append(
|
||||
f"model.layers.{layer_id}.mlp.{expert_sub_name}.{stem}"
|
||||
)
|
||||
|
||||
case NextNDisabledConfig():
|
||||
if envs.SGLANG_NVFP4_CKPT_FP8_GEMM_IN_ATTN.get():
|
||||
for layer_id in range(self.config.num_hidden_layers):
|
||||
for stem in attn_quant_modules:
|
||||
partial_names.append(
|
||||
f"model.layers.{layer_id}.self_attn.{stem}"
|
||||
)
|
||||
|
||||
# Early return if no quantization needed - avoid materializing all weights into memory
|
||||
if not partial_names:
|
||||
return weights
|
||||
|
||||
# Only materialize weights dict when quantization is actually needed
|
||||
weights_dict = dict(weights)
|
||||
|
||||
for partial_name in tqdm.tqdm(partial_names, desc="quant weights to fp8 ue8m0"):
|
||||
original_weight = weights_dict[f"{partial_name}.weight"]
|
||||
out_w, out_s = quant_weight_ue8m0(
|
||||
original_weight, weight_block_size=weight_block_size
|
||||
)
|
||||
weights_dict[f"{partial_name}.weight"] = out_w
|
||||
weights_dict[f"{partial_name}.weight_scale_inv"] = out_s
|
||||
|
||||
if isinstance(
|
||||
nextn_conf, NextNEnabledConfig
|
||||
) and enable_nextn_moe_bf16_cast_to_fp8(self.quant_config):
|
||||
self._mark_nextn_moe_weights_as_ue8m0()
|
||||
|
||||
return list(weights_dict.items())
|
||||
|
||||
def _mark_nextn_moe_weights_as_ue8m0(self):
|
||||
"""Mark NextN MoE weight scales as UE8M0 format to avoid requantization."""
|
||||
experts = self.model.decoder.mlp.experts
|
||||
w13_scale = (
|
||||
experts.w13_weight_scale_inv
|
||||
if hasattr(experts, "w13_weight_scale_inv")
|
||||
else experts.w13_weight_scale
|
||||
)
|
||||
w2_scale = (
|
||||
experts.w2_weight_scale_inv
|
||||
if hasattr(experts, "w2_weight_scale_inv")
|
||||
else experts.w2_weight_scale
|
||||
)
|
||||
w13_scale.format_ue8m0 = True
|
||||
w2_scale.format_ue8m0 = True
|
||||
@@ -0,0 +1,127 @@
|
||||
# Copyright 2026 SGLang Team
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
import logging
|
||||
import math
|
||||
from typing import Optional
|
||||
|
||||
import torch
|
||||
|
||||
from sglang.srt.environ import envs
|
||||
from sglang.srt.layers.moe.fused_moe_triton.layer import get_moe_runner_backend
|
||||
from sglang.srt.layers.quantization.base_config import QuantizationConfig
|
||||
from sglang.srt.layers.quantization.fp8_kernel import is_fp8_fnuz
|
||||
from sglang.srt.utils import (
|
||||
cpu_has_amx_support,
|
||||
get_bool_env_var,
|
||||
get_device_sm,
|
||||
get_hip_version,
|
||||
is_cpu,
|
||||
is_cuda,
|
||||
is_gfx95_supported,
|
||||
is_hip,
|
||||
is_musa,
|
||||
is_npu,
|
||||
is_nvidia_cublas_version_ge_12_9,
|
||||
is_xpu,
|
||||
)
|
||||
|
||||
_is_hip = is_hip()
|
||||
_is_cuda = is_cuda()
|
||||
_is_npu = is_npu()
|
||||
_is_musa = is_musa()
|
||||
_is_fp8_fnuz = is_fp8_fnuz()
|
||||
_use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip
|
||||
_is_cpu_amx_available = cpu_has_amx_support()
|
||||
_is_cpu = is_cpu()
|
||||
_is_xpu = is_xpu()
|
||||
_device_sm = get_device_sm()
|
||||
_is_gfx95_supported = is_gfx95_supported()
|
||||
_use_aiter_gfx95 = _use_aiter and _is_gfx95_supported
|
||||
_use_aiter_bpreshuffle_gfx95 = _use_aiter_gfx95 and get_hip_version() >= (7, 2, 0)
|
||||
|
||||
|
||||
_is_cublas_ge_129 = is_nvidia_cublas_version_ge_12_9()
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
NVFP4_CKPT_FP8_ATTN_QUANT_MODULES = ["q_b_proj"]
|
||||
|
||||
FORWARD_ABSORB_CORE_ATTENTION_BACKENDS = [
|
||||
"fa3",
|
||||
"dsa",
|
||||
"nsa", # Deprecated alias for "dsa"
|
||||
"flashinfer",
|
||||
"cutlass_mla",
|
||||
"trtllm_mla",
|
||||
"cutedsl_mla",
|
||||
"tokenspeed_mla",
|
||||
"ascend",
|
||||
"intel_xpu",
|
||||
]
|
||||
|
||||
|
||||
def awq_dequantize_func():
|
||||
"""
|
||||
Get the AWQ dequantize function for the current device
|
||||
|
||||
Return:
|
||||
- The AWQ dequantize function for the current device.
|
||||
- None if the current device is not supported.
|
||||
"""
|
||||
if _is_cuda:
|
||||
from sgl_kernel import awq_dequantize
|
||||
|
||||
return awq_dequantize
|
||||
elif _is_hip:
|
||||
from sglang.kernel_api_logging import debug_kernel_api
|
||||
from sglang.srt.layers.quantization.awq.awq_triton import (
|
||||
awq_dequantize_triton as awq_dequantize,
|
||||
)
|
||||
|
||||
return debug_kernel_api(awq_dequantize, op_name="DeepseekCommon.awq_dequantize")
|
||||
elif _is_npu:
|
||||
from sglang.kernel_api_logging import debug_kernel_api
|
||||
from sglang.srt.layers.quantization.awq.awq_triton import (
|
||||
awq_dequantize_decomposition as awq_dequantize,
|
||||
)
|
||||
|
||||
return debug_kernel_api(awq_dequantize, op_name="DeepseekCommon.awq_dequantize")
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def enable_nextn_moe_bf16_cast_to_fp8(
|
||||
quant_config: Optional[QuantizationConfig],
|
||||
) -> bool:
|
||||
return (
|
||||
envs.SGLANG_NVFP4_CKPT_FP8_NEXTN_MOE.get()
|
||||
and quant_config is not None
|
||||
and quant_config.get_name() == "modelopt_fp4"
|
||||
and get_moe_runner_backend().is_deep_gemm()
|
||||
)
|
||||
|
||||
|
||||
def yarn_get_mscale(scale: float = 1, mscale: float = 1) -> float:
|
||||
if scale <= 1:
|
||||
return 1.0
|
||||
return 0.1 * mscale * math.log(scale) + 1.0
|
||||
|
||||
|
||||
def _get_llama_4_scaling(
|
||||
original_max_position_embeddings: int, scaling_beta: float, positions: torch.Tensor
|
||||
) -> torch.Tensor:
|
||||
scaling = 1 + scaling_beta * torch.log(
|
||||
1 + torch.floor(positions / original_max_position_embeddings)
|
||||
)
|
||||
return scaling[..., None, None]
|
||||
Reference in New Issue
Block a user