使用 TensorRT 時,通常需要將模型轉(zhuǎn)換為 ONNX 格式,再將 ONNX 轉(zhuǎn)換為 TensorRT 格式,然后在 TensorRT、Triton Server 中進行推理。
但這個轉(zhuǎn)換過程并不簡單,經(jīng)常會遇到各種報錯,需要對模型結(jié)構(gòu)、平臺算子有一定的掌握,具備轉(zhuǎn)換和調(diào)試能力。而 TensorRT-LLM 的目標就是降低這一過程的復雜度,讓大模型更容易跑在 TensorRT 引擎上。
需要注意的是,TensorRT 針對的是具體硬件,不同的 GPU 型號需要編譯不同的 TensorRT 格式模型。這與 ONNX 模型格式的通用性定位顯著不同。
同時,TensortRT-LLM 并不支持全部 GPU 型號,僅支持 H100、L40S、A100、A30、V100 等顯卡。
docker run --gpus device=0 -v $PWD:/app/tensorrt_llm/models -it --rm hubimage/nvidia-tensorrt-llm:v0.7.1 bash
--gpus device=0 表示使用編號為 0 的 GPU 卡,這里的 hubimage/nvidia-tensorrt-llm:v0.7.1 對應的就是 TensorRT-LLM v0.7.1 的 Release 版本。
由于自行打鏡像非常麻煩,這里提供幾個可選版本的鏡像:
在上述容器環(huán)境下,執(zhí)行命令:
python examples/baichuan/build.py --model_version v2_7b / --model_dir ./models/Baichuan2-7B-Chat / --dtype float16 / --parallel_build / --use_inflight_batching / --enable_context_fmha / --use_gemm_plugin float16 / --use_gpt_attention_plugin float16 / --output_dir ./models/Baichuan2-7B-trt-engines
生成的文件主要有三個:
python examples/run.py --input_text "世界上第二高的山峰是哪座?" / --max_output_len=200 / --tokenizer_dir ./models/Baichuan2-7B-Chat / --engine_dir=./models/Baichuan2-7B-trt-engines
[02/03/2024-10:02:58] [TRT-LLM] [W] Found pynvml==11.4.1. Please use pynvml>=11.5.0 to get accurate memory usageInput [Text 0]: "世界上第二高的山峰是哪座?"Output [Text 0 Beam 0]: "珠穆朗瑪峰(Mount Everest)是地球上最高的山峰,海拔高度為8,848米(29,029英尺)。第二高的山峰是喀喇昆侖山脈的喬戈里峰(K2),海拔高度為8,611米(28,251英尺)。"
模型推理優(yōu)化,可以替換算子、量化、裁剪反向傳播等手段,但有一個基本線一定要達到,那就是模型不能退化很多。
在精度損失可接受的范圍內(nèi),模型的推理優(yōu)化才有意義。TensorRT-LLM 項目提供的 summarize.py 可以跑一些測試,給模型打分,rouge1、rouge2 和 rougeLsum 是用于評價文本生成質(zhì)量的指標,可以用于評估模型推理質(zhì)量。
pip install datasets nltk rouge_score -i https://pypi.tuna.tsinghua.edu.cn/simple
由于目前 optimum 不支持 Baichuan 模型,因此,需要編輯 examples/summarize.py 注釋掉 model.to_bettertransformer(),這個問題在最新的 TensorRT-LLM 代碼中已經(jīng)解決,我使用的是當前最新的 Release 版本 (v0.7.1)。
python examples/summarize.py --test_hf / --hf_model_dir ./models/Baichuan2-7B-Chat / --data_type fp16 / --engine_dir ./models/Baichuan2-7B-trt-engines
輸出結(jié)果:
[02/03/2024-10:21:45] [TRT-LLM] [I] Hugging Face (total latency: 31.27020287513733 sec)[02/03/2024-10:21:45] [TRT-LLM] [I] HF beam 0 result[02/03/2024-10:21:45] [TRT-LLM] [I] rouge1 : 28.847385241217726[02/03/2024-10:21:45] [TRT-LLM] [I] rouge2 : 9.519352831698162[02/03/2024-10:21:45] [TRT-LLM] [I] rougeL : 20.85486489462602[02/03/2024-10:21:45] [TRT-LLM] [I] rougeLsum : 24.090111126907733
python examples/summarize.py --test_trt_llm / --hf_model_dir ./models/Baichuan2-7B-Chat / --data_type fp16 / --engine_dir ./models/Baichuan2-7B-trt-engines
輸出結(jié)果:
[02/03/2024-10:23:16] [TRT-LLM] [I] TensorRT-LLM (total latency: 28.360705375671387 sec)[02/03/2024-10:23:16] [TRT-LLM] [I] TensorRT-LLM beam 0 result[02/03/2024-10:23:16] [TRT-LLM] [I] rouge1 : 26.557043897453102[02/03/2024-10:23:16] [TRT-LLM] [I] rouge2 : 8.28672928021811[02/03/2024-10:23:16] [TRT-LLM] [I] rougeL : 19.13639628365737[02/03/2024-10:23:16] [TRT-LLM] [I] rougeLsum : 22.0436013250798
TensorRT-LLM 編譯之后的模型,rougeLsum 從 24 降到了 22,說明能力會有退化,但只要在可接受的范圍之內(nèi),還是可以使用的,因為推理速度會有較大的提升。
完成這步之后,就可以退出容器了,推理是在另外一個容器中進行。
Triton Server 是一個推理框架,提供用戶規(guī)模化進行推理的能力。具體包括:
圖片
上面是 Triton Server 的架構(gòu)圖,簡單點說 Triton Server 是一個端(模型)到端(應用)的推理框架,提供了圍繞推理的生命周期過程管理,配置好模型之后,就能直接對應用層提供服務。
在 Triton 社區(qū)的示例中,通常會有這樣四個目錄:
.├── ensemble│ ├── 1│ └── config.pbtxt├── postprocessing│ ├── 1│ │ └── model.py│ └── config.pbtxt├── preprocessing│ ├── 1│ │ └── model.py│ └── config.pbtxt└── tensorrt_llm ├── 1 └── config.pbtxt9 directories, 6 files
對于 Triton Server 來說,上面的目錄格式實際上是定義了四個模型,分別是 preprocessing、tensorrt_llm、postprocessing、ensemble,只不過 ensemble 是一個組合模型,定義多個模型來融合。
ensemble 存在的原因在于 tensorrt_llm 的推理并不是 text2text ,借助 Triton Server 的 Pipeline 能力,通過 preprocessing 對輸入進行 Tokenizing,postprocessing 對輸出進行 Detokenizing,就能夠?qū)崿F(xiàn)端到端的推理能力。否則,在客戶端直接使用 TensorRT-LLM 時,還需要自行處理詞與索引的雙向映射。
這四個模型具體作用如下:
上面定義的模型都有一個 1 目錄表示版本 1 ,在版本目錄中放置模型文件,在模型目錄下放置 config.pbtxt 描述推理的參數(shù) input、output、version 等。
Triton Server 通過參數(shù) --model-control-mode 來控制模型加載的方式,目前有三種加載模式:
Triton Server 在模型的配置文件 config.pbtxt 中提供有 Version Policy,每個模型可以有多個版本共存。默認使用版本號為 1 的模型,目前有三種版本策略:
version_policy: { all: {}}
version_policy: { latest: { num_versions: 3}}
version_policy: { specific: { versions: [1, 3, 5]}}
本文示例相關的配置已經(jīng)整理了一份到 GitHub 上,拷貝模型到指定的目之后,就可以直接進行推理了。
git clone https://github.com/shaowenchen/modelops
cp Baichuan2-7B-trt-engines/* modelops/triton-tensorrtllm/Baichuan2-7B-Chat/tensorrt_llm/1/
cp -r Baichuan2-7B-Chat modelops/triton-tensorrtllm/downloads
此時文件的目錄結(jié)構(gòu)是:
tree modelops/triton-tensorrtllmmodelops/triton-tensorrtllm├── Baichuan2-7B-Chat│ ├── end_to_end_grpc_client.py│ ├── ensemble│ │ ├── 1│ │ └── config.pbtxt│ ├── postprocessing│ │ ├── 1│ │ │ ├── model.py│ │ │ └── __pycache__│ │ │ └── model.cpython-310.pyc│ │ └── config.pbtxt│ ├── preprocessing│ │ ├── 1│ │ │ ├── model.py│ │ │ └── __pycache__│ │ │ └── model.cpython-310.pyc│ │ └── config.pbtxt│ └── tensorrt_llm│ ├── 1│ │ ├── baichuan_float16_tp1_rank0.engine│ │ ├── config.json│ │ └── model.cache│ └── config.pbtxt└── downloads └── Baichuan2-7B-Chat ├── Baichuan2 模型社區(qū)許可協(xié)議.pdf ├── Community License for Baichuan2 Model.pdf ├── config.json ├── configuration_baichuan.py ├── generation_config.json ├── generation_utils.py ├── modeling_baichuan.py ├── pytorch_model.bin ├── quantizer.py ├── README.md ├── special_tokens_map.json ├── tokenization_baichuan.py ├── tokenizer_config.json └── tokenizer.model13 directories, 26 files
docker run --gpus device=0 --rm -p 38000:8000 -p 38001:8001 -p 38002:8002 / -v $PWD/modelops/triton-tensorrtllm:/models / hubimage/nvidia-triton-trt-llm:v0.7.1 / tritonserver --model-repository=/models/Baichuan2-7B-Chat / --disable-auto-complete-config / --backend-cnotallow=python,shm-region-prefix-name=prefix0_:
如果一臺機器上運行了多個 triton server,那么需要用 shm-region-prefix-name=prefix0_ 區(qū)分一下共享內(nèi)存的前綴,詳情可以參考 https://github.com/triton-inference-server/server/issues/4145 。
啟動日志:
I0129 10:27:31.658112 1 server.cc:619]+-------------+-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Backend | Path | Config |+-------------+-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| python | /opt/tritonserver/backends/python/libtriton_python.so | {"cmdline":{"auto-complete-config":"false","backend-directory":"/opt/tritonserver/backends","min-compute-capability":"6.000000","shm-region-prefix-name":"prefix0_:","default-max-batch-size":"4"}} || tensorrtllm | /opt/tritonserver/backends/tensorrtllm/libtriton_tensorrtllm.so | {"cmdline":{"auto-complete-config":"false","backend-directory":"/opt/tritonserver/backends","min-compute-capability":"6.000000","default-max-batch-size":"4"}} |+-------------+-----------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+I0129 10:27:31.658192 1 server.cc:662]+----------------+---------+--------+| Model | Version | Status |+----------------+---------+--------+| ensemble | 1 | READY || postprocessing | 1 | READY || preprocessing | 1 | READY || tensorrt_llm | 1 | READY |+----------------+---------+--------+...I0129 10:27:31.745587 1 grpc_server.cc:2513] Started GRPCInferenceService at 0.0.0.0:8001I0129 10:27:31.745810 1 http_server.cc:4497] Started HTTPService at 0.0.0.0:8000I0129 10:27:31.787129 1 http_server.cc:270] Started Metrics Service at 0.0.0.0:8002
四個模型都處于 READY 狀態(tài),就可以正常推理了。
curl localhost:38000/v2/models/ensemble/config{"name":"ensemble","platform":"ensemble","backend":"","version_policy":{"latest":{"num_versions":1}},"max_batch_size":32,"input":[{"name":"text_input","data_type":"TYPE_STRING",...
可以查看模型的推理參數(shù)。如果使用的是 auto-complete-config,那么這個接口可以用于導出 Triton Server 自動生成的模型推理參數(shù),用于修改和調(diào)試。
curl -v localhost:38000/v2/health/ready< HTTP/1.1 200 OK< Content-Length: 0< Content-Type: text/plain
pip install tritonclient[grpc] -i https://pypi.tuna.tsinghua.edu.cn/simple
Triton GRPC 接口的性能顯著高于 HTTP 接口,同時在容器中,我也沒有找到 HTTP 接口的示例,這里就直接用 GRPC 了。
wget https://raw.githubusercontent.com/shaowenchen/modelops/master/triton-tensorrtllm/Baichuan2-7B-Chat/end_to_end_grpc_client.py
python3 ./end_to_end_grpc_client.py -u 127.0.0.1:38001 -p "世界上第三高的山峰是哪座?" -S -o 128珠穆朗瑪峰(Mount Everest)是世界上最高的山峰,海拔高度為8,848米(29,029英尺)。在世界上,珠穆朗瑪峰之后,第二高的山峰是喀喇昆侖山脈的喬戈里峰(K2,又稱K2峰),海拔高度為8,611米(28,251英尺)。第三高的山峰是喜馬拉雅山脈的坎欽隆加峰(Kangchenjunga),海拔高度為8,586米(28,169英尺)。</s>
Triton Server 已經(jīng)提供了推理指標,監(jiān)聽在 8002 端口。在本文的示例中,就是 38002 端口。
curl -v localhost:38002/metricsnv_inference_request_success{model="ensemble",versinotallow="1"} 1nv_inference_request_success{model="tensorrt_llm",versinotallow="1"} 1nv_inference_request_success{model="preprocessing",versinotallow="1"} 1nv_inference_request_success{model="postprocessing",versinotallow="1"} 128# HELP nv_inference_request_failure Number of failed inference requests, all batch sizes# TYPE nv_inference_request_failure counternv_inference_request_failure{model="ensemble",versinotallow="1"} 0nv_inference_request_failure{model="tensorrt_llm",versinotallow="1"} 0nv_inference_request_failure{model="preprocessing",versinotallow="1"} 0nv_inference_request_failure{model="postprocessing",versinotallow="1"} 0
在 Grafana 中可以導入面板 https://grafana.com/grafana/dashboards/18737-triton-inference-server/ 查看指標,如下圖:
圖片
本文主要是在學習使用 TensorRT 和 Triton Server 進行推理過程的記錄,主要內(nèi)容如下:
本文鏈接:http://www.www897cc.com/showinfo-26-72432-0.html容器下在 Triton Server 中使用 TensorRT-LLM 進行推理
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 如何在PHP中使用 Caddy2 協(xié)同服務
下一篇: 在Go中使用接口:實用性與脆弱性的平衡