[Bug]: Completion logprobs text_offset uses the current token length, producing incorrect text spans
/v1/completions logprob offsets advance by the length of the current token rather than the preceding token. For A wonderful world!, the returned tokens are ['A', ' wonderful', ' world', '!'], but their offsets are [0, 10, 16, 17] instead of [0, 1, 11, 17]. Consumers using those offsets associate probabilities with the wrong text spans. The helper is shared by streaming and non-streaming completions.
System Info
- Source:
main,9fd891783959eed960a6c58144241db38f9b1580. - CPU-only reproduction, Python 3.12; no model inference required.
- Regression tests use real package imports and the installed
1.4.0rc1.dev202610140007wheel's native artifacts. Those native artifacts were built fromda676ffb8b611a1a0a83c862aa094e736f9fe5aa, 39 commits behind the Python checkout. The changed logic is Python postprocessing.
Reproduction
This uses a small local tokenizer; it needs no model download.
from tokenizers import Tokenizer, decoders, models
from transformers import PreTrainedTokenizerFast
from tensorrt_llm.serve.postprocess_handlers import create_completion_logprobs
tokens = ['A', ' wonderful', ' world', '!']
backend = Tokenizer(models.WordLevel(dict(zip(tokens, range(4)))))
backend.decoder = decoders.Fuse()
tokenizer = PreTrainedTokenizerFast(tokenizer_object=backend)
result = create_completion_logprobs([0, 1, 2, 3], tokenizer, [-0.5] * 4)
print(result.tokens)
print(result.text_offset)
I also reproduced the mismatch with the unmodified Qwen2.5-1.5B-Instruct tokenizer, comparing against its offset_mapping for English, Chinese, and accented text.
Expected behavior
[0, 1, 11, 17]: each offset identifies the beginning of the corresponding token.
Actual behavior
[0, 10, 16, 17].
Additional notes
A tested patch appends the running offset before advancing it by the decoded token's length. Twelve CPU regression cases cover unequal token lengths, complete Unicode characters, empty tokens, empty/singleton inputs, scalar/top-k logprobs, and both completion postprocessors, including a nonzero streaming offset. The same test file gives 8 failures / 4 passes before, 12 passes after; pre-commit checks pass. The new test is registered in l0_cpu.yml.
This preserves the existing character-offset convention and token decoding. It does not address tokens that individually decode to Unicode replacement characters, prompt-echo offsets, or inference performance. No GPU inference claim is made.
I checked the current and closed issue/PR search results for text_offset, create_completion_logprobs, and logprob-related changes. #10809 introduced this helper; the inspected nearby changes (#6845, #19072, #19964) do not correct these offsets. May I open a PR limited to this offset arithmetic and its regression tests?