|
13 | 13 | # ==============================================================================
|
14 | 14 |
|
15 | 15 | import multiprocessing as mp
|
| 16 | +import os |
| 17 | +import random |
16 | 18 | import unittest
|
| 19 | +from typing import List |
17 | 20 |
|
18 |
| -from utils import TORCH_DTYPES, LoRAAdaptor, LoRAModelCase, run_lora_test_by_batch |
| 21 | +from utils import ( |
| 22 | + ALL_OTHER_MULTI_LORA_MODELS, |
| 23 | + CI_MULTI_LORA_MODELS, |
| 24 | + TORCH_DTYPES, |
| 25 | + LoRAModelCase, |
| 26 | +) |
19 | 27 |
|
20 |
| -from sglang.test.test_utils import CustomTestCase |
| 28 | +from sglang.test.runners import HFRunner, SRTRunner |
| 29 | +from sglang.test.test_utils import CustomTestCase, calculate_rouge_l, is_in_ci |
21 | 30 |
|
22 |
| -PROMPTS = [ |
| 31 | +TEST_MULTIPLE_BATCH_PROMPTS = [ |
23 | 32 | """
|
24 |
| -### Instruction: |
25 |
| -Write a poem about the transformers Python library. |
26 |
| -Mention the word "large language models" in that poem. |
27 |
| -### Response: |
28 |
| -The Transformers are large language models, |
29 |
| -They're used to make predictions on text. |
30 |
| -""", |
| 33 | + ### Instruction: |
| 34 | + Tell me about llamas and alpacas |
| 35 | + ### Response: |
| 36 | + Llamas are large, long-necked animals with a woolly coat. They have two toes on each foot instead of three like other camelids (camels, dromedaries). Llamas live in the Andean mountains of South America where they graze on grasses and shrubs. Alpaca is another name for domesticated llama. The word "alpaca" comes from an Incan language meaning "golden fleece." Alpacas look very similar to llamas but are smaller than their wild relatives. Both species were used by ancient people as pack animals and for meat. Today both llamas and alpacas are raised primarily for their fiber which can be spun into yarn or knitted into clothing. |
| 37 | + ### Question 2: |
| 38 | + What do you know about llamas? |
| 39 | + ### Answer: |
| 40 | + """, |
| 41 | + """ |
| 42 | + ### Instruction: |
| 43 | + Write a poem about the transformers Python library. |
| 44 | + Mention the word "large language models" in that poem. |
| 45 | + ### Response: |
| 46 | + The Transformers are large language models, |
| 47 | + They're used to make predictions on text. |
| 48 | + """, |
31 | 49 | "AI is a field of computer science focused on",
|
32 |
| -] |
33 |
| - |
34 |
| -LORA_MODELS_WITH_NONE = [ |
35 |
| - LoRAModelCase( |
36 |
| - base="meta-llama/Llama-3.1-8B-Instruct", |
37 |
| - adaptors=[ |
38 |
| - LoRAAdaptor( |
39 |
| - name="algoprog/fact-generation-llama-3.1-8b-instruct-lora", |
40 |
| - ), |
41 |
| - LoRAAdaptor( |
42 |
| - name=None, |
43 |
| - ), |
44 |
| - ], |
45 |
| - max_loras_per_batch=2, |
46 |
| - ), |
47 |
| - LoRAModelCase( |
48 |
| - base="meta-llama/Llama-3.1-8B-Instruct", |
49 |
| - adaptors=[ |
50 |
| - LoRAAdaptor( |
51 |
| - name=None, |
52 |
| - ), |
53 |
| - LoRAAdaptor( |
54 |
| - name="algoprog/fact-generation-llama-3.1-8b-instruct-lora", |
55 |
| - ), |
56 |
| - ], |
57 |
| - max_loras_per_batch=2, |
58 |
| - ), |
| 50 | + "Computer science is the study of", |
| 51 | + "Write a short story.", |
| 52 | + "What are the main components of a computer?", |
59 | 53 | ]
|
60 | 54 |
|
61 | 55 |
|
62 | 56 | class TestLoRA(CustomTestCase):
|
63 |
| - def test_lora_batch_with_none(self): |
64 |
| - for model_case in LORA_MODELS_WITH_NONE: |
65 |
| - prompts = PROMPTS |
| 57 | + |
| 58 | + def _run_lora_multiple_batch_on_model_cases(self, model_cases: List[LoRAModelCase]): |
| 59 | + for model_case in model_cases: |
66 | 60 | for torch_dtype in TORCH_DTYPES:
|
67 |
| - run_lora_test_by_batch( |
68 |
| - prompts, |
69 |
| - model_case, |
70 |
| - torch_dtype, |
71 |
| - max_new_tokens=32, |
72 |
| - backend="triton", |
73 |
| - test_tag="test_lora_batch_with_none", |
| 61 | + max_new_tokens = 32 |
| 62 | + backend = "triton" |
| 63 | + base_path = model_case.base |
| 64 | + lora_adapter_paths = [a.name for a in model_case.adaptors] |
| 65 | + assert len(lora_adapter_paths) >= 2 |
| 66 | + |
| 67 | + batches = [ |
| 68 | + ( |
| 69 | + [ |
| 70 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 71 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 72 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 73 | + ], |
| 74 | + [ |
| 75 | + None, |
| 76 | + lora_adapter_paths[0], |
| 77 | + lora_adapter_paths[1], |
| 78 | + ], |
| 79 | + ), |
| 80 | + ( |
| 81 | + [ |
| 82 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 83 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 84 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 85 | + ], |
| 86 | + [ |
| 87 | + lora_adapter_paths[0], |
| 88 | + None, |
| 89 | + lora_adapter_paths[1], |
| 90 | + ], |
| 91 | + ), |
| 92 | + ( |
| 93 | + [ |
| 94 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 95 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 96 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 97 | + ], |
| 98 | + [lora_adapter_paths[0], lora_adapter_paths[1], None], |
| 99 | + ), |
| 100 | + ( |
| 101 | + [ |
| 102 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 103 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 104 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 105 | + ], |
| 106 | + [None, lora_adapter_paths[1], None], |
| 107 | + ), |
| 108 | + ( |
| 109 | + [ |
| 110 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 111 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 112 | + random.choice(TEST_MULTIPLE_BATCH_PROMPTS), |
| 113 | + ], |
| 114 | + [None, None, None], |
| 115 | + ), |
| 116 | + ] |
| 117 | + |
| 118 | + print( |
| 119 | + f"\n========== Testing multiple batches on base '{base_path}' with backend={backend}, dtype={torch_dtype} ---" |
| 120 | + ) |
| 121 | + |
| 122 | + # Initialize runners |
| 123 | + srt_runner = SRTRunner( |
| 124 | + base_path, |
| 125 | + torch_dtype=torch_dtype, |
| 126 | + model_type="generation", |
| 127 | + lora_paths=[lora_adapter_paths[0], lora_adapter_paths[1]], |
| 128 | + max_loras_per_batch=len(lora_adapter_paths) + 1, |
| 129 | + lora_backend=backend, |
| 130 | + disable_radix_cache=True, |
74 | 131 | )
|
| 132 | + hf_runner = HFRunner( |
| 133 | + base_path, torch_dtype=torch_dtype, model_type="generation" |
| 134 | + ) |
| 135 | + |
| 136 | + with srt_runner, hf_runner: |
| 137 | + for i, (prompts, lora_paths) in enumerate(batches): |
| 138 | + print( |
| 139 | + f"\n--- Running Batch {i+1} --- prompts: {prompts}, lora_paths: {lora_paths}" |
| 140 | + ) |
| 141 | + |
| 142 | + srt_outputs = srt_runner.batch_forward( |
| 143 | + prompts, |
| 144 | + max_new_tokens=max_new_tokens, |
| 145 | + lora_paths=lora_paths, |
| 146 | + ) |
| 147 | + |
| 148 | + hf_outputs = hf_runner.forward( |
| 149 | + prompts, |
| 150 | + max_new_tokens=max_new_tokens, |
| 151 | + lora_paths=lora_paths, |
| 152 | + ) |
| 153 | + |
| 154 | + print("SRT outputs:", [s for s in srt_outputs.output_strs]) |
| 155 | + print("HF outputs:", [s for s in hf_outputs.output_strs]) |
| 156 | + |
| 157 | + for srt_out, hf_out in zip( |
| 158 | + srt_outputs.output_strs, hf_outputs.output_strs |
| 159 | + ): |
| 160 | + srt_str = srt_out.strip() |
| 161 | + hf_str = hf_out.strip() |
| 162 | + rouge_tol = model_case.rouge_l_tolerance |
| 163 | + rouge_score = calculate_rouge_l([srt_str], [hf_str])[0] |
| 164 | + if rouge_score < rouge_tol: |
| 165 | + raise AssertionError( |
| 166 | + f"ROUGE-L score {rouge_score} below tolerance {rouge_tol} " |
| 167 | + f"for base '{base_path}', adaptor '{lora_paths}', backend '{backend}', prompt: '{prompts}...'" |
| 168 | + ) |
| 169 | + |
| 170 | + print(f"--- Batch {i+1} Comparison Passed --- ") |
| 171 | + |
| 172 | + def test_ci_lora_models(self): |
| 173 | + self._run_lora_multiple_batch_on_model_cases(CI_MULTI_LORA_MODELS) |
| 174 | + |
| 175 | + def test_all_lora_models(self): |
| 176 | + if is_in_ci(): |
| 177 | + return |
| 178 | + |
| 179 | + filtered_models = [] |
| 180 | + for model_case in ALL_OTHER_MULTI_LORA_MODELS: |
| 181 | + if "ONLY_RUN" in os.environ and os.environ["ONLY_RUN"] != model_case.base: |
| 182 | + continue |
| 183 | + filtered_models.append(model_case) |
| 184 | + |
| 185 | + self._run_lora_multiple_batch_on_model_cases(filtered_models) |
75 | 186 |
|
76 | 187 |
|
77 | 188 | if __name__ == "__main__":
|
|
0 commit comments