Skip to content
9 changes: 7 additions & 2 deletions python/sglang/srt/managers/detokenizer_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def event_loop(self):
def trim_matched_stop(
self, output: Union[str, List[int]], finished_reason: Dict, no_stop_trim: bool
):
if no_stop_trim or not finished_reason:
if not finished_reason:
return output

matched = finished_reason.get("matched", None)
Expand All @@ -167,10 +167,15 @@ def trim_matched_stop(
# Trim stop str.
if isinstance(matched, str) and isinstance(output, str):
pos = output.find(matched)
return output[:pos] if pos != -1 else output
if pos == -1:
return output
end = pos + len(matched)
return output[:end] if no_stop_trim else output[:pos]

# Trim stop token.
if isinstance(matched, int) and isinstance(output, list):
if no_stop_trim:
return output
# 200012 <|call|> is the tool call token and one of eos tokens for gpt-oss model
if output[-1] == 200012 and self.is_tool_call_parser_gpt_oss:
return output
Expand Down
60 changes: 49 additions & 11 deletions python/sglang/srt/managers/schedule_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,20 +1119,26 @@ def init_incremental_detokenize(self):

return self.surr_and_decode_ids, self.read_offset - self.surr_offset

def tail_str(self) -> str:
def _stop_match_tail_len(self, new_accepted_len: int) -> int:
max_len_tail_str = max(
self.sampling_params.stop_str_max_len + 1,
self.sampling_params.stop_regex_max_len + 1,
)
# Cover all newly accepted tokens so an early stop string is not missed
# when speculative decoding accepts multiple tokens per step.
return min(
max_len_tail_str + max(new_accepted_len - 1, 0), len(self.output_ids)
)

def tail_str(self, new_accepted_len: int = 1) -> str:
# Check stop strings and stop regex patterns together
if (
len(self.sampling_params.stop_strs) == 0
and len(self.sampling_params.stop_regex_strs) == 0
):
return ""

max_len_tail_str = max(
self.sampling_params.stop_str_max_len + 1,
self.sampling_params.stop_regex_max_len + 1,
)

tail_len = min(max_len_tail_str, len(self.output_ids))
tail_len = self._stop_match_tail_len(new_accepted_len)
return self.tokenizer.decode(self.output_ids[-tail_len:])

def check_match_stop_str_prefix(self) -> bool:
Expand Down Expand Up @@ -1188,18 +1194,47 @@ def _check_token_based_finish(self, new_accepted_tokens: List[int]) -> bool:

return False

def _check_str_based_finish(self):
def _locate_str_stop_finished_len(
self,
new_accepted_len: int,
*,
stop_str: Optional[str] = None,
stop_regex: Optional[str] = None,
) -> int:
"""Map a matched stop string/regex to output_ids length (stop included)."""
def matched(text: str) -> bool:
if stop_str is not None:
return stop_str in text
return re.search(stop_regex, text) is not None

tail_len = self._stop_match_tail_len(new_accepted_len)
start = len(self.output_ids) - tail_len
token_window = self.output_ids[start:]

for token_count in range(1, len(token_window)):
Comment thread
hnyls2002 marked this conversation as resolved.
Resolved
if matched(self.tokenizer.decode(token_window[:token_count])):
return start + token_count

# The full tail window is already known to match by the caller.
return len(self.output_ids)

def _check_str_based_finish(self, new_accepted_len: int = 1):
if (
len(self.sampling_params.stop_strs) > 0
or len(self.sampling_params.stop_regex_strs) > 0
):
tail_str = self.tail_str()
tail_str = self.tail_str(new_accepted_len)

# Check stop strings
if len(self.sampling_params.stop_strs) > 0:
for stop_str in self.sampling_params.stop_strs:
if stop_str in tail_str or stop_str in self.decoded_text:
stop_str_in_tail = stop_str in tail_str
if stop_str_in_tail or stop_str in self.decoded_text:
self.finished_reason = FINISH_MATCHED_STR(matched=stop_str)
if stop_str_in_tail:
self.finished_len = self._locate_str_stop_finished_len(
new_accepted_len, stop_str=stop_str
)
return True

# Check stop regex
Expand All @@ -1209,6 +1244,9 @@ def _check_str_based_finish(self):
self.finished_reason = FINISHED_MATCHED_REGEX(
matched=stop_regex_str
)
self.finished_len = self._locate_str_stop_finished_len(
new_accepted_len, stop_regex=stop_regex_str
)
return True

return False
Expand Down Expand Up @@ -1258,7 +1296,7 @@ def update_finish_state(self, new_accepted_len: int = 1):
if self._check_vocab_boundary_finish(new_accepted_tokens):
return

if self._check_str_based_finish():
if self._check_str_based_finish(new_accepted_len):
return

def reset_for_retract(self):
Expand Down
Loading