Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 5 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/srt/hardware_backend/npu/graph_runner/npu_graph_runner.py b/python/sglang/srt/hardware_backend/npu/graph_runner/npu_graph_runner.pyindex b82c973bc340c072ea79877ef4a0d183bd6af32d..75317d31995980111d4fa1cd07ec3b71bf355f54 100644--- a/python/sglang/srt/hardware_backend/npu/graph_runner/npu_graph_runner.py+++ b/python/sglang/srt/hardware_backend/npu/graph_runner/npu_graph_runner.py@@ -239,11 +239,15 @@ class NPUGraphRunner(DecodeCudaGraphRunner): is_deepseek_dsa(self.model_runner.model_config.hf_config) or is_deepseek_v4(self.model_runner.model_config.hf_config) ):+ seq_lens_cpu = forward_batch.seq_lens_cpu+ if seq_lens_cpu is None:+ seq_lens_cpu = forward_batch.seq_lens.cpu() if forward_batch.forward_mode.is_target_verify():- seq_lens_cpu = forward_batch.seq_lens.cpu() + self.captured_req_width- seq_lens = seq_lens_cpu.tolist() + [0] * (self.bs - self.raw_bs)+ seq_lens = (seq_lens_cpu + self.captured_req_width).tolist() + [+ 0+ ] * (self.bs - self.raw_bs) else:- seq_lens = forward_batch.seq_lens.cpu().tolist() + [0] * (+ seq_lens = seq_lens_cpu.tolist() + [0] * ( self.bs - self.raw_bs ) output = self.backend.replay_with_input_update(diff --git a/python/sglang/srt/hardware_backend/npu/utils.py b/python/sglang/srt/hardware_backend/npu/utils.pyindex 664bb2881873dcfad7859bba9b8900d58ed1cc52..35da6a8702e283367e4e49247f361d49f7d7ab47 100644--- a/python/sglang/srt/hardware_backend/npu/utils.py+++ b/python/sglang/srt/hardware_backend/npu/utils.py@@ -77,6 +77,21 @@ def set_default_server_args(args: "ServerArgs"): # NPU does not support CustomAllReduce args.disable_custom_all_reduce = True + # A short decode-time admission window lets the scheduler combine the+ # small prefills that otherwise interrupt an NPU graph one request at a+ # time. The queue trigger is inactive for an idle engine, and its+ # wall-clock cap keeps the added TTFT bounded under sparse traffic.+ if (+ not args.enable_prefill_delayer+ and not args.disable_overlap_schedule+ and args.dp_size == 1+ ):+ args.enable_prefill_delayer = True+ if args.prefill_delayer_queue_min_ratio is None:+ args.prefill_delayer_queue_min_ratio = 0.1+ if args.prefill_delayer_max_delay_ms is None:+ args.prefill_delayer_max_delay_ms = 60.0+ # handles hierarchical cache configs if args.enable_hierarchical_cache: args.hicache_io_backend = "kernel_ascend"diff --git a/python/sglang/srt/models/qwen3.py b/python/sglang/srt/models/qwen3.pyindex 9fbd046709e6d2d557b005f22d8b8586727fcec4..eb48f74c17d30ec74afb8a001f4631f28632fa50 100644--- a/python/sglang/srt/models/qwen3.py+++ b/python/sglang/srt/models/qwen3.py@@ -43,6 +43,7 @@ _is_cuda = is_cuda() _is_hip = is_hip() _is_npu = is_npu() _use_aiter = get_bool_env_var("SGLANG_USE_AITER") and _is_hip+_use_npu_fia = get_bool_env_var("ASCEND_USE_FIA", "False") and _is_npu _has_fused_qk_norm_mrope = False if _use_aiter:@@ -58,6 +59,7 @@ if _is_npu: from sgl_kernel_npu.norm.split_qkv_rmsnorm_rope import split_qkv_rmsnorm_rope from sglang.srt.hardware_backend.npu.cmo import get_cmo_stream, wait_cmo_stream+ from sglang.srt.hardware_backend.npu.memory_pool_npu import NPUMHATokenToKVPool class Qwen3Attention(nn.Module):@@ -183,11 +185,38 @@ class Qwen3Attention(nn.Module): q, k = self.rotary_emb(positions, q, k) return q, k, v - def forward_prepare_npu(self, positions, hidden_states, forward_batch):+ def forward_prepare_npu(+ self, positions, hidden_states, forward_batch, write_kv_cache=False+ ): qkv, _ = self.qkv_proj(hidden_states) if self.attn.layer_id == self.start_layer: self.rotary_emb.get_cos_sin_with_position(positions)+ cache_args = {}+ if write_kv_cache:+ token_to_kv_pool = get_token_to_kv_pool()+ write_kv_cache = type(token_to_kv_pool) is NPUMHATokenToKVPool+ if write_kv_cache:+ k_cache, v_cache = token_to_kv_pool.get_kv_buffer(self.attn.layer_id)+ cache_loc = forward_batch.out_cache_loc+ write_kv_cache = (+ cache_loc.is_contiguous()+ and cache_loc.numel() >= qkv.shape[0]+ and k_cache.device == qkv.device+ and v_cache.device == qkv.device+ and k_cache.is_contiguous()+ and v_cache.is_contiguous()+ and k_cache.dtype == qkv.dtype+ and v_cache.dtype == qkv.dtype+ and k_cache.shape[-2] * k_cache.shape[-1] == self.kv_size+ and v_cache.shape[-2] * v_cache.shape[-1] == self.kv_size+ )+ if write_kv_cache:+ cache_args = dict(+ k_cache=k_cache,+ v_cache=v_cache,+ cache_loc=cache_loc,+ ) q, k, v = split_qkv_rmsnorm_rope( qkv, self.rotary_emb.position_sin,@@ -200,8 +229,9 @@ class Qwen3Attention(nn.Module): k_weight=self.k_norm.weight, q_bias=getattr(self.q_norm, "bias", None), k_bias=getattr(self.k_norm, "bias", None),+ **cache_args, )- return q, k, v+ return q, k, v, write_kv_cache def forward_prepare_aiter_fused_mrope( self, positions, hidden_states, forward_batch@@ -280,6 +310,18 @@ class Qwen3Attention(nn.Module): and forward_batch.forward_mode.is_decode() and get_exec().deterministic.rl_on_policy_target is None )+ use_npu_fused_cache = (+ _is_npu+ and get_exec().deterministic.rl_on_policy_target is None+ and (+ forward_batch.forward_mode.is_decode()+ or (+ forward_batch.forward_mode.is_extend()+ and not _use_npu_fia+ and forward_batch.attn_cp_metadata is None+ )+ )+ ) if use_aiter_fused: q, k, v = self.forward_prepare_aiter_fused_mrope(@@ -292,11 +334,14 @@ class Qwen3Attention(nn.Module): hidden_states=hidden_states, ) else:- q, k, v = self.forward_prepare_npu(+ q, k, v, wrote_kv_cache = self.forward_prepare_npu( positions=positions, hidden_states=hidden_states, forward_batch=forward_batch,+ write_kv_cache=use_npu_fused_cache, )+ if wrote_kv_cache:+ save_kv_cache = False if get_exec().deterministic.rl_on_policy_target is not None: q = q.to(torch.bfloat16)diff --git a/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/__init__.py b/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/__init__.pyindex 9b43453aa322ae20dc6be66fdf522561e9252644..c124adbeef6092c1519eb8790adb7da8b318fc37 100644--- a/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/__init__.py+++ b/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/__init__.py@@ -2,6 +2,13 @@ import os import pathlib from functools import lru_cache, wraps +# Some CANN components initialize their file sinks while torch_npu is imported.+# Keep both paths defined so that initialization neither targets the filesystem+# root nor emits non-JSON diagnostics into embedding processes' stdout.+_default_ascend_work_path = os.environ.get("HOME", "/tmp")+os.environ.setdefault("ASCEND_PROCESS_LOG_PATH", _default_ascend_work_path)+os.environ.setdefault("ASCEND_WORK_PATH", _default_ascend_work_path)+ import torch import torch_npu diff --git a/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/norm/split_qkv_rmsnorm_rope.py b/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/norm/split_qkv_rmsnorm_rope.pyindex 2b41121ef1893a48688206a8b7594792477e35cc..d5227252802b914d9e389a58e0b36be263a3e5e0 100644--- a/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/norm/split_qkv_rmsnorm_rope.py+++ b/sgl-kernel-npu/python/sgl_kernel_npu/sgl_kernel_npu/norm/split_qkv_rmsnorm_rope.py@@ -13,6 +13,7 @@ def split_qkv_rmsnorm_rope_kernel( q_ptr, k_ptr, v_ptr,+ cache_loc_ptr, q_weight_ptr, q_bias_ptr, k_weight_ptr,@@ -32,6 +33,7 @@ def split_qkv_rmsnorm_rope_kernel( PASS_DIM: tl.constexpr, DO_PARTIAL: tl.constexpr, DO_HALF: tl.constexpr,+ WRITE_CACHE: tl.constexpr, ): row_pid = tl.program_id(0) col_pid = tl.program_id(1)@@ -200,8 +202,9 @@ def split_qkv_rmsnorm_rope_kernel( if BIAS: bias_values = tl.load(k_bias_ptr + tl.arange(0, HEAD_DIM)).to(tl.float32) input_offset = row_pid * total_hidden_size + q_hidden_size- output_offset = row_pid * kv_hidden_size- output_offset_step = row_step * kv_hidden_size+ if not WRITE_CACHE:+ output_offset = row_pid * kv_hidden_size+ output_offset_step = row_step * kv_hidden_size for row_idx in tl.range(row_pid, batch_size, row_step): col_indices = col_pid * KV_BLOCK_SIZE + tl.arange(0, KV_BLOCK_SIZE) valid_mask = col_indices < kv_hidden_size@@ -346,26 +349,37 @@ def split_qkv_rmsnorm_rope_kernel( normalized_values = roped_k # store+ if WRITE_CACHE:+ store_offset = tl.load(cache_loc_ptr + row_idx) * kv_hidden_size+ else:+ store_offset = output_offset tl.store(- k_ptr + output_offset + col_indices,+ k_ptr + store_offset + col_indices, normalized_values.reshape(KV_BLOCK_SIZE).to(k_ptr.dtype.element_ty), mask=valid_mask, ) input_offset += input_offset_step- output_offset += output_offset_step+ if not WRITE_CACHE:+ output_offset += output_offset_step # v input_offset = row_pid * total_hidden_size + q_hidden_size + kv_hidden_size- output_offset = row_pid * kv_hidden_size- for _ in tl.range(row_pid, batch_size, row_step):+ if not WRITE_CACHE:+ output_offset = row_pid * kv_hidden_size+ for row_idx in tl.range(row_pid, batch_size, row_step): col_indices = col_pid * KV_BLOCK_SIZE + tl.arange(0, KV_BLOCK_SIZE) valid_mask = col_indices < kv_hidden_size input_values = tl.load( input_ptr + input_offset + col_indices, mask=valid_mask, other=0.0 )- tl.store(v_ptr + output_offset + col_indices, input_values, mask=valid_mask)+ if WRITE_CACHE:+ store_offset = tl.load(cache_loc_ptr + row_idx) * kv_hidden_size+ else:+ store_offset = output_offset+ tl.store(v_ptr + store_offset + col_indices, input_values, mask=valid_mask) input_offset += input_offset_step- output_offset += output_offset_step+ if not WRITE_CACHE:+ output_offset += output_offset_step kernels = {}@@ -384,6 +398,9 @@ def split_qkv_rmsnorm_rope( q_bias=None, k_bias=None, is_neox_style=True,+ k_cache=None,+ v_cache=None,+ cache_loc=None, ): _, num_vectorcore = get_device_properties() @@ -396,12 +413,24 @@ def split_qkv_rmsnorm_rope( q_output = torch.empty( batch_size, q_hidden_size, device=input.device, dtype=input.dtype )- k_output = torch.empty(- batch_size, kv_hidden_size, device=input.device, dtype=input.dtype- )- v_output = torch.empty(- batch_size, kv_hidden_size, device=input.device, dtype=input.dtype- )+ write_cache = cache_loc is not None+ if write_cache:+ assert k_cache is not None and v_cache is not None+ assert cache_loc.is_contiguous() and cache_loc.numel() >= batch_size+ assert k_cache.device == input.device and v_cache.device == input.device+ assert k_cache.is_contiguous() and v_cache.is_contiguous()+ assert k_cache.dtype == input.dtype and v_cache.dtype == input.dtype+ assert k_cache.shape[-2] * k_cache.shape[-1] == kv_hidden_size+ assert v_cache.shape[-2] * v_cache.shape[-1] == kv_hidden_size+ k_output, v_output = k_cache, v_cache+ else:+ assert k_cache is None and v_cache is None+ k_output = torch.empty(+ batch_size, kv_hidden_size, device=input.device, dtype=input.dtype+ )+ v_output = torch.empty(+ batch_size, kv_hidden_size, device=input.device, dtype=input.dtype+ ) n_cols = kv_hidden_size // KV_BLOCK_SIZE n_rows = (num_vectorcore + n_cols - 1) // n_cols BIAS = q_bias is not None@@ -414,6 +443,7 @@ def split_qkv_rmsnorm_rope( q_output, k_output, v_output,+ cache_loc, q_weight, q_bias, k_weight,@@ -433,8 +463,11 @@ def split_qkv_rmsnorm_rope( head_dim - rope_dim, DO_PARTIAL=(head_dim != rope_dim), DO_HALF=is_neox_style,+ WRITE_CACHE=write_cache, ) + if write_cache:+ return q_output, None, None return q_output, k_output, v_output