|
| 1 | +from typing import OrderedDict, Type |
| 2 | + |
| 3 | +from utils.llm_data import llm_models_root |
| 4 | +from utils.util import duplicate_list_to_length, flatten_list, similar |
| 5 | + |
| 6 | +from tensorrt_llm import SamplingParams |
| 7 | +from tensorrt_llm.executor.request import LoRARequest |
| 8 | +from tensorrt_llm.llmapi.llm import BaseLLM |
| 9 | + |
| 10 | + |
| 11 | +def check_llama_7b_multi_unique_lora_adapters_from_request( |
| 12 | + lora_adapter_count_per_call: list[int], repeat_calls: int, |
| 13 | + repeats_per_call: int, llm_class: Type[BaseLLM], **llm_kwargs): |
| 14 | + """Calls llm.generate s.t. for each C in lora_adapter_count_per_call, llm.generate is called with C requests |
| 15 | + repeated 'repeats_per_call' times, where each request is configured with a unique LoRA adapter ID. |
| 16 | + This entire process is done in a loop 'repeats_per_call' times with the same requests. |
| 17 | + Asserts the output of each llm.generate call is similar to the expected. |
| 18 | + """ # noqa: D205 |
| 19 | + total_lora_adapters = sum(lora_adapter_count_per_call) |
| 20 | + hf_model_dir = f"{llm_models_root()}/llama-models/llama-7b-hf" |
| 21 | + hf_lora_dirs = [ |
| 22 | + f"{llm_models_root()}/llama-models/luotuo-lora-7b-0.1", |
| 23 | + f"{llm_models_root()}/llama-models/Japanese-Alpaca-LoRA-7b-v0" |
| 24 | + ] |
| 25 | + # Each prompt should have a reference for every LoRA adapter dir (in the same order as in hf_lora_dirs) |
| 26 | + prompt_to_references = OrderedDict({ |
| 27 | + "美国的首都在哪里? \n答案:": [ |
| 28 | + "美国的首都是华盛顿。\n\n美国的", |
| 29 | + "纽约\n\n### カンファレンスの", |
| 30 | + ], |
| 31 | + "アメリカ合衆国の首都はどこですか? \n答え:": [ |
| 32 | + "华盛顿。\n\n英国の首都是什", |
| 33 | + "ワシントン\nQ1. アメリカ合衆国", |
| 34 | + ], |
| 35 | + }) |
| 36 | + |
| 37 | + prompts_to_generate = duplicate_list_to_length( |
| 38 | + flatten_list([[prompt] * len(hf_lora_dirs) |
| 39 | + for prompt in prompt_to_references.keys()]), |
| 40 | + total_lora_adapters) |
| 41 | + references = duplicate_list_to_length( |
| 42 | + flatten_list(list(prompt_to_references.values())), total_lora_adapters) |
| 43 | + lora_requests = [ |
| 44 | + LoRARequest(str(i), i, hf_lora_dirs[i % len(hf_lora_dirs)]) |
| 45 | + for i in range(total_lora_adapters) |
| 46 | + ] |
| 47 | + llm = llm_class(hf_model_dir, **llm_kwargs) |
| 48 | + |
| 49 | + # Perform repeats of the same requests to test reuse and reload of adapters previously unloaded from cache |
| 50 | + try: |
| 51 | + for _ in range(repeat_calls): |
| 52 | + last_idx = 0 |
| 53 | + for adapter_count in lora_adapter_count_per_call: |
| 54 | + sampling_params = SamplingParams(max_tokens=20) |
| 55 | + outputs = llm.generate( |
| 56 | + prompts_to_generate[last_idx:last_idx + adapter_count] * |
| 57 | + repeats_per_call, |
| 58 | + sampling_params, |
| 59 | + lora_request=lora_requests[last_idx:last_idx + |
| 60 | + adapter_count] * |
| 61 | + repeats_per_call) |
| 62 | + for output, ref in zip( |
| 63 | + outputs, references[last_idx:last_idx + adapter_count] * |
| 64 | + repeats_per_call): |
| 65 | + assert similar(output.outputs[0].text, ref) |
| 66 | + last_idx += adapter_count |
| 67 | + finally: |
| 68 | + llm.shutdown() |
| 69 | + |
| 70 | + |
| 71 | +def check_llama_7b_multi_lora_from_request_test_harness( |
| 72 | + llm_class: Type[BaseLLM], **llm_kwargs) -> None: |
| 73 | + hf_model_dir = f"{llm_models_root()}/llama-models/llama-7b-hf" |
| 74 | + hf_lora_dir1 = f"{llm_models_root()}/llama-models/luotuo-lora-7b-0.1" |
| 75 | + hf_lora_dir2 = f"{llm_models_root()}/llama-models/Japanese-Alpaca-LoRA-7b-v0" |
| 76 | + prompts = [ |
| 77 | + "美国的首都在哪里? \n答案:", |
| 78 | + "美国的首都在哪里? \n答案:", |
| 79 | + "美国的首都在哪里? \n答案:", |
| 80 | + "アメリカ合衆国の首都はどこですか? \n答え:", |
| 81 | + "アメリカ合衆国の首都はどこですか? \n答え:", |
| 82 | + "アメリカ合衆国の首都はどこですか? \n答え:", |
| 83 | + ] |
| 84 | + references = [ |
| 85 | + "沃尔玛\n\n## 新闻\n\n* ", |
| 86 | + "美国的首都是华盛顿。\n\n美国的", |
| 87 | + "纽约\n\n### カンファレンスの", |
| 88 | + "Washington, D.C.\nWashington, D.C. is the capital of the United", |
| 89 | + "华盛顿。\n\n英国の首都是什", |
| 90 | + "ワシントン\nQ1. アメリカ合衆国", |
| 91 | + ] |
| 92 | + key_words = [ |
| 93 | + "沃尔玛", |
| 94 | + "华盛顿", |
| 95 | + "纽约", |
| 96 | + "Washington", |
| 97 | + "华盛顿", |
| 98 | + "ワシントン", |
| 99 | + ] |
| 100 | + lora_req1 = LoRARequest("luotuo", 1, hf_lora_dir1) |
| 101 | + lora_req2 = LoRARequest("Japanese", 2, hf_lora_dir2) |
| 102 | + sampling_params = SamplingParams(max_tokens=20) |
| 103 | + |
| 104 | + llm = llm_class(hf_model_dir, **llm_kwargs) |
| 105 | + try: |
| 106 | + outputs = llm.generate(prompts, |
| 107 | + sampling_params, |
| 108 | + lora_request=[ |
| 109 | + None, lora_req1, lora_req2, None, lora_req1, |
| 110 | + lora_req2 |
| 111 | + ]) |
| 112 | + finally: |
| 113 | + llm.shutdown() |
| 114 | + for output, ref, key_word in zip(outputs, references, key_words): |
| 115 | + assert similar(output.outputs[0].text, |
| 116 | + ref) or key_word in output.outputs[0].text |
0 commit comments