Build an inference environment with TensorRT-LLM
This topic shows how to install and use the TensorRT-LLM inference engine on a GPU instance to build a high-performance inference environment for large language models (LLMs) like Llama, ChatGLM, Baichuan, and Qwen. This environment is ideal for natural language processing (NLP) applications, such as intelligent chatbots and text analytics.
TensorRT-LLM is an open-source library from NVIDIA designed to accelerate the inference performance of large language models. For more information about the models and GPUs supported by TensorRT-LLM, see TensorRT-LLM.
Limitations
-
You can install TensorRT-LLM only on GPU-accelerated compute-optimized instances. For more information, see GPU-accelerated compute-optimized instance families. This topic uses a gn6i instance as an example.
-
In Alibaba Cloud Marketplace, only Ubuntu 22.04 64-bit marketplace images have TensorRT-LLM pre-installed.
-
Only the Ubuntu 22.04 64-bit public image supports installing TensorRT-LLM.
Install TensorRT-LLM
Some marketplace images have TensorRT-LLM pre-installed. To install it automatically, select one of these images when you create a GPU instance. Alternatively, you can create a GPU instance and then install TensorRT-LLM manually.
Automatic installation (marketplace image)
-
Obtain a marketplace image and create a GPU instance.
Marketplace images with TensorRT-LLM pre-installed are available from the following locations.
ECS purchase page
-
Go to the instance creation page.
-
Select the Custom Launch tab.
-
Configure settings such as billing method, region, network, zone, instance type, and image based on your requirements.
Note the following parameters. For details about other parameters, see Configuration parameters.
-
Instance: This topic uses the
ecs.gn6i-c4g1.xlargeinstance type as an example. -
Images: On the Marketplace Images tab, search for
tensorrt-llmand select the image you want.When you create a GPU-accelerated compute-optimized instance, you can select the VM image Ubuntu 22.04 with tensorrt-llm pre-installed. This image allows you to use TensorRT-LLM immediately without additional configuration. The image and version details are as follows:
Instance types
Pre-installed image
Latest version
Ubuntu 22.04 with tensorrt-llm pre-installed
v0.10.0
-
-
Follow the on-screen instructions and click Confirm Order.
-
On the payment page, review the total cost of the instance and complete the payment as prompted.
Alibaba Cloud Marketplace
-
Go to the Alibaba Cloud Marketplace page.
-
In the search bar, enter
tensorrt-llmand press Enter.The search results display both Docker and VM versions of the TensorRT-LLM image.
-
Select the image type you need and click Details.
The marketplace offers the VM image Ubuntu 22.04 with tensorrt-llm pre-installed. This image allows you to use TensorRT-LLM immediately without additional configuration. The image and version details are as follows:
Instance types
Pre-installed image
Latest version
Ubuntu 22.04 with tensorrt-llm pre-installed
v0.10.0
-
On the image details page, click Purchase Now.
NoteThe system image is free. You only pay for the GPU instance.
-
On the instance purchase page, go to the Images section and confirm that your purchased image is selected on the Marketplace Images tab.
If the purchased image is already selected, no action is needed. If it is not selected, click Select a New Image and choose the correct image.
-
On the instance purchase page, configure the remaining parameters and create the GPU instance.
For more information, see Configuration parameters.
-
-
Remotely connect to the GPU instance.
For detailed instructions, see Connect to a Linux instance by using Workbench.
-
Run the following command to check the TensorRT-LLM installation status and version:
python3 -c "import tensorrt_llm"If TensorRT-LLM is installed successfully, the command outputs the version information.
[TensorRT-LLM] TensorRT-LLM version: 0.10.0
Manual installation (public image)
Create a GPU instance with the Ubuntu 22.04 64-bit Public Image, and then install TensorRT-LLM on the instance.
-
Create a GPU instance.
-
Go to the instance creation page.
-
Select the Custom Launch tab.
-
Configure settings such as billing method, region, network, zone, instance type, and image based on your requirements.
Note the following parameters. For details about other parameters, see Configuration parameters.
-
Instance: This topic uses the
ecs.gn6i-c4g1.xlargeinstance type as an example. -
Images: On the Public Image tab, select Ubuntu 22.04 64-bit.
-
-
Follow the on-screen instructions and click Confirm Order.
-
On the payment page, review the total cost of the instance and complete the payment as prompted.
-
-
Remotely connect to the GPU instance.
For detailed instructions, see Connect to a Linux instance by using Workbench.
-
Run the following commands to install the Tesla driver and CUDA Toolkit for the GPU instance:
wget https://developer.download.nvidia.com/compute/cuda/12.5.0/local_installers/cuda_12.5.0_555.42.02_linux.run sudo sh cuda_12.5.0_555.42.02_linux.run -
Run the following commands to configure the environment variables:
export PATH=/usr/local/cuda-12.5/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-12.5/lib64:$LD_LIBRARY_PATH -
Run the following commands to verify that the GPU driver and CUDA are installed successfully:
nvidia-smi nvcc -V -
Run the following commands to install TensorRT-LLM and its dependencies:
NoteThis process can be time-consuming.
sudo apt-get update sudo apt-get -y install python3.10 python3-pip openmpi-bin libopenmpi-dev sudo pip3 install tensorrt_llm -U --extra-index-url https://pypi.nvidia.com -
Run the following command to check the TensorRT-LLM installation status and version:
python3 -c "import tensorrt_llm"If TensorRT-LLM is installed successfully, the command outputs the version information.
[TensorRT-LLM] TensorRT-LLM version: 0.10.0
Use TensorRT-LLM
TensorRT-LLM provides high-performance inference optimization. This section uses the Qwen1.5-4B-Chat model as an example to demonstrate how to quickly run a model with TensorRT-LLM.
-
Run the following commands to download the TensorRT-LLM source code.
This example uses TensorRT-LLM v0.10.0. Adjust the version based on your requirements.
wget https://github.com/NVIDIA/TensorRT-LLM/archive/refs/tags/v0.10.0.tar.gz tar xvf v0.10.0.tar.gz -
Run the following command to download the open-source Qwen1.5-4B-Chat model.
This example uses the Qwen1.5-4B-Chat model from ModelScope.
ImportantThe model is large. Before you start the download, ensure you have a stable, high-bandwidth network connection.
git-lfs clone https://modelscope.cn/qwen/Qwen1.5-4B-Chat.git -
Build the model Engine.
-
Run the following command to navigate to the extracted TensorRT-LLM source code directory:
cd TensorRT-LLM-0.10.0/examples/qwen -
Run the following command to install the required dependencies for the model:
sudo pip install -r requirements.txt -
Run the following commands to configure model parameters and build the model Engine:
python3 convert_checkpoint.py --model_dir /home/ecs-user/Qwen1.5-4B-Chat --output_dir /home/ecs-user/trt_checkpoint --dtype float16trtllm-build --checkpoint_dir /home/ecs-user/trt_checkpoint --output_dir /home/ecs-user/trt_engines/qwen1.5-4b-chat/1-gpu --gemm_plugin float16NoteIf the model Engine build fails, adjust the parameters according to the error message and retry. You can also enable features such as int8 and int4 quantization. For more information, see the
README.mdfile in the model directory.
-
-
Run the following command to perform model inference with TensorRT-LLM.
For example, run model inference with TensorRT-LLM, using
Hello, who are you?as the text input parameter.python3 ../run.py --input_text "你好,你是谁?" --max_output_len=500 --tokenizer_dir /home/ecs-user/Qwen1.5-4B-Chat --engine_dir=/home/ecs-user/trt_engines/qwen1.5-4b-chat/1-gpuThe inference result is displayed:
Input [Text 0]: "<|im_start|>system You are a helpful assistant.<|im_end|> <|im_start|>user 你好, 你是谁? <|im_end|> <|im_start|>assistant" Output [Text 0 Beam 0]: "我是来自阿里云的大规模语言模型,我叫通义千问。"