為了測試vllm的并行加速效果,采用同樣的5個提問,編寫兩個不同的python腳本,分別是compare_vllm.py和compare_chatglm3.py,其中compare_vllm.py采用vllm加速。
服務器參數:
操作系統 | ubuntu 22.04 |
CPU | i7 14700k |
內存 | dd5 128G |
顯卡 | 3090 24G 兩塊 |
compare_vllm.py的代碼如下:
import time
from vllm import LLM, SamplingParamsdef main():# 定義批量數據desc = "這張圖片中有一位母親和兒子正在一起開心的笑母親穿著花裙子,兒子穿著運動鞋和牛仔短褲,他們站在方形的磚塊地面"query = f"對于以下圖片描述提取標簽,每一個標簽作為數組的一個元素,以JSON格式輸出。只輸出標簽,不用解釋:\n'{desc}'"prompts = ["中華人民共和國成立的日期是哪一天?","為什么AI在這一兩年爆發了?",query,"中美人口分別是多少?美國有多少中國的移民?","你擅長數學計算嗎?",]sampling_params = SamplingParams(temperature=0.1, top_p=0.5, max_tokens=4096)path = '/home/data/model/zhipu/chatglm3-6b'llm = LLM(model=path, trust_remote_code=True, tokenizer_mode="auto", tensor_parallel_size=2, dtype="auto")start_time = time.time() # 獲取當前時間outputs = llm.generate(prompts, sampling_params)# 輸出結果for output in outputs:prompt = output.promptgenerated_text = output.outputs[0].textprint(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")end_time = time.time() # 獲取當前時間print(f"The code run for {end_time - start_time} seconds.") if __name__ == "__main__":main()
compare_chatglm3.py的代碼如下:
import time
from transformers import AutoTokenizer, AutoModeldef main():MODEL_PATH = "/home/data/model/zhipu/chatglm3-6b"tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)model = AutoModel.from_pretrained(MODEL_PATH, trust_remote_code=True, device_map="auto").eval()desc = "這張圖片中有一位母親和兒子正在一起開心的笑母親穿著花裙子,兒子穿著運動鞋和牛仔短褲,他們站在方形的磚塊地面"query = f"對于以下圖片描述提取標簽,每一個標簽作為數組的一個元素,以JSON格式輸出。只輸出標簽,不用解釋:\n'{desc}'"prompts = ["中華人民共和國成立的日期是哪一天?","為什么AI在這一兩年爆發了?",query,"中美人口分別是多少?美國有多少中國的移民?","你擅長數學計算嗎?",]start_time = time.time() # 獲取當前時間for p in prompts:response, history = model.chat(tokenizer, p, history=[], role="user") #,top_p=0.8, temperature=0.2print(response)end_time = time.time() # 獲取當前時間print(f"The code run for {end_time - start_time} seconds.") if __name__ == "__main__":main()
python compare_vllm.py:
? 輸出:The code run for 3.9577383995056152 seconds.
? 最大顯存使用(因為動態在變化,這個并不精確):2個各22G,總計44G
??
compare_chatglm3.py:
? 輸出:The code run for 12.522217512130737 seconds.
? 最大顯存使用(因為動態在變化,這個并不精確):2個各6G,總計12G
很明顯,vllm確實具備并行加速性能,差不多是3倍。當然顯存的峰值使用量明顯增多,差不多是不用vllm的3.7倍。當然,可以考慮采用進程方式部署多個非vllm服務實現并行,但是那樣的話部署會麻煩一些。