-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
[Refactor] Introduce basic Renderer for completion-style request #24010
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
+416
−27
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b0ad3f
Add renderer
sfeng33 e625e49
pre-commit
sfeng33 42fe605
pre-commit
sfeng33 5b45282
address comments
sfeng33 2ddd111
Address comments on abstract class
sfeng33 9be7350
Rename class
sfeng33 5a39600
Add mixed input validation
sfeng33 4630edb
Pre commit
sfeng33 5c6913a
Pre commit
sfeng33 b7909d5
remove assert
sfeng33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
from dataclasses import dataclass | ||
from typing import Optional | ||
from unittest.mock import AsyncMock, MagicMock | ||
|
||
import pytest | ||
|
||
from vllm.entrypoints.renderer import CompletionRenderer | ||
|
||
|
||
@dataclass | ||
class MockModelConfig: | ||
max_model_len: int = 100 | ||
encoder_config: Optional[dict] = None | ||
|
||
|
||
class MockTokenizerResult: | ||
|
||
def __init__(self, input_ids): | ||
self.input_ids = input_ids | ||
|
||
|
||
@pytest.fixture | ||
def mock_model_config(): | ||
return MockModelConfig() | ||
|
||
|
||
@pytest.fixture | ||
def mock_tokenizer(): | ||
tokenizer = MagicMock() | ||
return tokenizer | ||
|
||
|
||
@pytest.fixture | ||
def mock_async_tokenizer(): | ||
async_tokenizer = AsyncMock() | ||
return async_tokenizer | ||
|
||
|
||
@pytest.fixture | ||
def renderer(mock_model_config, mock_tokenizer): | ||
return CompletionRenderer(model_config=mock_model_config, | ||
tokenizer=mock_tokenizer, | ||
async_tokenizer_pool={}) | ||
|
||
|
||
class TestRenderPrompt: | ||
"""Test Category A: Basic Functionality Tests""" | ||
|
||
@pytest.mark.asyncio | ||
async def test_token_input(self, renderer): | ||
tokens = [101, 7592, 2088] | ||
results = await renderer.render_prompt(prompt_or_prompts=tokens, | ||
max_length=100) | ||
|
||
assert len(results) == 1 | ||
assert results[0]["prompt_token_ids"] == tokens | ||
|
||
@pytest.mark.asyncio | ||
async def test_token_list_input(self, renderer): | ||
token_lists = [[101, 7592, 2088], [102, 1234, 5678, 9012], [103, 4567]] | ||
results = await renderer.render_prompt(prompt_or_prompts=token_lists, | ||
max_length=100) | ||
|
||
assert len(results) == 3 | ||
assert results[0]["prompt_token_ids"] == [101, 7592, 2088] | ||
assert results[1]["prompt_token_ids"] == [102, 1234, 5678, 9012] | ||
assert results[2]["prompt_token_ids"] == [103, 4567] | ||
|
||
@pytest.mark.asyncio | ||
async def test_text_input(self, renderer, mock_async_tokenizer): | ||
mock_async_tokenizer.return_value = MockTokenizerResult( | ||
[101, 7592, 2088]) | ||
renderer.async_tokenizer_pool[ | ||
renderer.tokenizer] = mock_async_tokenizer | ||
|
||
results = await renderer.render_prompt(prompt_or_prompts="Hello world", | ||
max_length=100) | ||
|
||
assert len(results) == 1 | ||
assert results[0]["prompt_token_ids"] == [101, 7592, 2088] | ||
mock_async_tokenizer.assert_called_once() | ||
|
||
@pytest.mark.asyncio | ||
async def test_text_list_input(self, renderer, mock_async_tokenizer): | ||
mock_async_tokenizer.return_value = MockTokenizerResult( | ||
[101, 7592, 2088]) | ||
renderer.async_tokenizer_pool[ | ||
renderer.tokenizer] = mock_async_tokenizer | ||
|
||
text_list_input = ["Hello world", "How are you?", "Good morning"] | ||
results = await renderer.render_prompt( | ||
prompt_or_prompts=text_list_input, max_length=100) | ||
|
||
assert len(results) == 3 | ||
for result in results: | ||
assert result["prompt_token_ids"] == [101, 7592, 2088] | ||
assert mock_async_tokenizer.call_count == 3 | ||
|
||
@pytest.mark.asyncio | ||
async def test_no_truncation(self, renderer, mock_async_tokenizer): | ||
mock_async_tokenizer.return_value = MockTokenizerResult( | ||
[101, 7592, 2088]) | ||
renderer.async_tokenizer_pool[ | ||
renderer.tokenizer] = mock_async_tokenizer | ||
|
||
results = await renderer.render_prompt(prompt_or_prompts="Hello world", | ||
max_length=100) | ||
|
||
assert len(results) == 1 | ||
call_args = mock_async_tokenizer.call_args | ||
assert "truncation" not in call_args.kwargs or call_args.kwargs[ | ||
"truncation"] is False | ||
|
||
@pytest.mark.asyncio | ||
async def test_truncation_positive(self, renderer, mock_async_tokenizer): | ||
mock_async_tokenizer.return_value = MockTokenizerResult( | ||
[101, 7592, 2088]) # Truncated | ||
renderer.async_tokenizer_pool[ | ||
renderer.tokenizer] = mock_async_tokenizer | ||
|
||
results = await renderer.render_prompt(prompt_or_prompts="Hello world", | ||
max_length=100, | ||
truncate_prompt_tokens=50) | ||
|
||
assert len(results) == 1 | ||
call_args = mock_async_tokenizer.call_args | ||
assert call_args.kwargs["truncation"] is True | ||
assert call_args.kwargs["max_length"] == 50 | ||
|
||
@pytest.mark.asyncio | ||
async def test_token_truncation_last_elements(self, renderer): | ||
# Test that token truncation keeps the last N elements | ||
long_tokens = [100, 101, 102, 103, 104, 105, 106, 107, 108, | ||
109] # 10 tokens | ||
results = await renderer.render_prompt(prompt_or_prompts=long_tokens, | ||
max_length=100, | ||
truncate_prompt_tokens=5) | ||
|
||
assert len(results) == 1 | ||
# Should keep the last 5 tokens: [105, 106, 107, 108, 109] | ||
assert results[0]["prompt_token_ids"] == [105, 106, 107, 108, 109] | ||
|
||
@pytest.mark.asyncio | ||
async def test_max_length_exceeded(self, renderer): | ||
long_tokens = list(range(150)) # Exceeds max_model_len=100 | ||
|
||
with pytest.raises(ValueError, match="maximum context length"): | ||
await renderer.render_prompt(prompt_or_prompts=long_tokens, | ||
max_length=100) | ||
|
||
@pytest.mark.asyncio | ||
async def test_no_tokenizer_for_text(self, mock_model_config): | ||
renderer_no_tokenizer = CompletionRenderer( | ||
model_config=mock_model_config, | ||
tokenizer=None, | ||
async_tokenizer_pool={}) | ||
|
||
with pytest.raises(ValueError, match="No tokenizer available"): | ||
await renderer_no_tokenizer.render_prompt( | ||
prompt_or_prompts="Hello world", max_length=100) |
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
Oops, something went wrong.
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.