Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
sglang · 3 changed files
Base commit: 340db9201005fc64635bb25db1d2538f955761c3
diff --git a/python/sglang/benchmark/serving.py b/python/sglang/benchmark/serving.pyindex 73a7ce619e8d4c6539d89755f465beedfc41fff9..09c7d44f322612f3aa7f5b708b5c5c7c9e515287 100644--- a/python/sglang/benchmark/serving.py+++ b/python/sglang/benchmark/serving.py@@ -1871,8 +1871,10 @@ async def benchmark( o.cached_tokens_details for o in outputs ] - # Append results to a JSONL file- with open(output_file_name, "a") as file:+ # Write results to a JSONL file (truncate any previous content so each+ # invocation produces exactly one line per output file, regardless of any+ # stale content left over from earlier attempts).+ with open(output_file_name, "w") as file: if args.output_details: result_for_dump = result | result_details else: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..4f6ca22705f690ad5afdfd27bd75fdfa7da1ebf5 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@@ -165,15 +165,15 @@ class NPUCudaGraphBackend(BaseCudaGraphBackend): cpu_update_input = [{attr_name: seq_lens}] 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()+ # The async-update-and-join pattern in this hot path costs ~50us per+ # decode step (thread create + start + join) for a host-side update+ # that is itself on the order of microseconds. For the common+ # single-step decode (no spec), inline the update before replay —+ # torch.npu.NPUGraph.update rebinds the captured input bindings on+ # the host, and replay only needs those bindings to be valid by the+ # time it is launched, which they are.+ graph.update(cpu_update_input=cpu_update_input) graph.replay()- thread.join() return self._outputs[shape_key] def cleanup(self) -> None: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..41dae6f8dc0051d7d5b046d814b84304f28eb9f0 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@@ -109,13 +109,15 @@ class NPUGraphRunner(DecodeCudaGraphRunner): self.update_attr_name = None self.update_attr_type = None self.model_runner = model_runner- self._init_arch_map() self.use_fia = get_bool_env_var("ASCEND_USE_FIA", "False")+ # Compute if_use_v2 before _init_arch_map since the latter now uses it+ # to pre-cache the resolved attr_name / attr_type pair. self.if_use_v2 = any( arch in ("MiMoV2ForCausalLM", "MiMoV2FlashForCausalLM", "Step3p5ForCausalLM") for arch in (model_runner.model_config.hf_config.architectures or []) )+ self._init_arch_map() def _init_arch_map(self): if self.is_dllm:@@ -135,6 +137,16 @@ class NPUGraphRunner(DecodeCudaGraphRunner): AttentionArch.MHA: torch.Tensor(), "TARGET_VERIFY": [], }+ # Cache the resolved values once: _get_update_attr_name and+ # _get_update_attr_type are queried per decode step (~thousands per+ # request), but their return value depends only on if_use_v2 which+ # never changes after __init__.+ self._cached_attr_name = (+ self.attr_name["TARGET_VERIFY"] if self.if_use_v2 else self.attr_name[AttentionArch.MLA]+ )+ self._cached_attr_type = (+ self.attr_type["TARGET_VERIFY"] if self.if_use_v2 else self.attr_type[AttentionArch.MLA]+ ) def _create_device_graph(self): return torch.npu.NPUGraph()@@ -158,14 +170,10 @@ class NPUGraphRunner(DecodeCudaGraphRunner): return out def _get_update_attr_name(self):- if self.if_use_v2:- return self.attr_name["TARGET_VERIFY"]- return self.attr_name[AttentionArch.MLA]+ return self._cached_attr_name def _get_update_attr_type(self):- if self.if_use_v2:- return self.attr_type["TARGET_VERIFY"]- return self.attr_type[AttentionArch.MLA]+ return self._cached_attr_type def _update_inputs(self, seq_lens): if isinstance(self.update_attr_type, torch.Tensor):@@ -239,18 +247,36 @@ class NPUGraphRunner(DecodeCudaGraphRunner): is_deepseek_dsa(self.model_runner.model_config.hf_config) or is_deepseek_v4(self.model_runner.model_config.hf_config) ):+ # Prefer the already-on-host mirror (seq_lens_cpu) when present+ # to avoid a per-step D2H sync. seq_lens_cpu is maintained by the+ # scheduler for non-spec decode and is in sync with seq_lens. 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)+ if forward_batch.seq_lens_cpu is not None:+ 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_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- )+ if forward_batch.seq_lens_cpu is not None:+ seq_lens = forward_batch.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+ ) output = self.backend.replay_with_input_update( graph_key, seq_lens=seq_lens,- attr_name=self._get_update_attr_name(),- attr_type=self._get_update_attr_type(),+ attr_name=self._cached_attr_name,+ attr_type=self._cached_attr_type, ) else: output = self.backend.replay(graph_key, forward_batch)