[Perf][NPU] Enable native GQA & fused AddNorm for MiniMax-H3 encoder (#6040)

Signed-off-by: MarkPoloChina <1228479724@qq.com>
Signed-off-by: XiongZhongwei <1228479724@qq.com>
This commit is contained in:
XiongZhongwei authored and GitHub committed 2026-08-26 09:14:05 +08:00
1 parent a708ae5b7a
commit 40dcfdbb43
11 files changed
+520 -28

No files matched your search

@@ -19,6 +19,7 @@ from vllm_omni.platforms.npu.layers.rotary_embedding import (
logger = init_logger(__name__)
_ROPE_PATCHED = False
_SDPA_PATCHED = False
_SWIGLU_PATCHED = False
@@ -48,6 +49,50 @@ def apply_minimax_h3_qwen3vl_patch() -> None:
logger.debug("Applied NPU fused RoPE patch for MiniMax H3 Qwen3-VL text encoder")
def _scaled_dot_product_attention_npu(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
) -> torch.Tensor:
"""Run causal SDPA with compressed K/V heads through NPU native GQA."""
num_heads = query.shape[1]
num_key_value_heads = key.shape[1]
num_value_heads = value.shape[1]
if num_key_value_heads != num_value_heads:
raise ValueError(
"GQA requires key and value to have the same number of heads, "
f"got k_heads={num_key_value_heads} and v_heads={num_value_heads}."
)
if num_key_value_heads == 0:
raise ValueError("GQA requires at least one KV head.")
if num_heads % num_key_value_heads != 0:
raise ValueError(
"GQA requires query heads to be a multiple of KV heads, "
f"got q_heads={num_heads} and kv_heads={num_key_value_heads}."
)
return F.scaled_dot_product_attention(
query,
key,
value,
dropout_p=0.0,
is_causal=True,
enable_gqa=num_heads != num_key_value_heads,
)
def apply_minimax_h3_qwen3vl_sdpa_patch() -> None:
"""Route MiniMax H3 Qwen3-VL text attention to NPU native GQA."""
global _SDPA_PATCHED
if _SDPA_PATCHED:
return
from vllm_omni.diffusion.models.minimax_h3 import encoder
encoder._scaled_dot_product_attention = _scaled_dot_product_attention_npu
_SDPA_PATCHED = True
logger.debug("Applied NPU SDPA patch for MiniMax H3 Qwen3-VL text encoder")
def npu_swiglu_from_packed(gate_up: torch.Tensor) -> torch.Tensor:
"""Apply fused SwiGLU to a packed gate/up projection tensor."""
return torch_npu.npu_swiglu(gate_up, dim=-1)
+3 -1
View File
@@ -107,16 +107,18 @@ class NPUOmniPlatform(OmniPlatform, NPUPlatform):
from vllm_omni.platforms.npu.models.minimax_h3 import (
apply_minimax_h3_qwen3vl_patch,
apply_minimax_h3_qwen3vl_sdpa_patch,
apply_minimax_h3_qwen3vl_swiglu_patch,
)
# Both patches import the MiniMax encoder package, whose __init__ loads
# These patches import the MiniMax encoder package, whose __init__ loads
# pipeline_minimax_h3 → diffusion.data. Doing that during platform
# construction races vllm_omni/__init__.py (patch before config) and
# closes a cycle through pipeline_registry → PI0_PIPELINE →
# DiffusionOutput. Apply them only after the platform exists, before
# the diffusion pipeline is loaded.
apply_minimax_h3_qwen3vl_patch()
apply_minimax_h3_qwen3vl_sdpa_patch()
apply_minimax_h3_qwen3vl_swiglu_patch()
set_mc2_tokens_capacity(vllm_config, od_config.max_num_seqs, 1)
set_mc2_mask(vllm_config, device)