隨著生成式人工智能 (Generative AI,GenAI) 革命的全面推進(jìn),使用 Llama 2 等開源 transformer 模型生成文本已成為新風(fēng)尚。人工智能愛好者及開發(fā)人員正在尋求利用此類模型的生成能力來賦能不同的場景及應(yīng)用。本文展示了如何基于 Optimum Habana 以及我們實現(xiàn)的流水線類輕松使用 Llama 2 系列模型 (7b、13b 及 70b) 生成文本 - 僅需幾行代碼,即可運行!
我們設(shè)計并實現(xiàn)了一個旨在為用戶提供極大的靈活性和易用性流水線類。它提供了高層級的抽象以支持包含預(yù)處理和后處理在內(nèi)的端到端文本生成。同時,用戶也可以通過多種方法使用該流水線類 - 你可以在 Optimum Habana 代碼庫中直接運行 run_pipeline.py
腳本,也可以在你自己的 python 腳本中調(diào)用該流水線類,還可以用該流水線類來初始化 LangChain。
準(zhǔn)備工作
由于 Llama 2 模型實行的是許可式訪問,因此如果你尚未申請訪問權(quán)限,需要首先申請訪問權(quán)限。方法如下: 首先,訪問 Meta 網(wǎng)站 并接受相應(yīng)條款。一旦 Meta 授予你訪問權(quán)限 (可能需要一兩天),你需要使用你當(dāng)時使用的電子郵箱地址申請 Hugging Face Llama 2 模型庫 的訪問權(quán)限。
獲取訪問權(quán)限后,可通過運行以下命令登錄你的 Hugging Face 帳戶 (此時會需要一個訪問令牌,你可從 你的用戶個人資料頁面 上獲取):
huggingface-cli login
你還需要安裝最新版本的 Optimum Habana 并拉取其代碼庫以獲取后續(xù)要使用的腳本。命令如下:
pip install optimum-habana==1.10.4
git clone -b v1.10-release https://github.com/huggingface/optimum-habana.git
如果想運行分布式推理,還需要根據(jù)你的 SynapseAI 版本安裝對應(yīng)的 DeepSpeed。在本例中,我使用的是 SynapseAI 1.14.0。
pip install git+https://github.com/HabanaAI/DeepSpeed.git@1.14.0
至此,準(zhǔn)備完畢!
方法一: 通過命令直接使用流水線腳本
首先,使用如下命令進(jìn)入 optimum-habana
的相應(yīng)目錄,然后按照 README
中的說明更新 PYTHONPATH
。
cd optimum-habana/examples/text-generation
pip install -r requirements.txt
cd text-generation-pipeline
如果你想用自己的提示生成文本序列,下面給出了一個示例:
python run_pipeline.py \
--model_name_or_path meta-llama/Llama-2-7b-hf \
--use_hpu_graphs \
--use_kv_cache \
--max_new_tokens 100 \
--do_sample \
--prompt "Here is my prompt"
你還可以傳入多個提示作為輸入,并更改生成的溫度或 top_p
值,如下所示:
python run_pipeline.py \
--model_name_or_path meta-llama/Llama-2-13b-hf \
--use_hpu_graphs \
--use_kv_cache \
--max_new_tokens 100 \
--do_sample \
--temperature 0.5 \
--top_p 0.95 \
--prompt "Hello world" "How are you?"
如果想用 Llama-2-70b 等大尺寸模型生成文本,下面給出了一個用 DeepSpeed 啟動流水線的示例命令:
python ../../gaudi_spawn.py \
--use_deepspeed \
--world_size 8 run_pipeline.py \
--model_name_or_path meta-llama/Llama-2-70b-hf \
--max_new_tokens 100 \
--bf16 \
--use_hpu_graphs \
--use_kv_cache \
--do_sample \
--temperature 0.5 \
--top_p 0.95 \
--prompt "Hello world" "How are you?" "Here is my prompt" "Once upon a time"
方法二: 在自己的 Python 腳本中調(diào)用流水線類
你還可以在自己的 Python 腳本中調(diào)用我們實現(xiàn)的流水線類,如下例所示。你需要在 optimum-habana/examples/text-generation/text- generation-pipeline
目錄下運行該示例腳本 [譯者注: 原因是 GaudiTextGenerationPipeline
這個類的定義在該目錄的 pipeline.py
中]。
import argparse
import logging
from pipeline import GaudiTextGenerationPipeline
from run_generation import setup_parser
# Define a logger
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# Set up an argument parser
parser = argparse.ArgumentParser()
args = setup_parser(parser)
# Define some pipeline arguments. Note that --model_name_or_path is a required argument for this script
args.num_return_sequences = 1
args.model_name_or_path = "meta-llama/Llama-2-7b-hf"
args.max_new_tokens = 100
args.use_hpu_graphs = True
args.use_kv_cache = True
args.do_sample = True
# Initialize the pipeline
pipe = GaudiTextGenerationPipeline(args, logger)
# You can provide input prompts as strings
prompts = ["He is working on", "Once upon a time", "Far far away"]
# Generate text with pipeline
for prompt in prompts:
print(f"Prompt: {prompt}")
output = pipe(prompt)
print(f"Generated Text: {repr(output)}")
你需要用
python <name_of_script>.py --model_name_or_path a_model_name
命令來運行上述腳本,其中--model_name_or_path
是必需的參數(shù)。當(dāng)然,你也可以在代碼中直接更改模型名稱 (如上述 Python 代碼片段所示)。
上述代碼段表明我們實現(xiàn)的流水線類 GaudiTextGenerationPipeline
會對輸入字符串執(zhí)行生成文本所需的全部操作,包括數(shù)據(jù)預(yù)處理及后處理在內(nèi)。
方法二: 在 LangChain 中使用流水線類
如果在構(gòu)造時傳入 use_with_langchain
參數(shù)的話,我們的文本生成流水線還可以作為 LangChain 的兼容組件使用。首先,按照如下方式安裝 LangChain:
pip install langchain==0.0.191
下面給出了一個如何在 LangChain 中使用我們的流水線類的代碼示例。
import argparse
import logging
from langchain.llms import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from pipeline import GaudiTextGenerationPipeline
from run_generation import setup_parser
# Define a logger
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
# Set up an argument parser
parser = argparse.ArgumentParser()
args = setup_parser(parser)
# Define some pipeline arguments. Note that --model_name_or_path is a required argument for this script
args.num_return_sequences = 1
args.model_name_or_path = "meta-llama/Llama-2-13b-chat-hf"
args.max_input_tokens = 2048
args.max_new_tokens = 1000
args.use_hpu_graphs = True
args.use_kv_cache = True
args.do_sample = True
args.temperature = 0.2
args.top_p = 0.95
# Initialize the pipeline
pipe = GaudiTextGenerationPipeline(args, logger, use_with_langchain=True)
# Create LangChain object
llm = HuggingFacePipeline(pipeline=pipe)
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer,\
just say that you don't know, don't try to make up an answer.
Context: Large Language Models (LLMs) are the latest models used in NLP.
Their superior performance over smaller models has made them incredibly
useful for developers building NLP enabled applications. These models
can be accessed via Hugging Face's `transformers` library, via OpenAI
using the `openai` library, and via Cohere using the `cohere` library.
Question: {question}
Answer: """
prompt = PromptTemplate(input_variables=["question"], template=template)
llm_chain = LLMChain(prompt=prompt, llm=llm)
# Use LangChain object
question = "Which libraries and model providers offer LLMs?"
response = llm_chain(prompt.format(question=question))
print(f"Question 1: {question}")
print(f"Response 1: {response['text']}")
question = "What is the provided context about?"
response = llm_chain(prompt.format(question=question))
print(f"\nQuestion 2: {question}")
print(f"Response 2: {response['text']}")
該流水線類當(dāng)前僅在 LangChain 0.0.191 版上驗證通過,其他版本可能不兼容。
總結(jié)
我們在英特爾? Gaudi? 2 AI 加速器上實現(xiàn)了一個自定義的文本生成流水線,其可接受單個或多個提示作為輸入。該流水線類靈活支持各種模型尺寸及各種影響文本生成質(zhì)量參數(shù)。此外,不管是直接使用還是將它插入你自己的腳本都非常簡單,并且其還與 LangChain 兼容。
使用預(yù)訓(xùn)練模型需遵守第三方許可,如 “Llama 2 社區(qū)許可協(xié)議”(LLAMAV2)。有關(guān) LLAMA2 模型的預(yù)期用途有哪些、哪些行為會被視為濫用或超范圍使用、預(yù)期使用者是誰以及其他條款,請仔細(xì)閱讀此 鏈接 中的說明。用戶需自主承擔(dān)遵守任何第三方許可的責(zé)任和義務(wù),Habana Labs 不承擔(dān)任何與用戶使用或遵守第三方許可相關(guān)的責(zé)任。為了能夠運行像
Llama-2-70b-hf
這樣的受限模型,你需要:
- 有一個 Hugging Face 帳戶
- 同意 HF Hub 上模型卡中的模型使用條款
- 設(shè)好訪問令牌
- 使用 HF CLI 登錄你的帳戶,即在啟動腳本之前運行
huggingface-cli login
英文原文: https://hf.co/blog/textgen-pipe-gaudi
原文作者: Siddhant Jagtap文章來源:http://www.zghlxwxcb.cn/news/detail-839511.html
譯者: Matrix Yao (姚偉峰),英特爾深度學(xué)習(xí)工程師,工作方向為 transformer-family 模型在各模態(tài)數(shù)據(jù)上的應(yīng)用及大規(guī)模模型的訓(xùn)練推理。文章來源地址http://www.zghlxwxcb.cn/news/detail-839511.html
到了這里,關(guān)于基于英特爾? Gaudi? 2 AI 加速器的文本生成流水線的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!