Skip to content

Commit 965a179

Browse files
author
niushengxiao
committed
opt
1 parent 7666087 commit 965a179

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

lightllm/server/api_openai.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,18 @@ async def _collect_generation_results(
524524
final_text = "".join(final_output)
525525
if finish_reason == "stop" and sampling_params.stop_sequences.size > 0:
526526
valid_stop_strings = sampling_params.stop_sequences.to_strings()
527-
for stop_str in valid_stop_strings:
528-
stop_index = final_text.rfind(stop_str)
529-
if stop_index != -1:
530-
logger.debug(f"removed stop sequence in tail: '{final_text[stop_index:]}'")
531-
final_text = final_text[:stop_index]
532-
break
527+
if valid_stop_strings:
528+
max_stop_len = len(valid_stop_strings[0])
529+
search_len = min(len(final_text), max_stop_len + 20) # 搜索长度为最长停止序列长度加20
530+
tail_text = final_text[-search_len:] if search_len > 0 else final_text
531+
tail_start_pos = len(final_text) - search_len
532+
for stop_str in valid_stop_strings:
533+
stop_index = tail_text.rfind(stop_str)
534+
if stop_index != -1:
535+
earliest_stop_index = tail_start_pos + stop_index
536+
logger.info(f"removed stop sequence in tail: '{final_text[earliest_stop_index:]}'")
537+
final_text = final_text[:earliest_stop_index]
538+
break
533539

534540
return {
535541
"index": prompt_index,

lightllm/server/core/objs/req.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def get_status(self):
3232
def is_finished(self):
3333
return self.FINISHED_STOP <= self.status <= self.FINISHED_LENGTH
3434

35+
def is_stopped(self):
36+
return self.status == self.FINISHED_STOP
37+
3538
def get_finish_reason(self):
3639
if self.status == self.FINISHED_STOP:
3740
return "stop"

lightllm/server/detokenization/decode_req.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def __init__(
3232
self.input_len = self.req.input_len
3333
self.prefix_str = ""
3434
self.stop_strs: List[str] = self.req.sample_params.stop_sequences.to_strings()
35-
self.stop_str_max_len = max([len(e) for e in self.stop_strs] + [0])
35+
# to_strings()已经做了倒序排列,第一个元素就是最长字符串
36+
self.stop_str_max_len = len(self.stop_strs[0]) if self.stop_strs else 0
3637

3738
def init_token_healing_prefix_str(self, token_id_to_token: Dict[int, str], tokenizer):
3839
tokens = [token_id_to_token[token_id] for token_id in self.req.prefix_token_ids.get_token_ids()]

lightllm/server/detokenization/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def gen_token_out(self):
139139
decode_req.output_strs.append(new_text)
140140

141141
# 停止字符串匹配
142-
if decode_req.stop_sequences_str_match():
142+
if not decode_req.req.finish_status.is_stopped() and decode_req.stop_sequences_str_match():
143143
decode_req.req.stop_str_matched_token_index = src_index
144144
decode_req.req.stop_str_matched = True
145145

0 commit comments

Comments
 (0)