大模型推理优化--TensorRT-LLM初体验

TensorRT-LLM初体验

千呼万唤始出来,备受期待的Tensorrt-LLM终于发布,发布版本0.5.0。

在这里插入图片描述

github:
https://github.com/NVIDIA/TensorRT-LLM/tree/main

1.介绍

TensorRT-LLM可以视为TensorRT和FastTransformer的结合体,旨为大模型推理加速而生。

1.1丰富的优化特性

除了FastTransformer对Transformer做的attention优化、softmax优化、算子融合等方式之外,还引入了众多的大模型推理优化特性:

Multi-head Attention(MHA)
Multi-query Attention (MQA)
Group-query Attention(GQA)
In-flight Batching
Paged KV Cache for the Attention
Tensor Parallelism
Pipeline Parallelism
INT4/INT8 Weight-Only Quantization (W4A16 & W8A16)
SmoothQuant
GPTQ
AWQ
FP8
Greedy-search
Beam-search
RoPE
1.2 丰富的模型支持

对众多开源大模型都做了调用实例,其中包括:

Baichuan
Bert
Blip2
BLOOM
ChatGLM-6B
ChatGLM2-6B
Falcon
GPT
GPT-J
GPT-Nemo
GPT-NeoX
LLaMA
LLaMA-v2
MPT
OPT
SantaCoder
StarCoder

使用上仍然保持了TensorRT两阶段的调用方式——build+run:
build:通过配置参数将模型文件序列化为tensorRT的engine文件
run:加载engine文件,传入数据,进行inference

2.体验

2.1 编译TensorRT-LLM

目前,TensorRT-LLM必须由源码编译获得,为方便构建编译环境,官方提供了docker构建方式
详细指引可参考:
https://github.com/NVIDIA/TensorRT-LLM/blob/main/docs/source/installation.md

Build env
下载源码后,在代码根目录,执行构建命令

docker build --pull  \
             --target devel \
             --file docker/Dockerfile.multi \
             --tag tensorrt_llm/devel:latest \
             .

Build TensorRT-LLM

# To build the TensorRT-LLM code.
python3 ./scripts/build_wheel.py --trt_root /usr/local/tensorrt

# Deploy TensorRT-LLM in your environment.
pip install ./build/tensorrt_llm*.whl

也可以通过创建本地环境的方式完成编译过程,可参考:
http://t.csdnimg.cn/IrB2e

2.2 实例

./examples中有较多参考示例
此处选择bert例子做简单测试,设置layer=12

build
将huggingface格式的模型转为tensor engine文件
python3 build.py --dtype=float16 --log_level=verbose

run
python3 run.py

性能对比
pytorch

max_seqlenbatch_sizeinference time(ms)gpumem(MB)
3219.119631.0
6419.177653.0
12819.069701.0
25619.193701.0
3249.673705.0
6449.708721.0
12849.751821.0
256414.1541321.0

tensorrt-llm

max_seqlenbatch_sizeinference time(ms)gpumem(MB)
3212.317843.0
6412.404843.0
12812.868843.0
25613.564843.0
3242.788843.0
6443.372843.0
12845.252843.0
256410.54863.0

|128|1|2.868|843.0|
|256|1|3.564|843.0|
|32|4|2.788|843.0|
|64|4|3.372|843.0|
|128|4|5.252|843.0|
|256|4|10.54|863.0|

3 more LLMS

GPT2
git clone https://huggingface.co/gpt2-medium
模型保存在/root/autodl-tmp/models/gpt/

运行步骤参考/root/autodl-tmp/files/TensorRT-LLM/examples/gpt目录下的readme文档

(trt-llm) root@xxx:~/autodl-tmp/files/TensorRT-LLM/examples/gpt# python3 hf_gpt_convert.py -i gpt2 -o ./gpt2-trt --tensor-parallelism 1 --storage-type float16

(trt-llm) root@xxx:~/autodl-tmp/files/TensorRT-LLM/examples/gpt# python3 build.py --model_dir=./gpt2-trt/1-gpu --use_gpt_attention_plugin --remove_input_padding

(trt-llm) root@xxx:~/autodl-tmp/files/TensorRT-LLM/examples/gpt# python3 run.py --max_output_len=16 
Input: "Born in north-east France, Soyer trained as a"
Output: " chef and a cook at the local restaurant, La Boulangerie. He"


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值