-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[Bugfix] Validate logit biases to prevent out of vocab ids crashing engine #16529
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
tests/entrypoints/openai/test_chat_logit_bias_validation.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import openai | ||
| import pytest | ||
| import pytest_asyncio | ||
|
|
||
| from vllm.config import ModelConfig | ||
|
|
||
| from ...utils import RemoteOpenAIServer | ||
|
|
||
| MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct" | ||
|
|
||
|
|
||
| def get_vocab_size(model_name): | ||
| config = ModelConfig( | ||
| model=model_name, | ||
| task="auto", | ||
| tokenizer=model_name, | ||
| tokenizer_mode="auto", | ||
| trust_remote_code=False, | ||
| seed=0, | ||
| dtype="bfloat16", | ||
| ) | ||
| return config.get_vocab_size() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def server(): | ||
| args = [ | ||
| "--dtype", | ||
| "bfloat16", | ||
| "--max-model-len", | ||
| "1024", | ||
| "--enforce-eager", | ||
| ] | ||
|
|
||
| with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: | ||
| yield remote_server | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture | ||
| async def client(server): | ||
| async with server.get_async_client() as async_client: | ||
| yield async_client | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_chat_logit_bias_valid(client): | ||
| """Test that valid logit_bias values are accepted in chat completions.""" | ||
| vocab_size = get_vocab_size(MODEL_NAME) | ||
| valid_token_id = vocab_size - 1 | ||
|
|
||
| completion = await client.chat.completions.create( | ||
| model=MODEL_NAME, | ||
| messages=[{ | ||
| "role": "user", | ||
| "content": "Testing valid logit bias" | ||
| }], | ||
| max_tokens=5, | ||
| logit_bias={str(valid_token_id): 1.0}, | ||
| ) | ||
|
|
||
| assert completion.choices[0].message.content is not None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_chat_logit_bias_invalid(client): | ||
| """Test that invalid logit_bias values are rejected in chat completions.""" | ||
| vocab_size = get_vocab_size(MODEL_NAME) | ||
| invalid_token_id = vocab_size + 1 | ||
|
|
||
| with pytest.raises(openai.BadRequestError) as excinfo: | ||
| await client.chat.completions.create( | ||
| model=MODEL_NAME, | ||
| messages=[{ | ||
| "role": "user", | ||
| "content": "Testing invalid logit bias" | ||
| }], | ||
| max_tokens=5, | ||
| logit_bias={str(invalid_token_id): 1.0}, | ||
| ) | ||
|
|
||
| error = excinfo.value | ||
| error_message = str(error) | ||
|
|
||
| assert error.status_code == 400 | ||
| assert str(invalid_token_id) in error_message | ||
| assert str(vocab_size) in error_message |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @rymc question, why do we need out-of-vocab token id validation inside of the logits bias logits processor? It appears that this PR already added validation within the frontend (in
processor.py); it appears that the_validate_logit_bias()method will be called for every new request that is submitted to both the sync and async engines. Would it be acceptable to remove this logit bias validation from the internals of the logit bias logits processor?(I know this PR is merged already, I'm asking because #16728 emits this out-of-vocab token check from the logit bias logits processor but keeps the frontend
validate_logit_bias()check`)CC @njhill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when I made the above comment I may have missed the fact that this PR adds the validation in both places. Given that, all that's needed is to remove the validation within the sampler / omit it from the vectorized impl that we'll move to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. The reason was because I wasn't confident there wasn't a path that ends up here without going through the frontend processor and I didn't want a crash. Given that it does, I'm happy to submit a new PR with the sampler validation removed. Let me know :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rymc thanks for the response :) most likely the omission of the redundant checks will be accomplished by #16728