IPEX-LLM
IPEX-LLM is a PyTorch library for running LLM on Intel CPU and GPU (e.g., local PC with iGPU, discrete GPU such as Arc, Flex and Max) with very low latency.
Intel GPU 上的 IPEX-LLM
此示例介绍了如何使用 LangChain 在 Intel GPU 上与 ipex-llm 交互以进行文本生成。
Note
It is recommended that only Windows users with Intel Arc A-Series GPU (except for Intel Arc A300-Series or Pro A60) run Jupyter notebook directly for section "IPEX-LLM on Intel GPU". For other cases (e.g. Linux users, Intel iGPU, etc.), it is recommended to run the code with Python scripts in terminal for best experiences.
安装先决条件
为了在英特尔GPU上受益于IPEX-LLM,需要完成一些工具安装和环境准备的先决步骤。
如果您是Windows用户,请访问在Windows上使用Intel GPU安装IPEX-LLM指南,并按照安装先决条件更新GPU驱动程序(可选)并安装Conda。
如果您是Linux用户,请访问在配备Intel GPU的Linux上安装IPEX-LLM,并按照安装先决条件安装GPU驱动程序、Intel® oneAPI基础工具包2024.0以及Conda。
设置
在安装完先决条件后,你应该已经创建了一个 conda 环境,并且所有先决条件都已安装。在这个 conda 环境中启动 Jupyter 服务:
%pip install -qU langchain langchain-community
在Intel GPU上安装IEPX-LLM以在本地运行大型语言模型。
%pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
Note
You can also use
https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/as the extra-indel-url.
运行时配置
为了获得最佳性能,建议根据您的设备设置几个环境变量:
适用于使用Intel Core Ultra集成显卡的Windows用户
import os
os.environ["SYCL_CACHE_PERSISTENT"] = "1"
os.environ["BIGDL_LLM_XMX_DISABLED"] = "1"
适用于拥有Intel Arc A系列显卡的Windows用户
import os
os.environ["SYCL_CACHE_PERSISTENT"] = "1"
Note
For the first time that each model runs on Intel iGPU/Intel Arc A300-Series or Pro A60, it may take several minutes to compile.
For other GPU type, please refer to here for Windows users, and here for Linux users.
基本用法
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 更高效,因为它跳过了模型转换步骤。您也可以类似地设置 device 到 "xpu" 在 model_kwargs 中,以便将 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
从保存的低比特模型路径加载模型,如下所示。
Note that the saved path for the low-bit model only includes the model itself but not the tokenizers. If you wish to have everything in one place, you will need to manually download or copy the tokenizer files from the original model's directory to the location where the low-bit model is saved.
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)
Intel CPU上的IPEX-LLM
此示例介绍了如何使用 LangChain 在 Intel CPU 上与 ipex-llm 交互以进行文本生成。
设置
# Update Langchain
%pip install -qU langchain langchain-community
在Intel CPU上本地运行LLM,安装IEPX-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
从保存的低比特模型路径加载模型,如下所示。
Note that the saved path for the low-bit model only includes the model itself but not the tokenizers. If you wish to have everything in one place, you will need to manually download or copy the tokenizer files from the original model's directory to the location where the low-bit model is saved.
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)