|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +""" |
| 4 | +This script contains: |
| 5 | +1. test lora with speculative decoding for batch inference |
| 6 | +""" |
| 7 | + |
| 8 | +import random |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | +import torch |
| 13 | + |
| 14 | +from vllm import LLM, SamplingParams |
| 15 | +from vllm.distributed import cleanup_dist_env_and_memory |
| 16 | +from vllm.lora.request import LoRARequest |
| 17 | +from vllm.platforms import current_platform |
| 18 | + |
| 19 | +LORA_TEST_PROMPT_MAP: dict[str, str] = {} |
| 20 | + |
| 21 | +LORA_TEST_PROMPT_MAP["premjatin/qwen-linear-algebra-coder"] = """ |
| 22 | +### INSTRUCTION: |
| 23 | +You are an AI assistant that generates Python code to solve linear |
| 24 | +algebra problems. |
| 25 | +
|
| 26 | +### PROBLEM: |
| 27 | +Find the eigenvalues and eigenvectors of the following 3x3 matrix: |
| 28 | +[[3, 2, 0], |
| 29 | + [2, 3, 0], |
| 30 | + [0, 0, 2]] |
| 31 | +
|
| 32 | +### OUTPUT FORMAT (STRICT): |
| 33 | +Numbers should be represented as integers only. |
| 34 | +
|
| 35 | +### PYTHON SOLUTION: |
| 36 | +""" |
| 37 | + |
| 38 | +SEED = 42 |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.skipif(not current_platform.is_cuda(), reason="CUDA not available") |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "model_setup", |
| 44 | + [ |
| 45 | + ( |
| 46 | + "eagle3", |
| 47 | + "Qwen/Qwen3-1.7B", |
| 48 | + "AngelSlim/Qwen3-1.7B_eagle3", |
| 49 | + "premjatin/qwen-linear-algebra-coder", |
| 50 | + 1, |
| 51 | + ) |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_batch_inference_correctness( |
| 55 | + monkeypatch: pytest.MonkeyPatch, |
| 56 | + model_setup: tuple[str, str, str, str, int], |
| 57 | +): |
| 58 | + """ |
| 59 | + Compare the outputs of a LLM with only Lora and a LLM with both SD and Lora. |
| 60 | + Should be the same and no failure when doing batch inference. |
| 61 | + model_setup: (method, model_name, spec_model_name, lora_path, tp_size) |
| 62 | + """ |
| 63 | + with monkeypatch.context() as m: |
| 64 | + m.setenv("VLLM_USE_V1", "1") |
| 65 | + |
| 66 | + # Disable randomness |
| 67 | + m.setenv("CUBLAS_WORKSPACE_CONFIG", ":4096:8") |
| 68 | + torch.manual_seed(SEED) |
| 69 | + np.random.seed(SEED) |
| 70 | + random.seed(SEED) |
| 71 | + torch.cuda.manual_seed_all(SEED) |
| 72 | + torch.backends.cudnn.benchmark = False |
| 73 | + torch.backends.cudnn.deterministic = True |
| 74 | + |
| 75 | + method, model_name, spec_model_name, lora_path, tp_size = model_setup |
| 76 | + |
| 77 | + # without speculative decoding |
| 78 | + ref_llm = LLM( |
| 79 | + model=model_name, |
| 80 | + trust_remote_code=True, |
| 81 | + tensor_parallel_size=tp_size, |
| 82 | + max_model_len=2048, |
| 83 | + max_num_seqs=4, |
| 84 | + enable_lora=True, |
| 85 | + max_loras=1, |
| 86 | + max_cpu_loras=1, |
| 87 | + max_lora_rank=16, |
| 88 | + ) |
| 89 | + |
| 90 | + prompts = [LORA_TEST_PROMPT_MAP[lora_path]] * 100 |
| 91 | + lora_request = LoRARequest("adapter", 1, lora_path) |
| 92 | + sampling_params = SamplingParams( |
| 93 | + temperature=0.0, top_p=1.0, top_k=-1, seed=SEED, max_tokens=128 |
| 94 | + ) |
| 95 | + |
| 96 | + ref_outputs = ref_llm.generate( |
| 97 | + prompts, sampling_params, lora_request=lora_request |
| 98 | + ) |
| 99 | + del ref_llm |
| 100 | + torch.cuda.empty_cache() |
| 101 | + cleanup_dist_env_and_memory() |
| 102 | + |
| 103 | + lora_spec_llm = LLM( |
| 104 | + model=model_name, |
| 105 | + trust_remote_code=True, |
| 106 | + tensor_parallel_size=tp_size, |
| 107 | + speculative_config={ |
| 108 | + "method": method, |
| 109 | + "model": spec_model_name, |
| 110 | + "num_speculative_tokens": 3, |
| 111 | + "max_model_len": 2048, |
| 112 | + }, |
| 113 | + max_model_len=2048, |
| 114 | + max_num_seqs=4, |
| 115 | + enable_lora=True, |
| 116 | + max_loras=1, |
| 117 | + max_cpu_loras=1, |
| 118 | + max_lora_rank=16, |
| 119 | + ) |
| 120 | + |
| 121 | + lora_spec_outputs = lora_spec_llm.generate( |
| 122 | + prompts, sampling_params, lora_request=lora_request |
| 123 | + ) |
| 124 | + |
| 125 | + matches = 0 |
| 126 | + misses = 0 |
| 127 | + for ref_output, spec_output in zip(ref_outputs, lora_spec_outputs): |
| 128 | + if ref_output.outputs[0].text == spec_output.outputs[0].text: |
| 129 | + matches += 1 |
| 130 | + else: |
| 131 | + misses += 1 |
| 132 | + print(f"ref_output: {ref_output.outputs[0].text}") |
| 133 | + print(f"spec_output: {spec_output.outputs[0].text}") |
| 134 | + |
| 135 | + # Heuristic: expect at least 90% of the prompts to match exactly |
| 136 | + # Upon failure, inspect the outputs to check for inaccuracy. |
| 137 | + print(f"match ratio: {matches}/{len(ref_outputs)}") |
| 138 | + assert matches > int(0.90 * len(ref_outputs)) |
| 139 | + del lora_spec_llm |
| 140 | + torch.cuda.empty_cache() |
| 141 | + cleanup_dist_env_and_memory() |
0 commit comments