LLM推理部署(二):英伟达LLM推理部署工具TensorRT-LLM

81 篇文章 7 订阅
62 篇文章 3 订阅

       在大模型时代,各大公司在陆续推出和优化各自的底座大模型,不断刷新榜单,然而大模型的超大参数给生产部署带来了很大的困难,由此也带来大模型部署框架的蓬勃发展(可以参考之前写的LLM推理部署(一):LLM七种推理服务框架总结),本文将介绍英伟达(NVIDIA)在TensorRT基础上针对LLM优化所推出的推理加速引擎TensorRT-LLM(https://nvidia.github.io/TensorRT-LLM/)。

TensorRT-LLM介绍

       TensorRT-LLM针对主流LLM使用TensorRT在GPU高效推理的能力,以NVIDIA Triton(https://developer.nvidia.com/nvidia-triton-inference-server)推理服务器集成为后端,可以在python和C++环境下运行,并且支持单GPU和多GPU(Tensor并行和Pipeline并行)推理,同时也支持量化模式。

      TensorRT-LLM为用户提供了一个易于使用的Python API,类似PyTorch API,比如包含einsum、softmax、matmul或view等功能,layer模块(https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/tensorrt_llm/layers)提供了一些常用的LLM组件,比如Attention、MLP或者完整的Transformer等。主要的python API如下图所示:

图片

TensorRT-LLM安装

对于windows操作系统可以参考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/windows

TensorRT-LLM必须从源码安装,参考链接如下:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/installation.md

TensorRT-LLM使用

使用TensorRT-LLM部署大模型大致分为如下三个步骤:

  1. 下载预训练模型权重;

  2. 创建大模型的全优化引擎;

  3. 部署该引擎

下面以BLOOM-560m模型为例介绍使用TensorRT-LLM部署大模型的步骤:

Step0:在docker容器中安装所需要的环境

pip install -r examples/bloom/requirements.txtgit lfs install

Step1:从Huggingface中下载BLOOM-650m模型​​​​​​​

cd examples/bloomrm -rf ./bloom/560Mmkdir -p ./bloom/560M && git clone https://huggingface.co/bigscience/bloom-560m ./bloom/560M

Step2:创建引擎​​​​​​​

# Single GPU on BLOOM 560Mpython build.py --model_dir ./bloom/560M/ \                --dtype float16 \                --use_gemm_plugin float16 \                --use_gpt_attention_plugin float16 \                --output_dir ./bloom/560M/trt_engines/fp16/1-gpu/

Note:关于参数的细节可以参考https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/examples/bloom

Step3:运行引擎​​​​​​​

python summarize.py --test_trt_llm \                    --hf_model_location ./bloom/560M/ \                    --data_type fp16 \                    --engine_dir ./bloom/560M/trt_engines/fp16/1-gpu/

除了上述example之外,我们来看一下具体API的使用说明:

       在模型级别,TensorRT-LLM采用复杂的策略,如内核融合,将其中多个操作合并到单个内核中,以减少启动多个内核的开销。它还利用量化,大大加快了计算速度,减少了内存需求,而不影响模型精度。​​​​​​​

import tensorrtllm as trtllm
 # Initialize the model model = trtllm.LargeLanguageModel('./path_to_your_model')
 # Apply kernel fusion and quantization optimization_flags = trtllm.OptimizationFlag.FUSE_OPERATIONS | trtllm.OptimizationFlag.QUANTIZE optimized_model = model.optimize(flags=optimization_flags)

       在运行时级别,TensorRT-LLM具有连续批处理等功能,允许同时计算多个推理请求,有效地提高GPU利用率和吞吐量。分页注意力是另一个新特性,优化了注意力计算过程中的内存使用,这是大型语言模型的一个常见瓶颈。​​​​​​​

# Enable in-flight batching and paged attention runtime_parameters = {    'in_flight_batching': True,    'paged_attention': True }
 # Build the engine with these runtime optimizations engine = optimized_model.build_engine(runtime_parameters=runtime_parameters)

       在当今的数字时代,速度是至关重要的。TensorRT-LLM可与传统方法相比,提供高达8倍的吞吐量。

       这种性能上的飞跃在很大程度上归功于in_flight_batching。与传统的批处理不同,在传统的批处理中,推理请求是分组处理的(导致单个请求的延迟),而在线批处理重叠了不同请求的计算,在不影响批大小的情况下大大减少了推理时间。​​​​​​​

 input_data = [...] # your input data here results = engine.execute_with_inflight_batching(input_data)

       丰富多样的大型语言模型(llm),每个模型都是为特定任务量身定制的。推理工具的效用因其与各种模型无缝集成的能力而大大增强。TensorRT-LLM在这一领域表现出色,并且提供了广泛的兼容性,从Meta的Llama 1和2到ChatGLM、Falcon、MPT、Baichuan、Starcoder等一系列llm。​​​​​​​

import tensorrtllm as trtllm  # Define and load different LLMs llama_model = trtllm.LargeLanguageModel('./path_to_llama_model') chatglm_model = trtllm.LargeLanguageModel('./path_to_chatglm_model')  # Build optimized engines for different LLMs llama_engine = llama_model.build_engine() chatglm_engine = chatglm_model.build_engine()

       部署人工智能的经济方面通常是人工智能驱动项目可行性的决定性因素。除了原始计算性能之外,TensorRT-LLM的设计还具有成本效益,解决了包括直接和间接费用在内的总拥有成本(TCO)问题。通过提高计算效率,TensorRT-LLM减少了对大量硬件资源的依赖,从而降低了能耗。​​​​​​​

import tensorrtllm as trtllm  # Initialize the model model = trtllm.LargeLanguageModel('./path_to_your_model')  # Optimize the model with energy-efficient settings optimized_model = model.optimize(energy_efficient=True)  # Monitor energy consumption energy_usage = optimized_model.monitor_energy_usage()

       进入大型语言模型(llm)的世界不需要计算机科学博士学位或多年的编程经验。TensorRT-LLM的设计以用户友好为核心。通过其直观的Python API, TensorRT-LLM使LLM优化和推理平民化,使这些先进技术能够为更广泛的受众所使用。​​​​​​​

import tensorrtllm as trtllm  # Initialize and load the model model = trtllm.LargeLanguageModel('./path_to_your_model')  # Perform common operations through easy-to-understand methods model.optimize() model.build_engine() model.execute(input_data)

       模型的规模呈指数级增长,管理计算资源至关重要。TensorRT-LLM的量化支持允许使用较低的精度(如FP8)进行计算,TensorRT-LLM在资源消耗、执行速度和模型精度之间实现了良好的平衡。这不仅加快了推理速度,还减少了内存使用,这对于在受限环境中部署大型模型至关重要。​​​​​​​

import tensorrtllm as trtllm  # Initialize the model model = trtllm.LargeLanguageModel('./path_to_your_model')  # Enable quantization quantized_model = model.enable_quantization(precision='FP8')  # Build and execute the quantized model engine = quantized_model.build_engine() result = engine.execute(input_data)

       作为NVIDIA官方产品,TensorRT-LLM在构建时考虑了适应性,准备与新兴的LLM生态系统集成。随着新模型架构的出现和现有模型的完善,TensorRT-LLM支持与前沿开发的无缝集成。​​​​​​​

 import tensorrtllm as trtllm  # Initialize the model model = trtllm.LargeLanguageModel('./path_to_your_model')  # Update the model with new kernels or architectures updated_model = model.update_components(new_kernels='./path_to_new_kernels',                                        new_architectures='./path_to_new_architectures')  # Re-optimize and deploy the updated model updated_engine = updated_model.build_engine()

TensorRT-LLM支持的GPU类型

       目前支持H100、L40S、A100、A30、V100,除此之外的GPU型号可以使用Volta, Turing, Ampere, Hopper和Ada Lovelace架构运行,不过可能会有限制,比如对数据精度的支持就有所不同,如下表所示:

FP32

FP16BF16FP8INT8INT4
Volta (SM70)YYNNYY
Turing (SM75)YYNNYY
Ampere (SM80, SM86)YYYNYY
Ada-Lovelace (SM89)YYYYYY
Hopper (SM90)YYYYYY

更多可以参考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/precision.md

TensorRT-LLM支持的核心技术点

图片

       每个大模型对上述技术的支持不同,具体可以参考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/examples

TensorRT-LLM支持的大模型

图片

TensorRT-LLM大模型的推理性能

       GPT-J, LLAMA-7B, LLAMA-70B, Falcon-180B模型在H100, L40S 和 A100GPU下测试的性能可以参考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/performance.md

TensorRT-LLM的一些高级话题

Quantization

https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/precision.md

In-flight Batching

https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/batch_manager.md

Attention

https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/gpt_attention.md

Graph Rewriting

https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/graph-rewriting.md

参考文献:

[1] https://github.com/NVIDIA/TensorRT-LLM

[2] https://nvidia.github.io/TensorRT-LLM/

[3] https://mp.weixin.qq.com/s/pIZ9ceJzTG8kMZMn1m5oQw

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wshzd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值