Batch detokenization across positions in detokenize_top_logprobs_tokens - #24447
Open
Aphoh wants to merge 6 commits into
Open
Batch detokenization across positions in detokenize_top_logprobs_tokens#24447Aphoh wants to merge 6 commits into
Aphoh wants to merge 6 commits into
Conversation
Previously the top-k detokenization called batch_decode once per position. Flatten all top-k token ids across non-empty positions into a single batch_decode call and slice the decoded texts back, and skip the tokenizer entirely when decode_to_text is False. Adds unit tests.
Aphoh
requested review from
Ying1123,
hnyls2002,
merrymercy and
xiezhq-hermann
as code owners
May 5, 2026 21:14
Contributor
There was a problem hiding this comment.
Code Review
This pull request optimizes the detokenize_top_logprobs_tokens method in tokenizer_manager.py by batching the detokenization of top-k tokens across all positions, replacing the previous per-position approach to reduce overhead. A new test suite was also added to validate the batched implementation. Feedback suggests reusing the detokenize_logprob_tokens method when text decoding is not required to minimize code duplication.
…ize-topk # Conflicts: # test/manual/test_tokenizer_manager.py
Addresses PR review: the decode_to_text=False path duplicates detokenize_logprob_tokens's no-text logic. Delegate to it instead. Update the unit-test stub to expose detokenize_logprob_tokens so the production code path runs as written.
12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
TokenizerManager.detokenize_top_logprobs_tokenspreviously iterated over positions and calledtokenizer.batch_decodeonce per position, leading to N tokenizer round-trips for N positions. The original code carried a TODO to batch all top-k tokens across all positions; this PR addresses it.Modifications
detokenize_top_logprobs_tokensinpython/sglang/srt/managers/tokenizer_manager.pyto flatten every position's top-k token ids into a single decode call over one-token sequences, then slice the decoded texts back to per-position lists. Whendecode_to_text=False, the tokenizer is skipped entirely._batch_decode_token_idsso both regular logprob detokenization and top-logprob detokenization usebackend_tokenizer.decode_batch(..., skip_special_tokens=False)when available, with a fallback totokenizer.batch_decode.TestDetokenizeTopLogprobsTokenscoverage for empty inputs, mixed empty/non-empty positions, the single decode-call path, backend decode preference, and equivalence against a per-position reference implementation.Performance / Repro
Helper-level benchmark only, no server e2e path. This compares the old per-position helper against the PR implementation on a long output sequence:
transformers: 5.5.4tokenizers: 0.22.2gpt2top_k=5, so 500,000 top-logprob token ids decoded withdecode_to_text=TrueSpeedup: 2.50x.
Repro script, run from the repo root in an environment with SGLang installed, for example after
pip install -e "python":Checklist