[Misc] Rename think_start_str/think_end_str to reasoning_start_str/reasoning_end_str (#38242)
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
+24
-22
@@ -12,7 +12,7 @@ from vllm.tokenizers import cached_tokenizer_from_config
|
||||
class ReasoningConfig:
|
||||
"""Configuration for reasoning models.
|
||||
|
||||
Set `think_start_str` and `think_end_str` to the strings that delimit
|
||||
Set `reasoning_start_str` and `reasoning_end_str` to the strings that delimit
|
||||
the reasoning block (e.g. `"<think>"` and `"</think>"`). The
|
||||
corresponding token IDs are derived automatically via
|
||||
`initialize_token_ids` and are not intended to be set directly.
|
||||
@@ -20,53 +20,55 @@ class ReasoningConfig:
|
||||
|
||||
# NOTE: These parameters are temporary, the intent is to derive them
|
||||
# automatically from the reasoning parser in a future version.
|
||||
think_start_str: str = "<think>"
|
||||
reasoning_start_str: str = "<think>"
|
||||
"""String that indicates the start of reasoning."""
|
||||
think_end_str: str = "</think>"
|
||||
reasoning_end_str: str = "</think>"
|
||||
"""String that indicates the end of reasoning content."""
|
||||
|
||||
_think_start_token_ids: list[int] | None = field(
|
||||
_reasoning_start_token_ids: list[int] | None = field(
|
||||
default=None, init=False, repr=False
|
||||
)
|
||||
"""Private backing field for `think_start_token_ids`. Set by
|
||||
"""Private backing field for `reasoning_start_token_ids`. Set by
|
||||
`initialize_token_ids`. Not intended to be configured directly."""
|
||||
_think_end_token_ids: list[int] | None = field(default=None, init=False, repr=False)
|
||||
"""Private backing field for `think_end_token_ids`. Set by
|
||||
_reasoning_end_token_ids: list[int] | None = field(
|
||||
default=None, init=False, repr=False
|
||||
)
|
||||
"""Private backing field for `reasoning_end_token_ids`. Set by
|
||||
`initialize_token_ids`. Not intended to be configured directly."""
|
||||
|
||||
@property
|
||||
def think_start_token_ids(self) -> list[int] | None:
|
||||
"""Token IDs derived from `think_start_str`. Set automatically by
|
||||
def reasoning_start_token_ids(self) -> list[int] | None:
|
||||
"""Token IDs derived from `reasoning_start_str`. Set automatically by
|
||||
`initialize_token_ids`. Not intended to be configured directly."""
|
||||
return self._think_start_token_ids
|
||||
return self._reasoning_start_token_ids
|
||||
|
||||
@property
|
||||
def think_end_token_ids(self) -> list[int] | None:
|
||||
"""Token IDs derived from `think_end_str`. Set automatically by
|
||||
def reasoning_end_token_ids(self) -> list[int] | None:
|
||||
"""Token IDs derived from `reasoning_end_str`. Set automatically by
|
||||
`initialize_token_ids`. Not intended to be configured directly."""
|
||||
return self._think_end_token_ids
|
||||
return self._reasoning_end_token_ids
|
||||
|
||||
def initialize_token_ids(self, model_config: ModelConfig) -> None:
|
||||
"""Initialize reasoning token IDs from strings using the tokenizer."""
|
||||
if (
|
||||
self._think_start_token_ids is not None
|
||||
and self._think_end_token_ids is not None
|
||||
self._reasoning_start_token_ids is not None
|
||||
and self._reasoning_end_token_ids is not None
|
||||
):
|
||||
return
|
||||
|
||||
tokenizer = cached_tokenizer_from_config(model_config=model_config)
|
||||
|
||||
self._think_start_token_ids = tokenizer.encode(
|
||||
self.think_start_str, add_special_tokens=False
|
||||
self._reasoning_start_token_ids = tokenizer.encode(
|
||||
self.reasoning_start_str, add_special_tokens=False
|
||||
)
|
||||
self._think_end_token_ids = tokenizer.encode(
|
||||
self.think_end_str, add_special_tokens=False
|
||||
self._reasoning_end_token_ids = tokenizer.encode(
|
||||
self.reasoning_end_str, add_special_tokens=False
|
||||
)
|
||||
|
||||
if not self._think_start_token_ids or not self._think_end_token_ids:
|
||||
if not self._reasoning_start_token_ids or not self._reasoning_end_token_ids:
|
||||
raise ValueError(
|
||||
f"ReasoningConfig: failed to tokenize reasoning strings: "
|
||||
f"think_start_str='{self.think_start_str}', "
|
||||
f"think_end_str='{self.think_end_str}'. "
|
||||
f"reasoning_start_str='{self.reasoning_start_str}', "
|
||||
f"reasoning_end_str='{self.reasoning_end_str}'. "
|
||||
"Ensure the strings are valid tokens in the model's vocabulary."
|
||||
)
|
||||
|
||||
@@ -303,10 +303,12 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
# Check if thinking is enabled
|
||||
self.is_enabled = reasoning_config is not None
|
||||
|
||||
self.think_start_token_ids = getattr(
|
||||
reasoning_config, "think_start_token_ids", []
|
||||
self.reasoning_start_token_ids = getattr(
|
||||
reasoning_config, "reasoning_start_token_ids", []
|
||||
)
|
||||
self.reasoning_end_token_ids = getattr(
|
||||
reasoning_config, "reasoning_end_token_ids", []
|
||||
)
|
||||
self.think_end_token_ids = getattr(reasoning_config, "think_end_token_ids", [])
|
||||
|
||||
self.pin_memory = is_pin_memory
|
||||
self.device = device
|
||||
@@ -357,15 +359,15 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
think_count = 0
|
||||
else:
|
||||
last_start = self._find_last_sequence_index(
|
||||
prompt_tok_ids, self.think_start_token_ids
|
||||
prompt_tok_ids, self.reasoning_start_token_ids
|
||||
)
|
||||
last_end = self._find_last_sequence_index(
|
||||
prompt_tok_ids, self.think_end_token_ids
|
||||
prompt_tok_ids, self.reasoning_end_token_ids
|
||||
)
|
||||
in_think = last_start > last_end
|
||||
if in_think:
|
||||
think_count = len(prompt_tok_ids) - (
|
||||
last_start + len(self.think_start_token_ids)
|
||||
last_start + len(self.reasoning_start_token_ids)
|
||||
)
|
||||
else:
|
||||
think_count = 0
|
||||
@@ -405,8 +407,8 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
state["prev_output_length"] = current_length
|
||||
|
||||
# Check if new tokens contain think start or end sequences
|
||||
start_len = len(self.think_start_token_ids)
|
||||
end_len = len(self.think_end_token_ids)
|
||||
start_len = len(self.reasoning_start_token_ids)
|
||||
end_len = len(self.reasoning_end_token_ids)
|
||||
|
||||
# Look for think sequences in recent tokens (including boundary)
|
||||
# Check overlapping regions where sequences might span boundaries
|
||||
@@ -415,10 +417,10 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
|
||||
# Find any think start/end sequences in recent tokens
|
||||
recent_start_pos = self._find_last_sequence_index(
|
||||
recent_tokens, self.think_start_token_ids
|
||||
recent_tokens, self.reasoning_start_token_ids
|
||||
)
|
||||
recent_end_pos = self._find_last_sequence_index(
|
||||
recent_tokens, self.think_end_token_ids
|
||||
recent_tokens, self.reasoning_end_token_ids
|
||||
)
|
||||
|
||||
# Update state based on recent sequences
|
||||
@@ -469,7 +471,7 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
else:
|
||||
# In end mode
|
||||
state["end_count"] += 1
|
||||
if state["end_count"] >= len(self.think_end_token_ids):
|
||||
if state["end_count"] >= len(self.reasoning_end_token_ids):
|
||||
state.update(
|
||||
{
|
||||
"in_end": False,
|
||||
@@ -530,7 +532,9 @@ class ThinkingTokenBudgetLogitsProcessor(LogitsProcessor):
|
||||
state = self._state.get(i)
|
||||
if state and state["in_end"]:
|
||||
self.mask[i] = True
|
||||
self.force_token_ids[i] = self.think_end_token_ids[state["end_count"]]
|
||||
self.force_token_ids[i] = self.reasoning_end_token_ids[
|
||||
state["end_count"]
|
||||
]
|
||||
|
||||
# Check in CPU first not to sync with GPU
|
||||
has_active_thinking = any(
|
||||
|
||||
Reference in New Issue
Block a user