常用推理加速框架及用法(vLLM/DeepSpeed-MII/LightLLM/TensorRT-LLM)

本文介绍了三个开源的深度学习语言模型库:vLLM以高效服务和多GPU支持见长,DeepSpeed-MII关注吞吐量和成本效益,TensorRT-LLM则提供了高效的TensorRT引擎。文章详细阐述了它们的特点、安装步骤和使用方法。
摘要由CSDN通过智能技术生成

1.1 vLLM

1.1.1 简介

vLLM是一个快速且易于使用的LLM推理和服务库。vLLM具有以下特点:
(1) 先进的服务吞吐量;
(2) PagedAttention对注意力Keys和Values存储的有效管理;
(3) 连续批处理传入请求;
(4) 使用CUDA/HIP图快速执行模型;
(5) 量化:GPTQ、AWQ、SqueezeLLM、FP8KV缓存
(6) 优化的CUDA内核;
(7) 具有各种解码算法的高吞吐量服务,包括并行采样、波束搜索等;
(8) 分布式推理的张量并行性支持;
(9) 兼容OpenAI的API服务器;
(10) 前缀缓存支持、Multi-lora支持;
项目地址:https://github.com/vllm-project/vllm

1.1.2 快速开始

(1) 安装环境
OS:Linux Python:3.8-3.11 GPU:V100/T4/A100/RTX20XX/H100
(2) 安装教程

Pip安装(适用于CUDA12.1)(建议使用全新的CUDA环境安装):
#(Recommended) Create a new conda environment.
conda create -n myenv python=3.9 -y
conda activate myenv
#Install vLLM with CUDA 12.1.
pip install vllm

注意:安装完成后,运行时可能不支持多卡张量并行,需要重新安装nccl,
安装方式详见:
https://blog.csdn.net/A_Student10000/article/details/131726078

从源码生成:
git clone https://github.com/vllm-project/vllm.git
cd vllm
pip install -e .  # This may take 5-10 minutes.
如果编译出错,建议用NVIDIA Pytorch Docker或者重装CUDA Toolkit:
#Use `--ipc=host` to make sure the shared memory is large enough.
docker run --gpus all -it --rm --ipc=host nvcr.io/nvidia/pytorch:23.10-py3

(3) 用法
默认情况下,vLLM从HuggingFace下载模型。如果您想在以下示例中使用ModelScope中的模型,请设置环境变量:export VLLM_USE_MODELSCOPE=True

from vllm import LLM, SamplingParams
prompts = [
    "Hello, my name is",
    "The president of the United States is",
    "The capital of France is",
    "The future of AI is",
]
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
llm = LLM(model="facebook/opt-125m")
outputs = llm.generate(prompts, sampling_params)
#Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")

1.2 DeepSpeed-MII

1.2.1 简介

DeepSpeed设计的开源Python库,旨在使强大的模型推理民主化,重点关注高吞吐量、低延迟和成本效益。
高吞吐技术
(1) 块KV缓存;
(2) 连续批处理;
(3) Dynamic SplitFuse
(4) 高性能CUDA核;
低延迟技术
(1) 张量并行;
(2) DeepFusion for Transformers;
(3) 资源约束系统的ZeRO推理;
(4) 编译器优化;
项目地址:https://github.com/microsoft/DeepSpeed-MII

1.2.2 快速开始

1) 安装
pip install deepspeed-mii
2) 使用

基础:
import mii
pipe = mii.pipeline("mistralai/Mistral-7B-v0.1")
response = pipe(["DeepSpeed is", "Seattle is"], max_new_tokens=128)
print(response)
张量并行版:
#Run on a single GPU
deepspeed --num_gpus 1 mii-example.py
#Run on multiple GPUs
deepspeed --num_gpus 2 mii-example.py
超参数:
model_name_or_path: str HuggingFace模型的名称或本地路径;
max_length:int设置提示+响应的默认最大tokens长度;
all_rank_output:bool启用后,所有列都会返回生成的文本。默认情况下,只有等级0将返回文本。

1.3 Light_LLM

1.3.1 简介

LightLLM是一个基于Python的LLM(大型语言模型)推理和服务框架,以其轻量级设计、易于扩展和高速性能而闻名。LightLLM利用了许多备受好评的开源实现的优势,包括但不限于FasterTransformer、TGI、vLLM和FlashAttention。特点:
(1) 三进程异步协作:tokenization, model inference,detokenization异步执行;
(2) 提供对跨多个模型的nopad注意力操作的支持,以有效地处理具有较大长度差异的请求;
(3) 动态批处理:启用请求的动态批处理调度;
(4) FlashAttention:结合FlashAttendance以提高速度并减少推理过程中的GPU内存占用;
(5) 张量并行性:利用多个GPU上的张量并行性进行更快的推理;
(6) TokenAttention:实现token级的KV缓存内存管理机制,允许在推理过程中零内存浪费;
(7) 高性能路由器:与Token Attention合作,精心管理每个代币的GPU内存,从而优化系统吞吐量;
项目地址:https://github.com/ModelTC/lightllm

1.3.2 快速开始

(1) 安装

Pip安装:
pip install -r requirements.txt
Docker容器(版本适用于CUDA11.8):
a)	拉取容器
docker pull ghcr.io/modeltc/lightllm:main
b)	运行容器
     docker run -it --gpus all -p 8080:8080                  \
        --shm-size 1g -v your_local_path:/data/         \
        ghcr.io/modeltc/lightllm:main /bin/bash
c)	构建个人容器
docker build -t <image_name> .
docker run -it --gpus all -p 8080:8080                  \
        --shm-size 1g -v your_local_path:/data/         \
        <image_name> /bin/bash
注意:如果使用多个GPU,则可能需要通过在docker run命令中添加--shm-size来增加共享内存大小。
从源码安装(推荐):下载项目对应GitHub代码,python setup.py install
d)安装Triton软件包(用于模型部署)
pip install triton==2.1.0 --no-deps

(2) 使用

运行:
python -m lightllm.server.api_server \
    --host 0.0.0.0                 \
    --port 8080                    \
    --tp 1                         \
    --max_total_token_num 12000    \
    --trust_remote_code            \
    --enable_multimodal            \
    --cache_capacity 1000          \
--model_dir /path/of/Qwen-VL or /path/of/Qwen-VL-Chat
	To query from Python:
import time
import requests
import json
url = 'http://localhost:8080/generate'
headers = {'Content-Type': 'application/json'}
data = {
    'inputs': 'What is AI?',
    "parameters": {
        'do_sample': False,
        'ignore_eos': False,
        'max_new_tokens': 1024,
    }
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
    print(response.json())
else:
    print('Error:', response.status_code, response.text)

1.4 TensorRT-LLM

1.4.1 简介

TensorRT-LLM为用户提供了一个易于使用的Python API,用于定义大型语言模型(LLM)并构建包含最先进优化的TensorRT引擎,以便在NVIDIA GPU上高效地执行推理。它还包括一个与NVIDIA Triton推理服务器集成的后端;为LLM服务的生产质量体系。支持H100/A00/T4/V100。
(1) 支持张量并行和流水线并行;
(2) 支持量化模型,INT4或INT8;
(3) In-flight Batching;
(4) MQA/GQA;
(5) PageAttention;
(6) 贪婪搜索、波束搜索
项目地址:https://github.com/NVIDIA/TensorRT-LLM

4.1.1 快速开始

(1) 安装

#Install openmpi
参考:https://blog.csdn.net/liu_feng_zi_/article/details/107429347 
#Install the latest preview version (corresponding to the main branch) of TensorRT-LLM. If you want to install the stable version (corresponding to the release branch), please remove the `--pre` option.
pip install tensorrt_llm -U --pre --extra-index-url https://pypi.nvidia.com
#Check installation
python3 -c "import tensorrt_llm"

(2) 下载模型

git clone https://huggingface.co/Qwen/Qwen-7B-Chat   ./tmp/Qwen/7B
git clone https://huggingface.co/Qwen/Qwen-14B-Chat  ./tmp/Qwen/14B
git clone https://huggingface.co/Qwen/Qwen-72B-Chat  ./tmp/Qwen/72B
或
git clone https://www.modelscope.cn/qwen/Qwen-7B-Chat.git   ./tmp/Qwen/7B
git clone https://www.modelscope.cn/qwen/Qwen-14B-Chat.git  ./tmp/Qwen/14B
git clone https://www.modelscope.cn/qwen/Qwen-72B-Chat.git  ./tmp/Qwen/72B

(3) 构建TensorRT引擎
通常trtllm-build只需要一个GPU,但如果在推理时已经获得了所需的所有GPU,则可以通过添加–workers参数来启用并行构建,从而加快引擎构建过程。请注意,当前辅助功能仅支持单个节点。

#Build a single-GPU float16 engine from HF weights.
#Try --gemm_plugin to prevent accuracy issue.
#Build the Qwen-7B-Chat model using a single GPU and FP16.
python convert_checkpoint.py --model_dir ./tmp/Qwen/7B/ \
                              --output_dir ./tllm_checkpoint_1gpu_fp16 \
                              --dtype float16
trtllm-build --checkpoint_dir ./tllm_checkpoint_1gpu_fp16 \
            --output_dir ./tmp/qwen/7B/trt_engines/fp16/1-gpu \
            --gemm_plugin float16
推理:
#With fp16 inference
python3 ../run.py --input_text "你好,请问你叫什么?" \
                  --max_output_len=50 \
                  --tokenizer_dir ./tmp/Qwen/7B/ \
                  --engine_dir=./tmp/Qwen/7B/trt_engines/fp16/1-gpu/
  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值