|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import pytest |
| 4 | +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, ModelConfig, |
| 5 | + VllmConfig) |
| 6 | +from vllm.lora.request import LoRARequest |
| 7 | +from vllm.sampling_params import SamplingParams |
| 8 | +from vllm.transformers_utils.tokenizer_group import init_tokenizer_from_configs |
| 9 | +from vllm.v1.engine.processor import Processor |
| 10 | + |
| 11 | + |
| 12 | +def test_allowed_token_ids_with_lora_vocab(llama_2_7b_base_huggingface_id, |
| 13 | + sql_lora_files): |
| 14 | + """ |
| 15 | + Test that we properly resolve the range of allowed token ids for lora |
| 16 | + adapters that define additional tokens. |
| 17 | + """ |
| 18 | + |
| 19 | + # Setup a base model compatible with the sql_lora_files adapter and |
| 20 | + # a known number of tokens in the base model. |
| 21 | + model_config = ModelConfig( |
| 22 | + model=llama_2_7b_base_huggingface_id, |
| 23 | + tokenizer=llama_2_7b_base_huggingface_id, |
| 24 | + tokenizer_mode="auto", |
| 25 | + ) |
| 26 | + |
| 27 | + vllm_config = VllmConfig( |
| 28 | + model_config=model_config, |
| 29 | + cache_config=CacheConfig(), |
| 30 | + device_config=DeviceConfig(), |
| 31 | + lora_config=LoRAConfig(), |
| 32 | + ) |
| 33 | + |
| 34 | + tokenizer = init_tokenizer_from_configs( |
| 35 | + model_config=vllm_config.model_config, |
| 36 | + scheduler_config=vllm_config.scheduler_config, |
| 37 | + lora_config=vllm_config.lora_config) |
| 38 | + processor = Processor(vllm_config, tokenizer) |
| 39 | + |
| 40 | + lora_request = LoRARequest("1", 1, str(sql_lora_files)) |
| 41 | + request_id = "1" |
| 42 | + prompt = "a prompt" |
| 43 | + |
| 44 | + # tokens added in the lora adapter should not raise an error |
| 45 | + lora_token_ids = [32000, 32001, 32002, 32003] |
| 46 | + processor.process_inputs( |
| 47 | + request_id, |
| 48 | + prompt, |
| 49 | + params=SamplingParams(allowed_token_ids=lora_token_ids), |
| 50 | + lora_request=lora_request) |
| 51 | + |
| 52 | + # tokens in the base model should not raise an error |
| 53 | + base_token_ids = [1000, 1001, 1002, 1003] |
| 54 | + processor.process_inputs( |
| 55 | + request_id, |
| 56 | + prompt, |
| 57 | + params=SamplingParams(allowed_token_ids=base_token_ids), |
| 58 | + lora_request=lora_request) |
| 59 | + |
| 60 | + # tokens not in the lora adapter should raise an error |
| 61 | + invalid_token_ids = [35000, 35001, 35002, 35003] |
| 62 | + with pytest.raises(ValueError): |
| 63 | + processor.process_inputs( |
| 64 | + request_id, |
| 65 | + prompt, |
| 66 | + params=SamplingParams(allowed_token_ids=invalid_token_ids), |
| 67 | + lora_request=lora_request) |
| 68 | + |
| 69 | + # tokens in the lora adapter with no lora request should raise an error |
| 70 | + with pytest.raises(ValueError): |
| 71 | + processor.process_inputs( |
| 72 | + request_id, |
| 73 | + prompt, |
| 74 | + params=SamplingParams(allowed_token_ids=lora_token_ids), |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def test_allowed_token_ids_with_lora_adapter_no_vocab( |
| 79 | + qwen25vl_base_huggingface_id, qwen25vl_lora_files): |
| 80 | + """ |
| 81 | + Test that we properly resolve the range of allowed token ids for lora |
| 82 | + adapters that do not define additional tokens. |
| 83 | + """ |
| 84 | + |
| 85 | + # Setup a base model compatible with the qwen25vl_lora_files adapter and |
| 86 | + # a known number of tokens in the base model. |
| 87 | + model_config = ModelConfig( |
| 88 | + model=qwen25vl_base_huggingface_id, |
| 89 | + tokenizer=qwen25vl_base_huggingface_id, |
| 90 | + tokenizer_mode="auto", |
| 91 | + ) |
| 92 | + |
| 93 | + vllm_config = VllmConfig( |
| 94 | + model_config=model_config, |
| 95 | + cache_config=CacheConfig(), |
| 96 | + device_config=DeviceConfig(), |
| 97 | + lora_config=LoRAConfig(), |
| 98 | + ) |
| 99 | + |
| 100 | + tokenizer = init_tokenizer_from_configs( |
| 101 | + model_config=vllm_config.model_config, |
| 102 | + scheduler_config=vllm_config.scheduler_config, |
| 103 | + lora_config=vllm_config.lora_config) |
| 104 | + processor = Processor(vllm_config, tokenizer) |
| 105 | + |
| 106 | + lora_request = LoRARequest("1", 1, str(qwen25vl_lora_files)) |
| 107 | + request_id = "1" |
| 108 | + prompt = "a prompt" |
| 109 | + |
| 110 | + # tokens in the base model should not raise an error |
| 111 | + base_token_ids = [1000, 1001, 1002, 1003] |
| 112 | + processor.process_inputs( |
| 113 | + request_id, |
| 114 | + prompt, |
| 115 | + params=SamplingParams(allowed_token_ids=base_token_ids), |
| 116 | + lora_request=lora_request) |
| 117 | + |
| 118 | + # tokens in the base model with no lora request should not raise an error |
| 119 | + base_token_ids = [1000, 1001, 1002, 1003] |
| 120 | + processor.process_inputs( |
| 121 | + request_id, |
| 122 | + prompt, |
| 123 | + params=SamplingParams(allowed_token_ids=base_token_ids), |
| 124 | + ) |
| 125 | + |
| 126 | + # tokens not in the base model should raise an error |
| 127 | + invalid_token_ids = [200000, 200001, 200002, 200003] |
| 128 | + with pytest.raises(ValueError): |
| 129 | + processor.process_inputs( |
| 130 | + request_id, |
| 131 | + prompt, |
| 132 | + params=SamplingParams(allowed_token_ids=invalid_token_ids), |
| 133 | + lora_request=lora_request) |
0 commit comments