Skip to content

[Bugfix] Fix logprobs token-string collision from SentencePiece space…#48674

Open
aoshen02 wants to merge 3 commits into
vllm-project:mainfrom
aoshen02:fix/logprobs-token-string-collision
Open

[Bugfix] Fix logprobs token-string collision from SentencePiece space…#48674
aoshen02 wants to merge 3 commits into
vllm-project:mainfrom
aoshen02:fix/logprobs-token-string-collision

Conversation

@aoshen02

@aoshen02 aoshen02 commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

… stripping

Fixes #44319

SentencePiece's decode() strips the leading space from the first token (add_dummy_prefix inverse), causing distinct tokens like "▁true" (id=1565) and "true" (id=3009) to both decode to "true". When the Legacy Completions API builds top_logprobs as dict[str, float], these collide and overwrite each other — making logprob values appear to change with the requested count.

Fix: after decode(), check the raw vocab piece via convert_ids_to_tokens() and restore any leading spaces that were stripped. This keeps output human-readable while making dict keys collision-free.

Simplified from #44504 — uses convert_ids_to_tokens() directly instead of probing for SentencePiece's id_to_piece through multiple getattr chains, since they return identical results.

Purpose

Test Plan

Test Result


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.

… stripping

Fixes vllm-project#44319

SentencePiece's decode() strips the leading space from the first token
(add_dummy_prefix inverse), causing distinct tokens like "▁true" (id=1565)
and "true" (id=3009) to both decode to "true". When the Legacy Completions
API builds top_logprobs as dict[str, float], these collide and overwrite
each other — making logprob values appear to change with the requested count.

Fix: after decode(), check the raw vocab piece via convert_ids_to_tokens()
and restore any leading spaces that were stripped. This keeps output
human-readable while making dict keys collision-free.

Simplified from vllm-project#44504 — uses convert_ids_to_tokens() directly instead of
probing for SentencePiece's id_to_piece through multiple getattr chains,
since they return identical results.

Co-authored-by: Ao Shen <aoshen@inferact.ai>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Signed-off-by: Allen Shen <aoshen@inferact.ai>
@mergify mergify Bot added the bug Something isn't working label Jul 15, 2026
@aoshen02 aoshen02 marked this pull request as ready for review July 15, 2026 02:38

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Replace hardcoded _LEADING_SPACE_MARKERS frozenset with dynamic detection
via _get_leading_space_marker() which reads the pre_tokenizer config.
Only Metaspace pre_tokenizers (Llama/Mistral/T5) need the fix; ByteLevel
(GPT-2) and others skip entirely — no convert_ids_to_tokens call needed.

Co-authored-by: Ao Shen <aoshen@inferact.ai>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Signed-off-by: Allen Shen <aoshen@inferact.ai>
@aoshen02 aoshen02 force-pushed the fix/logprobs-token-string-collision branch from f4e7b8a to d6c7466 Compare July 15, 2026 02:54
@aoshen02 aoshen02 added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 15, 2026
@Isotr0py Isotr0py requested a review from chaunceyjiang July 15, 2026 03:05

@chaunceyjiang chaunceyjiang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I’ll run some tests locally today.

@chaunceyjiang chaunceyjiang self-assigned this Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Logprobs values change based on the number of requested log probabilities

3 participants