Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 2 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/srt/layers/quantization/unquant.py b/python/sglang/srt/layers/quantization/unquant.pyindex 5a66d91b2402c3efb4f30b8509031bf9753dfab5..a87477cdf13e95adecc9b26a872db765aac96e3e 100644--- a/python/sglang/srt/layers/quantization/unquant.py+++ b/python/sglang/srt/layers/quantization/unquant.py@@ -201,6 +201,20 @@ class UnquantizedLinearMethod(LinearMethodBase): def process_weights_after_loading(self, layer: torch.nn.Module) -> None: if _is_cpu and _is_cpu_amx_available: _amx_process_weight_after_loading(layer, ["weight"])+ elif _is_npu and layer.weight.shape[-2] < layer.weight.shape[-1]:+ # Contracting BF16 projections (for example an MLP down projection)+ # benefit from FRACTAL_NZ at large prefill token counts. Keep the ND+ # parameter for captured decode GEMMs, where NZ is slower, and hold+ # the alternate physical layout as a non-persistent runtime buffer.+ from sglang.srt.hardware_backend.npu.utils import npu_format_cast++ prefill_weight = npu_format_cast(layer.weight.data)+ if "_npu_prefill_weight" in layer._buffers:+ layer._buffers["_npu_prefill_weight"] = prefill_weight+ else:+ layer.register_buffer(+ "_npu_prefill_weight", prefill_weight, persistent=False+ ) def apply( self,@@ -252,7 +266,12 @@ class UnquantizedLinearMethod(LinearMethodBase): return output.view(*x_shapes[:-1], -1) return F.linear(x, layer.weight, bias) - return F.linear(x, layer.weight, bias)+ weight = layer.weight+ if _is_npu and hasattr(layer, "_npu_prefill_weight"):+ num_tokens = x.numel() // x.shape[-1]+ if num_tokens >= 4096:+ weight = layer._npu_prefill_weight+ return F.linear(x, weight, bias) def apply_into( self,diff --git a/python/sglang/srt/models/qwen3.py b/python/sglang/srt/models/qwen3.pyindex 9fbd046709e6d2d557b005f22d8b8586727fcec4..f8b40e6d9a2549d02fe15ebb9c1e8d5bba01d5fd 100644--- a/python/sglang/srt/models/qwen3.py+++ b/python/sglang/srt/models/qwen3.py@@ -15,7 +15,7 @@ from sglang.srt.layers.logits_processor import LogitsProcessor from sglang.srt.layers.pooler import Pooler, PoolingType from sglang.srt.layers.quantization.base_config import QuantizationConfig from sglang.srt.layers.radix_attention import RadixAttention-from sglang.srt.layers.rotary_embedding import get_rope+from sglang.srt.layers.rotary_embedding import RotaryEmbedding, get_rope from sglang.srt.layers.rotary_embedding.mrope import MRotaryEmbedding from sglang.srt.layers.utils import PPMissingLayer, get_layer_id from sglang.srt.layers.vocab_parallel_embedding import ParallelLMHead@@ -56,9 +56,14 @@ if _use_aiter: if _is_npu: from sgl_kernel_npu.norm.split_qkv_rmsnorm_rope import split_qkv_rmsnorm_rope+ from sgl_kernel_npu.norm.split_qkv_rmsnorm_rope_pos_cache_half_npu import (+ split_qkv_rmsnorm_rope_pos_cache_half_npu,+ ) from sglang.srt.hardware_backend.npu.cmo import get_cmo_stream, wait_cmo_stream + _NPU_POS_CACHE_MIN_TOKENS = 6144+ class Qwen3Attention(nn.Module): def __init__(@@ -146,6 +151,12 @@ class Qwen3Attention(nn.Module): base=rope_theta, rope_scaling=rope_scaling, )+ self.use_npu_pos_cache_kernel = (+ _is_npu+ and type(self.rotary_emb) is RotaryEmbedding+ and self.rotary_emb.is_neox_style+ and self.rotary_emb.rotary_dim == self.head_dim+ ) self.attn = RadixAttention( self.num_heads, self.head_dim,@@ -186,6 +197,29 @@ class Qwen3Attention(nn.Module): def forward_prepare_npu(self, positions, hidden_states, forward_batch): qkv, _ = self.qkv_proj(hidden_states) + # Direct cache indexing avoids materializing repeated per-token sin/cos+ # tensors. It wins for large prefill batches; the existing kernel is+ # faster for the much smaller decode shapes.+ if (+ self.use_npu_pos_cache_kernel+ and qkv.shape[0] >= _NPU_POS_CACHE_MIN_TOKENS+ ):+ return split_qkv_rmsnorm_rope_pos_cache_half_npu(+ qkv,+ positions,+ self.rotary_emb.cos_sin_cache,+ self.q_size,+ self.kv_size,+ self.head_dim,+ eps=self.q_norm.variance_epsilon,+ q_weight=self.q_norm.weight,+ k_weight=self.k_norm.weight,+ q_bias=getattr(self.q_norm, "bias", None),+ k_bias=getattr(self.k_norm, "bias", None),+ rope_dim=self.rotary_emb.rotary_dim,+ cast_norm_to_bf16=False,+ )+ if self.attn.layer_id == self.start_layer: self.rotary_emb.get_cos_sin_with_position(positions) q, k, v = split_qkv_rmsnorm_rope(