推理1760億參數的BLOOMZ,性能時延僅3.7秒
隨著大語言模型 (Large Language Model, LLM) 的規模越來越大,在生產環境部署和使用這些模型來進行推理也變得越來越具挑戰性。為應對這些挑戰,無論硬件還是軟件,都經歷了多次創新。
下面,我們將一同探索如何有效克服這些挑戰。
BLOOMZ 簡介
BLOOM[3]是一個擁有 1760 億參數的自回歸模型,訓練后可用于生成文本序列。它可以處理 46 種語言和 13 種編程語言。
作為 BigScience[4]計劃中的一個開放科學項目,BLOOM 的設計和訓練吸引了世界各地眾多研究人員和工程師的共同參與。
BLOOMZ[5]是最近發布的、與 BLOOM 架構完全相同的模型,它是 BLOOM 基于多個任務的調優版本,具有更出色的泛化和零樣本[6]能力。
無論是訓練[7]還是推理[8]場景,這類大模型都對內存和速度提出了新挑戰。
即便使用 16 位精度,一個實例所需的內存仍高達 352 GB!目前具有如此大內存的設備可能很難找到,但像 Habana? Gaudi?2 這樣的先進硬件卻足以讓 BLOOM 和 BLOOMZ 模型以更低的時延執行推理。
Habana? Gaudi?2
Gaudi?2 是 Habana Labs 設計的第二代 AI 硬件加速器。單個服務器包含 8 張加速卡(稱為 Habana 處理單元,即 Habana Processing Unit,簡稱為 HPU),每張卡內存高達 96 GB,可提供足夠的空間來容納大模型。
然而,如果計算速度很慢,那么為大模型提供大內存的意義也不大。所幸,Gaudi?2 的計算速度也非常出色。
Gaudi?2 與 GPU 的主要區別在于它的架構能讓加速器并行執行通用矩陣乘法 (GeMM) 和其他運算,從而加快深度學習工作流。這些特性使 Gaudi?2 成為 LLM 訓練和推理的理想選擇。
Habana 的 SDK SynapseAITM支持使用 PyTorch 和 DeepSpeed 來加速 LLM 訓練和推理。SynapseAI 圖形編譯器[9]可優化圖形中所累積的操作的執行(如算子融合、數據布局管理、并行化、流水線、內存管理、圖優化等)。
此外,SynapseAI 最近引入了對 HPU Graph[10]和DeepSpeed-inference[11]的支持,這兩者都非常適合時延敏感型應用(參見下文基準測試)。
以上所有功能均已集成至 Optimum Habana[12]庫,因此在 Gaudi? 上部署模型非常簡單。
訪問此鏈接https://huggingface.co/docs/optimum/habana/quickstart,查看快速入門頁面。
如欲試用 Gaudi?2,請登錄英特爾? Developer Cloud[13]并按照本指南[14]操作。
基準測試
本節將提供 BLOOMZ 在 Gaudi?2 和第一代 Gaudi? 上的基準測試結果。
雖然 Gaudi?2 和第一代 Gaudi? 的內存都不小,但由于模型過大,單個設備仍無法容納單個 BLOOMZ 實例。
為解決這一問題,本文使用了深度學習優化庫 DeepSpeed[15]來實現多種內存和速度優化,進而加速模型推理并使模型與設備適配。
本文方案需依賴 DeepSpeed-inference[16]:它引入了諸如模型(或流水線)并行[17]等多個功能特性,可充分利用可用設備。
對于 Gaudi?2,則使用了已添加 HPU 支持的 Habana 的 DeepSpeed[18]分支。
時延
本文基于兩種不同規模但參數均達數十億的 BLOOMZ 模型(批大小為 1 個樣本)進行了實驗測試,兩種模型的參數大小分別為:
- 1760 億[19]參數 (BLOOMZ-176B)
- 70 億[20]參數 (BLOOMZ-7B)
本文使用 DeepSpeed-inference 以 16 位精度在 8 個設備上運行推理,并且使用 key-value 緩存。值得注意的是,盡管 CUDA Graph 目前與 DeepSpeed 中的模型并行不兼容(DeepSpeed v0.8.2,參見文末[21]),但 Habana 的 DeepSpeed 分支是支持 HPU Graph 的。
所有基準測試都使用貪心搜索 (Greedy Search)[22]生成 100 個詞元。輸入提示為:
DeepSpeed is a machine learning framework
BLOOM 分詞器會將該提示分為 7 個詞元。
推理時延測試結果如下圖所示(單位為秒):
△圖 1. BLOOMZ 在 Gaudi?2 和第一代 Gaudi? 上的推理時延測試結果
Habana 團隊最近在 SynapseAI 1.8 中引入了 DeepSpeed-inference 支持,可快速賦能 1000 多億參數模型的推理。
根據圖 1 測試結果可知:對于參數量達 1760 億的模型 BLOOMZ,Gaudi?2 性能表現出色,時延僅為約 3.7 秒 ;對于參數量為 70 億的較小模型 BLOOMZ-7B,Gaudi?2 的時延優勢更加顯著,單設備約為第一代 Gaudi? 的37.21%,而當設備數量都增加為 8 后,這一百分比進一步下降至約 24.33%。
Habana 團隊會在后續發布的新 SynapseAI 版本中繼續優化這些模型的性能。例如,在 SynapseAI 1.9 的預覽版中,BLOOMZ-176B 的時延從 3.7 秒進一步降低到 3.5 秒。
在完整數據集上運行推理
我們編寫的腳本支持模型在完整的數據集上完成所有句子的推理。這尤其適用于想在自有數據集上嘗試使用 Gaudi?2 進行 BLOOMZ 推理的情況。
這里我們以 tldr_news 數據集為例。該數據集包含多篇文章的標題和內容(均可在 Hugging Face Hub 中對其進行可視化處理)。前幾個樣本如下所示:
Input: 'Businesses Will Not Be Able to Hide': Spy Satellites May Give Edge From Above ;
Output: 'Businesses Will Not Be Able to Hide': Spy Satellites to Track Illegal Logging in Indonesia
The Indonesian government has announced that it will use spy satellites to track illegal logging in the country.
Input: Alphabet and SoftBank’s solar-powered drone provides first LTE connection ;
Output: Alphabet and SoftBank’s solar-powered drone provides first LTE connection connection from the stratosphere. The drone, which is about the size of a small car, is equipped with a solar panel and can fly for up
Input: SQLFlow (GitHub Repo);
Output: SQLFlow (GitHub Repo):
https://github.com/davidfowl/SQLFlow
The code is written in Java and is available on GitHub. It is a simple, lightweight
Input: Secret to keeping ice cream creamy (not crunchy);
Output: Secret to keeping ice cream creamy (not crunchy) is to freeze it in a container that is airtight.
Input: Tesla's giant battery saved $40 million during its first year, report says ;
Output: Tesla's giant battery saved $40 million during its first year, report says
Tesla's giant battery saved $40 million during its first year, report says
Tesla's giant battery saved $40 million during its first year,
Input: Python 3.9: Cool New Features for You to Try (28 minute read);
Output: Python 3.9: Cool New Features for You to Try (28 minute read): This is a great article for those
Input: A company aims to power the world for millions of years by digging the deepest holes ever ;
Output: A company aims to power the world for millions of years by digging the deepest hole ever made in
Input: In Nevada desert, a technology firm aims to be a government ;
Output: In Nevada desert, a technology firm aims to be a government Introduction
The use of the Internet has become a common practice in the daily life of people. The Internet has become△若代碼顯示不全,請左右滑動
下一節將展示如何用該腳本來執行基準測試,以及如何將其應用于 Hugging Face Hub 中任何您喜歡的數據集。
如何復現這些結果?
訪問文末[23]獲取在 Gaudi?2 和第一代 Gaudi? 上對 BLOOMZ 進行基準測試的腳本。
在運行上述腳本之前,請確保按照Habana 提供的指南[24]安裝了最新版本的 SynapseAI 和 Gaudi? 驅動程序。
然后,運行以下命令:
git clone https://github.com/huggingface/optimum-habana.git
cd optimum-habana && pip install . && cd examples/text-generation
pip install git+https://github.com/HabanaAI/DeepSpeed.git@1.8.0△若代碼顯示不全,請左右滑動
最后,按如下方式運行腳本:
git clone https://github.com/huggingface/optimum-habana.git
cd optimum-habana && pip install . && cd examples/text-generation
pip install git+https://github.com/HabanaAI/DeepSpeed.git@1.8.0△若代碼顯示不全,請左右滑動
關于多節點推理,請查看和遵循 Optimum Habana 文檔中的指南[25]。
使用參數 —dataset_name my_dataset_name 即可加載來自 Hugging Face Hub 的任何數據集以獲取用于文本生成的提示。
此基準測試基于 Transformers v4.27.1、SynapseAI v1.8.0,和源碼安裝的 Optimum Habana。
對于 GPU,此代碼庫[26]包含了可用于復現本文[27]前述測試結果的腳本。靜態形狀 (static shape) 是使用 CUDA Graph 的必要條件,而 Transformers 并不支持靜態形狀。因此,您需使用 Habana 團隊編寫的代碼[28]來啟用靜態形狀。
結論
從本文可以看出,Habana? Gaudi?2 在執行 BLOOMZ 推理時,具有較優的速度優勢,且無需編寫復雜的腳本,因為 Optimum Habana[29]提供了易于使用的工具,來支持在 HPU 上運行數十億參數模型的推理。
Habana 的 SynapseAI SDK 將于后續版本實現進一步的性能提升。隨著 SynapseAI 上大語言模型推理優化的不斷推進,我們也將定期對此基準測試進行更新,同時也期待 Gaudi?2 為 FP8 推理帶來更多性能優勢。
如有興趣使用最新 AI 硬件加速器和軟件庫來加速機器學習訓練和推理工作流,請查看 Hugging Face 的專家加速計劃[30]。
如需了解有關 Habana 解決方案的更多信息,請閱讀了解 Habana 與 Hugging Face 的合作關系[31]并聯系 Habana[32]。
如需詳細了解 Hugging Face 如何讓 AI 硬件加速器更易于使用,請查看 Hugging Face 的硬件合作伙伴計劃[33]。
英特爾研究院認知AI團隊研究科學家Philip Howard和Anahita Bhiwandiwalla還介紹了Gaudi?2 與 BLOOMZ 的相關測試。
可點擊觀看視頻[34],了解如何在 Gaudi?2 上輕松部署 BLOOMZ 等大語言模型。
[1]https://huggingface.co/blog/zh/habana-gaudi-2-bloom
[2]https://habana.ai/products/gaudi2/
[3]https://arxiv.org/abs/2211.05100
[4]https://bigscience.huggingface.co/
[5]https://arxiv.org/abs/2211.01786
[6]“零樣本”是指模型基于新輸入數據或無準備輸入數據(即未提供任何訓練示例的數據)完成任務的能力。我們向模型提供提示和以自然語言描述的指令(即我們希望模型做什么)。零樣本分類不包括與正在完成的任務相關的任何示例。這區別于單樣本或少樣本分類,因為這些任務包括特定任務的一個或多個示例。
[7]https://huggingface.co/blog/bloom-megatron-deepspeed
[8]https://huggingface.co/blog/bloom-inference-optimization
[9]https://docs.habana.ai/en/latest/Gaudi_Overview/SynapseAI_Software_Suite.html#graph-compiler-and-runtime
[10]https://docs.habana.ai/en/latest/PyTorch/Inference_on_PyTorch/Inference_Using_HPU_Graphs.html
[11]https://docs.habana.ai/en/latest/PyTorch/DeepSpeed/Inference_Using_DeepSpeed.html
[12]https://github.com/huggingface/optimum-habana
[13]https://huggingface.co/docs/optimum/habana/quickstart
[14]https://huggingface.co/blog/habana-gaudi-2-benchmark#how-to-get-access-to-gaudi2
[15]https://www.deepspeed.ai/
[16]https://arxiv.org/abs/2207.00032
[17]https://huggingface.co/blog/bloom-megatron-deepspeed#pipeline-parallelism
[18]https://github.com/HabanaAI/deepspeed
[19]bigscience/bloomz · Hugging Face
[20]bigscience/bloomz-7b1 · Hugging Face
[21]https://github.com/microsoft/DeepSpeed/blob/v0.8.2/deepspeed/inference/engine.py#L158
[22]https://huggingface.co/blog/how-to-generate#greedy-search
[23]https://github.com/huggingface/optimum-habana/tree/main/examples/text-generation
[24]https://docs.habana.ai/en/latest/Installation_Guide/index.html
[25]https://huggingface.co/docs/optimum/habana/usage_guides/multi_node_training
[26]transformers-bloom-inference/bloom-inference-scripts at main · huggingface /transformers-bloom-inference · GitHub
[27]IncrediblyFast BLOOM Inference with DeepSpeed and Accelerate (huggingface.co)
[28]Model-References/PyTorch/nlp/bloom at 1.8.0 · HabanaAI/Model-References · GitHub
[29]https://huggingface.co/docs/optimum/habana/index
[30]https://huggingface.co/support
[31]https://huggingface.co/hardware/habana
[32]https://habana.ai/contact-us/
[33]https://huggingface.co/hardware
[34]https://videos.sproutvideo.com/embed/799fd9b8141be0c6f0/79c248b5ecd76231?playerColor=0270c1&endFrame=posterFrame&autoplay=true&lightbox=true