Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-lv11 committed May 23, 2024
1 parent 40ff83c commit 0955829
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 538 deletions.
297 changes: 97 additions & 200 deletions docs/docs/examples/llm/ipex_llm_gpu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,75 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# IPEX-LLM \n",
"\n",
"# IPEX-LLM\n",
"> [IPEX-LLM](https://github.com/intel-analytics/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.\n",
"\n",
"This example goes over how to use LlamaIndex to interact with [`ipex-llm`](https://github.com/intel-analytics/ipex-llm/) for text generation and chat on GPU. \n",
"This example goes over how to use LlamaIndex to interact with [`ipex-llm`](https://github.com/intel-analytics/ipex-llm/) for text generation and chat on intel GPU. \n",
"\n",
"For more examples and usage, refer to [Examples](https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/llms/llama-index-llms-ipex-llm/examples)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install `llama-index-llms-ipex-llm`. This will also install `ipex-llm` and its dependencies."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> **Note**\n",
">\n",
"> You could refer to [here](https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/llms/llama-index-llms-ipex-llm/examples) for full examples of `IpexLLM`. Please note that for running on Intel GPU, please specify `-d 'xpu'` in command argument when running the examples.\n",
"\n",
"## Install Prerequisites\n",
"To benefit from IPEX-LLM on Intel GPUs, there are several prerequisite steps for tools installation and environment preparation.\n",
"\n",
"If you are a Windows user, visit the [Install IPEX-LLM on Windows with Intel GPU Guide](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Quickstart/install_windows_gpu.html), and follow [**Install Prerequisites**](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Quickstart/install_windows_gpu.html#install-prerequisites) to update GPU driver (optional) and install Conda.\n",
"\n",
"If you are a Linux user, visit the [Install IPEX-LLM on Linux with Intel GPU](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Quickstart/install_linux_gpu.html), and follow [**Install Prerequisites**](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Quickstart/install_linux_gpu.html#install-prerequisites) to install GPU driver, Intel® oneAPI Base Toolkit 2024.0, and Conda.\n",
"\n",
"## Install `llama-index-llms-ipex-llm`\n",
"\n",
"After the prerequisites installation, you should have created a conda environment with all prerequisites installed, activate your conda environment and install `llama-index-llms-ipex-llm` as follows:\n",
"\n",
"```bash\n",
"conda activate <your-conda-env-name>\n",
"\n",
"pip install llama-index-llms-ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example we'll use [HuggingFaceH4/zephyr-7b-alpha](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha) model for demostration. It requires updating `transformers` and `tokenizers` packages."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"This step will also install `ipex-llm` and its dependencies.\n",
"\n",
"> **Note**\n",
">\n",
"> You can also use `https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/` as the `extra-indel-url`.\n",
"\n",
"\n",
"## Runtime Configuration\n",
"\n",
"For optimal performance, it is recommended to set several environment variables based on your device:\n",
"\n",
"### For Windows Users with Intel Core Ultra integrated GPU\n",
"\n",
"In Anaconda Prompt:\n",
"\n",
"```\n",
"set SYCL_CACHE_PERSISTENT=1\n",
"set BIGDL_LLM_XMX_DISABLED=1\n",
"```\n",
"\n",
"### For Linux Users with Intel Arc A-Series GPU\n",
"\n",
"```bash\n",
"pip install -U transformers==4.37.0 tokenizers==0.15.2\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before loading the Zephyr model, you'll need to define `completion_to_prompt` and `messages_to_prompt` for formatting prompts. This is essential for preparing inputs that the model can interpret accurately."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Configure oneAPI environment variables. Required step for APT or offline installed oneAPI.\n",
"# Skip this step for PIP-installed oneAPI since the environment has already been configured in LD_LIBRARY_PATH.\n",
"source /opt/intel/oneapi/setvars.sh\n",
"\n",
"# Recommended Environment Variables for optimal performance\n",
"export USE_XETLA=OFF\n",
"export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1\n",
"export SYCL_CACHE_PERSISTENT=1\n",
"```\n",
"\n",
"> **Note**\n",
">\n",
"> 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.\n",
">\n",
"> For other GPU type, please refer to [here](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Overview/install_gpu.html#runtime-configuration) for Windows users, and [here](https://ipex-llm.readthedocs.io/en/latest/doc/LLM/Overview/install_gpu.html#id5) for Linux users.\n",
"\n",
"## `IpexLLM`\n",
"\n",
"Setting `device_map=\"xpu\"` when initializing `IpexLLM` will put the embedding model on Intel GPU and benefit from IPEX-LLM optimizations:\n",
"\n",
"```python\n",
"# Transform a string into input zephyr-specific input\n",
"def completion_to_prompt(completion):\n",
" return f\"<|system|>\\n</s>\\n<|user|>\\n{completion}</s>\\n<|assistant|>\\n\"\n",
Expand All @@ -82,29 +96,7 @@
" # add final assistant prompt\n",
" prompt = prompt + \"<|assistant|>\\n\"\n",
"\n",
" return prompt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Usage\n",
"\n",
"Load the Zephyr model locally using IpexLLM using `IpexLLM.from_model_id`. It will load the model directly in its Huggingface format and convert it automatically to low-bit format for inference. Use `device_map` to load the model to xpu. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"\n",
"warnings.filterwarnings(\n",
" \"ignore\", category=UserWarning, message=\".*padding_mask.*\"\n",
")\n",
" return prompt\n",
"\n",
"from llama_index.llms.ipex_llm import IpexLLM\n",
"\n",
Expand All @@ -117,133 +109,51 @@
" completion_to_prompt=completion_to_prompt,\n",
" messages_to_prompt=messages_to_prompt,\n",
" device_map=\"xpu\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you can proceed to use the loaded model for text completion and interactive chat. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Text Completion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
")\n",
"```\n",
"\n",
"> Please note that in this example we'll use [HuggingFaceH4/zephyr-7b-alpha](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha) model for demostration. It requires updating `transformers` and `tokenizers` packages.\n",
"> ```bash\n",
"> pip install -U transformers==4.37.0 tokenizers==0.15.2\n",
"> ```\n",
"\n",
"You could then conduct the completion task or chat task as normal:\n",
"\n",
"```python\n",
"print(\"----------------- Complete ------------------\")\n",
"completion_response = llm.complete(\"Once upon a time, \")\n",
"print(completion_response.text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streaming Text Completion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(completion_response.text)\n",
"print(\"----------------- Stream Complete ------------------\")\n",
"response_iter = llm.stream_complete(\"Once upon a time, there's a little girl\")\n",
"for response in response_iter:\n",
" print(response.delta, end=\"\", flush=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Chat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" print(response.delta, end=\"\", flush=True)\n",
"print(\"----------------- Chat ------------------\")\n",
"from llama_index.core.llms import ChatMessage\n",
"\n",
"message = ChatMessage(role=\"user\", content=\"Explain Big Bang Theory briefly\")\n",
"resp = llm.chat([message])\n",
"print(resp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Streaming Chat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(resp)\n",
"print(\"----------------- Stream Chat ------------------\")\n",
"message = ChatMessage(role=\"user\", content=\"What is AI?\")\n",
"resp = llm.stream_chat([message], max_tokens=256)\n",
"for r in resp:\n",
" print(r.delta, end=\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Save/Load Low-bit Model\n",
"Alternatively, you might save the low-bit model to disk once and use `from_model_id_low_bit` instead of `from_model_id` to reload it for later use - even across different machines. It is space-efficient, as the low-bit model demands significantly less disk space than the original model. And `from_model_id_low_bit` is also more efficient than `from_model_id` in terms of speed and memory usage, as it skips the model conversion step."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To save the low-bit model, use `save_low_bit` as follows."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" print(r.delta, end=\"\")\n",
"```\n",
"\n",
"Alternatively, you might save the low-bit model to disk once and use `from_model_id_low_bit` instead of `from_model_id` to reload it for later use - even across different machines. It is space-efficient, as the low-bit model demands significantly less disk space than the original model. And `from_model_id_low_bit` is also more efficient than `from_model_id` in terms of speed and memory usage, as it skips the model conversion step. \n",
"\n",
"To save the low-bit model, use `save_low_bit` as follows. Then load the model from saved lowbit model path as follows. Also use `device_map` to load the model to xpu. \n",
"> 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.\n",
"\n",
"Try stream completion using the loaded low-bit model. \n",
"```python\n",
"saved_lowbit_model_path = (\n",
" \"./zephyr-7b-alpha-low-bit\" # path to save low-bit model\n",
")\n",
"\n",
"llm._model.save_low_bit(saved_lowbit_model_path)\n",
"del llm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the model from saved lowbit model path as follows. Also use `device_map` to load the model to xpu. \n",
"> 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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"del llm\n",
"\n",
"llm_lowbit = IpexLLM.from_model_id_low_bit(\n",
" model_name=saved_lowbit_model_path,\n",
" tokenizer_name=\"HuggingFaceH4/zephyr-7b-alpha\",\n",
Expand All @@ -253,25 +163,12 @@
" completion_to_prompt=completion_to_prompt,\n",
" generate_kwargs={\"do_sample\": False},\n",
" device_map=\"xpu\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try stream completion using the loaded low-bit model. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
")\n",
"\n",
"response_iter = llm_lowbit.stream_complete(\"What is Large Language Model?\")\n",
"for response in response_iter:\n",
" print(response.delta, end=\"\", flush=True)"
" print(response.delta, end=\"\", flush=True)\n",
"```"
]
}
],
Expand Down
Loading

0 comments on commit 0955829

Please sign in to comment.