-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[BugFix] Fix handling of stop strings and stop token ids #3672
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
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
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
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,111 @@ | ||
| from typing import Any, List, Optional | ||
|
|
||
| import pytest | ||
|
|
||
| from vllm import CompletionOutput, LLMEngine, SamplingParams | ||
|
|
||
| MODEL = "meta-llama/llama-2-7b-hf" | ||
| MAX_TOKENS = 200 | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def vllm_model(vllm_runner): | ||
| return vllm_runner(MODEL) | ||
|
|
||
|
|
||
| @pytest.mark.skip_global_cleanup | ||
| def test_stop_basic(vllm_model): | ||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop=["."], | ||
| include_in_output=False, | ||
| expected_output="VLLM is a 100% volunteer organization", | ||
| expected_reason=".") | ||
|
|
||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop=["."], | ||
| include_in_output=True, | ||
| expected_output="VLLM is a 100% volunteer organization.", | ||
| expected_reason=".") | ||
|
|
||
|
|
||
| @pytest.mark.skip_global_cleanup | ||
| def test_stop_multi_tokens(vllm_model): | ||
| _test_stopping( | ||
| vllm_model.model.llm_engine, | ||
| stop=["group of peo", "short"], | ||
| include_in_output=False, | ||
| expected_output="VLLM is a 100% volunteer organization. We are a ", | ||
| expected_reason="group of peo") | ||
|
|
||
| _test_stopping( | ||
| vllm_model.model.llm_engine, | ||
| stop=["group of peo", "short"], | ||
| include_in_output=True, | ||
| expected_output= | ||
| "VLLM is a 100% volunteer organization. We are a group of peo", | ||
| expected_reason="group of peo") | ||
|
|
||
|
|
||
| @pytest.mark.skip_global_cleanup | ||
| def test_stop_partial_token(vllm_model): | ||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop=["gani"], | ||
| include_in_output=False, | ||
| expected_output="VLLM is a 100% volunteer or", | ||
| expected_reason="gani") | ||
|
|
||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop=["gani"], | ||
| include_in_output=True, | ||
| expected_output="VLLM is a 100% volunteer organi", | ||
| expected_reason="gani") | ||
|
|
||
|
|
||
| @pytest.mark.skip_global_cleanup | ||
| def test_stop_token_id(vllm_model): | ||
| # token id 13013 => " organization" | ||
|
|
||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop_token_ids=[13013], | ||
| include_in_output=False, | ||
| expected_output="VLLM is a 100% volunteer", | ||
| expected_reason=13013) | ||
|
|
||
| _test_stopping(vllm_model.model.llm_engine, | ||
| stop_token_ids=[13013], | ||
| include_in_output=True, | ||
| expected_output="VLLM is a 100% volunteer organization", | ||
| expected_reason=13013) | ||
|
|
||
|
|
||
| def _test_stopping(llm_engine: LLMEngine, | ||
| expected_output: str, | ||
| expected_reason: Any, | ||
| stop: Optional[List[str]] = None, | ||
| stop_token_ids: Optional[List[int]] = None, | ||
| include_in_output: bool = False) -> None: | ||
| llm_engine.add_request( | ||
| "id", "A story about vLLM:\n", | ||
| SamplingParams( | ||
| temperature=0.0, | ||
| max_tokens=MAX_TOKENS, | ||
| stop=stop, | ||
| stop_token_ids=stop_token_ids, | ||
| include_stop_str_in_output=include_in_output, | ||
| ), None) | ||
|
|
||
| output: Optional[CompletionOutput] = None | ||
| output_text = "" | ||
| stop_reason = None | ||
| while llm_engine.has_unfinished_requests(): | ||
| (request_output, ) = llm_engine.step() | ||
| (output, ) = request_output.outputs | ||
|
|
||
| # Ensure we don't backtrack | ||
| assert output.text.startswith(output_text) | ||
| output_text = output.text | ||
| stop_reason = output.stop_reason | ||
|
|
||
| assert output is not None | ||
| assert output_text == expected_output | ||
| assert stop_reason == expected_reason |
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
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
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.
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.
Do we want to set seq.stop_reason to eos_token_id here?
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.
@sroy745 it's a good question. It was decided to keep the
stop_reasonasNonein this case so that clients can know that it's due to EOS in simple cases without having to know the token ids. See discussion in #2976.