参考:

部署环境

腾讯云GPU计算型GN7

https://cloud.tencent.com/document/product/560/19700#GN7

  • IP 49.233.55.37
  • 用户名 ubuntu
  • CPU: 8 核
  • 内存: 32GB
  • GPU: 1 * NVIDIA T4
  • 镜像: Pytorch 1.9.1 Ubuntu 18.04 GPU基础镜像 (预装460驱动)

部署过程

下载 ChatGLB

git clone [https://github.com/THUDM/ChatGLM-6B](https://github.com/THUDM/ChatGLM-6B)

安装依赖

  1. cd ChatGLM-6B
  2. pip install -r requirements.txt

安装完成后,在 /home/ubuntu/.local/bin/ 生成的脚本

部署测试 - 图1

使用模型

编写 python 代码

  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
  3. model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
  4. model = model.eval()
  5. response, history = model.chat(tokenizer, "你好", history=[])
  6. print(response)
  7. response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
  8. print(response)
  • 以上代码自动下载 chatglm-6b 模型,模型保存地址 /home/ubuntu/.cache/huggingface
  • 下载时间较长,耐心等待

运行网页版

依赖 gradio

  1. pip install gradio
  2. python web_demo.py

发布到公网,编辑 web_demo.py

  1. demo.queue().launch(share=False, inbrowser=True)

绑定 127.0.0.1:

通过nginx端口转发绑定到公网IP

  1. 安装 nginx, sudo apt install nginx
  2. 添加配置文件 chatglm.conf
  1. server {
  2. listen 80 default_server;
  3. listen [::]:80 default_server;
  4. server_name _;
  5. location / {
  6. # Serve GRADIO 7860
  7. proxy_pass http://localhost:7860;
  8. proxy_http_version 1.1;
  9. proxy_set_header Upgrade $http_upgrade;
  10. proxy_set_header Connection 'upgrade';
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. proxy_cache_bypass $http_upgrade;
  15. proxy_read_timeout 86400;
  16. proxy_redirect off;
  17. }
  18. location /static/ {
  19. # Serve backend from port
  20. rewrite /static/(.*) /$1 break;
  21. proxy_pass http://localhost:8000;
  22. proxy_http_version 1.1;
  23. proxy_set_header Upgrade $http_upgrade;
  24. proxy_set_header Connection 'upgrade';
  25. proxy_set_header Host $host;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. proxy_cache_bypass $http_upgrade;
  29. proxy_read_timeout 86400;
  30. proxy_redirect off;
  31. }
  32. }

访问:http://49.233.55.37

部署测试 - 图2

查看 GPU 信息

执行命令: nvidia-smi

部署测试 - 图3

服务停止后,释放 GPU

部署测试 - 图4

微调

参考文档 https://github.com/THUDM/ChatGLM-6B/blob/main/ptuning/README.md

ADGEN 数据集任务为根据输入(content)生成一段广告词(summary)。
  1. {
  2. "content": "类型#上衣*版型#宽松*版型#显瘦*图案#线条*衣样式#衬衫*衣袖型#泡泡袖*衣款式#抽绳",
  3. "summary": "这件衬衫的款式非常的宽松,利落的线条可以很好的隐藏身材上的小缺点,穿在身上有着很好的显瘦效果。领口装饰了一个可爱的抽绳,漂亮的绳结展现出了十足的个性,配合时尚的泡泡袖型,尽显女性甜美可爱的气息。"
  4. }

训练数据:https://cloud.tsinghua.edu.cn/f/b3f119a008264b1cabd1/?dl=1

解压到 ChatGLM/ptunning/ 目录

训练

  1. bash train.sh
  • 执行时间长,10小时

部署测试 - 图5训练结果输出到 output 目录

部署测试 - 图6

  • 加载模型

<font style="color:rgb(31, 35, 40);">bash web_demo.sh</font>

  • 问题

训练后只能回答训练集中的答案,可能是参数配置有问题

部署测试 - 图7