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..d17eeaf511a2fa285d5af27f2ebbbfafa20f79bc 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+from concurrent.futures import ThreadPoolExecutor 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 on a second host thread. Keep+ # that thread alive instead of creating and joining a new OS thread+ # for every generated token.+ self._update_executor = 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,19 @@ 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()- graph.replay()- thread.join()+ update = self._update_executor.submit(+ graph.update, cpu_update_input=cpu_update_input+ )+ try:+ graph.replay()+ finally:+ # Propagate update failures and ensure its input payload stays+ # alive through the asynchronous vendor call.+ 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/layers/rotary_embedding/base.py b/python/sglang/srt/layers/rotary_embedding/base.pyindex f009f4d1f642fcda92b1b2bc9535bb59fd70dfa5..7f61334ff5feff9f5cc092e743f0507a9c6d222f 100644--- a/python/sglang/srt/layers/rotary_embedding/base.py+++ b/python/sglang/srt/layers/rotary_embedding/base.py@@ -217,13 +217,16 @@ class RotaryEmbedding(MultiPlatformOp): ) cos_sin = self.cos_sin_cache.index_select(0, positions.flatten()) last_dim = cos_sin.size()[-1]- cos, sin = (- cos_sin.reshape(-1, 2, last_dim // 2).repeat(1, 1, 2).chunk(2, dim=-2)- )+ cos_half, sin_half = cos_sin.chunk(2, dim=-1)+ # Building the two contiguous results directly avoids materializing a+ # repeated [tokens, 2, dim] intermediate and then copying both of its+ # strided slices. Values and final BSNH layouts are unchanged.+ cos = torch.cat((cos_half, cos_half), dim=-1)+ sin = torch.cat((sin_half, sin_half), dim=-1) # BSNH self.position_cos, self.position_sin = (- cos.view(-1, 1, 1, last_dim).contiguous(),- sin.view(-1, 1, 1, last_dim).contiguous(),+ cos.view(-1, 1, 1, last_dim),+ sin.view(-1, 1, 1, last_dim), ) def get_cos_sin(self, seqlen: int) -> tuple[torch.Tensor, torch.Tensor]: