| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import os |
| 5 | import tempfile |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from tests.utils import RemoteOpenAIServer |
| 10 | from vllm.model_executor.model_loader.weight_utils import download_weights_from_hf |
| 11 | from vllm.tokenizers import get_tokenizer |
| 12 | |
| 13 | MODEL_NAME = "Qwen/Qwen3-0.6B" |
| 14 | MODEL_PATH = os.path.join(tempfile.gettempdir(), "qwen3_06b") |
| 15 | |
| 16 | |
| 17 | @pytest.fixture(scope="module") |
| 18 | def server(): |
| 19 | global MODEL_PATH |
| 20 | MODEL_PATH = download_weights_from_hf( |
| 21 | MODEL_NAME, |
| 22 | allow_patterns=["*"], |
| 23 | cache_dir=MODEL_PATH, |
| 24 | ignore_patterns=["tokenizer*", "vocab*", "*.safetensors"], |
| 25 | ) |
| 26 | args = [ |
| 27 | "--max-model-len", |
| 28 | "2048", |
| 29 | "--max-num-seqs", |
| 30 | "128", |
| 31 | "--enforce-eager", |
| 32 | "--skip-tokenizer-init", |
| 33 | "--load-format", |
| 34 | "dummy", |
| 35 | ] |
| 36 | with RemoteOpenAIServer(MODEL_PATH, args) as remote_server: |
| 37 | yield remote_server |
| 38 | |
| 39 | |
| 40 | @pytest.mark.asyncio |
| 41 | async def test_token_in_token_out_and_logprobs(server): |
| 42 | """Test token-in-token-out and token_ids align with prompt_logprobs |
| 43 | & logprobs when return_tokens_as_token_ids is enabled. |
| 44 | """ |
| 45 | tokenizer = get_tokenizer(tokenizer_name=MODEL_NAME) |
| 46 | text = "Hello, world! How are you today?" |
| 47 | token_ids = tokenizer.encode(text) |
| 48 | async with server.get_async_client() as client: |
| 49 | # Test with both return_token_ids and return_tokens_as_token_ids enabled |
| 50 | completion = await client.completions.create( |
| 51 | model=MODEL_PATH, |
| 52 | prompt=token_ids, |
| 53 | max_tokens=20, |
| 54 | temperature=0, |
| 55 | echo=True, |
| 56 | extra_body={ |
| 57 | "return_token_ids": True, |
| 58 | }, |
| 59 | ) |
| 60 | |
| 61 | # Verify all fields are present |
| 62 | assert ( |
| 63 | completion.choices[0].token_ids is not None |
| 64 | and 0 < len(completion.choices[0].token_ids) <= 20 |
| 65 | ) |
| 66 | assert completion.choices[0].prompt_token_ids is not None |
| 67 | |
| 68 | # Decode prompt tokens |
| 69 | if completion.choices[0].prompt_token_ids: |
| 70 | prompt_text = tokenizer.decode(completion.choices[0].prompt_token_ids) |
| 71 | # The decoded prompt should match or close to original prompt |
| 72 | assert prompt_text == text |