| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | from transformers import AutoTokenizer |
| 6 | |
| 7 | from tests.v1.engine.utils import ( |
| 8 | FULL_STRINGS, |
| 9 | NUM_PROMPT_LOGPROBS_UNDER_TEST, |
| 10 | NUM_SAMPLE_LOGPROBS_UNDER_TEST, |
| 11 | PROMPT_LEN, |
| 12 | TOKENIZER_NAME, |
| 13 | DummyOutputProcessorTestVectors, |
| 14 | generate_dummy_prompt_logprobs_tensors, |
| 15 | generate_dummy_sample_logprobs, |
| 16 | ) |
| 17 | from vllm.engine.arg_utils import EngineArgs |
| 18 | |
| 19 | from ...distributed.conftest import publisher_config, random_port # noqa: F401 |
| 20 | |
| 21 | |
| 22 | def _build_test_vectors_no_logprobs() -> DummyOutputProcessorTestVectors: |
| 23 | """Generate output processor dummy test vectors, without logprobs. |
| 24 | |
| 25 | Returns: |
| 26 | DummyOutputProcessorTestVectors instance with no logprobs |
| 27 | |
| 28 | """ |
| 29 | tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME) |
| 30 | vllm_config = EngineArgs(model=TOKENIZER_NAME).create_engine_config() |
| 31 | # Tokenize prompts under test & create dummy generated tokens |
| 32 | prompt_tokens = [tokenizer(text).input_ids[:PROMPT_LEN] for text in FULL_STRINGS] |
| 33 | generation_tokens = [ |
| 34 | tokenizer(text).input_ids[PROMPT_LEN:] for text in FULL_STRINGS |
| 35 | ] |
| 36 | # Generate prompt strings |
| 37 | prompt_strings = [ |
| 38 | tokenizer.decode(prompt_tokens, skip_special_tokens=True) |
| 39 | for prompt_tokens in prompt_tokens |
| 40 | ] |
| 41 | prompt_strings_len = [len(prompt_string) for prompt_string in prompt_strings] |
| 42 | return DummyOutputProcessorTestVectors( |
| 43 | tokenizer=tokenizer, |
| 44 | vllm_config=vllm_config, |
| 45 | full_tokens=[tokenizer(text).input_ids for text in FULL_STRINGS], |
| 46 | prompt_tokens=prompt_tokens, |
| 47 | generation_tokens=generation_tokens, |
| 48 | prompt_strings=prompt_strings, |
| 49 | prompt_strings_len=prompt_strings_len, |
| 50 | generation_strings=[ |
| 51 | text[prompt_len:] |
| 52 | for text, prompt_len in zip(FULL_STRINGS, prompt_strings_len) |
| 53 | ], |
| 54 | prompt_logprobs=[], |
| 55 | generation_logprobs=[], |
| 56 | ) |
| 57 | |
| 58 | |
| 59 | @pytest.fixture |
| 60 | def dummy_test_vectors() -> DummyOutputProcessorTestVectors: |
| 61 | """Generate output processor dummy test vectors, with logprobs. |
| 62 | |
| 63 | Returns: |
| 64 | DummyOutputProcessorTestVectors instance with logprobs |
| 65 | |
| 66 | """ |
| 67 | # Build dummy test vectors without logprobs |
| 68 | dtv = _build_test_vectors_no_logprobs() |
| 69 | # Inject logprobs into dummy test vectors |
| 70 | # data structure |
| 71 | dtv.generation_logprobs = [ |
| 72 | generate_dummy_sample_logprobs( |
| 73 | sampled_tokens_list=tokens_list, |
| 74 | num_logprobs=NUM_SAMPLE_LOGPROBS_UNDER_TEST, |
| 75 | tokenizer=dtv.tokenizer, |
| 76 | ) |
| 77 | for tokens_list in dtv.generation_tokens |
| 78 | ] |
| 79 | dtv.prompt_logprobs = [ |
| 80 | generate_dummy_prompt_logprobs_tensors( |
| 81 | prompt_tokens_list=tokens_list, |
| 82 | num_logprobs=NUM_PROMPT_LOGPROBS_UNDER_TEST, |
| 83 | tokenizer=dtv.tokenizer, |
| 84 | ) |
| 85 | for tokens_list in dtv.prompt_tokens |
| 86 | ] |
| 87 | return dtv |