[Bugfix][Frontend] Truncate pooling prompts before padding them (#54364)

Signed-off-by: hotragn <hotragn.pettugani_2024@woxsen.edu.in>
This commit is contained in:
Hotragn Pettugani
2026-08-30 22:12:03 -07:00
committed by GitHub
parent 44fe2a392b
commit 687db59744
4 changed files with 142 additions and 17 deletions
@@ -55,6 +55,27 @@ def _apply_post_tokenization_to_token_type_ids(
tok_params: TokenizeParams,
token_type_ids: list[int],
) -> list[int]:
# Must stay in the same order as `TokenizeParams._validate_tokens`, which
# truncates before padding. These ids are parallel to the prompt tokens
# and locate the query/document boundary for the cross-encoder; applying
# the two steps in a different order silently misplaces that boundary.
max_length = tok_params.truncate_prompt_tokens
if max_length is not None and max_length < 0:
max_length = tok_params.max_input_tokens
if max_length is not None and max_length < len(token_type_ids):
if max_length == 0:
token_type_ids = token_type_ids[:0]
else:
side = tok_params.truncation_side or (
tokenizer.truncation_side if tokenizer is not None else None
)
token_type_ids = (
token_type_ids[-max_length:]
if side == "left"
else token_type_ids[:max_length]
)
pad_length = tok_params.pad_prompt_tokens
if pad_length is not None and pad_length < 0:
pad_length = tok_params.max_input_tokens
@@ -65,22 +86,7 @@ def _apply_post_tokenization_to_token_type_ids(
pad_length - len(token_type_ids)
)
max_length = tok_params.truncate_prompt_tokens
if max_length is not None and max_length < 0:
max_length = tok_params.max_input_tokens
if max_length is None or max_length >= len(token_type_ids):
return token_type_ids
if max_length == 0:
return token_type_ids[:0]
side = tok_params.truncation_side or (
tokenizer.truncation_side if tokenizer is not None else None
)
if side == "left":
return token_type_ids[-max_length:]
return token_type_ids[:max_length]
return token_type_ids
class ScoringIOProcessor(PoolingIOProcessor):