推理1760億參數(shù)的BLOOMZ,性能時(shí)延僅3.7秒
隨著大語(yǔ)言模型 (Large Language Model, LLM) 的規(guī)模越來(lái)越大,在生產(chǎn)環(huán)境部署和使用這些模型來(lái)進(jìn)行推理也變得越來(lái)越具挑戰(zhàn)性。為應(yīng)對(duì)這些挑戰(zhàn),無(wú)論硬件還是軟件,都經(jīng)歷了多次創(chuàng)新。
下面,我們將一同探索如何有效克服這些挑戰(zhàn)。
BLOOMZ 簡(jiǎn)介
BLOOM[3]是一個(gè)擁有 1760 億參數(shù)的自回歸模型,訓(xùn)練后可用于生成文本序列。它可以處理 46 種語(yǔ)言和 13 種編程語(yǔ)言。
作為 BigScience[4]計(jì)劃中的一個(gè)開(kāi)放科學(xué)項(xiàng)目,BLOOM 的設(shè)計(jì)和訓(xùn)練吸引了世界各地眾多研究人員和工程師的共同參與。
BLOOMZ[5]是最近發(fā)布的、與 BLOOM 架構(gòu)完全相同的模型,它是 BLOOM 基于多個(gè)任務(wù)的調(diào)優(yōu)版本,具有更出色的泛化和零樣本[6]能力。
無(wú)論是訓(xùn)練[7]還是推理[8]場(chǎng)景,這類(lèi)大模型都對(duì)內(nèi)存和速度提出了新挑戰(zhàn)。
即便使用 16 位精度,一個(gè)實(shí)例所需的內(nèi)存仍高達(dá) 352 GB!目前具有如此大內(nèi)存的設(shè)備可能很難找到,但像 Habana? Gaudi?2 這樣的先進(jìn)硬件卻足以讓 BLOOM 和 BLOOMZ 模型以更低的時(shí)延執(zhí)行推理。
Habana? Gaudi?2
Gaudi?2 是 Habana Labs 設(shè)計(jì)的第二代 AI 硬件加速器。單個(gè)服務(wù)器包含 8 張加速卡(稱(chēng)為 Habana 處理單元,即 Habana Processing Unit,簡(jiǎn)稱(chēng)為 HPU),每張卡內(nèi)存高達(dá) 96 GB,可提供足夠的空間來(lái)容納大模型。
然而,如果計(jì)算速度很慢,那么為大模型提供大內(nèi)存的意義也不大。所幸,Gaudi?2 的計(jì)算速度也非常出色。
Gaudi?2 與 GPU 的主要區(qū)別在于它的架構(gòu)能讓加速器并行執(zhí)行通用矩陣乘法 (GeMM) 和其他運(yùn)算,從而加快深度學(xué)習(xí)工作流。這些特性使 Gaudi?2 成為 LLM 訓(xùn)練和推理的理想選擇。
Habana 的 SDK SynapseAITM支持使用 PyTorch 和 DeepSpeed 來(lái)加速 LLM 訓(xùn)練和推理。SynapseAI 圖形編譯器[9]可優(yōu)化圖形中所累積的操作的執(zhí)行(如算子融合、數(shù)據(jù)布局管理、并行化、流水線(xiàn)、內(nèi)存管理、圖優(yōu)化等)。
此外,SynapseAI 最近引入了對(duì) HPU Graph[10]和DeepSpeed-inference[11]的支持,這兩者都非常適合時(shí)延敏感型應(yīng)用(參見(jiàn)下文基準(zhǔn)測(cè)試)。
以上所有功能均已集成至 Optimum Habana[12]庫(kù),因此在 Gaudi? 上部署模型非常簡(jiǎn)單。
訪(fǎng)問(wèn)此鏈接https://huggingface.co/docs/optimum/habana/quickstart,查看快速入門(mén)頁(yè)面。
如欲試用 Gaudi?2,請(qǐng)登錄英特爾? Developer Cloud[13]并按照本指南[14]操作。
基準(zhǔn)測(cè)試
本節(jié)將提供 BLOOMZ 在 Gaudi?2 和第一代 Gaudi? 上的基準(zhǔn)測(cè)試結(jié)果。
雖然 Gaudi?2 和第一代 Gaudi? 的內(nèi)存都不小,但由于模型過(guò)大,單個(gè)設(shè)備仍無(wú)法容納單個(gè) BLOOMZ 實(shí)例。
為解決這一問(wèn)題,本文使用了深度學(xué)習(xí)優(yōu)化庫(kù) DeepSpeed[15]來(lái)實(shí)現(xiàn)多種內(nèi)存和速度優(yōu)化,進(jìn)而加速模型推理并使模型與設(shè)備適配。
本文方案需依賴(lài) DeepSpeed-inference[16]:它引入了諸如模型(或流水線(xiàn))并行[17]等多個(gè)功能特性,可充分利用可用設(shè)備。
對(duì)于 Gaudi?2,則使用了已添加 HPU 支持的 Habana 的 DeepSpeed[18]分支。
時(shí)延
本文基于兩種不同規(guī)模但參數(shù)均達(dá)數(shù)十億的 BLOOMZ 模型(批大小為 1 個(gè)樣本)進(jìn)行了實(shí)驗(yàn)測(cè)試,兩種模型的參數(shù)大小分別為:
- 1760 億[19]參數(shù) (BLOOMZ-176B)
- 70 億[20]參數(shù) (BLOOMZ-7B)
本文使用 DeepSpeed-inference 以 16 位精度在 8 個(gè)設(shè)備上運(yùn)行推理,并且使用 key-value 緩存。值得注意的是,盡管 CUDA Graph 目前與 DeepSpeed 中的模型并行不兼容(DeepSpeed v0.8.2,參見(jiàn)文末[21]),但 Habana 的 DeepSpeed 分支是支持 HPU Graph 的。
所有基準(zhǔn)測(cè)試都使用貪心搜索 (Greedy Search)[22]生成 100 個(gè)詞元。輸入提示為:
DeepSpeed is a machine learning framework
BLOOM 分詞器會(huì)將該提示分為 7 個(gè)詞元。
推理時(shí)延測(cè)試結(jié)果如下圖所示(單位為秒):
△圖 1. BLOOMZ 在 Gaudi?2 和第一代 Gaudi? 上的推理時(shí)延測(cè)試結(jié)果
Habana 團(tuán)隊(duì)最近在 SynapseAI 1.8 中引入了 DeepSpeed-inference 支持,可快速賦能 1000 多億參數(shù)模型的推理。
根據(jù)圖 1 測(cè)試結(jié)果可知:對(duì)于參數(shù)量達(dá) 1760 億的模型 BLOOMZ,Gaudi?2 性能表現(xiàn)出色,時(shí)延僅為約 3.7 秒 ;對(duì)于參數(shù)量為 70 億的較小模型 BLOOMZ-7B,Gaudi?2 的時(shí)延優(yōu)勢(shì)更加顯著,單設(shè)備約為第一代 Gaudi? 的37.21%,而當(dāng)設(shè)備數(shù)量都增加為 8 后,這一百分比進(jìn)一步下降至約 24.33%。
Habana 團(tuán)隊(duì)會(huì)在后續(xù)發(fā)布的新 SynapseAI 版本中繼續(xù)優(yōu)化這些模型的性能。例如,在 SynapseAI 1.9 的預(yù)覽版中,BLOOMZ-176B 的時(shí)延從 3.7 秒進(jìn)一步降低到 3.5 秒。
在完整數(shù)據(jù)集上運(yùn)行推理
我們編寫(xiě)的腳本支持模型在完整的數(shù)據(jù)集上完成所有句子的推理。這尤其適用于想在自有數(shù)據(jù)集上嘗試使用 Gaudi?2 進(jìn)行 BLOOMZ 推理的情況。
這里我們以 tldr_news 數(shù)據(jù)集為例。該數(shù)據(jù)集包含多篇文章的標(biāo)題和內(nèi)容(均可在 Hugging Face Hub 中對(duì)其進(jìn)行可視化處理)。前幾個(gè)樣本如下所示:
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△若代碼顯示不全,請(qǐng)左右滑動(dòng)
下一節(jié)將展示如何用該腳本來(lái)執(zhí)行基準(zhǔn)測(cè)試,以及如何將其應(yīng)用于 Hugging Face Hub 中任何您喜歡的數(shù)據(jù)集。
如何復(fù)現(xiàn)這些結(jié)果?
訪(fǎng)問(wèn)文末[23]獲取在 Gaudi?2 和第一代 Gaudi? 上對(duì) BLOOMZ 進(jìn)行基準(zhǔn)測(cè)試的腳本。
在運(yùn)行上述腳本之前,請(qǐng)確保按照Habana 提供的指南[24]安裝了最新版本的 SynapseAI 和 Gaudi? 驅(qū)動(dòng)程序。
然后,運(yùn)行以下命令:
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△若代碼顯示不全,請(qǐng)左右滑動(dòng)
最后,按如下方式運(yùn)行腳本:
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△若代碼顯示不全,請(qǐng)左右滑動(dòng)
關(guān)于多節(jié)點(diǎn)推理,請(qǐng)查看和遵循 Optimum Habana 文檔中的指南[25]。
使用參數(shù) —dataset_name my_dataset_name 即可加載來(lái)自 Hugging Face Hub 的任何數(shù)據(jù)集以獲取用于文本生成的提示。
此基準(zhǔn)測(cè)試基于 Transformers v4.27.1、SynapseAI v1.8.0,和源碼安裝的 Optimum Habana。
對(duì)于 GPU,此代碼庫(kù)[26]包含了可用于復(fù)現(xiàn)本文[27]前述測(cè)試結(jié)果的腳本。靜態(tài)形狀 (static shape) 是使用 CUDA Graph 的必要條件,而 Transformers 并不支持靜態(tài)形狀。因此,您需使用 Habana 團(tuán)隊(duì)編寫(xiě)的代碼[28]來(lái)啟用靜態(tài)形狀。
結(jié)論
從本文可以看出,Habana? Gaudi?2 在執(zhí)行 BLOOMZ 推理時(shí),具有較優(yōu)的速度優(yōu)勢(shì),且無(wú)需編寫(xiě)復(fù)雜的腳本,因?yàn)?Optimum Habana[29]提供了易于使用的工具,來(lái)支持在 HPU 上運(yùn)行數(shù)十億參數(shù)模型的推理。
Habana 的 SynapseAI SDK 將于后續(xù)版本實(shí)現(xiàn)進(jìn)一步的性能提升。隨著 SynapseAI 上大語(yǔ)言模型推理優(yōu)化的不斷推進(jìn),我們也將定期對(duì)此基準(zhǔn)測(cè)試進(jìn)行更新,同時(shí)也期待 Gaudi?2 為 FP8 推理帶來(lái)更多性能優(yōu)勢(shì)。
如有興趣使用最新 AI 硬件加速器和軟件庫(kù)來(lái)加速機(jī)器學(xué)習(xí)訓(xùn)練和推理工作流,請(qǐng)查看 Hugging Face 的專(zhuān)家加速計(jì)劃[30]。
如需了解有關(guān) Habana 解決方案的更多信息,請(qǐng)閱讀了解 Habana 與 Hugging Face 的合作關(guān)系[31]并聯(lián)系 Habana[32]。
如需詳細(xì)了解 Hugging Face 如何讓 AI 硬件加速器更易于使用,請(qǐng)查看 Hugging Face 的硬件合作伙伴計(jì)劃[33]。
英特爾研究院認(rèn)知AI團(tuán)隊(duì)研究科學(xué)家Philip Howard和Anahita Bhiwandiwalla還介紹了Gaudi?2 與 BLOOMZ 的相關(guān)測(cè)試。
可點(diǎn)擊觀(guān)看視頻[34],了解如何在 Gaudi?2 上輕松部署 BLOOMZ 等大語(yǔ)言模型。
[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]“零樣本”是指模型基于新輸入數(shù)據(jù)或無(wú)準(zhǔn)備輸入數(shù)據(jù)(即未提供任何訓(xùn)練示例的數(shù)據(jù))完成任務(wù)的能力。我們向模型提供提示和以自然語(yǔ)言描述的指令(即我們希望模型做什么)。零樣本分類(lèi)不包括與正在完成的任務(wù)相關(guān)的任何示例。這區(qū)別于單樣本或少樣本分類(lèi),因?yàn)檫@些任務(wù)包括特定任務(wù)的一個(gè)或多個(gè)示例。
[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