[Bugfix] Preserve padding token types for cross encoders (#58143)

Signed-off-by: zhaoye <772971548@qq.com>
Co-authored-by: Taneem Ibrahim <taneem.ibrahim@gmail.com>
This commit is contained in:
Feanor
2026-10-15 03:09:24 +00:00
committed by GitHub
co-authored by Taneem Ibrahim
parent 429a3da665
commit 5d5c3d7c43
5 changed files with 64 additions and 25 deletions
@@ -84,7 +84,7 @@ def _apply_post_tokenization_to_token_type_ids(
pad_length = tok_params.max_input_tokens
if pad_length is not None and pad_length > len(token_type_ids):
pad_token_type_id = token_type_ids[-1] if token_type_ids else 0
pad_token_type_id = getattr(tokenizer, "pad_token_type_id", 0) or 0
token_type_ids = token_type_ids + [pad_token_type_id] * (
pad_length - len(token_type_ids)
)
+19 -10
View File
@@ -262,21 +262,30 @@ def parse_score_data(
return prompt_1, prompt_2, mm_items, mm_uuids
def compress_token_type_ids(token_type_ids: list[int]) -> int:
"""Return position of the first 1 or the length of the list
if not found.
def compress_token_type_ids(token_type_ids: list[int]) -> tuple[int, int]:
"""Return the half-open range containing token type 1.
Right padding uses the tokenizer's padding token type, which is commonly
zero. Therefore, a padded query-document pair can contain zeros followed
by ones followed by trailing zeros.
"""
first_one = len(token_type_ids)
first_zero_after_one = len(token_type_ids)
err_msg = (
"Token type ids are expected to be a sequence"
" of zeros followed by a sequence of ones"
" of zeros followed by ones and optional trailing zeros"
)
for i, type_id in enumerate(token_type_ids):
if type_id == 0 and first_one < i:
raise ValueError(err_msg)
elif type_id == 1 and first_one > i:
first_one = i
elif type_id > 1:
if type_id not in (0, 1):
raise ValueError(err_msg)
if type_id == 1:
if first_zero_after_one < len(token_type_ids):
raise ValueError(err_msg)
if first_one == len(token_type_ids):
first_one = i
elif first_one < len(token_type_ids) and first_zero_after_one == len(
token_type_ids
):
first_zero_after_one = i
return first_one
return first_one, first_zero_after_one
@@ -105,12 +105,15 @@ class EncoderOnlyModelState(DefaultModelState):
if pooling_params is None or pooling_params.extra_kwargs is None:
return
token_type_start = pooling_params.extra_kwargs.get("compressed_token_type_ids")
if token_type_start is not None:
token_type_range = pooling_params.extra_kwargs.get("compressed_token_type_ids")
if token_type_range is not None:
assert new_req_data.prompt_token_ids is not None
token_type_start, token_type_end = token_type_range
positions = torch.arange(
len(new_req_data.prompt_token_ids), dtype=torch.int32
)
self.token_type_ids[new_req_data.req_id] = (
torch.arange(len(new_req_data.prompt_token_ids), dtype=torch.int32)
>= token_type_start
(positions >= token_type_start) & (positions < token_type_end)
).to(torch.int32)
def remove_request(self, req_id: str) -> None:
+3 -2
View File
@@ -1079,8 +1079,9 @@ class GPUModelRunner(
for i in range(num_reqs):
seq_len_i = seq_lens_cpu[i]
pos = token_type_id_requests.get(i, seq_len_i)
ids = (torch.arange(seq_len_i) >= pos).int()
start, end = token_type_id_requests.get(i, (seq_len_i, seq_len_i))
positions = torch.arange(seq_len_i)
ids = ((positions >= start) & (positions < end)).int()
token_type_ids.append(ids)
token_type_ids_cpu = torch.empty(