在 Triton 中部署 Hugging Face Llama2-7b 模型#
TensorRT-LLM 是 Nvidia 推荐的在 Nvidia GPU 上运行大型语言模型 (LLM) 的解决方案。阅读更多关于 TensoRT-LLM 的信息,请访问 此处,以及 Triton 的 TensorRT-LLM 后端,请访问 此处。
注意: 如果本教程的某些部分无法工作,可能是 tutorials
和 tensorrtllm_backend
仓库之间存在版本不匹配。如有必要,请参考 llama.md 以获取更详细的修改信息。如果您熟悉 python,也可以尝试使用 高级 API 进行 LLM 工作流程。
获取 Llama2-7B 模型#
在本教程中,我们使用带有预训练权重的 Llama2-7B HuggingFace 模型。克隆带有权重和令牌的模型仓库,请访问 此处。您需要获得 Llama2 仓库的权限以及访问 huggingface cli。要获得访问 huggingface cli 的权限,请访问:huggingface.co/settings/tokens。
使用 Triton CLI 部署#
Triton CLI 是一个开源命令行界面,使用户能够创建、部署和分析由 Triton 推理服务器提供的模型。
启动 Triton TensorRT-LLM 容器#
启动带有 TensorRT-LLM 后端的 Triton docker 容器。请注意,我们将获取的 Llama2-7b 模型挂载到 docker 容器中的 /root/.cache/huggingface
,以便 Triton CLI 可以使用它并跳过下载步骤。
在 docker 外部创建一个 engines
文件夹,以便在将来的运行中重用引擎。请确保将 <xx.yy> 替换为您要使用的 Triton 版本。
docker run --rm -it --net host --shm-size=2g \
--ulimit memlock=-1 --ulimit stack=67108864 --gpus all \
-v </path/to/Llama2/repo>:/root/.cache/huggingface \
-v </path/to/engines>:/engines \
nvcr.io/nvidia/tritonserver:<xx.yy>-trtllm-python-py3
安装 Triton CLI#
安装 最新版本 的 Triton CLI
GIT_REF=<LATEST_RELEASE>
pip install git+https://github.com/triton-inference-server/triton_cli.git@${GIT_REF}
准备 Triton 模型仓库#
Triton CLI 有一个命令 triton import
,它可以自动将 HF 检查点转换为 TensorRT-LLM 检查点格式,构建 TensorRT-LLM 引擎,并准备 Triton 模型仓库
ENGINE_DEST_PATH=/engines triton import -m llama-2-7b --backend tensorrtllm
请注意,指定 ENGINE_DEST_PATH
是可选的,但如果您想在将来重用编译后的引擎,建议这样做。
成功运行 triton import
后,您应该在控制台中看到打印的模型仓库结构
...
triton - INFO - Current repo at /root/models:
models/
├── llama-2-7b/
│ ├── 1/
│ │ ├── lib/
│ │ │ ├── decode.py
│ │ │ └── triton_decoder.py
│ │ └── model.py
│ └── config.pbtxt
├── postprocessing/
│ ├── 1/
│ │ └── model.py
│ └── config.pbtxt
├── preprocessing/
│ ├── 1/
│ │ └── model.py
│ └── config.pbtxt
└── tensorrt_llm/
├── 1/
└── config.pbtxt
启动 Triton 推理服务器#
启动服务器,指向默认模型仓库
triton start
发送推理请求#
使用 generate endpoint 向已部署的模型发送推理请求。
curl -X POST localhost:8000/v2/models/llama-2-7b/generate -d '{"text_input": "What is ML?", "max_tokens": 50, "bad_words": "", "stop_words": "", "pad_id": 2, "end_id": 2}'
您应该期望收到以下响应
{"context_logits":0.0,...,"text_output":"What is ML?\nML is a branch of AI that allows computers to learn from data, identify patterns, and make predictions. It is a powerful tool that can be used in a variety of industries, including healthcare, finance, and transportation."}
使用 Triton 推理服务器部署#
如果您想更好地控制部署过程,接下来的步骤将指导您完成 TensorRT-LLM 引擎构建过程和 Triton 模型仓库设置。
前提条件:TensorRT-LLM 后端#
本教程需要 TensorRT-LLM 后端仓库。请注意,为了获得最佳用户体验,我们建议使用 tensorrtllm_backend
的最新 发布标签 和最新的 Triton 服务器容器。
要克隆 TensorRT-LLM 后端仓库,请确保运行以下命令集。
git clone https://github.com/triton-inference-server/tensorrtllm_backend.git --branch <release branch>
# Update the submodules
cd tensorrtllm_backend
# Install git-lfs if needed
apt-get update && apt-get install git-lfs -y --no-install-recommends
git lfs install
git submodule update --init --recursive
启动 Triton TensorRT-LLM 容器#
启动带有 TensorRT-LLM 后端的 Triton docker 容器。请注意,为了简单起见,我们将 tensorrtllm_backend
挂载到 /tensorrtllm_backend
,并将 Llama2 模型挂载到 docker 容器中的 /Llama-2-7b-hf
。在 docker 外部创建一个 engines
文件夹,以便在将来的运行中重用引擎。请确保将 <xx.yy> 替换为您要使用的 Triton 版本。
docker run --rm -it --net host --shm-size=2g \
--ulimit memlock=-1 --ulimit stack=67108864 --gpus all \
-v </path/to/tensorrtllm_backend>:/tensorrtllm_backend \
-v </path/to/Llama2/repo>:/Llama-2-7b-hf \
-v </path/to/engines>:/engines \
nvcr.io/nvidia/tritonserver:<xx.yy>-trtllm-python-py3
或者,如果您想构建专用容器,您可以按照 此处 的说明构建带有 Tensorrt-LLM 后端的 Triton 服务器。
启动容器时,请不要忘记允许 gpu 使用。
可选:为了简单起见,我们将以下所有步骤浓缩到一个 deploy_trtllm_llama.sh 脚本中。确保将 tutorials 仓库克隆到您的机器,并通过在上面列出的 docker run 命令中添加
-v /path/to/tutorials/:/tutorials
来启动 docker 容器,并将 tutorial 仓库挂载到/tutorials
。然后,当容器启动后,只需通过以下方式运行脚本:/tutorials/Popular_Models_Guide/Llama2/deploy_trtllm_llama.sh <WORLD_SIZE>
关于如何运行推理请求,请参考本教程的 客户端 部分。
为每个模型创建引擎 [如果您已经有引擎,请跳过此步骤]#
TensorRT-LLM 要求在运行之前为所需的配置编译每个模型。为此,在 Triton 服务器上首次运行模型之前,您需要创建一个 TensorRT-LLM 引擎。
从 24.04 版本 开始,Triton Server TensrRT-LLM 容器预装了 TensorRT-LLM 包,这允许用户在 Triton 容器内部构建引擎。只需按照以下步骤操作
HF_LLAMA_MODEL=/Llama-2-7b-hf
UNIFIED_CKPT_PATH=/tmp/ckpt/llama/7b/
ENGINE_DIR=/engines
CONVERT_CHKPT_SCRIPT=/tensorrtllm_backend/tensorrt_llm/examples/llama/convert_checkpoint.py
python3 ${CONVERT_CHKPT_SCRIPT} --model_dir ${HF_LLAMA_MODEL} --output_dir ${UNIFIED_CKPT_PATH} --dtype float16
trtllm-build --checkpoint_dir ${UNIFIED_CKPT_PATH} \
--remove_input_padding enable \
--gpt_attention_plugin float16 \
--context_fmha enable \
--gemm_plugin float16 \
--output_dir ${ENGINE_DIR} \
--paged_kv_cache enable \
--max_batch_size 4
可选:您可以检查模型的输出,使用位于同一 llama 示例文件夹中的
run.py
。python3 /tensorrtllm_backend/tensorrt_llm/examples/run.py --engine_dir=/engines/1-gpu/ --max_output_len 50 --tokenizer_dir /Llama-2-7b-hf --input_text "What is ML?"您应该期望收到以下响应
[TensorRT-LLM] TensorRT-LLM version: 0.9.0 ... [TensorRT-LLM][INFO] Max KV cache pages per sequence: 1 Input [Text 0]: "<s> What is ML?" Output [Text 0 Beam 0]: " ML is a branch of AI that allows computers to learn from data, identify patterns, and make predictions. It is a powerful tool that can be used in a variety of industries, including healthcare, finance, and transportation."
使用 Triton 提供服务#
最后一步是创建一个 Triton 可读的模型。您可以在 tensorrtllm_backend/all_models/inflight_batcher_llm 中找到使用 inflight batching 的模型模板。要运行我们的 Llama2-7B 模型,您需要
复制 inflight batcher 模型仓库
cp -R /tensorrtllm_backend/all_models/inflight_batcher_llm /opt/tritonserver/.
修改预处理、后处理和处理步骤的 config.pbtxt。以下脚本执行最小化配置以运行 tritonserver,但如果您想要最佳性能或自定义参数,请阅读 文档 和 perf_best_practices 中的详细信息:注意:
TRITON_BACKEND
有两个可能的选项:tensorrtllm
和python
。如果TRITON_BACKEND=python
,python 后端将部署model.py
。
# preprocessing
TOKENIZER_DIR=/Llama-2-7b-hf/
TOKENIZER_TYPE=auto
ENGINE_DIR=/engines
DECOUPLED_MODE=false
MODEL_FOLDER=/opt/tritonserver/inflight_batcher_llm
MAX_BATCH_SIZE=4
INSTANCE_COUNT=1
MAX_QUEUE_DELAY_MS=10000
TRITON_BACKEND=tensorrtllm
FILL_TEMPLATE_SCRIPT=/tensorrtllm_backend/tools/fill_template.py
python3 ${FILL_TEMPLATE_SCRIPT} -i ${MODEL_FOLDER}/preprocessing/config.pbtxt tokenizer_dir:${TOKENIZER_DIR},tokenizer_type:${TOKENIZER_TYPE},triton_max_batch_size:${MAX_BATCH_SIZE},preprocessing_instance_count:${INSTANCE_COUNT}
python3 ${FILL_TEMPLATE_SCRIPT} -i ${MODEL_FOLDER}/postprocessing/config.pbtxt tokenizer_dir:${TOKENIZER_DIR},tokenizer_type:${TOKENIZER_TYPE},triton_max_batch_size:${MAX_BATCH_SIZE},postprocessing_instance_count:${INSTANCE_COUNT}
python3 ${FILL_TEMPLATE_SCRIPT} -i ${MODEL_FOLDER}/tensorrt_llm_bls/config.pbtxt triton_max_batch_size:${MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},bls_instance_count:${INSTANCE_COUNT}
python3 ${FILL_TEMPLATE_SCRIPT} -i ${MODEL_FOLDER}/ensemble/config.pbtxt triton_max_batch_size:${MAX_BATCH_SIZE}
python3 ${FILL_TEMPLATE_SCRIPT} -i ${MODEL_FOLDER}/tensorrt_llm/config.pbtxt triton_backend:${TRITON_BACKEND},triton_max_batch_size:${MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},engine_dir:${ENGINE_DIR},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MS},batching_strategy:inflight_fused_batching
启动 Tritonserver
使用 launch_triton_server.py 脚本。这将使用 MPI 启动 tritonserver
的多个实例。
python3 /tensorrtllm_backend/scripts/launch_triton_server.py --world_size=<world size of the engine> --model_repo=/opt/tritonserver/inflight_batcher_llm
您应该期望收到以下响应
... I0503 22:01:25.210518 1175 grpc_server.cc:2463] Started GRPCInferenceService at 0.0.0.0:8001 I0503 22:01:25.211612 1175 http_server.cc:4692] Started HTTPService at 0.0.0.0:8000 I0503 22:01:25.254914 1175 http_server.cc:362] Started Metrics Service at 0.0.0.0:8002
要停止容器内的 Triton 服务器,请运行
pkill tritonserver
发送推理请求#
您可以使用以下命令测试运行结果
# Using the SDK container as an example
docker run --rm -it --net host --shm-size=2g \
--ulimit memlock=-1 --ulimit stack=67108864 --gpus all \
-v /path/to/tensorrtllm_backend/inflight_batcher_llm/client:/tensorrtllm_client \
-v /path/to/Llama2/repo:/Llama-2-7b-hf \
nvcr.io/nvidia/tritonserver:<xx.yy>-py3-sdk
# Install extra dependencies for the script
pip3 install transformers sentencepiece
python3 /tensorrtllm_client/inflight_batcher_llm_client.py --request-output-len 50 --tokenizer-dir /Llama-2-7b-hf/ --text "What is ML?"
您应该期望收到以下响应
... Input: What is ML? Output beam 0: ML is a branch of AI that allows computers to learn from data, identify patterns, and make predictions. It is a powerful tool that can be used in a variety of industries, including healthcare, finance, and transportation. ...
curl -X POST localhost:8000/v2/models/ensemble/generate -d '{"text_input": "What is ML?", "max_tokens": 50, "bad_words": "", "stop_words": "", "pad_id": 2, "end_id": 2}'
您应该期望收到以下响应
{"context_logits":0.0,...,"text_output":"What is ML?\nML is a branch of AI that allows computers to learn from data, identify patterns, and make predictions. It is a powerful tool that can be used in a variety of industries, including healthcare, finance, and transportation."}
使用 Gen-AI Perf 评估性能#
Gen-AI Perf 是一个命令行工具,用于测量通过推理服务器提供的生成式 AI 模型的吞吐量和延迟。您可以在 此处 阅读更多关于 Gen-AI Perf 的信息。
要使用 Gen-AI Perf,请在同一 Triton docker 容器中运行以下命令
genai-perf \
profile \
-m ensemble \
--service-kind triton \
--backend tensorrtllm \
--num-prompts 100 \
--random-seed 123 \
--synthetic-input-tokens-mean 200 \
--synthetic-input-tokens-stddev 0 \
--output-tokens-mean 100 \
--output-tokens-stddev 0 \
--output-tokens-mean-deterministic \
--tokenizer /Llama-2-7b-hf/ \
--concurrency 1 \
--measurement-interval 4000 \
--profile-export-file my_profile_export.json \
--url localhost:8001
您应该期望看到如下所示的输出
LLM Metrics
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┓
┃ Statistic ┃ avg ┃ min ┃ max ┃ p99 ┃ p90 ┃ p75 ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━┩
│ Request latency (ms) │ 1,630.23 │ 1,616.37 │ 1,644.65 │ 1,644.05 │ 1,638.70 │ 1,635.64 │
│ Output sequence length │ 300.00 │ 300.00 │ 300.00 │ 300.00 │ 300.00 │ 300.00 │
│ Input sequence length │ 200.00 │ 200.00 │ 200.00 │ 200.00 │ 200.00 │ 200.00 │
└────────────────────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┘
Output token throughput (per sec): 184.02
Request throughput (per sec): 0.61
2024-08-08 19:45 [INFO] genai_perf.export_data.json_exporter:56 - Generating artifacts/ensemble-triton-tensorrtllm-concurrency1/profile_export_genai_perf.json
2024-08-08 19:45 [INFO] genai_perf.export_data.csv_exporter:69 - Generating artifacts/ensemble-triton-tensorrtllm-concurrency1/profile_export_genai_perf.csv
参考资料#
有关更多示例,请随时参考 运行 llama 的端到端工作流程。