[Fix] Use pre-tokenized prompts in VLLMwithChatTemplate to avoid modifying model input#2434
Open
suhmily10 wants to merge 1 commit intoopen-compass:mainfrom
Open
Conversation
…fying model input The previous code called apply_chat_template(tokenize=False) to get text, then stripped the BOS token as a workaround for vLLM re-adding it during tokenization. This approach modifies the model's intended input sequence. Instead, use apply_chat_template(tokenize=True) to obtain token IDs directly, and pass them as pre-tokenized prompts (prompt_token_ids) to vLLM. This preserves the exact token sequence the chat template produces without any manual modification. Made-with: Cursor
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.
Summary
VLLMwithChatTemplate.generate()currently callsapply_chat_template(tokenize=False)to produce text, then manually strips the BOS token as a workaround for vLLM re-adding it during tokenization (add_special_tokens=True). This approach silently modifies the model's intended input sequence and can cause incorrect evaluation results for models whose chat templates deliberately include BOS.This PR fixes the issue by:
apply_chat_template(tokenize=True)to obtain token IDs directly{"prompt_token_ids": ...}) to vLLMThis preserves the exact token sequence the chat template produces, without any manual modification, and avoids the double-BOS problem entirely.
Motivation
The previous workaround (lines 128-134) had several issues:
prompt_token_ids, which bypasses its internal tokenization entirelyTest plan
apply_chat_templateproduces (no extra/missing BOS)Made with Cursor