[VLM] Adopt jit qk_norm kernel in VLM (#16171)

Co-authored-by: luoyuan.luo <luoyuan.luo@antgroup.com>
This commit is contained in:
Yuan Luo
2026-01-01 10:10:36 +08:00
committed by GitHub
co-authored by luoyuan.luo
parent 12b89e51d8
commit 3a42c5e341
4 changed files with 26 additions and 7 deletions
+19 -1
View File
@@ -11,8 +11,10 @@ import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from sglang.jit_kernel.norm import can_use_fused_inplace_qknorm as can_use_jit_qk_norm
from sglang.srt.environ import envs
from sglang.srt.layers.dp_attention import get_attention_tp_rank, get_attention_tp_size
from sglang.srt.models.utils import apply_qk_norm
from sglang.srt.utils import (
get_bool_env_var,
get_device_capability,
@@ -799,7 +801,23 @@ class VisionAttention(nn.Module):
# internvl
if self.qk_normalization:
q, k = self._apply_qk_norm(q, k)
# jit kernel
if can_use_jit_qk_norm(self.head_size):
# q: [tokens, head, head_size] -> [tokens, embed_dim]
head_dim_for_norm = head * self.head_size
q, k = apply_qk_norm(
q=q,
k=k,
q_norm=self.q_norm,
k_norm=self.k_norm,
head_dim=head_dim_for_norm,
alt_stream=self.aux_stream,
)
else:
q, k = self._apply_qk_norm(q, k)
output = self.qkv_backend.forward(
q=q,
+1 -1
View File
@@ -25,6 +25,7 @@ from sglang.jit_kernel.norm import can_use_fused_inplace_qknorm, fused_inplace_q
from sglang.jit_kernel.utils import register_jit_op
from sglang.srt.environ import envs
from sglang.srt.layers.radix_attention import RadixAttention
from sglang.srt.model_executor.cuda_graph_runner import get_is_capture_mode
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
from sglang.srt.utils import is_cuda
@@ -223,7 +224,6 @@ def apply_qk_norm(
Returns:
Tuple of normalized query and key tensors
"""
from sglang.srt.model_executor.cuda_graph_runner import get_is_capture_mode
batch_size = q.size(0)
q_eps = q_norm.variance_epsilon