MIT License
inflight_batching
实现最大吞吐/并发。本项目配套B站教程:
本项目配套博客适配概述:如何在 NVIDIA TensorRT-LLM 中支持 Qwen 模型
下载镜像。
官方triton镜像24.02,对应TensorRT-LLM版本为0.8.0,不含TensorRT-LLM开发包。
docker pull nvcr.io/nvidia/tritonserver:24.02-trtllm-python-py3
对于Windows用户想体验tritonserver部署的,或者无GPU的用户,可以使用AutoDL镜像,含tritonserver,版本为24.02(对应tensorrt_llm 0.8.0),链接,注:该链接包含完整编译教程。
拉取本项目代码
git clone https://github.com/Tlntin/Qwen-TensorRT-LLM.git
cd Qwen-TensorRT-LLM
进入项目目录,然后创建并启动容器,同时将本地examples
代码路径映射到/app/tensorrt_llm/examples
路径,然后打开8000和7860端口的映射,方便调试api和web界面。
docker run --gpus all \
--name trt_llm \
-d \
--ipc=host \
--ulimit memlock=-1 \
--restart=always \
--ulimit stack=67108864 \
-p 8000:8000 \
-p 7860:7860 \
-v ${PWD}/examples:/app/tensorrt_llm/examples \
nvcr.io/nvidia/tritonserver:24.02-trtllm-python-py3 sleep 8640000
进入docker容器里面的qwen2路径,
使用pip直接安装官方编译好的tensorrt_llm,需要先安装numpy1.x,不兼容numpy2.x。
pip install "numpy<2"
pip install tensorrt_llm==0.8.0 --extra-index-url https://pypi.nvidia.com --extra-index-url https://download.pytorch.org/whl/cu121
安装提供的Python依赖
cd /app/tensorrt_llm/examples/qwen2/
pip install -r requirements.txt
升级transformers版本,qwen2最低需要4.37以上版本,如果有警告依赖不匹配可以忽略。
pip install "transformers>=4.37"
从HuggingFace下载模型(暂时不支持其他平台),例如QWen1.5-7B-Chat
模型,然后将文件夹重命名为qwen1.5_7b_chat
,最后放到examples/qwen2/
路径下即可。
修改编译参数(可选)
default_config.py
中hf_model_dir
(模型路径)和tokenizer_dir
(分词器路径)以及int4_gptq_model_dir
(手动gptq量化输出路径),可以改成你自定义的路径。编译。
编译fp16(注:--remove_input_padding
和--enable_context_fmha
为可选参数,可以一定程度上节省显存)。
python3 build.py --remove_input_padding --enable_context_fmha
编译 int8 (weight only)。
python3 build.py --use_weight_only --weight_only_precision=int8
编译int4 (weight only)
python3 build.py --use_weight_only --weight_only_precision=int4
对于如果单卡装不下,又不想用int4/int8量化,可以选择尝试tp = 2,即启用两张GPU进行编译 (注:tp功能目前只支持从Huggingface格式构建engine)
python3 build.py --world_size 2 --tp_size 2
运行。编译完后,再试跑一下,输出Output: "您好,我是来自达摩院的大规模语言模型,我叫通义千问。"
这说明成功。
tp = 1(默认单GPU)时使用python直接运行run.py
python3 run.py
tp = 2(2卡用户,或者更多GPU卡)时,使用mpirun
命令来运行run.py
mpirun -n 2 --allow-run-as-root python run.py
使用官方24.02容器多卡可能会报错,提示:Failed, NCCL error /home/jenkins/agent/workspace/LLM/release-0.8/L0_PostMerge/tensorrt_llm/cpp/tensorrt_llm/plugins/ncclPlugin/allreducePlugin.cpp:183 'unknown result code'
,需要安装nccl2.20.3-1(使用压缩包,解压后导入系统环境变量或者使用apt命名安装均可),安装后即可正常运行。
export LD_LIBRARY_PATH=nccl_2.20.3-1+cuda12.3_x86_64/lib/:$LD_LIBRARY_PATH
# 或者,推荐下面这种
apt update && apt-get install -y --no-install-recommends libnccl2=2.20.3-1+cuda12.3 libnccl-dev=2.20.3-1+cuda12.3 -y
验证模型精度。可以试试跑一下summarize.py
,对比一下huggingface和trt-llm的rouge得分。这一步需要在线下载数据集,对于网络不好的用户,可以参考该方法:datasets离线加载huggingface数据集方法
跑hugggingface版
python3 summarize.py --test_hf
跑trt-llm版
python3 summarize.py --test_trt_llm
一般来说,如果trt-llm的rouge分数和huggingface差不多,略低一些(1以内)或者略高一些(2以内),则说明精度基本对齐。
测量模型吞吐速度和生成速度。需要下载ShareGPT_V3_unfiltered_cleaned_split.json
这个文件。
可以通过wget/浏览器直接下载,下载链接
也可通过百度网盘下载,链接: https://pan.baidu.com/s/12rot0Lc0hc9oCb7GxBS6Ng?pwd=jps5 提取码: jps5
下载后同样放到examples/qwen2/
路径下即可
测量前,如果需要改max_input_length/max_new_tokens,可以直接改default_config.py
即可。一般不推荐修改,如果修改了这个,则需要重新编译一次trt-llm,保证两者输入数据集长度统一。
测量huggingface模型
python3 benchmark.py --backend=hf --dataset=ShareGPT_V3_unfiltered_cleaned_split.json --hf_max_batch_size=1
测量trt-llm模型 (注意:--trt_max_batch_size
不应该超过build时候定义的最大batch_size,否则会出现内存错误。)
python3 benchmark.py --backend=trt_llm --dataset=ShareGPT_V3_unfiltered_cleaned_split.json --trt_max_batch_size=1
注意:运行Smooth Quant需要将huggingface模型完全加载到GPU里面,用于构建int8标定数据集,所以需要提前确保你的显存够大,能够完全加载整个模型。
将Huggingface格式的数据转成FT(FastTransformer)需要的数据格式,这一步需要在线下载数据集,对于网络不好的用户,可以参考该方法:datasets离线加载huggingface数据集方法
单卡
python3 hf_qwen_convert.py --smoothquant=0.5
多卡(以2卡为例)
python3 hf_qwen_convert.py --smoothquant=0.5 --tensor-parallelism=2
开始编译trt_engine
python3 build.py --use_smooth_quant --per_token --per_channel
python3 build.py --use_smooth_quant --per_token --per_channel --world_size 2 --tp_size 2
编译完成,run/summarize/benchmark等等都和上面的是一样的了。
注意:运行int8-kv-cache需要将huggingface模型完全加载到GPU里面,用于构建int8标定数据集,所以需要提前确保你的显存够大,能够完全加载整个模型。
将Huggingface格式的数据转成FT(FastTransformer)需要的数据格式。
python3 hf_qwen_convert.py --calibrate-kv-cache
python3 hf_qwen_convert.py --calibrate-kv-cache --tensor-parallelism=2
编译int8 weight only + int8-kv-cache
python3 build.py --use_weight_only --weight_only_precision=int8 --int8_kv_cache
python3 build.py --use_weight_only --weight_only_precision=int8 --int8_kv_cache --world_size 2 --tp_size 2
需要安装auto-gptq模块,并且升级transformers模块版本到最新版(建议optimum和transformers都用最新版,否则可能有乱码问题),参考issue/68。(注:安装完模块后可能会提示tensorrt_llm与其他模块版本不兼容,可以忽略该警告)
pip install auto-gptq optimum
pip install transformers -U
手动获取标定权重(可选)
--device=cpu
来尝试用cpu标定,但是时间会很长)
python3 gptq_convert.py
python build.py --use_weight_only \
--weight_only_precision int4_gptq \
--per_group
python build.py --use_weight_only \
--weight_only_precision int4_gptq \
--per_group \
--remove_input_padding \
--enable_context_fmha
使用官方int4权重,例如Qwen-xx-Chat-Int4模型(推荐)
--quant_ckpt_path
量化后权重路径均设置为同一个路径,下面是32b-gptq-int4模型的示例(其他gptq-int4模型也是一样操作)
python build.py --use_weight_only \
--weight_only_precision int4_gptq \
--per_group \
--hf_model_dir Qwen1.5-32B-Chat-GPTQ-Int4 \
--quant_ckpt_path Qwen1.5-32B-Chat-GPTQ-Int4
python3 run.py --tokenizer_dir=Qwen1.5-32B-Chat-GPTQ-Int4
pip install --no-cache-dir --extra-index-url https://pypi.nvidia.com nvidia-ammo~=0.7.0
python3 quantize.py --export_path ./qwen2_7b_4bit_gs128_awq.pt
python build.py --use_weight_only \
--weight_only_precision int4_awq \
--per_group \
--quant_ckpt_path ./qwen2_7b_4bit_gs128_awq.pt
python build.py --use_weight_only \
--weight_only_precision int4_awq \
--per_group \
--remove_input_padding \
--enable_context_fmha \
--quant_ckpt_path ./qwen2_7b_4bit_gs128_awq.pt