[MyPy][3/N] Fix MyPy errors in test groups (part 3) (#55939)
Signed-off-by: Taimys <ffy2281693949@gmail.com> Signed-off-by: Luyi Xiao <81631911+luyixiao95@users.noreply.github.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: Luyi Xiao <81631911+luyixiao95@users.noreply.github.com> Co-authored-by: Taimys <ffy2281693949@gmail.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Codex
Luyi Xiao
Taimys
Harry Mellor
Claude Opus 5.5
parent
c4fa6f36a7
commit
5225f1bacb
@@ -7,22 +7,26 @@ from unittest.mock import Mock
|
||||
import pytest
|
||||
|
||||
from vllm import LLM, SamplingParams
|
||||
from vllm.entrypoints.chat_utils import ChatCompletionMessageParam
|
||||
from vllm.inputs import TextPrompt
|
||||
from vllm.outputs import RequestOutput
|
||||
|
||||
|
||||
def _make_mock_llm() -> LLM:
|
||||
llm = object.__new__(LLM)
|
||||
llm.model_config = SimpleNamespace(
|
||||
llm.model_config = SimpleNamespace( # type: ignore[assignment]
|
||||
runner_type="generate", enable_prompt_embeds=False
|
||||
)
|
||||
return llm
|
||||
|
||||
|
||||
def test_generate_forwards_mm_processor_kwargs() -> None:
|
||||
def test_generate_forwards_mm_processor_kwargs(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"num_crops": 4}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
|
||||
llm._run_completion = Mock(return_value=["ok"])
|
||||
_run_completion = Mock(return_value=["ok"])
|
||||
monkeypatch.setattr(llm, "_run_completion", _run_completion)
|
||||
|
||||
outputs = llm.generate(
|
||||
"prompt",
|
||||
@@ -31,17 +35,18 @@ def test_generate_forwards_mm_processor_kwargs() -> None:
|
||||
)
|
||||
|
||||
assert outputs == ["ok"]
|
||||
assert llm._run_completion.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
assert _run_completion.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_enqueue_forwards_mm_processor_kwargs() -> None:
|
||||
def test_enqueue_forwards_mm_processor_kwargs(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"do_resize": False}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
|
||||
llm._add_completion_requests = Mock(return_value=["req-0"])
|
||||
_add_completion_requests = Mock(return_value=["req-0"])
|
||||
monkeypatch.setattr(llm, "_add_completion_requests", _add_completion_requests)
|
||||
|
||||
request_ids = llm.enqueue(
|
||||
"prompt",
|
||||
@@ -51,18 +56,19 @@ def test_enqueue_forwards_mm_processor_kwargs() -> None:
|
||||
)
|
||||
|
||||
assert request_ids == ["req-0"]
|
||||
assert llm._add_completion_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
assert _add_completion_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_chat_forwards_mm_processor_kwargs() -> None:
|
||||
def test_chat_forwards_mm_processor_kwargs(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"do_pan_and_scan": True}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
messages = [{"role": "user", "content": "hello"}]
|
||||
messages: list[ChatCompletionMessageParam] = [{"role": "user", "content": "hello"}]
|
||||
|
||||
llm._run_chat = Mock(return_value=["ok"])
|
||||
_run_chat = Mock(return_value=["ok"])
|
||||
monkeypatch.setattr(llm, "_run_chat", _run_chat)
|
||||
|
||||
outputs = llm.chat(
|
||||
messages,
|
||||
@@ -71,18 +77,19 @@ def test_chat_forwards_mm_processor_kwargs() -> None:
|
||||
)
|
||||
|
||||
assert outputs == ["ok"]
|
||||
assert llm._run_chat.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
assert _run_chat.call_args.kwargs["mm_processor_kwargs"] == mm_processor_kwargs
|
||||
|
||||
|
||||
def test_enqueue_chat_forwards_mm_processor_kwargs() -> None:
|
||||
def test_enqueue_chat_forwards_mm_processor_kwargs(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"do_pan_and_scan": True}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
messages = [{"role": "user", "content": "hello"}]
|
||||
messages: list[ChatCompletionMessageParam] = [{"role": "user", "content": "hello"}]
|
||||
|
||||
llm._add_chat_requests = Mock(return_value=["req-0"])
|
||||
_add_chat_requests = Mock(return_value=["req-0"])
|
||||
monkeypatch.setattr(llm, "_add_chat_requests", _add_chat_requests)
|
||||
|
||||
request_ids = llm.enqueue_chat(
|
||||
messages,
|
||||
@@ -92,67 +99,79 @@ def test_enqueue_chat_forwards_mm_processor_kwargs() -> None:
|
||||
)
|
||||
|
||||
assert request_ids == ["req-0"]
|
||||
assert llm._add_chat_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
assert _add_chat_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_run_chat_forwards_mm_processor_kwargs() -> None:
|
||||
def test_run_chat_forwards_mm_processor_kwargs(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"num_crops": 8}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
messages = [{"role": "user", "content": "hello"}]
|
||||
messages: list[ChatCompletionMessageParam] = [{"role": "user", "content": "hello"}]
|
||||
sentinel_output = ["done"]
|
||||
|
||||
llm._add_chat_requests = Mock()
|
||||
llm._run_engine = Mock(return_value=sentinel_output)
|
||||
_add_chat_requests = Mock()
|
||||
monkeypatch.setattr(llm, "_add_chat_requests", _add_chat_requests)
|
||||
_run_engine = Mock(return_value=sentinel_output)
|
||||
monkeypatch.setattr(llm, "_run_engine", _run_engine)
|
||||
|
||||
outputs = llm._run_chat(
|
||||
messages=messages,
|
||||
params=sampling_params,
|
||||
output_type=object,
|
||||
output_type=RequestOutput,
|
||||
use_tqdm=False,
|
||||
mm_processor_kwargs=mm_processor_kwargs,
|
||||
)
|
||||
|
||||
assert outputs == sentinel_output
|
||||
assert llm._add_chat_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
assert _add_chat_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_run_completion_forwards_mm_processor_kwargs() -> None:
|
||||
def test_run_completion_forwards_mm_processor_kwargs(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"min_pixels": 4 * 28 * 28}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
sentinel_output = ["done"]
|
||||
|
||||
llm._add_completion_requests = Mock()
|
||||
llm._run_engine = Mock(return_value=sentinel_output)
|
||||
_add_completion_requests = Mock()
|
||||
monkeypatch.setattr(llm, "_add_completion_requests", _add_completion_requests)
|
||||
_run_engine = Mock(return_value=sentinel_output)
|
||||
monkeypatch.setattr(llm, "_run_engine", _run_engine)
|
||||
|
||||
outputs = llm._run_completion(
|
||||
prompts=["prompt"],
|
||||
params=sampling_params,
|
||||
output_type=object,
|
||||
output_type=RequestOutput,
|
||||
use_tqdm=False,
|
||||
mm_processor_kwargs=mm_processor_kwargs,
|
||||
)
|
||||
|
||||
assert outputs == sentinel_output
|
||||
assert llm._add_completion_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
assert _add_completion_requests.call_args.kwargs["mm_processor_kwargs"] == (
|
||||
mm_processor_kwargs
|
||||
)
|
||||
|
||||
|
||||
def test_add_completion_requests_forwards_mm_processor_kwargs() -> None:
|
||||
def test_add_completion_requests_forwards_mm_processor_kwargs(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"max_dynamic_patch": 4}
|
||||
sampling_params = SamplingParams(max_tokens=1)
|
||||
|
||||
llm._params_to_seq = Mock(return_value=[sampling_params])
|
||||
llm._lora_request_to_seq = Mock(return_value=[None])
|
||||
llm._priority_to_seq = Mock(return_value=[0])
|
||||
llm._preprocess_cmpl_one = Mock(return_value={"prompt_token_ids": [1]})
|
||||
_params_to_seq = Mock(return_value=[sampling_params])
|
||||
monkeypatch.setattr(llm, "_params_to_seq", _params_to_seq)
|
||||
_lora_request_to_seq = Mock(return_value=[None])
|
||||
monkeypatch.setattr(llm, "_lora_request_to_seq", _lora_request_to_seq)
|
||||
_priority_to_seq = Mock(return_value=[0])
|
||||
monkeypatch.setattr(llm, "_priority_to_seq", _priority_to_seq)
|
||||
_preprocess_cmpl_one = Mock(return_value={"prompt_token_ids": [1]})
|
||||
monkeypatch.setattr(llm, "_preprocess_cmpl_one", _preprocess_cmpl_one)
|
||||
|
||||
captured_prompts = []
|
||||
|
||||
@@ -160,7 +179,8 @@ def test_add_completion_requests_forwards_mm_processor_kwargs() -> None:
|
||||
captured_prompts.extend(prompts)
|
||||
return ["req-0"]
|
||||
|
||||
llm._render_and_add_requests = Mock(side_effect=fake_render_and_add_requests)
|
||||
_render_and_add_requests = Mock(side_effect=fake_render_and_add_requests)
|
||||
monkeypatch.setattr(llm, "_render_and_add_requests", _render_and_add_requests)
|
||||
|
||||
request_ids = llm._add_completion_requests(
|
||||
prompts=["prompt"],
|
||||
@@ -170,7 +190,7 @@ def test_add_completion_requests_forwards_mm_processor_kwargs() -> None:
|
||||
)
|
||||
|
||||
assert request_ids == ["req-0"]
|
||||
llm._preprocess_cmpl_one.assert_called_once_with(
|
||||
_preprocess_cmpl_one.assert_called_once_with(
|
||||
"prompt",
|
||||
None,
|
||||
mm_processor_kwargs=mm_processor_kwargs,
|
||||
@@ -183,7 +203,7 @@ def test_preprocess_cmpl_applies_mm_processor_kwargs_to_renderer(
|
||||
) -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"num_crops": 8}
|
||||
prompt = {"prompt": "<image>", "multi_modal_data": {"image": object()}}
|
||||
prompt: TextPrompt = {"prompt": "<image>", "multi_modal_data": {"image": object()}}
|
||||
|
||||
renderer = Mock()
|
||||
renderer.default_cmpl_tok_params = Mock()
|
||||
@@ -213,7 +233,7 @@ def test_preprocess_cmpl_keeps_prompt_mm_processor_kwargs_when_no_override(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
llm = _make_mock_llm()
|
||||
prompt = {
|
||||
prompt: TextPrompt = {
|
||||
"prompt": "<image>",
|
||||
"multi_modal_data": {"image": object()},
|
||||
"mm_processor_kwargs": {"num_crops": 2},
|
||||
@@ -243,7 +263,9 @@ def test_preprocess_cmpl_keeps_prompt_mm_processor_kwargs_when_no_override(
|
||||
def test_preprocess_chat_applies_mm_processor_kwargs_to_renderer() -> None:
|
||||
llm = _make_mock_llm()
|
||||
mm_processor_kwargs = {"num_crops": 8}
|
||||
messages = [[{"role": "user", "content": "Describe this image."}]]
|
||||
messages: list[list[ChatCompletionMessageParam]] = [
|
||||
[{"role": "user", "content": "Describe this image."}]
|
||||
]
|
||||
|
||||
renderer = Mock()
|
||||
renderer.tokenizer = object()
|
||||
@@ -269,7 +291,9 @@ def test_preprocess_chat_applies_mm_processor_kwargs_to_renderer() -> None:
|
||||
|
||||
def test_preprocess_chat_omits_mm_processor_kwargs_when_no_override() -> None:
|
||||
llm = _make_mock_llm()
|
||||
messages = [[{"role": "user", "content": "Describe this image."}]]
|
||||
messages: list[list[ChatCompletionMessageParam]] = [
|
||||
[{"role": "user", "content": "Describe this image."}]
|
||||
]
|
||||
|
||||
renderer = Mock()
|
||||
renderer.tokenizer = object()
|
||||
@@ -291,7 +315,9 @@ def test_preprocess_chat_omits_mm_processor_kwargs_when_no_override() -> None:
|
||||
def test_preprocess_chat_defaults_add_special_tokens_to_false() -> None:
|
||||
# Matches `ChatCompletionRequest.add_special_tokens` on the server.
|
||||
llm = _make_mock_llm()
|
||||
messages = [[{"role": "user", "content": "hi"}]]
|
||||
messages: list[list[ChatCompletionMessageParam]] = [
|
||||
[{"role": "user", "content": "hi"}]
|
||||
]
|
||||
|
||||
renderer = Mock()
|
||||
renderer.tokenizer = object()
|
||||
@@ -309,7 +335,9 @@ def test_preprocess_chat_defaults_add_special_tokens_to_false() -> None:
|
||||
|
||||
def test_preprocess_chat_tokenization_kwargs_override_add_special_tokens() -> None:
|
||||
llm = _make_mock_llm()
|
||||
messages = [[{"role": "user", "content": "hi"}]]
|
||||
messages: list[list[ChatCompletionMessageParam]] = [
|
||||
[{"role": "user", "content": "hi"}]
|
||||
]
|
||||
|
||||
renderer = Mock()
|
||||
renderer.tokenizer = object()
|
||||
|
||||
@@ -47,18 +47,20 @@ async def client(server):
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def base64_encoded_audio() -> dict[str, str]:
|
||||
return {
|
||||
audio_url: encode_audio_base64(*fetch_audio(audio_url))
|
||||
for audio_url in TEST_AUDIO_URLS
|
||||
}
|
||||
encoded = {}
|
||||
for audio_url in TEST_AUDIO_URLS:
|
||||
audio, sample_rate = fetch_audio(audio_url)
|
||||
encoded[audio_url] = encode_audio_base64(audio, int(sample_rate))
|
||||
return encoded
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def url_encoded_audio() -> dict[str, str]:
|
||||
return {
|
||||
audio_url: encode_audio_url(*fetch_audio(audio_url))
|
||||
for audio_url in TEST_AUDIO_URLS
|
||||
}
|
||||
encoded = {}
|
||||
for audio_url in TEST_AUDIO_URLS:
|
||||
audio, sample_rate = fetch_audio(audio_url)
|
||||
encoded[audio_url] = encode_audio_url(audio, int(sample_rate))
|
||||
return encoded
|
||||
|
||||
|
||||
def dummy_messages_from_audio_url(
|
||||
|
||||
Reference in New Issue
Block a user