|
| 1 | +import contextlib |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import time |
| 6 | +from dataclasses import asdict |
| 7 | + |
| 8 | +from transformers import AutoTokenizer |
| 9 | + |
| 10 | +# setting for rerope |
| 11 | +os.environ["VLLM_USE_REROPE"] = "true" |
| 12 | + |
| 13 | +# Third Party |
| 14 | +from vllm import LLM, SamplingParams |
| 15 | +from vllm.config import KVTransferConfig |
| 16 | +from vllm.engine.arg_utils import EngineArgs |
| 17 | + |
| 18 | +from ucm.logger import init_logger |
| 19 | + |
| 20 | +logger = init_logger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +def setup_environment_variables(): |
| 24 | + os.environ["VLLM_USE_V1"] = "1" |
| 25 | + os.environ["PYTHONHASHSEED"] = "123456" |
| 26 | + |
| 27 | + os.environ["VLLM_ATTENTION_BACKEND"] = "TRITON_ATTN_VLLM_V1" |
| 28 | + os.environ["REROPE_WINDOW"] = "32768" |
| 29 | + os.environ["TRAINING_LENGTH"] = "32768" |
| 30 | + |
| 31 | + global data_dir |
| 32 | + data_dir = os.getenv("DATA_DIR", "/home/data/kv_cache") |
| 33 | + if not os.path.isdir(data_dir): |
| 34 | + data_dir = input( |
| 35 | + "Enter the directory for UCMStore to save kv cache, e.g. /home/data/kv_cache: " |
| 36 | + ) |
| 37 | + create = input(f"Directory {data_dir} dose not exist. Create it? (Y/n): ") |
| 38 | + if create.lower() == "y": |
| 39 | + os.makedirs(data_dir, exist_ok=True) |
| 40 | + else: |
| 41 | + print("Exiting. Directory not created.") |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + |
| 45 | +@contextlib.contextmanager |
| 46 | +def build_llm_with_uc(module_path: str, name: str, model: str): |
| 47 | + ktc = KVTransferConfig( |
| 48 | + kv_connector=name, |
| 49 | + kv_connector_module_path=module_path, |
| 50 | + kv_role="kv_both", |
| 51 | + kv_connector_extra_config={ |
| 52 | + "ucm_connectors": [ |
| 53 | + { |
| 54 | + "ucm_connector_name": "UcmNfsStore", |
| 55 | + "ucm_connector_config": { |
| 56 | + "storage_backends": data_dir, |
| 57 | + "use_direct": False, |
| 58 | + }, |
| 59 | + } |
| 60 | + ], |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + llm_args = EngineArgs( |
| 65 | + model=model, |
| 66 | + kv_transfer_config=ktc, |
| 67 | + hf_overrides={ |
| 68 | + "max_position_embeddings": 327680, |
| 69 | + }, |
| 70 | + gpu_memory_utilization=0.9, |
| 71 | + max_num_batched_tokens=8192, |
| 72 | + block_size=16, |
| 73 | + enforce_eager=True, |
| 74 | + tensor_parallel_size=2, |
| 75 | + ) |
| 76 | + |
| 77 | + llm = LLM(**asdict(llm_args)) |
| 78 | + try: |
| 79 | + yield llm |
| 80 | + finally: |
| 81 | + logger.info("LLM engine is exiting.") |
| 82 | + |
| 83 | + |
| 84 | +def print_output( |
| 85 | + llm: LLM, |
| 86 | + prompt: list[str], |
| 87 | + sampling_params: SamplingParams, |
| 88 | + req_str: str, |
| 89 | +): |
| 90 | + start = time.time() |
| 91 | + outputs = llm.generate(prompt, sampling_params) |
| 92 | + print("-" * 50) |
| 93 | + for output in outputs: |
| 94 | + generated_text = output.outputs[0].text |
| 95 | + print(f"Generated text: {generated_text!r}") |
| 96 | + print(f"Generation took {time.time() - start:.2f} seconds, {req_str} request done.") |
| 97 | + print("-" * 50) |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + module_path = "ucm.integration.vllm.ucm_connector" |
| 102 | + name = "UCMConnector" |
| 103 | + model = os.getenv("MODEL_PATH", "/home/models/Qwen2.5-14B-Instruct") |
| 104 | + if not os.path.isdir(model): |
| 105 | + model = input("Enter path to model, e.g. /home/models/Qwen2.5-14B-Instruct: ") |
| 106 | + if not os.path.isdir(model): |
| 107 | + print("Exiting. Incorrect model_path") |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + tokenizer = AutoTokenizer.from_pretrained(model, use_chat_template=True) |
| 111 | + setup_environment_variables() |
| 112 | + |
| 113 | + with build_llm_with_uc(module_path, name, model) as llm: |
| 114 | + |
| 115 | + data_all = [] |
| 116 | + path_to_dataset = os.getenv( |
| 117 | + "DATASET_PATH", "/home/data/Longbench/data/multifieldqa_zh.jsonl" |
| 118 | + ) |
| 119 | + if not os.path.isfile(path_to_dataset): |
| 120 | + path_to_dataset = input( |
| 121 | + "Enter path to one of the longbench dataset, e.g. /home/data/Longbench/data/multifieldqa_zh.jsonl: " |
| 122 | + ) |
| 123 | + if not os.path.isfile(path_to_dataset): |
| 124 | + print("Exiting. Incorrect dataset path") |
| 125 | + sys.exit(1) |
| 126 | + with open(path_to_dataset, "r", encoding="utf-8") as f: |
| 127 | + for line in f: |
| 128 | + data_all.append(json.loads(line)) |
| 129 | + |
| 130 | + materials = [] |
| 131 | + questions = [] |
| 132 | + references = [] |
| 133 | + batch_size = 30 |
| 134 | + num_batch = 2 |
| 135 | + for idx in range(num_batch): |
| 136 | + data = data_all[idx * batch_size : (idx + 1) * batch_size] |
| 137 | + |
| 138 | + materials.append( |
| 139 | + "\n\n".join( |
| 140 | + [ |
| 141 | + f"【语料{i+1}】\n{item.get('context', '')}" |
| 142 | + for i, item in enumerate(data) |
| 143 | + ] |
| 144 | + ) |
| 145 | + ) |
| 146 | + questions.append( |
| 147 | + "\n".join( |
| 148 | + [ |
| 149 | + f"{i+1}. {item.get('input', '')}" |
| 150 | + for i, item in enumerate(data[:15]) |
| 151 | + ] |
| 152 | + ) |
| 153 | + ) |
| 154 | + references.append( |
| 155 | + [ |
| 156 | + f"{i+1}. {item.get('answers', '')}" |
| 157 | + for i, item in enumerate(data[:15]) |
| 158 | + ] |
| 159 | + ) |
| 160 | + |
| 161 | + system_prompt = "你是一个AI助手,请根据以下材料回答问题。" |
| 162 | + tokenized_inputs = [] |
| 163 | + for material, question in zip(materials, questions): |
| 164 | + content = ( |
| 165 | + "请根据以下文本内容回答后面的问题:\n\n" |
| 166 | + "【文本内容开始】\n" |
| 167 | + f"{material}\n" |
| 168 | + "【文本内容结束】\n\n" |
| 169 | + "请直接回答以下问题:\n" |
| 170 | + f"{question}" |
| 171 | + ) |
| 172 | + |
| 173 | + messages = [ |
| 174 | + {"role": "system", "content": system_prompt}, |
| 175 | + {"role": "user", "content": content}, |
| 176 | + ] |
| 177 | + inputs = tokenizer.apply_chat_template( |
| 178 | + messages, |
| 179 | + add_generation_prompt=True, |
| 180 | + tokenize=False, |
| 181 | + ) |
| 182 | + tokenized_inputs.append(inputs) |
| 183 | + |
| 184 | + sampling_params = SamplingParams(temperature=0.8, top_p=0.95, max_tokens=2048) |
| 185 | + |
| 186 | + for req in range(num_batch): |
| 187 | + print_output( |
| 188 | + llm, tokenized_inputs[req], sampling_params, "request_" + str(req) |
| 189 | + ) |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + main() |
0 commit comments