Skip to content

Commit

Permalink
Add split_special_tokens to the Tokenize Endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Ruedi Steinmann <[email protected]>
  • Loading branch information
ruediste committed Jan 2, 2025
1 parent 7300144 commit f6d5301
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion vllm/entrypoints/openai/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,13 @@ class TokenizeCompletionRequest(OpenAIBaseModel):
default=True,
description=(
"If true (the default), special tokens (e.g. BOS) will be added to "
"the prompt."),
"the prompt.")
)
split_special_tokens: bool = Field(
default=False,
description=(
"If set to true, special tokens in the prompt will be split. For example, if <|fim_prefix|> is a special token"

Check failure on line 1259 in vllm/entrypoints/openai/protocol.py

View workflow job for this annotation

GitHub Actions / ruff (3.12)

Ruff (E501)

vllm/entrypoints/openai/protocol.py:1259:81: E501 Line too long (123 > 80)
"it would by default be tokenized for example to [151661]. With this flag set to true, it becomes [27,91,69,318,37151,91,29]")

Check failure on line 1260 in vllm/entrypoints/openai/protocol.py

View workflow job for this annotation

GitHub Actions / ruff (3.12)

Ruff (E501)

vllm/entrypoints/openai/protocol.py:1260:81: E501 Line too long (138 > 80)
)


Expand Down
11 changes: 9 additions & 2 deletions vllm/entrypoints/openai/serving_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ def _normalize_prompt_text_to_input(
prompt: str,
truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]],
add_special_tokens: bool,
split_special_tokens: bool=False,
) -> TextTokensPrompt:
if truncate_prompt_tokens is None:
encoded = tokenizer(prompt, add_special_tokens=add_special_tokens)
encoded = tokenizer(prompt, add_special_tokens=add_special_tokens, split_special_tokens=split_special_tokens)

Check failure on line 165 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / ruff (3.12)

Ruff (E501)

vllm/entrypoints/openai/serving_engine.py:165:81: E501 Line too long (121 > 80)

Check failure on line 165 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.9)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 165 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.10)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 165 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.11)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 165 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.12)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]
else:
encoded = tokenizer(prompt,

Check failure on line 167 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.9)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 167 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.10)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 167 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.11)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]

Check failure on line 167 in vllm/entrypoints/openai/serving_engine.py

View workflow job for this annotation

GitHub Actions / mypy (3.12)

Unexpected keyword argument "split_special_tokens" for "__call__" of "MistralTokenizer"; did you mean "add_special_tokens"? [call-arg]
add_special_tokens=add_special_tokens,
split_special_tokens=split_special_tokens,
truncation=True,
max_length=truncate_prompt_tokens)

Expand Down Expand Up @@ -298,6 +300,7 @@ def _tokenize_prompt_input_or_inputs(
input_or_inputs: Union[str, List[str], List[int], List[List[int]]],
truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None,
add_special_tokens: bool = True,
split_special_tokens: bool = False,
) -> List[TextTokensPrompt]:
"""
Tokenize/detokenize depending on the input format.
Expand All @@ -316,7 +319,9 @@ def _tokenize_prompt_input_or_inputs(
tokenizer,
prompt=prompt_input["content"],
truncate_prompt_tokens=truncate_prompt_tokens,
add_special_tokens=add_special_tokens)
add_special_tokens=add_special_tokens,
split_special_tokens=split_special_tokens,
)
if prompt_input["is_tokens"] is False else
self._normalize_prompt_tokens_to_input(
request,
Expand All @@ -333,13 +338,15 @@ async def _preprocess_completion(
input_or_inputs: Union[str, List[str], List[int], List[List[int]]],
truncate_prompt_tokens: Optional[Annotated[int, Field(ge=1)]] = None,
add_special_tokens: bool = True,
split_special_tokens: bool = False,
) -> Tuple[List[TextTokensPrompt], List[TokensPrompt]]:
request_prompts = await self._tokenize_prompt_input_or_inputs_async(
request,
tokenizer,
input_or_inputs,
truncate_prompt_tokens=truncate_prompt_tokens,
add_special_tokens=add_special_tokens,
split_special_tokens=split_special_tokens,
)

engine_prompts = [
Expand Down
1 change: 1 addition & 0 deletions vllm/entrypoints/openai/serving_tokenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async def create_tokenize(
tokenizer,
request.prompt,
add_special_tokens=request.add_special_tokens,
split_special_tokens=request.split_special_tokens,
)
except ValueError as e:
logger.exception("Error in preprocessing prompt inputs")
Expand Down

0 comments on commit f6d5301

Please sign in to comment.