[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:
Taimys
2026-09-30 11:06:07 +00:00
committed by GitHub
co-authored by Codex Luyi Xiao Taimys Harry Mellor Claude Opus 5.5
parent c4fa6f36a7
commit 5225f1bacb
118 changed files with 948 additions and 605 deletions
@@ -341,6 +341,7 @@ def test_batch_allowed_needs_one_shared_set():
assert states.batch_allowed([]) is None
shared = states.batch_allowed([0])
assert shared is not None
assert shared.tolist() == [1, 2, 3]
assert shared.dtype == torch.int64
assert states.batch_allowed([0, 0]) is shared
@@ -34,6 +34,8 @@ def test_stats_match_reference(vocab: int, dtype: torch.dtype):
)
assert torch.equal(argmax, ref_argmax)
torch.testing.assert_close(entropy, ref_entropy, atol=2e-3, rtol=1e-3)
assert probs is not None
assert ref_probs is not None
torch.testing.assert_close(probs.float(), ref_probs.float(), atol=5e-3, rtol=2e-2)
assert probs.dtype == torch.bfloat16
assert probs.shape == logits.shape
@@ -100,6 +102,7 @@ def test_masked_logits_keep_a_finite_entropy():
assert entropy[0].item() < 1e-6 and entropy[1].item() < 1e-6
assert abs(entropy[2].item() - math.log(2.0)) < 1e-4
assert torch.equal(argmax[[0, 1, 3]], torch.tensor([3, 3, 3], device="cuda"))
assert probs is not None
assert probs[0, 3].item() == 1.0 and probs[0].sum().item() == 1.0
_, _, ref_entropy, _ = sample_row_stats_reference(logits, temps, 1, None)
assert torch.isfinite(ref_entropy).all()
+6 -3
View File
@@ -340,7 +340,7 @@ def _make_v2_state(
device=device,
all_token_ids=SimpleNamespace(gpu=all_tokens),
)
state = DryState(_make_vllm_config(), req_states)
state = DryState(_make_vllm_config(), req_states) # type: ignore[arg-type]
if tokenizer is not None:
# Re-resolve the default set now that a tokenizer is installed.
state._tokenizer = tokenizer
@@ -446,7 +446,7 @@ def test_spec_decode_is_refused_at_construction():
)
config = _make_vllm_config(speculative_config=SimpleNamespace())
with pytest.raises(ValueError, match="speculative decoding"):
DryState(config, req_states)
DryState(config, req_states) # type: ignore[arg-type]
def test_v2_draft_expanded_logits_raise():
@@ -917,7 +917,10 @@ def test_loads_through_the_loader_by_fqcn():
total_len=None,
)
procs = loader.build_custom_logits_processors(
_make_vllm_config(), req_states, False, [DRY_FQCN]
_make_vllm_config(),
req_states, # type: ignore[arg-type]
False,
[DRY_FQCN],
)
assert [type(p) for p in procs] == [DryState]
+21 -7
View File
@@ -16,6 +16,7 @@ from vllm.model_executor.layers.logits_processor import LogitsProcessor
from vllm.model_executor.layers.vocab_parallel_embedding import (
ParallelLMHead,
UnquantizedEmbeddingMethod,
VocabParallelEmbedding,
)
@@ -32,10 +33,17 @@ class _FakeLmHead:
self.tp_size = 1
def _fake_lm_head(
weight: torch.Tensor, quantized: bool = False, shard_indices: object | None = None
) -> VocabParallelEmbedding:
# The dtype tests only need the projection fields, not distributed setup.
return _FakeLmHead(weight, quantized, shard_indices) # type: ignore[return-value]
def _build_processor(vocab_size: int) -> LogitsProcessor:
lp = LogitsProcessor(vocab_size)
# The TP gather is orthogonal to the dtype behavior under test.
lp._gather_logits = lambda logits: logits
lp._gather_logits = lambda logits: logits # type: ignore[method-assign]
return lp
@@ -47,8 +55,9 @@ def test_fp32_head_runs_projection_in_fp32(default_vllm_config):
hidden_states = torch.randn(num_tokens, hidden_size, dtype=torch.bfloat16)
weight = torch.randn(vocab_size, hidden_size, dtype=torch.bfloat16)
logits = lp._get_logits(hidden_states, _FakeLmHead(weight), None)
logits = lp._get_logits(hidden_states, _fake_lm_head(weight), None)
assert logits is not None
assert logits.dtype == torch.float32
assert torch.isfinite(logits).all()
expected = torch.nn.functional.linear(hidden_states.float(), weight.float())
@@ -66,8 +75,9 @@ def test_non_fp32_head_dtype_uses_cast_path(default_vllm_config):
hidden_states = torch.randn(4, hidden_size, dtype=torch.bfloat16)
weight = torch.randn(vocab_size, hidden_size, dtype=torch.bfloat16)
logits = lp._get_logits(hidden_states, _FakeLmHead(weight), None)
logits = lp._get_logits(hidden_states, _fake_lm_head(weight), None)
assert logits is not None
assert logits.dtype == torch.float16
expected = torch.nn.functional.linear(hidden_states.half(), weight.half())
torch.testing.assert_close(logits, expected)
@@ -82,7 +92,7 @@ def test_head_dtype_equal_to_model_dtype_uses_quant_method(default_vllm_config):
hidden_states = torch.randn(4, hidden_size, dtype=torch.bfloat16)
weight = torch.randn(vocab_size, hidden_size, dtype=torch.bfloat16)
lm_head = _FakeLmHead(weight)
lm_head = _fake_lm_head(weight)
with mock.patch.object(
lm_head.quant_method,
@@ -94,6 +104,7 @@ def test_head_dtype_equal_to_model_dtype_uses_quant_method(default_vllm_config):
logits = lp._get_logits(hidden_states, lm_head, None)
apply_mock.assert_called_once()
assert logits is not None
assert logits.dtype == torch.bfloat16
@@ -120,9 +131,10 @@ def test_fp32_head_uses_mm_fast_path_on_device(default_vllm_config):
with mock.patch(
"vllm.model_executor.layers.logits_processor.F.linear"
) as linear_mock:
logits = lp._get_logits(hidden_states, _FakeLmHead(weight), None)
logits = lp._get_logits(hidden_states, _fake_lm_head(weight), None)
linear_mock.assert_not_called()
assert logits is not None
assert logits.dtype == torch.float32
expected = torch.nn.functional.linear(hidden_states.float(), weight.float())
torch.testing.assert_close(logits, expected)
@@ -131,7 +143,7 @@ def test_fp32_head_uses_mm_fast_path_on_device(default_vllm_config):
def test_fp32_head_rejects_quantized_lm_head(default_vllm_config):
lp = _build_processor(64)
lp.head_dtype = torch.float32
lm_head = _FakeLmHead(torch.randn(64, 16, dtype=torch.bfloat16), quantized=True)
lm_head = _fake_lm_head(torch.randn(64, 16, dtype=torch.bfloat16), quantized=True)
with pytest.raises(ValueError, match="unquantized"):
lp._get_logits(torch.randn(4, 16, dtype=torch.bfloat16), lm_head, None)
@@ -197,7 +209,7 @@ def test_get_top_tokens_honors_head_dtype(default_vllm_config):
hidden_states = torch.randn(4, hidden_size, dtype=torch.bfloat16)
weight = torch.randn(vocab_size, hidden_size, dtype=torch.bfloat16)
lm_head = _FakeLmHead(
lm_head = _fake_lm_head(
weight,
shard_indices=types.SimpleNamespace(
num_org_vocab_padding=0, org_vocab_start_index=0
@@ -236,7 +248,9 @@ def test_fp32_head_e2e_no_nan():
for output in outputs:
for completion in output.outputs:
assert completion.logprobs is not None
for token_id, position in zip(completion.token_ids, completion.logprobs):
assert position is not None
# The sampled token survived filtering, so its logprob is finite.
assert math.isfinite(position[token_id].logprob)
# No returned logprob is NaN.
+33 -17
View File
@@ -24,6 +24,7 @@ from vllm import SamplingParams
from vllm.config.model import LogprobsMode
from vllm.distributed import cleanup_dist_env_and_memory
from vllm.exceptions import VLLMValidationError
from vllm.logprobs import Logprob
from vllm.platforms import current_platform
from vllm.v1.engine.input_processor import InputProcessor
@@ -145,7 +146,7 @@ def _repeat_logprob_config(
def _run_and_validate(
vllm_model: VllmRunner,
test_prompts: list[str],
vllm_sampling_params: SamplingParams,
vllm_sampling_params: SamplingParams | list[SamplingParams],
hf_logprobs: list[list[torch.Tensor]],
hf_outputs: list[tuple[list[int], str]],
logprob_prompt_logprob_list: BatchLogprobsSpecType,
@@ -163,11 +164,12 @@ def _run_and_validate(
# Extract request-level (prompt)logprobs config
num_top_logprobs, num_top_prompt_logprobs = logprob_prompt_logprob
assert vllm_result.prompt_token_ids is not None
# Test whether sampled token output is consistent between vLLM and HF
# vLLM prompt+completion should match HF output
if temperature == 0.0:
assert (
vllm_result.prompt_token_ids + vllm_result.outputs[0].token_ids
vllm_result.prompt_token_ids + list(vllm_result.outputs[0].token_ids)
== hf_output[0]
)
else:
@@ -191,7 +193,9 @@ def _run_and_validate(
# Confirm that the output token appears among the logprobs
assert token_id in logprobs
token_in_topk = logprobs[token_id].rank <= num_top_logprobs
rank = logprobs[token_id].rank
assert rank is not None
token_in_topk = rank <= num_top_logprobs
# If the output token is not included in the top K
# logprob, it can return 1 more data
@@ -207,8 +211,10 @@ def _run_and_validate(
output_text = vllm_result.outputs[0].text
output_string_from_most_likely_tokens_lst: list[str] = []
for top_logprobs in vllm_result.outputs[0].logprobs:
top_logprob = next(iter(top_logprobs.values()))
for output_logprobs in vllm_result.outputs[0].logprobs:
assert output_logprobs is not None
top_logprob = next(iter(output_logprobs.values()))
assert top_logprob.decoded_token is not None
output_string_from_most_likely_tokens_lst.append(
top_logprob.decoded_token
)
@@ -226,8 +232,9 @@ def _run_and_validate(
# Compare vLLM sample logprobs to HF
vllm_sample_logprobs = vllm_result.outputs[0].logprobs
for i, top_logprobs in enumerate(vllm_sample_logprobs):
for token_id, sample_logprob in top_logprobs.items():
for i, sample_logprobs in enumerate(vllm_sample_logprobs):
assert sample_logprobs is not None
for token_id, sample_logprob in sample_logprobs.items():
if temperature == 0.0 or i == 0:
logprob = sample_logprob.logprob
torch.testing.assert_close(
@@ -271,9 +278,9 @@ def _run_and_validate(
# Confirm that the prompt token appears among the logprobs
assert prompt_token_id in prompt_logprobs
token_in_topk = (
prompt_logprobs[prompt_token_id].rank <= num_top_prompt_logprobs
)
prompt_rank = prompt_logprobs[prompt_token_id].rank
assert prompt_rank is not None
token_in_topk = prompt_rank <= num_top_prompt_logprobs
# If the prompt token is not included in the top K
# logprob, it can return 1 more data
@@ -294,9 +301,10 @@ def _run_and_validate(
# 1:.
vllm_prompt_logprobs = vllm_result.prompt_logprobs[1:]
for i, vllm_prompt_logprob_dict in enumerate(vllm_prompt_logprobs):
for token_id, logprob in vllm_prompt_logprob_dict.items():
assert vllm_prompt_logprob_dict is not None
for token_id, prompt_lp in vllm_prompt_logprob_dict.items():
torch.testing.assert_close(
logprob.logprob,
prompt_lp.logprob,
hf_logprob[0][i][token_id].item(),
atol=2e-2,
rtol=2e-2,
@@ -478,12 +486,12 @@ def test_prompt_logprob_token_ids_require_v2_model_runner():
params = SamplingParams(prompt_logprob_token_ids=[1, 2])
with patch.object(SamplingParams, "verify"):
with pytest.raises(VLLMValidationError, match="V2 model runner"):
InputProcessor._validate_params(processor, params, ("generate",))
InputProcessor._validate_params(processor, params, ("generate",)) # type: ignore[arg-type]
processor.vllm_config.use_v2_model_runner = True
InputProcessor._validate_params(processor, params, ("generate",))
InputProcessor._validate_params(processor, params, ("generate",)) # type: ignore[arg-type]
processor.vllm_config.cache_config.kv_sharing_fast_prefill = True
with pytest.raises(VLLMValidationError, match="fast-prefill"):
InputProcessor._validate_params(processor, params, ("generate",))
InputProcessor._validate_params(processor, params, ("generate",)) # type: ignore[arg-type]
def test_none_logprobs(vllm_model, example_prompts):
@@ -607,7 +615,9 @@ def test_logprobs_mode(logprobs_mode: LogprobsMode):
total_token_with_logprobs = 0
positive_values = 0
for output in results[0].outputs:
assert output.logprobs is not None
for logprobs in output.logprobs:
assert logprobs is not None
for token_id in logprobs:
logprob = logprobs[token_id]
if logprobs_mode in ("raw_logprobs", "processed_logprobs"):
@@ -648,6 +658,7 @@ def test_prompt_logprobs_mode():
)
assert results[0].prompt_logprobs is not None
assert results[0].prompt_logprobs[1] is not None
assert results[0].prompt_token_ids is not None
tok_id = results[0].prompt_token_ids[1]
values[mode] = results[0].prompt_logprobs[1][tok_id].logprob
finally:
@@ -1228,10 +1239,12 @@ def test_spec_decode_logprobs(
[prompt, prompt], [sampling_params, penalty_sampling_params]
)
# Collect logprobs outputs from reference LLM.
ref_logprobs = []
ref_logprobs: list[Logprob] = []
for results in ref_results:
for output in results.outputs:
assert output.logprobs is not None
for logprobs in output.logprobs:
assert logprobs is not None
ref_logprobs.extend(logprobs.values())
del ref_llm
torch.accelerator.empty_cache()
@@ -1249,10 +1262,12 @@ def test_spec_decode_logprobs(
[prompt, prompt], [sampling_params, penalty_sampling_params]
)
# Collect logprobs outputs from spec decode LLM.
spec_logprobs = []
spec_logprobs: list[Logprob] = []
for results in spec_results:
for output in results.outputs:
assert output.logprobs is not None
for logprobs in output.logprobs:
assert logprobs is not None
spec_logprobs.extend(logprobs.values())
del spec_llm
torch.accelerator.empty_cache()
@@ -1326,6 +1341,7 @@ def test_prompt_logprobs_with_chunking_and_preemption():
"Unexpected number of prompt logprob positions"
)
assert sampling_params.prompt_logprobs is not None
# Each position should have the requested number of logprobs
for pos, logprobs_dict in enumerate(prompt_logprobs):
if logprobs_dict is not None: # First token may be None
+1
View File
@@ -333,6 +333,7 @@ def test_sampler_repetition_penalty(
for batch_idx in range(batch_size):
non_penalized_token_id = logits[batch_idx].argmax().item()
penalized_token_id = logits[batch_idx].argmin().item()
assert sampling_metadata.prompt_token_ids is not None
prompt_tokens = sampling_metadata.prompt_token_ids[batch_idx][:].tolist()
output_tokens = sampling_metadata.output_token_ids[batch_idx]
if repetition_penalty > 1.0:
@@ -4,6 +4,7 @@
import torch
from tests.v1.sample.utils import create_mock_reasoning_config
from vllm.sampling_params import SamplingParams
from vllm.v1.sample.logits_processor.interface import (
BatchUpdate,
@@ -12,14 +13,9 @@ from vllm.v1.sample.logits_processor.interface import (
from vllm.v1.sample.thinking_budget_state import ThinkingBudgetStateHolder
class _MockReasoningConfig:
reasoning_start_token_ids = [151667]
reasoning_end_token_ids = [151668]
def _make_holder() -> ThinkingBudgetStateHolder:
return ThinkingBudgetStateHolder(
_MockReasoningConfig(),
create_mock_reasoning_config([151667], [151668]),
8,
0,
torch.device("cpu"),
+2 -1
View File
@@ -104,7 +104,8 @@ def test_trace_decode_token_ids_rejects_speculative_decoding():
params = SamplingParams(trace_decode_token_ids=[1])
with pytest.raises(ValueError, match="not supported with speculative decoding"):
params._validate_trace_replay(
_make_model_config(vocab_size=100), speculative_config=object()
_make_model_config(vocab_size=100),
speculative_config=object(), # type: ignore[arg-type] # Only non-None presence is tested.
)
+17 -1
View File
@@ -4,16 +4,28 @@
from collections.abc import Iterator
from enum import Enum
from typing import NamedTuple
from unittest.mock import MagicMock
import regex as re
import torch
from vllm import CompletionOutput
from vllm.config import ReasoningConfig
from vllm.utils.torch_utils import make_tensor_with_pad
from vllm.v1.sample.logits_processor import BatchUpdate, LogitsProcessor
from vllm.v1.sample.metadata import SamplingMetadata
def create_mock_reasoning_config(
start_token_ids: list[int], end_token_ids: list[int]
) -> ReasoningConfig:
return MagicMock(
spec=ReasoningConfig,
reasoning_start_token_ids=start_token_ids,
reasoning_end_token_ids=end_token_ids,
)
class BatchLogprobsComposition(Enum):
"""Types of logprobs configs to include in test batch."""
@@ -141,7 +153,11 @@ def compute_correct_cumulative_logprob(completion_output: CompletionOutput) -> f
token_ids = completion_output.token_ids
logprobs = completion_output.logprobs
assert logprobs is not None
return sum([lp[tok_id].logprob for tok_id, lp in zip(token_ids, logprobs)])
values: list[float] = []
for tok_id, lp in zip(token_ids, logprobs):
assert lp is not None
values.append(lp[tok_id].logprob)
return sum(values)
def create_fake_logits(batch_size: int, vocab_size: int) -> torch.Tensor: