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/hardware_backend/npu/graph_runner/npu_cudagraph_backend.py b/python/sglang/srt/hardware_backend/npu/graph_runner/npu_cudagraph_backend.pyindex 105034c03c0fb36d35254f822b63908f5bd0a2a0..0e6b179b0e74c92c540b70ca45a5d3d07774bcbb 100644--- a/python/sglang/srt/hardware_backend/npu/graph_runner/npu_cudagraph_backend.py+++ b/python/sglang/srt/hardware_backend/npu/graph_runner/npu_cudagraph_backend.py@@ -12,7 +12,7 @@ non-NPU hosts. from __future__ import annotations -import threading+import concurrent.futures from contextlib import AbstractContextManager, contextmanager from functools import partial from typing import TYPE_CHECKING, Any, Callable, Dict, Optional@@ -63,6 +63,15 @@ class NPUCudaGraphBackend(BaseCudaGraphBackend): self._enable_torch_compile = getattr( cuda_graph_runner, "enable_torch_compile", False )+ # NPUGraph.update must overlap replay, but constructing a Python thread+ # for every generated token adds avoidable host overhead. A dedicated+ # worker preserves that overlap and keeps its NPU device context warm.+ self._update_executor = concurrent.futures.ThreadPoolExecutor(+ max_workers=1,+ thread_name_prefix="sglang-npu-graph-update",+ initializer=self._device_module.set_device,+ initargs=(self._device_id,),+ ) @contextmanager def capture_session(self, stream):@@ -166,17 +175,15 @@ class NPUCudaGraphBackend(BaseCudaGraphBackend): graph = self._graphs[shape_key] - def _update():- self._device_module.set_device(self._device_id)- graph.update(cpu_update_input=cpu_update_input)-- thread = threading.Thread(target=_update)- thread.start()+ update = self._update_executor.submit(+ graph.update, cpu_update_input=cpu_update_input+ ) graph.replay()- thread.join()+ update.result() return self._outputs[shape_key] def cleanup(self) -> None:+ self._update_executor.shutdown(wait=True) self._graphs.clear() self._outputs.clear() self._pool = Nonediff --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..99e5ba3d835a52f2408e59f4a90a60e9aa9a84c0 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,13 +239,18 @@ class NPUGraphRunner(DecodeCudaGraphRunner): is_deepseek_dsa(self.model_runner.model_config.hf_config) or is_deepseek_v4(self.model_runner.model_config.hf_config) ):+ # ScheduleBatch already maintains an authoritative CPU mirror of+ # seq_lens. Reading the device tensor here introduces a stream+ # synchronization on every decode step, immediately before graph+ # replay. Keep the device fallback for hand-built ForwardBatch+ # instances that do not provide the mirror.+ seq_lens_cpu = forward_batch.seq_lens_cpu+ if seq_lens_cpu is None:+ seq_lens_cpu = forward_batch.seq_lens.cpu()+ seq_lens = seq_lens_cpu[: self.raw_bs].tolist() 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)- else:- seq_lens = forward_batch.seq_lens.cpu().tolist() + [0] * (- self.bs - self.raw_bs- )+ seq_lens = [value + self.captured_req_width for value in seq_lens]+ seq_lens.extend([0] * (self.bs - self.raw_bs)) output = self.backend.replay_with_input_update( graph_key, seq_lens=seq_lens,