Skip to content

[Bugfix]: Fix messy code when using logprobs #19209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2025

Conversation

chaunceyjiang
Copy link
Contributor

@chaunceyjiang chaunceyjiang commented Jun 5, 2025

FIX #19177

Copy link

github-actions bot commented Jun 5, 2025

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @chaunceyjiang, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

As gemini-code-assist, I've reviewed this pull request. The primary goal of this PR, as indicated by the title and linked issue (#19177), is to fix an issue where the output, particularly when using log probabilities, appears "messy" or incorrectly formatted. The core change addresses this by modifying the detokenization logic within the vllm/transformers_utils/detokenizer_utils.py file. Specifically, it switches from using tokenizer.convert_ids_to_tokens to tokenizer.decode to convert token IDs back into strings. This change is likely aimed at improving how special tokens, whitespace, and subword units are handled during the conversion process, leading to cleaner and more accurate text output, which is essential for correct logprob interpretation.

Highlights

  • Bugfix: Addresses issue #19177 concerning messy output when log probabilities are enabled.
  • Detokenization Logic Update: Changes the method used for converting token IDs back to strings in the convert_ids_list_to_tokens function.

Changelog

  • vllm/transformers_utils/detokenizer_utils.py
    • Modified the convert_ids_list_to_tokens function (line 92) to use tokenizer.decode(token_ids) instead of tokenizer.convert_ids_to_tokens(token_ids) for detokenization, aiming to fix output formatting issues related to logprobs.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


IDs flow from model,
Convert to text, a story told.
Decode makes it clean,
A bug fixed, a better scene.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request addresses an issue related to messy code when using log probabilities. The change replaces tokenizer.convert_ids_to_tokens with tokenizer.decode in the convert_ids_list_to_tokens function. This seems like a reasonable fix. I have a minor suggestion.

Merge Readiness

The change appears to be a straightforward bug fix. The suggestion is minor and can be addressed before or after merging. I am unable to directly approve the pull request, and other reviewers should review and approve this code before merging.

@DarkLight1337 DarkLight1337 requested a review from njhill June 5, 2025 12:18
@chaunceyjiang chaunceyjiang force-pushed the decode branch 3 times, most recently from e35828c to 2da2408 Compare June 6, 2025 09:06
@chaunceyjiang
Copy link
Contributor Author

/cc @njhill PTAL

Copy link
Member

@ywang96 ywang96 left a comment

Choose a reason for hiding this comment

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

I left a comment - I think it also makes sense to add a unit test for this if possible, but we can probably leave it for a later PR

@chaunceyjiang
Copy link
Contributor Author

test

from transformers import AutoTokenizer
from vllm.transformers_utils.detokenizer_utils import convert_ids_list_to_tokens
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct")
token_ids = tokenizer.encode("Hello, world!", add_special_tokens=True)
print(token_ids)
print(tokenizer.convert_ids_to_tokens([9707]))
print(tokenizer.convert_ids_to_tokens([11]))
print(tokenizer.convert_ids_to_tokens([1879]))
print(tokenizer.convert_ids_to_tokens([0]))
print(tokenizer.convert_ids_to_tokens(token_ids))
print("-----")
print(tokenizer.decode([9707]))
print(tokenizer.decode([11]))
print(tokenizer.decode([1879]))
print(tokenizer.decode([0]))
print("-----")
texts = convert_ids_list_to_tokens(tokenizer,[9707, 11, 1879, 0])
print(texts)
[9707, 11, 1879, 0]
['Hello']
[',']
['Ġworld']
['!']
['Hello', ',', 'Ġworld', '!']
-----
Hello
,
 world
!
-----
['Hello', ',', ' world', '!']

We can observe that using tokenizer.convert_ids_to_tokens() leads to messy code.

@chaunceyjiang
Copy link
Contributor Author

@DarkLight1337 @ywang96 @ProExpertProg PTAL.

@chaunceyjiang chaunceyjiang force-pushed the decode branch 3 times, most recently from d320616 to 191897b Compare June 17, 2025 14:21
@chaunceyjiang
Copy link
Contributor Author

Directly using tokenizer.convert_ids_to_tokens() does not produce human-readable text; it still needs to be passed through tokenizer.convert_tokens_to_string(). However, using tokenizer.decode() directly yields human-readable output.

Reference: https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils.py#L1090-L1115

@aarnphm aarnphm added frontend ready ONLY add when PR is ready to merge/full CI is needed labels Jun 20, 2025
@mgoin
Copy link
Member

mgoin commented Jun 20, 2025

The CI failures look related to detokenize changes, PTAL

@chaunceyjiang
Copy link
Contributor Author

The CI failures look related to detokenize changes,

Yes. I'm currently investigating this issue.

@ericg108
Copy link

ericg108 commented Jul 1, 2025

@chaunceyjiang Hi, any update for this? thanks.

@chaunceyjiang
Copy link
Contributor Author

Hi, @ericg108 I was on vacation last week. I will complete this PR this week.

@ericg108
Copy link

ericg108 commented Jul 1, 2025

btw, is there any way to postprocess to get the expected token, like from the bytes array or messy code?

@ericg108
Copy link

ericg108 commented Jul 1, 2025

Hi, @ericg108 I was on vacation last week. I will complete this PR this week.

thank you for your kindly effort @chaunceyjiang

Copy link

mergify bot commented Jul 7, 2025

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @chaunceyjiang.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@chaunceyjiang
Copy link
Contributor Author

@DarkLight1337 @ywang96 The docker-build-image-cpu test failure is unrelated to my code. All other tests have passed.
PTAL.

@DarkLight1337 DarkLight1337 merged commit 93b9d9f into vllm-project:main Jul 8, 2025
65 checks passed
huydhn pushed a commit to huydhn/vllm that referenced this pull request Jul 8, 2025
Chen-zexi pushed a commit to Chen-zexi/vllm that referenced this pull request Jul 13, 2025
@DarkLight1337
Copy link
Member

for token_id in token_ids:
token_str = tokenizer.decode(
[token_id],
skip_special_tokens=skip_special_tokens,
Copy link
Member

Choose a reason for hiding this comment

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

Mistral models don't support skip_special_tokens=False

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, it's my mistake—I didn't notice that detail.

#20910

@chaunceyjiang chaunceyjiang deleted the decode branch July 14, 2025 07:47
patrickvonplaten pushed a commit to patrickvonplaten/vllm that referenced this pull request Jul 15, 2025
Signed-off-by: chaunceyjiang <[email protected]>
Signed-off-by: Patrick von Platen <[email protected]>
LyrisZhong pushed a commit to LyrisZhong/vllm that referenced this pull request Jul 23, 2025
avigny pushed a commit to avigny/vllm that referenced this pull request Jul 31, 2025
Pradyun92 pushed a commit to Pradyun92/vllm that referenced this pull request Aug 6, 2025
npanpaliya pushed a commit to odh-on-pz/vllm-upstream that referenced this pull request Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend ready ONLY add when PR is ready to merge/full CI is needed v1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: messy code when using logprobs with vllm>0.7.2
7 participants