当前位置:   article > 正文

qwen1.5-7B hf部署运行方法以及vllm推理框架部署方法

qwen1.5-7b

qwen1.5 模型的问答生成方式发生了变化,不再支持 mode.chat(),但整体来看, 1.5版本的问答效果确实有了很大提升。

本文仍在编辑中

qwen-7B 大语言模型的加载方式如下

qwen1.5B 大语言模型的加载方式如下:

  1. import pandas as pd
  2. import torch
  3. from transformers import AutoModelForCausalLM, AutoTokenizer # transformer>=4.37.2
  4. """================Qwen-7B-15GB-推理运行大小-17GB--理论上微调需要GPU显存至少要大于17*4,一般至少要5倍于推理运存--================="""
  5. device = "cuda"
  6. model_id = "../model/Qwen1.5-7B-Chat"
  7. # 这里设置torch_dtype=torch.bfloat16 ,否则模型会按照全精度加载,GPU推理运存会从17G翻倍到34G
  8. model= AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=False)
  9. tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=False)
  10. prompt = """帮我把空调打开。
  11. 空调温度调节到24℃。
  12. 空调打开制冷模式,风速设为低档。
  13. 根据上述信息,分别提取出空调的开关状态、温度设置、风速设置、空调模式 """
  14. print("=== * ==="*50)
  15. def qwen_chat(prompt):
  16. messages = [
  17. {"role": "system", "content": "You are a helpful assistant. "},
  18. {"role": "user", "content": prompt}
  19. ]
  20. text = tokenizer.apply_chat_template(
  21. messages,
  22. tokenize=False,
  23. add_generation_prompt=True
  24. )
  25. print("=== tokenizer is finished ===")
  26. model_inputs = tokenizer([text], return_tensors="pt").to(device)
  27. # 注意这里需要设置 pad_token_id=tokenizer.eos_token_id,否则会出现warnning错误, hf上的脚本示例没有写
  28. generated_ids = model.generate(
  29. model_inputs.input_ids,
  30. max_new_tokens=512,
  31. pad_token_id=tokenizer.eos_token_id
  32. )
  33. generated_ids = [
  34. output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
  35. ]
  36. response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
  37. print(f'response: {response}')
  38. return response
  39. if __name__ == '__main__':
  40. prompt = prompt
  41. output = qwen_chat(prompt=prompt)

vllm的推理部署的环境要求,相对hf,对环境要求比较严格, vllm需要 gpu的 cuda 11.8以上,最好是12以上,torch的版本要2.1以上,具体版本要求,可以查vllm github上不同版本对环境的依赖要求 requirements.txt ;

代码示例如下 :

  1. from vllm import LLM
  2. from transformers import TextIteratorStreamer
  3. from vllm.sampling_params import SamplingParams
  4. class QwenVllm(object):
  5. def __init__(self, gpu_num=2, max_tokens=512):
  6. self.gpu_num = gpu_num
  7. self.max_tokens = max_tokens
  8. self.model_path= "Qwen1.5-7B-Chat"
  9. self.model, self.tokenizer = self.model_load_with_vllm()
  10. def model_load_with_vllm(self):
  11. """ vllm 形式预加载 模型 """
  12. model = LLM(
  13. tokenizer=self.model_path,
  14. model=self.model_path,
  15. dtype="bfloat16",
  16. tokenizer_mode= 'slow',
  17. trust_remote_code=False,
  18. tensor_parallel_size=self.gpu_num,
  19. gpu_memory_utilization=0.8, # gpu 初始化显存占比,这里单卡48g显存
  20. max_context_len_to_capture=8192,
  21. max_model_len = 8192,
  22. )
  23. tokenizer = model.get_tokenizer()
  24. return model, tokenizer
  25. def qwen_chat_vllm(self, prompt):
  26. """ vllm batch推理注意 batch size 与 gpu 关系"""
  27. message= [
  28. {"role": "system", "content": "you are a great assistant."},
  29. {"role": "user", "content": prompt}
  30. ]
  31. text = self.tokenizer.apply_chat_template(
  32. history,
  33. tokenize=False,
  34. add_generation_prompt=True
  35. )
  36. # max_token to control the maximum output length
  37. sampling_params = SamplingParams(
  38. temperature=0.7,
  39. top_p=0.8,
  40. repetition_penalty=1.05,
  41. max_tokens=self.max_tokens)
  42. outputs = self.model.generate([text], sampling_params)
  43. response = []
  44. for output in outputs:
  45. # prompt = output.prompt
  46. generated_text = output.outputs[0].text
  47. response.append(generated_text)
  48. return response
  49. if __name__ == '__main__':
  50. run = QwenVllm(gpu_num=2, max_tokens=1024)
  51. # 大模型单论对话生成
  52. prompt = """你介绍一下上海市吧"""
  53. response = run.qwen_chat(prompt=prompt)
  54. print(response)

vllm加载模型的方式有很多种,不一定局限于上面的模型加载推理部署方式,具体可以到vllm官网文档去细看。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/618917
推荐阅读
相关标签
  

闽ICP备14008679号