• chatglm.cpp使用手记


    1.环境准备

    cat /etc/os-release
    NAME="Ubuntu"
    VERSION="20.04 LTS (Focal Fossa)"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 20.04 LTS"
    VERSION_ID="20.04"
    HOME_URL="https://www.ubuntu.com/"
    SUPPORT_URL="https://help.ubuntu.com/"
    BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
    PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
    VERSION_CODENAME=focal
    UBUNTU_CODENAME=focal
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2 githup下载项目

    git clone --recursive https://github.com/li-plus/chatglm.cpp.git && cd chatglm.cpp
    
    • 1

    3 安装加载和量化拥抱面模型所需的软件包

    python3 -m pip install -U pip
    python3 -m pip install torch tabulate tqdm transformers accelerate sentencepiece
    
    • 1
    • 2

    4 准备编译环境

    #准备>=3.12版本cmake
    #如果是ubuntu 2004默认安装的cmake是3.16,如果是Ubuntu1804需要如下步骤安装cmake
    #CMake 官方提供了一个 APT 存储库,你可以使用它来安装最新版本的 CMake。执行以下步骤:
    #安装 apt-transport-https 包以允许使用 HTTPS 下载
    sudo apt-get install apt-transport-https
    #添加 Kitware APT 存储库的 GPG 密钥:
    wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc | sudo apt-key add -
    #添加 Kitware APT 存储库到你的源列表:
    apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'
    apt-get update
    apt-get install cmake
    cmake --version
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5 开始编译项目

    pwd
    /software/chatglm.cpp
    
    mkdir build
    cp ./CMakeLists.txt build
    #纯CPU环境: 
    cmake -B build
    cmake --build build -j --config Release
    #有(nvidia cuda) GPU 环境:
    cmake -B build -DGGML_CUBLAS=ON && cmake --build build -j --config Release
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    6 模型转换

    #pytorch_model-00001-of-00007 网址https://cloud.tsinghua.edu.cn/d/674208019e314311ab5c/?p=%2F&mode=list
    python3 ./chatglm_cpp/convert.py -i tsinghua_mode/pytorch_model-00001-of-00007.bin -t q4_0  -o chatglm2-6b.bin
    
    
    • 1
    • 2
    • 3

    7 CLI 验证

    ./build/bin/main -m chatglm-ggml-f16.bin -i
    
    • 1

    请添加图片描述

    8 web 验证

    python3 ./examples/web_demo.py -m chatglm-ggml-f16.bin
    
    
    • 1
    • 2

    请添加图片描述

    9 api验证

    $MODEL=/software/chatglm.cpp/chatglm-ggml-f16.bin uvicorn api_demo:app --host 0.0.0.0 --port 8080
    INFO:     Started server process [282242]
    INFO:     Waiting for application startup.
    INFO:     Application startup complete.
    INFO:     Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
    中国四大名著是指《红楼梦》、《水浒传》、《三国演义》、《西游记》。一般认为它们的作者是曹雪芹、施耐庵、罗贯中、吴承恩。《红楼梦》通行本前八十回的作者是曹雪芹,后四十回续书的作者是高鹗。INFO:     127.0.0.1:53420 - "POST /v1/chat/completions HTTP/1.1" 200 OK
    ^CINFO:     Shutting down
    INFO:     Waiting for application shutdown.
    INFO:     Application shutdown complete.
    INFO:     Finished server process [282242]
    
    
    curl http://127.0.0.1:8080/v1/chat/completions -H 'Content-Type: application/json'     -d '{"messages": [{"role": "user", "content": "中国四大名著作者分别是谁"}]}'
    {"object":"chat.completion","response":"中国四大名著是指《红楼梦》、《水浒传》、《三国演义》、《西游记》。一般认为它们的作者是曹雪芹、施耐庵、罗贯中、吴承恩。《红楼梦》通行本前八十回的作者是曹雪芹,后四十回续书的作者是高鹗。","model":"chatglm2-6b","choices":[{"delta":{"content":"中国四大名著是指《红楼梦》、《水浒传》、《三国演义》、《西游记》。一般认为它们的作者是曹雪芹、施耐庵、罗贯中、吴承恩。《红楼梦》通行本前八十回的作者是曹雪芹,后四十回续书的作者
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    10 遗留问题

    1. huggingface 克隆不下来
    2. 清华模型转换失败
    3. api_demo.py 脚本获取不到

    请添加图片描述

  • 相关阅读:
    Python基础知识点总结
    Java的Lambda表达式学习笔记:如何在lambda表达式中引用方法
    RabbitMQ笔记
    NetAssist使用
    java毕业设计乒乓球俱乐部管理源码+lw文档+mybatis+系统+mysql数据库+调试
    Java的WebSocket服务端的使用
    2024双非网安保华五(中科大)电子信息经验分享
    关于mac终端 无法显示中文 + echo cut 组合问题
    备战秋招--redis篇
    【翻译】Fast Patch-based Style Transfer of Arbitrary Style
  • 原文地址:https://blog.csdn.net/qq_46229380/article/details/133775103