IPEX-LLM
IPEX-LLM 是一個 PyTorch 函式庫,用於在 Intel CPU 和 GPU (例如,具有 iGPU 的本地 PC,離散 GPU 如 Arc、Flex 和 Max) 上以非常低的延遲執行 LLM。
IPEX-LLM 在 Intel GPU 上
此範例說明如何使用 LangChain 與 ipex-llm
互動,以在 Intel GPU 上進行文字生成。
注意
建議僅有搭載 Intel Arc A 系列 GPU (Intel Arc A300 系列或 Pro A60 除外) 的 Windows 使用者直接執行 Jupyter Notebook 以進行「IPEX-LLM 在 Intel GPU 上」章節。對於其他情況 (例如 Linux 使用者、Intel iGPU 等),建議在終端機中使用 Python 腳本執行程式碼,以獲得最佳體驗。
安裝先決條件
為了從 Intel GPU 上的 IPEX-LLM 受益,需要幾個先決條件步驟來安裝工具和準備環境。
如果您是 Windows 使用者,請訪問在 Windows 上安裝 IPEX-LLM 與 Intel GPU 指南,並按照安裝先決條件更新 GPU 驅動程式 (可選) 並安裝 Conda。
如果您是 Linux 使用者,請訪問在 Linux 上安裝 IPEX-LLM 與 Intel GPU,並按照安裝先決條件安裝 GPU 驅動程式、Intel® oneAPI Base Toolkit 2024.0 和 Conda。
設定
在安裝先決條件後,您應該已經建立了一個安裝了所有先決條件的 conda 環境。在此 conda 環境中啟動 jupyter 服務
%pip install -qU langchain langchain-community
安裝 IEPX-LLM 以在 Intel GPU 上本地執行 LLM。
%pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
注意
您也可以使用
https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/
作為 extra-indel-url。
執行階段配置
為了獲得最佳效能,建議根據您的裝置設定幾個環境變數
適用於搭載 Intel Core Ultra 整合式 GPU 的 Windows 使用者
import os
os.environ["SYCL_CACHE_PERSISTENT"] = "1"
os.environ["BIGDL_LLM_XMX_DISABLED"] = "1"
適用於搭載 Intel Arc A 系列 GPU 的 Windows 使用者
import os
os.environ["SYCL_CACHE_PERSISTENT"] = "1"
注意
每個模型首次在 Intel iGPU/Intel Arc A300 系列或 Pro A60 上執行時,可能需要幾分鐘才能編譯。
基本用法
import warnings
from langchain.chains import LLMChain
from langchain_community.llms import IpexLLM
from langchain_core.prompts import PromptTemplate
warnings.filterwarnings("ignore", category=UserWarning, message=".*padding_mask.*")
為您的模型指定提示範本。在此範例中,我們使用 vicuna-1.5 模型。如果您使用不同的模型,請相應地選擇適當的範本。
template = "USER: {question}\nASSISTANT:"
prompt = PromptTemplate(template=template, input_variables=["question"])
使用 IpexLLM 和 IpexLLM.from_model_id
在本地載入模型。它會直接以 Huggingface 格式載入模型,並自動轉換為低位元格式以進行推論。在初始化 IpexLLM 時,在 model_kwargs
中將 device
設定為 "xpu"
,以便將 LLM 模型載入到 Intel GPU。
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={
"temperature": 0,
"max_length": 64,
"trust_remote_code": True,
"device": "xpu",
},
)
在鏈中使用它
llm_chain = prompt | llm
question = "What is AI?"
output = llm_chain.invoke(question)
儲存/載入低位元模型
或者,您可以將低位元模型儲存到磁碟一次,然後使用 from_model_id_low_bit
而不是 from_model_id
重新載入,以供日後使用 - 甚至跨不同的機器。它具有空間效率,因為低位元模型比原始模型需要的磁碟空間少得多。而且 from_model_id_low_bit
在速度和記憶體使用方面也比 from_model_id
更有效率,因為它跳過了模型轉換步驟。您可以類似地在 model_kwargs
中將 device
設定為 "xpu"
,以便將 LLM 模型載入到 Intel GPU。
若要儲存低位元模型,請如下使用 save_low_bit
。
saved_lowbit_model_path = "./vicuna-7b-1.5-low-bit" # path to save low-bit model
llm.model.save_low_bit(saved_lowbit_model_path)
del llm
從已儲存的低位元模型路徑載入模型,如下所示。
請注意,低位元模型的儲存路徑僅包含模型本身,而不包含 tokenizer。如果您希望將所有內容都放在同一個位置,您需要手動從原始模型的目錄下載或複製 tokenizer 檔案到儲存低位元模型的位置。
llm_lowbit = IpexLLM.from_model_id_low_bit(
model_id=saved_lowbit_model_path,
tokenizer_id="lmsys/vicuna-7b-v1.5",
# tokenizer_name=saved_lowbit_model_path, # copy the tokenizers to saved path if you want to use it this way
model_kwargs={
"temperature": 0,
"max_length": 64,
"trust_remote_code": True,
"device": "xpu",
},
)
在鏈中使用載入的模型
llm_chain = prompt | llm_lowbit
question = "What is AI?"
output = llm_chain.invoke(question)
IPEX-LLM 在 Intel CPU 上
此範例說明如何使用 LangChain 與 ipex-llm
互動,以在 Intel CPU 上進行文字生成。
設定
# Update Langchain
%pip install -qU langchain langchain-community
安裝 IEPX-LLM 以在 Intel CPU 上本地執行 LLM
適用於 Windows 使用者:
%pip install --pre --upgrade ipex-llm[all]
適用於 Linux 使用者:
%pip install --pre --upgrade ipex-llm[all] --extra-index-url https://download.pytorch.org/whl/cpu
基本用法
import warnings
from langchain.chains import LLMChain
from langchain_community.llms import IpexLLM
from langchain_core.prompts import PromptTemplate
warnings.filterwarnings("ignore", category=UserWarning, message=".*padding_mask.*")
為您的模型指定提示範本。在此範例中,我們使用 vicuna-1.5 模型。如果您使用不同的模型,請相應地選擇適當的範本。
template = "USER: {question}\nASSISTANT:"
prompt = PromptTemplate(template=template, input_variables=["question"])
使用 IpexLLM 和 IpexLLM.from_model_id
在本地載入模型。它會直接以 Huggingface 格式載入模型,並自動轉換為低位元格式以進行推論。
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 64, "trust_remote_code": True},
)
在鏈中使用它
llm_chain = prompt | llm
question = "What is AI?"
output = llm_chain.invoke(question)
儲存/載入低位元模型
或者,您可以將低位元模型儲存到磁碟一次,然後使用 from_model_id_low_bit
而不是 from_model_id
重新載入,以供日後使用 - 甚至跨不同的機器。它具有空間效率,因為低位元模型比原始模型需要的磁碟空間少得多。而且 from_model_id_low_bit
在速度和記憶體使用方面也比 from_model_id
更有效率,因為它跳過了模型轉換步驟。
若要儲存低位元模型,請如下使用 save_low_bit
saved_lowbit_model_path = "./vicuna-7b-1.5-low-bit" # path to save low-bit model
llm.model.save_low_bit(saved_lowbit_model_path)
del llm
從已儲存的低位元模型路徑載入模型,如下所示。
請注意,低位元模型的儲存路徑僅包含模型本身,而不包含 tokenizer。如果您希望將所有內容都放在同一個位置,您需要手動從原始模型的目錄下載或複製 tokenizer 檔案到儲存低位元模型的位置。
llm_lowbit = IpexLLM.from_model_id_low_bit(
model_id=saved_lowbit_model_path,
tokenizer_id="lmsys/vicuna-7b-v1.5",
# tokenizer_name=saved_lowbit_model_path, # copy the tokenizers to saved path if you want to use it this way
model_kwargs={"temperature": 0, "max_length": 64, "trust_remote_code": True},
)
在鏈中使用載入的模型
llm_chain = prompt | llm_lowbit
question = "What is AI?"
output = llm_chain.invoke(question)