当前位置:   article > 正文

THUDM/chatglm2-6b-int4部署实战

chatglm2-6b-int4
 

大规模语言模型(LLM)的出现对自然语言处理领域带来了变革,然后大模型的训练,部署,推理都需要占用大量的计算资源。针对这一问题,一些参数,体积相对小的开源模型出现,如LLama-,vicuna,chatglm出现,同时为了进一步方便部署,降低硬件要求,推出了量化版模型chatglm2-6b-int4。

GPU

GPU: Telsa T4   RAM16G

资源消耗:系统的RAM4G,GPU的RAM6G

  1. # 安装transformers等包
  2. !pip install protobuf transformers==4.30.2 cpm_kernels torch>=2.0 gradio mdtex2html sentencepiece accelerate
  3. # 导入AutoTokenizer, AutoModel
  4. from transformers import AutoTokenizer, AutoModel
  5. # 设置模型名称,选择THUDM/chatglm2-6b-int4模型,
  6. # AutoTokenizer从huggingface中导入THUDM/chatglm2-6b-int4模型的tokenizer
  7. model_id = "THUDM/chatglm2-6b-int4"
  8. tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
  9. # 输出“你今天吃了吗?”的tokenizer编码
  10. encoding = tokenizer("你今天吃了吗?")
  11. encoding
  12. # 使用AutoModel加载模型model,从huggingface下载
  13. # half()参数将模型数据从float32改为float16,缩小模型尺寸,加速模型
  14. # cuda()将模型加载到GPU上,使用GPU的并行处理进行加速
  15. # model.eval()运行在eval模式,关闭dropout等操作,更准确的输出
  16. model = AutoModel.from_pretrained(model_id, trust_remote_code=True).half().cuda()
  17. print(model.__class__)
  18. model = model.eval()
  19. # 进行推理
  20. # prompt是输入
  21. # response是推理的结果
  22. # 模型model通过chat类得到结果
  23. prompt = "你好"
  24. response, history = model.chat(tokenizer, prompt, history=[])
  25. response
  26. 你好
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/901234
    推荐阅读
    相关标签