-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Community] Support
llama-index-embeddings-ipex-llm
for Intel GPUs (#…
- Loading branch information
1 parent
5cb907d
commit c80e56a
Showing
7 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Local Embeddings with IPEX-LLM on Intel GPU\n", | ||
"\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 conduct embedding tasks with `ipex-llm` optimizations on Intel GPU. This would be helpful in applications such as RAG, document QA, etc.\n", | ||
"\n", | ||
"> **Note**\n", | ||
">\n", | ||
"> You could refer to [here](https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/embeddings/llama-index-embeddings-ipex-llm/examples) for full examples of `IpexLLMEmbedding`. 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 install Visual Studio 2022, GPU driver, Conda, and Intel® oneAPI Base Toolkit 2024.0.\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-embeddings-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-embeddings-ipex-llm` as follows:\n", | ||
"\n", | ||
"```bash\n", | ||
"conda activate <your-conda-env-name>\n", | ||
"\n", | ||
"pip install llama-index-embeddings-ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/\n", | ||
"```\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", | ||
"# 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", | ||
"## `IpexLLMEmbedding`\n", | ||
"\n", | ||
"Setting `device=\"xpu\"` when initializing `IpexLLMEmbedding` will put the embedding model on Intel GPU and benefit from IPEX-LLM optimizations:\n", | ||
"\n", | ||
"```python\n", | ||
"from llama_index.embeddings.ipex_llm import IpexLLMEmbedding\n", | ||
"\n", | ||
"embedding_model = IpexLLMEmbedding(\n", | ||
" model_name=\"BAAI/bge-large-en-v1.5\", device=\"xpu\"\n", | ||
")\n", | ||
"```\n", | ||
"\n", | ||
"> Please note that `IpexLLMEmbedding` currently only provides optimization for Hugging Face Bge models.\n", | ||
"\n", | ||
"You could then conduct the embedding tasks as normal:\n", | ||
"\n", | ||
"```python\n", | ||
"sentence = \"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", | ||
"query = \"What is IPEX-LLM?\"\n", | ||
"\n", | ||
"text_embedding = embedding_model.get_text_embedding(sentence)\n", | ||
"print(f\"embedding[:10]: {text_embedding[:10]}\")\n", | ||
"\n", | ||
"text_embeddings = embedding_model.get_text_embedding_batch([sentence, query])\n", | ||
"print(f\"text_embeddings[0][:10]: {text_embeddings[0][:10]}\")\n", | ||
"print(f\"text_embeddings[1][:10]: {text_embeddings[1][:10]}\")\n", | ||
"\n", | ||
"query_embedding = embedding_model.get_query_embedding(query)\n", | ||
"print(f\"query_embedding[:10]: {query_embedding[:10]}\")\n", | ||
"```" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
1 change: 1 addition & 0 deletions
1
llama-index-integrations/embeddings/llama-index-embeddings-ipex-llm/examples/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python_sources() |
23 changes: 23 additions & 0 deletions
23
...ndex-integrations/embeddings/llama-index-embeddings-ipex-llm/examples/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# IpexLLMEmbedding Examples | ||
|
||
This folder contains examples showcasing how to use LlamaIndex with `ipex-llm` Embeddings integration `llama_index.embeddings.ipex_llm.IpexLLMEmbedding` on Intel CPU and GPU. | ||
|
||
## Installation | ||
|
||
### On Intel CPU | ||
|
||
Please refer to [here](https://docs.llamaindex.ai/en/stable/examples/embeddings/ipex_llm/#install-llama-index-embeddings-ipex-llm) for installation details. | ||
|
||
### On Intel GPU | ||
|
||
Please refer to [here](https://docs.llamaindex.ai/en/stable/examples/embeddings/ipex_llm_gpu/) for install prerequisites, `llama-index-embeddings-ipex-llm` installation, and runtime configuration. | ||
|
||
## List of Examples | ||
|
||
### Basic Usage Example | ||
|
||
The example [basic.py](./basic.py) shows how to run `IpexLLMEmbedding` on Intel CPU or GPU and conduct embedding tasks such as text and query embedding. Run the example as following: | ||
|
||
```bash | ||
python basic.py -m <path_to_model> -d <cpu_or_xpu> -t <text_to_embed> -q <query_to_embed> | ||
``` |
54 changes: 54 additions & 0 deletions
54
llama-index-integrations/embeddings/llama-index-embeddings-ipex-llm/examples/basic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import argparse | ||
from llama_index.embeddings.ipex_llm import IpexLLMEmbedding | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="IpexLLMEmbedding Basic Usage Example") | ||
parser.add_argument( | ||
"--model-name", | ||
"-m", | ||
type=str, | ||
default="BAAI/bge-large-en-v1.5", | ||
help="The huggingface repo id for the embedding model to be downloaded" | ||
", or the path to the huggingface checkpoint folder", | ||
) | ||
parser.add_argument( | ||
"--device", | ||
"-d", | ||
type=str, | ||
default="cpu", | ||
choices=["cpu", "xpu"], | ||
help="The device (Intel CPU or Intel GPU) the embedding model runs on", | ||
) | ||
parser.add_argument( | ||
"--text", | ||
"-t", | ||
type=str, | ||
default="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.", | ||
help="The sentence you prefer for text embedding", | ||
) | ||
parser.add_argument( | ||
"--query", | ||
"-q", | ||
type=str, | ||
default="What is IPEX-LLM?", | ||
help="The sentence you prefer for query embedding", | ||
) | ||
|
||
args = parser.parse_args() | ||
model_name = args.model_name | ||
device = args.device | ||
text = args.text | ||
query = args.query | ||
|
||
# load the embedding model on Intel GPU with IPEX-LLM optimizations | ||
embedding_model = IpexLLMEmbedding(model_name=model_name, device=device) | ||
|
||
text_embedding = embedding_model.get_text_embedding(text) | ||
print(f"embedding[:10]: {text_embedding[:10]}") | ||
|
||
text_embeddings = embedding_model.get_text_embedding_batch([text, query]) | ||
print(f"text_embeddings[0][:10]: {text_embeddings[0][:10]}") | ||
print(f"text_embeddings[1][:10]: {text_embeddings[1][:10]}") | ||
|
||
query_embedding = embedding_model.get_query_embedding(query) | ||
print(f"query_embedding[:10]: {query_embedding[:10]}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters