You need to enable JavaScript to run this app.
导航

搭建ChatGLM-6B大语言模型

最近更新时间2023.12.08 11:35:04

首次发布时间2023.10.13 11:34:15

本文以搭载了第四代英特尔® 至强® 可扩展处理器(Sapphire Rapids)的计算型ecs.c3i.24xlarge为例,为您介绍如何在ECS云服务器上部署ChatGLM-6B大语言模型。

背景信息

ChatGLM-6B是一个具有62亿参数的、支持中英双语问答的对话语言模型,基于General Language Model(GLM)架构,结合模型量化技术,支持在消费级的显卡上进行本地部署(INT4量化级别下最低只需6GB显存)。ChatGLM-6B使用了和ChatGLM相同的技术,针对中文问答和对话进行了优化。经过约1T标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62亿参数的ChatGLM-6B已经能生成相当符合人类偏好的回答。

环境要求

实例配置

部署ChatGLM-6B模型,推荐的实例配置如下:

类型实例规格vCPU内存(GiB)操作系统
推荐ecs.c3i.24xlarge96192Ubuntu 22.04
性价比最佳ecs.g3i.8xlarge32128Ubuntu 22.04
最小配置ecs.r3i.4xlarge16128Ubuntu 22.04

运行环境

  • Anaconda:获取包且对包能够进行管理的工具,包含了Conda、Python在内的超过180个科学包及其依赖项,用于创建Python虚拟环境。本文以Anaconda 3和Python 3.8.3为例。
  • Transformers:一种神经网络架构,用于语言建模、文本生成和机器翻译等任务。本文以4.33.2为例。
  • Gradio:快速构建机器学习Web展示页面的开源Python库。本文以3.44.4为例。

使用说明

下载本文所需软件需要访问国外网站,建议您增加网络代理(例如FlexGW)以提高访问速度。您也可以将所需软件下载到本地,参考本地数据上传到实例中。

操作步骤

步骤一:创建实例

  1. 请参考通过向导购买实例创建一台符合以下条件的实例:

    • 基础配置:
      • 计算规格:ecs.c3i.24xlarge
      • 镜像:Ubuntu 22.04
      • 存储:云盘容量在100 GiB以上
        alt
    • 网络配置:勾选“分配弹性公网IP”。
  2. 创建成功后,在实例绑定的安全组中添加入方向规则:放行TCP 7860端口,具体操作请参见修改安全组访问规则

步骤二:创建虚拟环境

  1. 执行以下命令,下载Anaconda安装包。
    wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
  2. 执行以下命令,安装Anaconda。
    bash Anaconda3-2022.05-Linux-x86_64.sh
    1. 持续按“Enter”键进行安装。
      alt
    2. 输入“yes”,并按“Enter”键确认信息。
      alt
    3. Anaconda的安装路径/root/anaconda3,请按“Enter”键确认安装。
      alt
    4. 输入“yes”,并按“Enter”键确定初始化Anaconda。
      alt
  3. 执行以下命令使配置文件生效。
    source ~/.bashrc
    回显如下,表示配置成功,进入base环境。
    alt
  4. 创建一个名为“SPR”的虚拟环境,并指定该环境中的python版本为3.8.3。
    1. 执行conda create -n SPR python=3.8.3命令。
    2. 回显Proceed ([y]/n)?时输入“y”回车确认。
    3. 执行以下命令,激活虚拟环境。
      conda activate SPR
      回显如下,表示激活成功。
      alt

步骤三:部署模型并验证

  1. 执行以下命令,下载ChatGLM-6B大模型的权重文件。
    wget https://ai-llm.tos-cn-beijing.volces.com/chatglm-6b/GLM-6B-SPR.tar.gz

  2. 执行以下命令,解压文件包。
    tar -zxvf GLM-6B-SPR.tar.gz
    执行ll命令查看解压后的文件列表,如下图所示。
    alt

  3. 执行以下命令,安装相应的Intel依赖包。
    pip install mkl==2023.1.0 intel-openmp==2023.1.0

  4. 依次执行以下命令,在Conda环境下安装内存分配器Jemalloc。

    conda install jemalloc
    pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
    pip install transformers
    
  5. 安装Gradio。

    1. 执行pip install gradio sentencepiece命令,安装Gradio。
    2. 执行pip list | grep gradio命令进行验证,回显如下表示已成功安装。
      alt
  6. 修改源。

    1. 执行vim /etc/apt/sources.list命令,打开源文件。
    2. i进入编辑模式,添加如下内容。
      deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
      deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
      deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
      
    3. esc退出编辑模式,输入:wq并按Enter键,保存并退出文件。
  7. 执行apt update命令,更新软件包。

  8. 依次执行以下命令安装依赖包。

    cd bloom  #切换至bloom目录
    pip install bloom_fp16-0.0.0-cp38-cp38-linux_x86_64.whl
    pip install chatglm_hybrid-0.0.0-cp38-cp38-linux_x86_64.whl
    pip install chatglm_fp32-0.0.0-cp38-cp38-linux_x86_64.whl
    apt install numactl 
    
  9. 安装完成后,执行cd命令退出当前目录。

  10. 修改chatglm/webui目录下的demo文件导出路径,加载本地ChatGLM模型。

    1. 执行vim read_path.py命令,新建寻址文件,并添加如下内容。

      import torch
      import os
      import sys
      
      def get_pytorch_install_dir():
          return os.path.dirname(os.path.abspath(torch.__file__))
      
      pytorch_install_dir = get_pytorch_install_dir()
      python_dir = sys.prefix
      
      print("PyTorch Library Path: " + pytorch_install_dir)
      print("Python Library Path: " + python_dir + "/lib")
      print("MKL Dynamic Library Path: " + python_dir + "/lib/libmkl_rt.so.2")
      
    2. esc退出编辑模式,输入:wq并按Enter键,保存并退出文件。

    3. 依次执行以下命令,查看Dynamic路径。

      cd chatglm
      python read_path.py 
      

      如下图,本例中的Dynamic路径为root/anaconda3
      alt

    4. 执行以下命令,打开demo文件。
      vim /root/chatglm/webui/start_demo.sh*

    5. i进入编辑模式,修改导出路径为root/anaconda3(与Dynamic保持一致),如下图所示。
      alt

  11. 依次执行以下命令,运行start_demo.sh*脚本。

    cd webui
    ./start_demo.sh*	
    

    alt

  12. 浏览器访问public URL(https://<公网IP>.gradio.live),如下图所示,表示成功使用Gradio构建AI应用程序。
    alt