• 使用TensorRT-LLM进行高性能推理


    LLM的火爆之后,英伟达(NVIDIA)也发布了其相关的推理加速引擎TensorRT-LLM。TensorRT是nvidia家的一款高性能深度学习推理SDK。此SDK包含深度学习推理优化器和运行环境,可为深度学习推理应用提供低延迟和高吞吐量。而TensorRT-LLM是在TensorRT基础上针对大模型进一步优化的加速推理库,它号称可以增加4倍的推理速度。

    所以本文将对其做一个简单的使用介绍。

    前所未有的优化

    在AI世界中优化不仅是一种福利而且是必要的。TensorRT-LLM引入了一系列在模型和运行时级别都具有开创性的优化。

    在模型级别,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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在运行时级别,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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这些优化提供了实质性的性能改进,但它们需要仔细调优和彻底的测试。验证优化后模型的功能和性能完整性非常重要,以确保增强不会对模型的准确性或可靠性产生不利影响。

    加速推理

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

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

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

    另一个主要改进是针对GPU密集型操作优化的内存管理,这确保了GPU的最大计算能力得到利用。

    为了充分利用加速推理,平衡CPU和GPU之间的负载至关重要,确保两者都不会成为瓶颈。需要管理输入模型的数据管道和在GPU上执行的计算。监测系统的功率性能也非常重要,持续的高利用率操作可能会使系统资源紧张。

    广泛的模型支持

    丰富多样的大型语言模型(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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    节约成本

    部署人工智能的经济方面通常是人工智能驱动项目可行性的决定性因素。除了原始计算性能之外,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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    为了最大限度地节省成本,对性能指标进行持续的监控和分析是必不可少的。利用日志记录和监视工具跟踪能源使用情况、计算效率和硬件运行状况。这样可以定期审查运营成本,并准备根据这些见解调整使用模式或配置。

    易用性

    进入大型语言模型(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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    即使有了易于使用的API,可以极大地平滑学习曲线,并为更有效的使用提供有价值的见解。

    量化的支持

    模型的规模呈指数级增长,管理计算资源至关重要。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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    生态集成

    作为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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    总结

    为什么选择TensorRT-LLM呢?

    因为它是NVIDIA官方产品,兼容性和速度优化肯定是有保障的,它提供了无与伦比的速度增强、广泛的模型支持和显著的成本降低,同时简化了LLM优化的任务。它对多种模型的强大支持,通过节能计算实现成本效益的承诺,以及与动态AI生态系统的无缝集成,使TensorRT-LLM成为经验丰富的开发人员和新手都不可或缺的资产。

    https://avoid.overfit.cn/post/9601a73924024e2895957627f8e35bda

  • 相关阅读:
    vue 城市选择器的使用 element-china-area-data
    小程序开发 - 基本组件
    hyperf框架接入pgsql扩展包
    git使用详解
    SPI机制是什么?
    备份服务器数据库并保存到Git仓库
    变分推断(Variational Inference)解析
    Inception-v2/v3模型
    Remove the specified nodes in the linked list with dummy header
    LeetCode 热题 100-49. 字母异位词分组
  • 原文地址:https://blog.csdn.net/m0_46510245/article/details/133969443