[Frontend] Add truncation side to OpenAI endpoints (#43260)
Signed-off-by: Rui Zhang <rza21.bc@gmail.com> Signed-off-by: Rui Zhang <rui.zhang@globalrelay.net> Co-authored-by: Rui Zhang <rui.zhang@globalrelay.net>
This commit is contained in:
@@ -198,6 +198,13 @@ class TokenizeParams:
|
||||
max_input_tokens = self.max_input_tokens
|
||||
truncate_prompt_tokens = self.truncate_prompt_tokens
|
||||
|
||||
if self.truncation_side not in (None, "left", "right"):
|
||||
raise VLLMValidationError(
|
||||
"`truncation_side` must be either 'left' or 'right'.",
|
||||
parameter="truncation_side",
|
||||
value=self.truncation_side,
|
||||
)
|
||||
|
||||
if (
|
||||
max_output_tokens is not None
|
||||
and max_total_tokens is not None
|
||||
@@ -234,6 +241,9 @@ class TokenizeParams:
|
||||
truncate_prompt_tokens = tokenization_kwargs.pop(
|
||||
"truncate_prompt_tokens", self.truncate_prompt_tokens
|
||||
)
|
||||
truncation_side = tokenization_kwargs.pop(
|
||||
"truncation_side", self.truncation_side
|
||||
)
|
||||
do_lower_case = tokenization_kwargs.pop("do_lower_case", self.do_lower_case)
|
||||
add_special_tokens = tokenization_kwargs.pop(
|
||||
"add_special_tokens", self.add_special_tokens
|
||||
@@ -279,7 +289,7 @@ class TokenizeParams:
|
||||
),
|
||||
pad_prompt_tokens=pad_prompt_tokens,
|
||||
truncate_prompt_tokens=truncate_prompt_tokens,
|
||||
truncation_side=self.truncation_side,
|
||||
truncation_side=truncation_side,
|
||||
do_lower_case=do_lower_case,
|
||||
add_special_tokens=add_special_tokens,
|
||||
needs_detokenization=needs_detokenization,
|
||||
@@ -295,11 +305,11 @@ class TokenizeParams:
|
||||
# while still failing `self._token_len_check` as expected by users
|
||||
max_length = self.max_input_tokens + 1
|
||||
|
||||
# Left-side truncation requires the full token sequence so we can
|
||||
# slice from the end in _token_truncation. Disable HF-level
|
||||
# truncation (which would incorrectly truncate from the right for
|
||||
# pooling models) and let _token_truncation handle it.
|
||||
if self.truncation_side == "left":
|
||||
# Explicit truncation-side overrides require the full token sequence so
|
||||
# we can slice from the requested side in _token_truncation. Disable
|
||||
# tokenizer-level truncation because generation tokenizers default to
|
||||
# left truncation while callers may request right truncation.
|
||||
if self.truncation_side is not None and self.truncate_prompt_tokens is not None:
|
||||
return dict(
|
||||
truncation=False,
|
||||
add_special_tokens=self.add_special_tokens,
|
||||
|
||||
Reference in New Issue
Block a user