跳到主要内容
Open In ColabOpen on GitHub

IPEX-LLM

IPEX-LLM 是一个 PyTorch 库,用于在英特尔 CPU 和 GPU(例如,带有集成显卡的本地电脑,以及 Arc、Flex 和 Max 等独立显卡)上以极低延迟运行 LLM。

IPEX-LLM on Intel GPU

本示例介绍了如何使用 LangChain 与 ipex-llm 交互,以便在英特尔 GPU 上进行文本生成。

注意

建议只有拥有英特尔 Arc A 系列 GPU(英特尔 Arc A300 系列或 Pro A60 除外)的 Windows 用户直接运行 Jupyter Notebook 以查看“IPEX-LLM on Intel GPU”部分。对于其他情况(例如 Linux 用户、英特尔 iGPU 等),建议通过终端使用 Python 脚本运行代码以获得最佳体验。

安装先决条件

为了利用英特尔 GPU 上的 IPEX-LLM,工具安装和环境准备有几个先决步骤。

如果您是 Windows 用户,请访问在英特尔 GPU 上的 Windows 系统上安装 IPEX-LLM 指南,并按照安装先决条件更新 GPU 驱动(可选)并安装 Conda。

如果您是 Linux 用户,请访问在英特尔 GPU 上的 Linux 系统上安装 IPEX-LLM,并按照安装先决条件安装 GPU 驱动、Intel® oneAPI Base Toolkit 2024.0 和 Conda。

设置

安装完所有先决条件后,您应该已经创建了一个包含所有先决条件的 conda 环境。请在此 conda 环境中启动 jupyter 服务

%pip install -qU langchain langchain-community

安装 IEPX-LLM 以在英特尔 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。

运行时配置

为了获得最佳性能,建议根据您的设备设置几个环境变量

适用于配备英特尔酷睿 Ultra 集成显卡的 Windows 用户

import os

os.environ["SYCL_CACHE_PERSISTENT"] = "1"
os.environ["BIGDL_LLM_XMX_DISABLED"] = "1"

适用于配备英特尔 Arc A 系列 GPU 的 Windows 用户

import os

os.environ["SYCL_CACHE_PERSISTENT"] = "1"

注意

对于每个模型首次在英特尔 iGPU/英特尔 Arc A300 系列或 Pro A60 上运行,可能需要几分钟进行编译。

对于其他 GPU 类型,请参考 Windows 用户此处,以及 Linux 用户此处

基本用法

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.from_model_id 通过 IpexLLM 本地加载模型。它将直接以 Huggingface 格式加载模型,并自动将其转换为低位格式以进行推理。在初始化 IpexLLM 时,将 model_kwargs 中的 device 设置为 "xpu",以便将 LLM 模型加载到英特尔 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",
},
)

在 Chain 中使用

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 模型加载到英特尔 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

从已保存的低位模型路径加载模型,如下所示。

请注意,低位模型的保存路径仅包含模型本身,不包含分词器。如果您希望将所有内容放在一个位置,则需要手动从原始模型的目录下载或复制分词器文件到低位模型保存的位置。

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",
},
)

在 Chain 中使用加载的模型

llm_chain = prompt | llm_lowbit


question = "What is AI?"
output = llm_chain.invoke(question)

IPEX-LLM on Intel CPU

本示例介绍了如何使用 LangChain 与 ipex-llm 交互,以便在英特尔 CPU 上进行文本生成。

设置

# Update Langchain

%pip install -qU langchain langchain-community

安装 IEPX-LLM 以在英特尔 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.from_model_id 通过 IpexLLM 本地加载模型。它将直接以 Huggingface 格式加载模型,并自动将其转换为低位格式以进行推理。

llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 64, "trust_remote_code": True},
)

在 Chain 中使用

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

从已保存的低位模型路径加载模型,如下所示。

请注意,低位模型的保存路径仅包含模型本身,不包含分词器。如果您希望将所有内容放在一个位置,则需要手动从原始模型的目录下载或复制分词器文件到低位模型保存的位置。

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},
)

在 Chain 中使用加载的模型

llm_chain = prompt | llm_lowbit


question = "What is AI?"
output = llm_chain.invoke(question)