Add pydocstyle to the ruff rules (#52136)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
Signed-off-by: Harry Mellor <hej.mellor@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Harry Mellor
2026-09-16 16:44:29 +00:00
committed by GitHub
co-authored by Claude Opus 5
parent 804e5377aa
commit 8c1557a79c
2002 changed files with 15024 additions and 14269 deletions
@@ -1,7 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Test that batched_count_greater_than does not trigger 0/1 specialization
recompiles when batch_size varies."""
recompiles when batch_size varies.
"""
import torch
+13 -7
View File
@@ -127,6 +127,7 @@ def _repeat_logprob_config(
`logprob_prompt_logprob_list` enough times to match the
number of `test_prompts`, or else is truncated to match
the number of `test_prompts`
"""
num_test_prompts = len(test_prompts)
# Make sure there is a logprobs configuration for each test prompt
@@ -312,7 +313,7 @@ def test_get_logprobs_and_prompt_logprobs(
temperature: float,
example_prompts: list[str],
) -> None:
"""Test V1 Engine logprobs & prompt logprobs
"""Test V1 Engine logprobs & prompt logprobs.
Exercise a variety of combinations of `logprobs` and `prompt_logprobs`
settings and validate that
@@ -337,6 +338,7 @@ def test_get_logprobs_and_prompt_logprobs(
batch_logprobs_composition: logprobs configuration for test batch
temperature: "temperature" sampling parameter
example_prompts: example prompt fixture
"""
vllm_config = vllm_model.llm.llm_engine.vllm_config
do_apc = vllm_config.cache_config.enable_prefix_caching
@@ -389,7 +391,7 @@ def test_get_logprobs_and_prompt_logprobs(
def test_max_logprobs():
"""vLLM v1 engine should fail a request with `logprobs > max_logprobs`
"""VLLM v1 engine should fail a request with `logprobs > max_logprobs`
Should also fail for `prompt_logprobs > max_logprobs`
APC should not matter as this test checks basic request validation.
"""
@@ -431,11 +433,12 @@ def test_logprob_token_ids_validate_vocab_bounds_invalid(token_ids: list[int]):
def test_none_logprobs(vllm_model, example_prompts):
"""Engine should return `logprobs` and `prompt_logprobs` as `None`
"""Engine should return `logprobs` and `prompt_logprobs` as `None`.
Args:
vllm_model: vLLM model fixture
example_prompts: list of example prompts (test fixture)
"""
max_tokens = 5
@@ -459,11 +462,12 @@ def test_none_logprobs(vllm_model, example_prompts):
def test_zero_logprobs(vllm_model, example_prompts):
"""Engine should return sampled token and prompt token logprobs
"""Engine should return sampled token and prompt token logprobs.
Args:
vllm_model: vLLM model fixture
example_prompts: list of example prompts (test fixture)
"""
max_tokens = 5
@@ -491,10 +495,11 @@ def test_zero_logprobs(vllm_model, example_prompts):
def test_all_logprobs(example_prompts):
"""Engine should return all vocabulary logprobs and prompt logprobs
"""Engine should return all vocabulary logprobs and prompt logprobs.
Args:
example_prompts: list of example prompts (test fixture)
"""
with VllmRunner(
"facebook/opt-125m",
@@ -568,7 +573,8 @@ def test_logprobs_mode(logprobs_mode: LogprobsMode):
def test_prompt_logprobs_mode():
"""prompt_logprobs must respect logprobs_mode: *_logits and *_logprobs
must return different values. Prompt tokens skip sampling processors,
so processed_* == raw_* on the prompt side."""
so processed_* == raw_* on the prompt side.
"""
from vllm import LLM
values: dict[str, float] = {}
@@ -1127,6 +1133,7 @@ def test_spec_decode_logprobs(
logprobs_mode: logprobs mode.
model_setup: Tuple of (method, base model name,
speculative_config dict, top_logprobs).
"""
from vllm import LLM
@@ -1224,7 +1231,6 @@ def test_prompt_logprobs_with_chunking_and_preemption():
This test ensures that the num_prompt_logprobs tracking persists
across preemptions and prefill chunks.
"""
# Create prompts that will trigger chunking and preemption
prompts = [
"The following numbers of the sequence "
+14 -12
View File
@@ -54,7 +54,8 @@ def create_logits_tensor(
token_idx_to_override: int | None = None,
) -> torch.Tensor:
"""Helper function to create logits tensor that
will produce desired token ids on argmax"""
will produce desired token ids on argmax
"""
token_ids = [tokens[:-1] for tokens in output_token_ids]
num_total_tokens = sum(len(tokens) for tokens in token_ids)
logits = torch.full((num_total_tokens, vocab_size), -100.0, device=DEVICE_TYPE)
@@ -131,7 +132,7 @@ def create_sampling_metadata(
########################### Tests for Greedy Sampling ###################
def test_perfect_match(rejection_sampler):
"""Test when output tokens perfectly match speculated tokens"""
"""Test when output tokens perfectly match speculated tokens."""
spec_tokens = [[1, 2, 3]]
output_tokens = [[1, 2, 3, 4]] # 4 is the bonus token
@@ -152,7 +153,7 @@ def test_perfect_match(rejection_sampler):
def test_early_mismatch(rejection_sampler):
"""Test when there's an early mismatch in tokens"""
"""Test when there's an early mismatch in tokens."""
spec_tokens = [[1, 2, 3]]
output_tokens = [[1, 5, 3, 4]] # Mismatch at position 1
@@ -177,7 +178,7 @@ def test_early_mismatch(rejection_sampler):
def test_multiple_sequences(rejection_sampler):
"""Test handling multiple sequences of speculated tokens"""
"""Test handling multiple sequences of speculated tokens."""
spec_tokens = [[1, 2], [3]]
output_tokens = [[1, 2, 5], [3, 4]] # Two sequences with bonus tokens 5 and 4
@@ -202,7 +203,7 @@ def test_multiple_sequences(rejection_sampler):
def test_single_token_sequence(rejection_sampler):
"""Test handling sequences with single token"""
"""Test handling sequences with single token."""
spec_tokens = [[1]]
output_tokens = [[1, 2]] # Single token with bonus token 2
@@ -223,7 +224,7 @@ def test_single_token_sequence(rejection_sampler):
def test_empty_sequence(rejection_sampler):
"""Test handling empty sequence of speculated tokens"""
"""Test handling empty sequence of speculated tokens."""
spec_tokens: list[list[int]] = [[]]
output_tokens = [[5]] # Just the bonus token
@@ -244,7 +245,7 @@ def test_empty_sequence(rejection_sampler):
def test_multiple_mismatches(rejection_sampler):
"""Test handling multiple sequences with mismatches"""
"""Test handling multiple sequences with mismatches."""
spec_tokens = [[1, 2, 3], [4, 5, 6]]
output_tokens = [[1, 2, 7, 6], [4, 8, 6, 9]] # Mismatches in both sequences
@@ -286,7 +287,7 @@ def test_multiple_mismatches(rejection_sampler):
],
)
def test_parametrized_cases(rejection_sampler, spec_tokens, output_tokens, expected):
"""Parametrized test for various matching scenarios"""
"""Parametrized test for various matching scenarios."""
metadata = create_sampling_metadata(all_greedy=True)
logits = create_logits_tensor(output_tokens)
bonus_token_tensor = torch.tensor(
@@ -486,6 +487,7 @@ def estimate_rejection_sampling_pdf(
Returns:
Estimated probability distribution of the output tokens.
"""
mock_sampler = Mock(spec=Sampler)
mock_sampler.logprobs_mode = "raw_logprobs"
@@ -656,7 +658,7 @@ def _test_masked_logits(
@pytest.mark.parametrize("top_k", [1, 5, 99])
def test_top_k(rejection_sampler, top_k):
"""Test rejection sampling with top-k sampling"""
"""Test rejection sampling with top-k sampling."""
vocab_size = 100
batch_size = 100
num_draft_tokens = 3
@@ -699,7 +701,7 @@ def test_top_k(rejection_sampler, top_k):
@pytest.mark.parametrize("top_p", [0.5, 0.9, 0.99])
def test_top_p(rejection_sampler, top_p):
"""Test rejection sampling with top-p sampling"""
"""Test rejection sampling with top-p sampling."""
vocab_size = 100
batch_size = 100
num_draft_tokens = 3
@@ -746,7 +748,7 @@ def test_top_p(rejection_sampler, top_p):
########################### Tests for Logit Processors ###################
def test_frequency_penalties(rejection_sampler):
"""Test rejection sampling with frequency penalties"""
"""Test rejection sampling with frequency penalties."""
spec_tokens = [[1, 1, 1], [], [1, 1, 1]]
output_tokens = [[1, 1, 1, 1], [7], [1, 1, 1, 1]] # 1, 7 and 1 are the bonus tokens
@@ -829,7 +831,7 @@ def test_bad_words(rejection_sampler):
def test_allowed_token_ids(rejection_sampler):
"""Test rejection sampling with allowed token ids"""
"""Test rejection sampling with allowed token ids."""
spec_tokens = [[1, 2, 10], [10, 5, 3], [7, 10, 12]]
output_tokens = [[1, 2, 10, 5], [10, 5, 10, 5], [7, 10, 12, 5]]
# Not allowed tokens:
+7 -12
View File
@@ -169,8 +169,7 @@ def _create_default_sampling_metadata(
def _create_weighted_output_token_list(
batch_size: int, vocab_size: int
) -> tuple[list[list[int]], list[list[int]]]:
"""
Creates an output token list where each token occurs a distinct
"""Creates an output token list where each token occurs a distinct
number of times.
For each batch, a random subset of token IDs is selected from the
@@ -185,6 +184,7 @@ def _create_weighted_output_token_list(
- The second element is a list of distinct token IDs for each
batch, ordered by their frequency in the corresponding output
list.
"""
output_token_ids: list[list[int]] = []
sorted_token_ids_in_output: list[list[int]] = []
@@ -206,8 +206,7 @@ def _create_weighted_output_token_list(
def test_sampler_presence_penalty(
device: str, batch_size: int, presence_penalty: float
):
"""
Test to verify that if presence penalty is enabled then tokens
"""Test to verify that if presence penalty is enabled then tokens
are penalized as per their presence in the existing output.
"""
torch.set_default_device(device)
@@ -256,8 +255,7 @@ def test_sampler_presence_penalty(
def test_sampler_frequency_penalty(
device: str, batch_size: int, frequency_penalty: float
):
"""
Test to verify that if frequency penalty is enabled then tokens are
"""Test to verify that if frequency penalty is enabled then tokens are
penalized as per their frequency of occurrence.
"""
torch.set_default_device(device)
@@ -312,8 +310,7 @@ def test_sampler_frequency_penalty(
def test_sampler_repetition_penalty(
device: str, batch_size: int, repetition_penalty: float
):
"""
Test to verify that when the repetition penalty is enabled, tokens
"""Test to verify that when the repetition penalty is enabled, tokens
are penalized based on their presence in the prompt or the existing
output.
"""
@@ -370,8 +367,7 @@ def test_sampler_repetition_penalty(
def test_sampler_allowed_token_ids(
device: str, batch_size: int, num_allowed_token_ids: int
):
"""
Test to verify that when the repetition penalty is enabled, tokens
"""Test to verify that when the repetition penalty is enabled, tokens
are penalized based on their presence in the prompt or the existing
output.
"""
@@ -416,8 +412,7 @@ def test_sampler_allowed_token_ids(
def test_sampler_bad_words(
device: str, batch_size: int, bad_words_lengths: tuple[int, ...]
):
"""
Test to verify that when the bad words restriction is present, tokens
"""Test to verify that when the bad words restriction is present, tokens
are penalized based on their match with the bad words.
"""
torch.set_default_device(device)
@@ -17,7 +17,6 @@ def llm() -> LLM:
def test_n_gt_1(llm):
"""ParallelSampling is supported."""
params = SamplingParams(n=3)
outputs = llm.generate(PROMPT, params)
assert len(outputs[0].outputs) == 3
@@ -25,7 +24,6 @@ def test_n_gt_1(llm):
def test_penalties(llm):
"""Check that we do not get errors if applied."""
params = SamplingParams(
temperature=1.2,
presence_penalty=1.2,
@@ -40,7 +38,6 @@ def test_penalties(llm):
def test_stop(llm):
"""Check that we respect the stop words."""
output = llm.generate(PROMPT, SamplingParams(temperature=0))
split_text = output[0].outputs[0].text.split()
@@ -64,7 +61,6 @@ def test_stop(llm):
def test_stop_token_ids(llm):
"""Check that we respect the stop token ids."""
output = llm.generate(PROMPT, SamplingParams(temperature=0))
stop_token_id_0 = output[0].outputs[0].token_ids[5]
@@ -83,7 +79,6 @@ def test_stop_token_ids(llm):
def test_detokenize_false(llm):
"""Check that detokenize=False option works."""
output = llm.generate(PROMPT, SamplingParams(detokenize=False))
assert len(output[0].outputs[0].token_ids) > 0
assert len(output[0].outputs[0].text) == 0
@@ -106,7 +101,6 @@ def test_detokenize_false(llm):
def test_bad_words(llm):
"""Check that we respect bad words."""
tokenizer = llm.get_tokenizer()
def contains_bad_word(text: str, tokens: list[int], bad_word: str) -> bool:
@@ -147,7 +141,6 @@ def test_bad_words(llm):
def test_allowed_token_ids(llm):
"""Check that we can use allowed_token_ids."""
TOKEN_ID = 10
allowed_token_ids = [TOKEN_ID]
output = llm.generate(PROMPT, SamplingParams(allowed_token_ids=allowed_token_ids))
@@ -176,7 +169,6 @@ def test_allowed_token_ids(llm):
def test_seed(llm):
"""Check that seed impacts randomness."""
out_1 = llm.generate(PROMPT, SamplingParams(seed=42))
out_2 = llm.generate(PROMPT, SamplingParams(seed=42))
out_3 = llm.generate(PROMPT, SamplingParams(seed=43))
+4 -5
View File
@@ -50,8 +50,7 @@ def _seed_default_generator(seed: int) -> None:
@pytest.fixture(autouse=True)
def reset_default_device():
"""
Explicitly set the default device, which can affect subsequent tests.
"""Explicitly set the default device, which can affect subsequent tests.
Adding this fixture helps avoid this problem.
"""
original_device = torch.get_default_device()
@@ -171,8 +170,7 @@ def test_topk_impl_equivalence():
"interface differences between Python and FlashInfer implementations"
)
def test_flashinfer_sampler():
"""
This test verifies that the FlashInfer top-k and top-p sampling
"""This test verifies that the FlashInfer top-k and top-p sampling
implementation produces the same results as the Python implementation.
NOTE: FlashInfer did not directly expose an interface for fused top-k and
@@ -886,7 +884,8 @@ class TestFlashInferTopkToppRobustness:
def _make_logits(self, pattern: str) -> torch.Tensor:
"""Build (BATCH, VOCAB) logits with `pattern` applied to row 0
(rows 1..B-1 stay clean so we can detect cross-row corruption)."""
(rows 1..B-1 stay clean so we can detect cross-row corruption).
"""
logits = (
torch.randn(
self.BATCH,
+11 -7
View File
@@ -15,7 +15,7 @@ from vllm.v1.sample.metadata import SamplingMetadata
class BatchLogprobsComposition(Enum):
"""Types of logprobs configs to include in test batch"""
"""Types of logprobs configs to include in test batch."""
NONE = 0
SAMPLE = 1
@@ -29,7 +29,7 @@ BatchLogprobsSpecType = list[tuple[int | None, int | None]]
def get_test_batch(
batch_logprobs_composition: BatchLogprobsComposition,
) -> BatchLogprobsSpecType:
"""Generate logprobs configs for a batch of requests
"""Generate logprobs configs for a batch of requests.
A given request's logprobs configuration is (1) num_sample_logprobs and (2)
num_prompt_logprobs. The batch logprobs configuration is the list of request
@@ -53,9 +53,9 @@ def get_test_batch(
batch_logprobs_composition: types of logprobs configs to include in batch
Returns:
list of (Optional[num_sample_logprobs], Optional[num_prompt_logprobs])
tuples
"""
if batch_logprobs_composition == BatchLogprobsComposition.NONE:
# No requests with sample or prompt logprobs
@@ -101,7 +101,7 @@ def assert_incr_detok_str_matches_non_incr_detok_str(
non_incremental_detokenization_str: str,
msg: str,
) -> None:
"""Compare incrementally detok. text to non-incrementally detok. text
"""Compare incrementally detok. text to non-incrementally detok. text.
Fail if the strings mismatch after non-alphanumeric characters are stripped
out.
@@ -120,6 +120,7 @@ def assert_incr_detok_str_matches_non_incr_detok_str(
non_incremental_detokenization_str: non-incrementally-detokenized logprob
tokens
msg: error message if `assert` fails
"""
rgx = r"[^a-zA-Z0-9]+"
assert re.sub(rgx, "", incremental_detokenization_str) == re.sub(
@@ -128,13 +129,14 @@ def assert_incr_detok_str_matches_non_incr_detok_str(
def compute_correct_cumulative_logprob(completion_output: CompletionOutput) -> float:
"""Compute known-good value for evaluating cumulative logprob
"""Compute known-good value for evaluating cumulative logprob.
Args:
completion_output: completion output from engine
Returns:
Known-good cumulative logprob value
"""
token_ids = completion_output.token_ids
logprobs = completion_output.logprobs
@@ -170,7 +172,7 @@ def create_prompt_tokens_tensor(
class LogitsprocsTestFakes(NamedTuple):
"""Wraps fake data structures to support testing"""
"""Wraps fake data structures to support testing."""
logits: torch.Tensor
sampling_metadata: SamplingMetadata
@@ -186,6 +188,7 @@ class LogitsprocsTestFakes(NamedTuple):
Returns:
Iterator over logits processors
"""
return (
lp for lp in self.sampling_metadata.logitsprocs.all if isinstance(lp, cls)
@@ -201,7 +204,8 @@ def fake_update_logitsprocs_state(
batch_update: BatchUpdate | None,
) -> None:
"""Imitate logits processors persistent batch state update
in engine core"""
in engine core
"""
for logitproc in test_fakes.get_logitsprocs():
logitproc.update_state(batch_update)
holder = test_fakes.sampling_metadata.thinking_budget_state_holder