开源模型应用落地-qwen-7b-chat与vllm实现推理加速的正确姿势(一)

2024-03-25 1388阅读

一、前言

    基于真实生产级项目分享,帮助有需要的同学快速构建完整可交付项目

开源模型应用落地-qwen-7b-chat与vllm实现推理加速的正确姿势(一)
(图片来源网络,侵删)

    项目流程包括(去掉业务部分):

  1.   开源模型测试,包括baichuan、qwen、chatglm、bloom
  2.   数据爬取及清洗
  3.   模型微调及评估
  4.   搭建AI交互能力
  5.   搭建IM交互能力
  6.   搭建违禁词识别能力
  7.   优化模型推理速度
  8.   增强模型长期记忆能力

二、术语介绍

    2.1. vLLM

    vLLM是一个开源的大模型推理加速框架,通过PagedAttention高效地管理attention中缓存的张量,实现了比HuggingFace Transformers高14-24倍的吞吐量。

    2.2. qwen-7b

    通义千问-7B(Qwen-7B) 是阿里云研发的通义千问大模型系列的70亿参数规模的模型。 

    2.3.Anaconda

    Anaconda(官方网站)就是可以便捷获取包且对包能够进行管理,同时对环境可以统一管理的发行版本。Anaconda包含了conda、Python在内的超过180个科学包及其依赖项。


三、构建环境

    3.1. 基础环境及前置条件

  1.  操作系统:centos7
  2.  Tesla V100-SXM2-32GB  CUDA Version: 12.2
  3.  提前下载好qwen-7b-chat模型

          通过以下两个地址进行下载,优先推荐魔搭

          https://modelscope.cn/models/qwen/Qwen-7B-Chat/files

          ​​​​​​https://huggingface.co/Qwen/Qwen-7B-Chat/tree/main

          

    3.2. Anaconda安装

        1.  更新软件包

              sudo yum upgrade -y

         2. 下载Anaconda

             wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh

         3. 安装

             默认安装

             bash Anaconda3-2022.10-Linux-x86_64.sh

             -p 指定安装目录为/opt/anaconda3

             bash Anaconda3-2022.10-Linux-x86_64.sh -p /opt/anaconda3

          4. 初始化

             source ~/.bashrc

          5. 验证安装结果

              conda --version

          6. 配置镜像源

              conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

              conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

              conda config --set show_channel_urls yes

    3.3. 创建虚拟环境

        3.3.1.创建新环境

            conda create --name vllm python=3.10

        3.3.2.切换环境

            conda activate vllm

  3.4. vLLM安装

        3.4.1.安装软件包

             pip install vllm -i https://pypi.tuna.tsinghua.edu.cn/simple

             pip install tiktoken -i https://pypi.tuna.tsinghua.edu.cn/simple

             ps: vllm版本为0.2.7,tiktoken版本为0.5.2

        3.4.2.查看已软件包   

            conda list 或者 pip list 

            注意:上述命令必须先切换至vllm虚拟环境

四、部署服务

    4.1. 启动vllm服务

        python -m vllm.entrypoints.api_server  --model  /data/model/qwen-7b-chat  --swap-space 24 --disable-log-requests --trust-remote-code --max-num-seqs 256 --host 0.0.0.0 --port 9000  --dtype float16 --max-parallel-loading-workers 1  --enforce-eager

        常用参数:

         --model

           Name or path of the huggingface model to use.

         --trust-remote-code

           Trust remote code from huggingface

        --dtype {auto,half,float16,bfloat16,float,float32}

          Data type for model weights and activations.

             • “auto” will use FP16 precision for FP32 and FP16 models, and BF16 precision for BF16 models.

             • “half” for FP16. Recommended for AWQ quantization.

             • “float16” is the same as “half”.

             • “bfloat16” for a balance between precision and range.

            • “float” is shorthand for FP32 precision.

            • “float32” for FP32 precision

         --swap-space

            CPU swap space size (GiB) per GPU.

         --max-num-seqs

            Maximum number of sequences per iteratio

          --quantization (-q) {awq,squeezellm,None}

            Method used to quantize the weights.


五、测试

    5.1. 流式案例

import threading
import requests
import json
class MyThread(threading.Thread):
    def run(self):
        headers = {"User-Agent": "Stream Test"}
        pload = {
                "prompt": "system\n你是一位知名作家,名字叫张三,你擅长写作.\nuser\n以中秋为主写一篇1000字的文章\nassistant\n",
                "n": 1,
                "temperature": 0.35,
                "max_tokens": 8192,
                "stream": True,
                "stop": ["", "",]
            }
        #此处端口9000要与vLLM Server发布的端口一致
        response = requests.post("http://127.0.0.1:9000/generate", headers=headers, json=pload, stream=True)
        for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b"\0"):
                if chunk:
                    now_thread = threading.current_thread()
                    data = json.loads(chunk.decode("utf-8"))
                    output = data["text"]
                    print(f'now thread name: {now_thread.name},output: {output}')
if __name__ == '__main__':
    threads = []
    for i in range(1, 10, 1):
        t = MyThread()
        threads.append(t)
    # 启动线程
    for t in threads:
        t.start()
    # 等待所有线程完成
    for t in threads:
        t.join()

六、后续

  1. 支持多轮对话
  2. 支持高可用
  3. 兼容复杂业务场景
  4. 性能优化
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]