Skip to content

Commit 2810836

Browse files
committed
Add tests for PLessLogitsProcessor
Signed-off-by: Runyan <blackbird7@hotmail.com>
1 parent 92c54ee commit 2810836

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

tests/v1/logits_processors/test_correctness.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
MinPLogitsProcessor,
3131
MinTokensLogitsProcessor,
3232
MoveDirectionality,
33+
PLessLogitsProcessor,
3334
build_logitsprocs,
3435
)
3536
from vllm.v1.sample.metadata import SamplingMetadata
@@ -190,7 +191,7 @@ def _generate_test_fakes(batch_size: int, device: str) -> LogitsprocsTestFakes:
190191
def _sampling_params_from_logitproc(logitproc_type: LogitprocType) -> SamplingParams:
191192
"""Customize request SamplingParams for a specified logitproc"""
192193
# SamplingParams for req with no logitproc
193-
kwargs = {"min_p": 0.0, "logit_bias": None, "min_tokens": 0}
194+
kwargs = {"p_less": False, "min_p": 0.0, "logit_bias": None, "min_tokens": 0}
194195
if fxn := logitsprocs_test_mapping[logitproc_type].gen_request_fxn:
195196
fxn(kwargs)
196197
return SamplingParams(**kwargs)
@@ -296,6 +297,71 @@ def _logit_bias_validate(
296297
)
297298

298299

300+
def _p_less_params(kwargs: dict) -> None:
301+
"""
302+
The p-less logits processor configuration.
303+
"""
304+
kwargs["p_less"] = True
305+
306+
307+
def _p_less_validate(
308+
test_fakes: LogitsprocsTestFakes,
309+
persistent_batch: list[LogitsProcsRequestParams],
310+
logits_new: torch.Tensor,
311+
batch_index: int,
312+
request_params: LogitsProcsRequestParams,
313+
step_idx: int,
314+
) -> None:
315+
"""
316+
Validate that the p-less logits processor is applied correctly.
317+
"""
318+
logits_old_for_batch_id = test_fakes.logits[
319+
persistent_batch[batch_index].workload_index
320+
].cpu()
321+
logits_new_for_batch_id = logits_new[batch_index].cpu()
322+
if request_params.params.p_less:
323+
# p-less decoding is used when p_less is set to True
324+
# Convert logits to probabilities
325+
probabilities = logits_old_for_batch_id.softmax(dim=-1)
326+
# Calculate threshold probabilities for the batch for filtering out invalid
327+
# tokens
328+
threshold_probabilities = probabilities.square().sum(dim=-1, keepdim=True)
329+
# Create boolean mask for invalid tokens
330+
invalid_token_mask = probabilities < threshold_probabilities
331+
# Apply mask to convert logits of invalid tokens to negative infinity
332+
logits_new_exp = logits_old_for_batch_id.masked_fill(
333+
invalid_token_mask, -float("inf")
334+
)
335+
for token_id in range(VOCAB_SIZE):
336+
logit_new = logits_new_for_batch_id[token_id]
337+
logit_new_exp = logits_new_exp[token_id]
338+
if logit_new_exp != pytest.approx(logit_new):
339+
_raise_error_invalid(
340+
msg_suffix=(
341+
f"Token {token_id} logit value {logit_new} "
342+
f"does not match expected value {logit_new_exp}"
343+
),
344+
batch_index=batch_index,
345+
request_params=request_params,
346+
step_idx=step_idx,
347+
)
348+
else:
349+
# p-less decoding is not used when p_less is set to False
350+
for token_id in range(VOCAB_SIZE):
351+
logit_old = logits_old_for_batch_id[token_id]
352+
logit_new = logits_new_for_batch_id[token_id]
353+
if logit_new != pytest.approx(logit_old):
354+
_raise_error_invalid(
355+
msg_suffix=(
356+
f"Token {token_id} logit value {logit_old} "
357+
f"does not match expected value {logit_new}"
358+
),
359+
batch_index=batch_index,
360+
request_params=request_params,
361+
step_idx=step_idx,
362+
)
363+
364+
299365
def _min_p_params(kwargs: dict) -> None:
300366
"""Min-p logitproc config"""
301367
kwargs["min_p"] = 0.1
@@ -582,6 +648,9 @@ class LogitsprocTestHelpers(NamedTuple):
582648
LogitBiasLogitsProcessor: LogitsprocTestHelpers(
583649
gen_request_fxn=_logit_bias_params, eval_fxn=_logit_bias_validate
584650
),
651+
PLessLogitsProcessor: LogitsprocTestHelpers(
652+
gen_request_fxn=_p_less_params, eval_fxn=_p_less_validate
653+
),
585654
MinPLogitsProcessor: LogitsprocTestHelpers(
586655
gen_request_fxn=_min_p_params, eval_fxn=_min_p_validate
587656
),

tests/v1/logits_processors/test_custom_offline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def test_rejects_custom_logitsprocs(
269269
llm = LLM(**llm_kwargs)
270270
# Require that no custom logitsprocs have been loaded
271271
# (built-in processors may exist: MinTokensLogitsProcessor,
272-
# LogitBiasLogitsProcessor, MinPLogitsProcessor)
272+
# LogitBiasLogitsProcessor, MinPLogitsProcessor, PLessLogitsProcessor)
273273
worker = llm.llm_engine.model_executor.driver_worker.worker
274274
for proc in worker.model_runner.input_batch.logitsprocs.all:
275275
assert not isinstance(proc, DummyLogitsProcessor)

0 commit comments

Comments
 (0)