
如何快速實現REST API集成以優化業務流程
對于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部署大模型大致分為如下三個步驟:
下面以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()
目前支持H100、L40S、A100、A30、V100,除此之外的GPU型號可以使用Volta, Turing, Ampere, Hopper和Ada Lovelace架構運行,不過可能會有限制,比如對數據精度的支持就有所不同,如下表所示:
FP32 | FP16 | BF16 | FP8 | INT8 | INT4 | |
---|---|---|---|---|---|---|
Volta (SM70) | Y | Y | N | N | Y | Y |
Turing (SM75) | Y | Y | N | N | Y | Y |
Ampere (SM80, SM86) | Y | Y | Y | N | Y | Y |
Ada-Lovelace (SM89) | Y | Y | Y | Y | Y | Y |
Hopper (SM90) | Y | Y | Y | Y | Y | Y |
更多可以參考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/docs/source/precision.md
每個大模型對上述技術的支持不同,具體可以參考:https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/examples
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
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
文章轉自微信公眾號@ArronAI