大模型加速与性能对比

大模型加速与性能对比

阿里通义千问flash-attention加速

正常运行通义千问会提示安装flash-attention以获得更快的推理速度,缺少这个包并不影响模型运行。

事实证明安装之后对于推理速度的提升也很小(5%),网上说对于微调训练的速度提升比较大,因为是在内网环境下,安装还费了一番周折。本人环境

  • torch2.0.1+cu118
  • cuda11.8
  • nvidia-driver535.98
  • gcc9.4

直接在物理机上安装

这种情况比较简单,但是需要有cuda的devel环境,而不仅仅是runtime环境。

内网环境无法直接安装flash-attn,从github下载最新的代码,根据自己的环境下载flash-attn的whl包。

# 安装依赖
pip install packaging ninja
# 安装flash-attn
pip install ./flash_attn-2.2.0+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl
# 编译安装ayer_norm和rotary
cd ./csrc/layer_norm && python setup.py install
cd ./csrc/rotary && python setup.py install

注意版本问题,编译安装这两个对于cuda版本、显卡要求比较严格,版本不匹配会出现不支持的架构等问题。

docker安装

直接贴本人运行大模型和百度飞桨NLP专用镜像的Dockerfile,按照自己的需求修改。

# devel,cudnn
FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu20.04

LABEL author="huangchunfeng"

WORKDIR /root

RUN mkdir /root/.pip

# 指定内网apt源
ADD ./sources.list /etc/apt
# 内网pip源
ADD ./pip.conf /root/.pip
# 安装python依赖
RUN apt-get update && apt-get install -y \
    wget \
    build-essential \
    libssl-dev \
    zlib1g-dev \
    libncurses5-dev \
    libncursesw5-dev \
    libreadline-dev \
    libsqlite3-dev \
    libgdbm-dev \
    libdb5.3-dev \
    libbz2-dev \
    libexpat1-dev \
    liblzma-dev \
    libffi-dev \
    libgdbm-compat-dev

COPY ./Python-3.8.18.tgz /root/Python-3.8.18.tgz

RUN tar xzvf /root/Python-3.8.18.tgz -C /root

# 编译python并设置为默认解释器
RUN cd /root/Python-3.8.18 \
    && ./configure --enable-optimizations --with-ensurepip=install \
    && make -j $(nproc) \
    && make altinstall \
    && update-alternatives --install /usr/bin/python python /usr/local/bin/python3.8 1 \
    && update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.8 1 \
    && rm -rf /root/Python-3.10.18.tgz /root/Python-3.10.18

RUN pip install transformers uvicorn fastapi tiktoken transformers_stream_generator sentencepiece

RUN pip install packaging ninja torch==2.0.1+cu118

COPY './flash_attn-2.2.0+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl' /root

RUN pip install ./flash_attn-2.2.0+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl

COPY ./csrc /root/csrc
ENV CUDA_HOME=/usr/local/cuda-11.8

# 编译安装,比较耗时
RUN cd ./csrc/layer_norm && python setup.py install

RUN cd ./csrc/rotary && python setup.py install

RUN pip install paddlepaddle-gpu==2.5.0.post117

RUN pip install paddlenlp

RUN pip install LAC

RUN pip install vllm loguru

RUN rm -rf  ./csrc /root/.cache

fastllm加速

国产开源项目,github地址
C++重写了一些算子,支持量化加速。

vllm加速

github地址

提高整体吞吐量,单batch效果不明显,预先分配大量显存,提高推理速度。

vllm加速通义千问Chat模型

比较新的的vllm对qwen的支持更好,一行命令启动后,调用时需要额外指定两个参数

启动命令

python -m vllm.entrypoints.openai.api_server --model /Qwen-7B-Chat --served-model-name qwen-7b --trust-remote-code

调用参数

{
"model":"qwen-7b",
"max_tokens":1024, // 调大点
"stop":["<|im_end|>"],
"messages":[
    {"role":"system", "content":"You are a helpful assitant."},
    {"role":"user", "content":"你好"}
  ]
}

接收流式输出

在浏览器端,使用EventSource对象处理event-stream数据。
在服务器端,使用requests和aiohttp读取event-stream流。
使用requests

import requests

response = requests.get("http://127.0.0.1:8000/v1/chat/completions", stream=True,
                        json={'model': 'qwen-7b', 'messages': [{'role': 'user', 'content': '你好'}], 'stream': True})
while True:
    chunk = response.raw._fp.read1()
    if not chunk:
        break
    print(chunk)

使用aiohttp

import asyncio

import aiohttp


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("http://127.0.0.1:8000/v1/chat/completions", chunked=True,
                               json={'model': 'qwen-7b', 'messages': [{'role': 'user', 'content': '你好'}],
                                     'stream': True}) as response:
            async for chunk in response.content.iter_any():
            	print(chunk.decode())

性能对比

本文在国产化和非国产化软硬件环境下对系统分别进行了部署,测试验证通过,并对性能进行了对比分析。两套环境的软硬件情况如下表所示。

国产化环境非国产化环境
CPUHygon C86 7381 32-core Processor,32核128线程13th Gen Intel® Core™ i9-13900K,24核32线程
GPUDCU Z100L,32G显存RTX A6000,48G显存
OS银河麒麟V10(SP3)Ubuntu20.04,Linux version 5.15.0-79-generic
GPU ToolkitDTK23.04CUDA11.8
Pytorch曙光pytorch1.10pytorch2.1.2+cu118
vllm0.2.7(适配rocm)0.2.7+cu118

在两套环境下针对不同数据类型、不同推理加速框架,对清华的ChatGLM2-6B,阿里的Qwen-7B-Chat和Qwen-14B-Chat进行单个batch的测试,记录生成token/字符的速度如下表所示,不同的测试用例可能会有不同的结果。

环境数据类型推理加速ChatGLM2-6BQwen-7B-ChatQwen-14B-Chat
国产化float169.2/14.76.4/10.24.1/6.2
国产化float16fastllm24.9/40.718.4/29.012.5/20.04
国产化int8fastllm26.9/43.720.7/33.212.4/19.7
非国产化float16vllm51.9/86.543.4/67.424.2/35.1

fastllm和vllm是两套大模型推理加速框架,能够提升多种模型的推理速度,截至2024年2月20日,国产化环境适配了fastllm但没有适配vllm。

从上表可以看出,国产化环境的计算速度相比非国产化环境仍有较大差距。经int8量化,fastllm加速后模型的推理速度提升明显,Qwen-7B输出速度达到20.7token/s(33.2字符/每秒),在不考虑并发的情况下,该速度可以被用户接受。

多batch对比

有上一节单batch下的测试结果,综合考虑模型能力和推理速度,确定了使用的模型,量化参数和加速框架,国产化环境下分别是Qwen-7B-Chat,int8和fastllm,非国产化环境下分别是Qwen-7B-Chat和vllm。

本问答系统的响应耗时主要在大模型生成回答部分,检索耗时相对可忽略不计,所以对并发情况下模型服务的吞吐量、负载能力等进行了测试,国产化环境和非国产化环境结果分别如表x.x和x.x所示。

BatchLatancy/sRequest throughput/sInput token throughput/sOutput token throughput/sMean TTFT/msP99 TTFT/msMean TPOT/msP99 TPOT/ms
12.750.36381.255.102108.292108.29196.13196.13
412.650.32182.0312.883403.155379.25362.75560.57
822.230.36187.0613.417187.4510908.13634.551058.37
1654.260.29109.3019.9110710.5319530.09838.361731.22
64112.800.57111.7224.1335700.0072476.084461.344129.61
128Out of memory
BatchLatancy/sRequest throughput/sInput token throughput/sOutput token throughput/sMean TTFT/msP99 TTFT/msMean TPOT/msP99 TPOT/ms
10.511.962053.8527.463.963.9636.1436.14
42.631.52874.9972.5778.86213.6739.2951.71
82.952.721411.23109.97292.65483.5449.5372.77
166.232.57951.59188.71299.82559.3049.7289.84
648.937.171410.84335.86681.81942.69171.151977.13
12830.184.24711.37215.611430.704702.94218.552154.55

各指标含义如下表所示

指标含义
Batch并发数
Latancy完成所有请求的时延,单位:秒
Request throughput平均每秒处理的请求数
Input token throughput平均每秒处理输入的token数
Output token throughput平均每秒输出的token数
Mean TTFT(Time To First Token)从发起请求到输出第一个token的平均时延,单位:毫秒
P99 TTFT从发起请求到输出第一个token时延的99分位数,单位:毫秒
Mean TPOT(Time Per Output Token)输出每个token平均耗时,单位:毫秒
P99 TPOT输出每个token的耗时的99分位数,单位:毫秒

本次测试使用的用例比较贴合实际情况,让大模型结合较长段落回答问题,所以结果中平均每秒处理的输入token数较大。

从各项指标可以看出,国产化显卡DCUZ00L的算力还不足,软硬件适配程度不够,导致并发情况下吞吐量小,响应时间偏大。非国产化环境下使用vllm进行推理加速,并发情况下吞吐量提升明显,batch在8到16时,处理完所有请求的时间在2到3秒,是比较理想的情况。

尽管vllm支持多卡并行,但算力资源有限,本文并未对多卡并行的性能进行测试。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值