@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 9ce8fad2a9
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 7, 20:15 UTC

[Perf] Optimize Python Slice for Structured Output using `islice` instead of [:] (#33593)

Signed-off-by: yewentao256 <zhyanwentao@126.com> Signed-off-by: Wentao Ye <44945378+yewentao256@users.noreply.github.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

Wentao Ye committed Feb 24, 2026 at 12:02 UTC 9ce8fad2a9fe010ede46b085f6cb4099c8ec402d
8 files changed +23 -16
vllm/reasoning/abs_reasoning_parsers.py
+2 -2
@@ -4,7 +4,7 @@
4 import importlib
5 import os
6 from abc import abstractmethod
7 -from collections.abc import Callable, Sequence
7 +from collections.abc import Callable, Iterable, Sequence
8 from functools import cached_property
9 from typing import TYPE_CHECKING, Any
10
@@ -68,7 +68,7 @@ class ReasoningParser:
68 """
69
70 def is_reasoning_end_streaming(
71 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
71 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
72 ) -> bool:
73 """
74 Check if the reasoning content ends in the input_ids on a
vllm/reasoning/basic_parsers.py
+4 -3
@@ -2,7 +2,8 @@
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from abc import abstractmethod
5 -from collections.abc import Sequence
5 +from collections.abc import Iterable, Sequence
6 +from itertools import islice
7 from typing import TYPE_CHECKING, Any
8
9 from vllm.entrypoints.openai.engine.protocol import DeltaMessage
@@ -77,7 +78,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
78 return False
79
80 def is_reasoning_end_streaming(
80 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
81 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
82 ) -> bool:
83 end_token_id = self.end_token_id
84 return end_token_id in delta_ids
@@ -86,7 +87,7 @@ class BaseThinkingReasoningParser(ReasoningParser):
87 """
88 Extract the content after the end tokens
89 """
89 - if self.end_token_id not in input_ids[:-1]:
90 + if self.end_token_id not in islice(input_ids, 0, max(0, len(input_ids) - 1)):
91 return []
92 else:
93 return input_ids[input_ids.index(self.end_token_id) + 1 :]
vllm/reasoning/deepseek_v3_reasoning_parser.py
+2 -2
@@ -1,7 +1,7 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 -from collections.abc import Sequence
4 +from collections.abc import Iterable, Sequence
5
6 from transformers import PreTrainedTokenizerBase
7
@@ -41,7 +41,7 @@ class DeepSeekV3ReasoningParser(ReasoningParser):
41 return self._parser.is_reasoning_end(input_ids)
42
43 def is_reasoning_end_streaming(
44 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
44 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
45 ) -> bool:
46 return self._parser.is_reasoning_end_streaming(input_ids, delta_ids)
47
vllm/reasoning/identity_reasoning_parser.py
+2 -2
@@ -1,7 +1,7 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 -from collections.abc import Sequence
4 +from collections.abc import Iterable, Sequence
5
6 from transformers import PreTrainedTokenizerBase
7
@@ -36,7 +36,7 @@ class IdentityReasoningParser(ReasoningParser):
36 return True
37
38 def is_reasoning_end_streaming(
39 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
39 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
40 ) -> bool:
41 return True
42
vllm/reasoning/mistral_reasoning_parser.py
+1 -1
@@ -69,7 +69,7 @@ class MistralReasoningParser(BaseThinkingReasoningParser):
69 def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
70 has_eot_token = False
71
72 - for id in input_ids[::-1]:
72 + for id in reversed(input_ids):
73 if id == self.start_token_id:
74 # Reasoning ends only if a BOT token is found before a EOT token.
75 return has_eot_token
vllm/reasoning/step3_reasoning_parser.py
+6 -3
@@ -1,7 +1,8 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 -from collections.abc import Sequence
4 +from collections.abc import Iterable, Sequence
5 +from itertools import islice
6
7 import regex as re
8 from transformers import PreTrainedTokenizerBase
@@ -104,13 +105,15 @@ class Step3ReasoningParser(ReasoningParser):
105 return self.think_end_token_id in input_ids
106
107 def is_reasoning_end_streaming(
107 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
108 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
109 ) -> bool:
110 end_token_id = self.think_end_token_id
111 return end_token_id in delta_ids
112
113 def extract_content_ids(self, input_ids: list[int]) -> list[int]:
113 - if self.think_end_token_id not in input_ids[:-1]:
114 + if self.think_end_token_id not in islice(
115 + input_ids, 0, max(0, len(input_ids) - 1)
116 + ):
117 return []
118 else:
119 return input_ids[input_ids.index(self.think_end_token_id) + 1 :]
vllm/reasoning/step3p5_reasoning_parser.py
+2 -2
@@ -1,7 +1,7 @@
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 -from collections.abc import Sequence
4 +from collections.abc import Iterable, Sequence
5
6 from vllm.entrypoints.openai.chat_completion.protocol import (
7 ChatCompletionRequest,
@@ -51,7 +51,7 @@ class Step3p5ReasoningParser(BaseThinkingReasoningParser):
51 return self.end_offset < 1
52
53 def is_reasoning_end_streaming(
54 - self, input_ids: Sequence[int], delta_ids: Sequence[int]
54 + self, input_ids: Sequence[int], delta_ids: Iterable[int]
55 ) -> bool:
56 if self.end_token_id in input_ids and self.end_offset > 0:
57 self.end_offset -= 1
vllm/v1/structured_output/__init__.py
+4 -1
@@ -324,8 +324,11 @@ class StructuredOutputManager:
324 # Check if reasoning ends in *this* step
325 delta_from = request.num_computed_tokens - request.num_output_placeholders
326 all_token_ids = request.all_token_ids
327 + start = (
328 + delta_from if delta_from >= 0 else max(len(all_token_ids) + delta_from, 0)
329 + )
330 if self.reasoner.is_reasoning_end_streaming(
328 - all_token_ids, all_token_ids[delta_from:]
331 + all_token_ids, itertools.islice(all_token_ids, start, None)
332 ):
333 # Reasoning just ended, so we shouldn't advance til
334 # next pass