| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from vllm import SamplingParams |
| 7 | from vllm.logprobs import FlatLogprobs |
| 8 | |
| 9 | MODELS = ["distilbert/distilgpt2"] |
| 10 | MAX_TOKENS = 5 |
| 11 | NUM_TOP_LOGPROBS = 5 |
| 12 | NUM_PROMPT_LOGPROBS = 7 |
| 13 | MAX_LOGPROBS = max(NUM_TOP_LOGPROBS, NUM_PROMPT_LOGPROBS) |
| 14 | |
| 15 | |
| 16 | @pytest.mark.parametrize("model", MODELS) |
| 17 | @pytest.mark.parametrize("dtype", ["half"]) |
| 18 | @pytest.mark.parametrize("greedy", [True, False]) |
| 19 | @pytest.mark.parametrize("flat_logprobs", [True, False]) |
| 20 | def test_ranks( |
| 21 | vllm_runner, |
| 22 | model, |
| 23 | dtype, |
| 24 | greedy, |
| 25 | flat_logprobs, |
| 26 | example_prompts, |
| 27 | ): |
| 28 | with vllm_runner(model, dtype=dtype, max_logprobs=MAX_LOGPROBS) as vllm_model: |
| 29 | tokenizer = vllm_model.llm.get_tokenizer() |
| 30 | example_prompt_tokens = [tokenizer.encode(prompt) for prompt in example_prompts] |
| 31 | sampling_params = SamplingParams( |
| 32 | temperature=0.0 if greedy else 1.0, |
| 33 | top_p=1.0, |
| 34 | max_tokens=MAX_TOKENS, |
| 35 | logprobs=NUM_TOP_LOGPROBS, |
| 36 | prompt_logprobs=NUM_PROMPT_LOGPROBS, |
| 37 | flat_logprobs=flat_logprobs, |
| 38 | ) |
| 39 | results = vllm_model.generate_w_logprobs(example_prompts, sampling_params) |
| 40 | |
| 41 | assert len(results) == len(example_prompt_tokens) |
| 42 | for i, (result, prompt_tokens) in enumerate(zip(results, example_prompt_tokens)): |
| 43 | decode_tokens, _, decode_logprobs, prompt_logprobs = result |
| 44 | |
| 45 | # Ensure the return type of logprobs is accurate |
| 46 | assert isinstance(prompt_logprobs, FlatLogprobs if flat_logprobs else list) |
| 47 | assert isinstance(decode_logprobs, FlatLogprobs if flat_logprobs else list) |
| 48 | |
| 49 | ######################## |
| 50 | # Check prompt logprobs |
| 51 | ######################## |
| 52 | assert len(prompt_tokens) == len(prompt_logprobs) |
| 53 | # No logprob for first prompt token |
| 54 | assert not prompt_logprobs[0] |
| 55 | for position, (token, logprobs) in enumerate( |
| 56 | zip(prompt_tokens[1:], prompt_logprobs[1:]), start=1 |
| 57 | ): |
| 58 | # Ensure logprobs of prompt token is always returned |
| 59 | logprob = logprobs.get(token) |
| 60 | assert logprob is not None |
| 61 | assert logprob.rank >= 1 |
| 62 | # Ensure # of returned logprobs should be |
| 63 | # either NUM_PROMPT_LOGPROBS or NUM_PROMPT_LOGPROBS+1 |
| 64 | assert NUM_PROMPT_LOGPROBS <= len(logprobs) <= NUM_PROMPT_LOGPROBS + 1 |
| 65 | # Ensure top NUM_PROMPT_LOGPROBS is always extracted |
| 66 | assert set(range(1, NUM_PROMPT_LOGPROBS + 1)).issubset( |
| 67 | {logprob.rank for logprob in logprobs.values()} |
| 68 | ) |
| 69 | |
| 70 | ######################## |
| 71 | # Check sample logprobs |
| 72 | ######################## |
| 73 | assert len(decode_tokens) == len(decode_logprobs) |
| 74 | for position, (token, logprobs) in enumerate( |
| 75 | zip(decode_tokens, decode_logprobs) |
| 76 | ): |
| 77 | # Ensure logprobs of chosen token is always returned |
| 78 | logprob = logprobs.get(token) |
| 79 | assert logprob is not None |
| 80 | if greedy: |
| 81 | # For greedy sampling, all chosen logprob should be top ranked |
| 82 | assert logprob.rank == 1 |
| 83 | else: |
| 84 | assert logprob.rank >= 1 |
| 85 | # Ensure # of returned logprobs should be |
| 86 | # either NUM_TOP_LOGPROBS or NUM_TOP_LOGPROBS+1 |
| 87 | assert NUM_TOP_LOGPROBS <= len(logprobs) <= NUM_TOP_LOGPROBS + 1 |
| 88 | # Ensure top NUM_TOP_LOGPROBS logprobs is always extracted |
| 89 | assert set(range(1, NUM_TOP_LOGPROBS + 1)).issubset( |
| 90 | {logprob.rank for logprob in logprobs.values()} |
| 91 | ) |