|
| 1 | +# Llama3.1 |
| 2 | +In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on Llama3.1 models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [meta-llama/Meta-Llama-3.1-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) as a reference Llama3.1 models. |
| 3 | + |
| 4 | +## 0. Requirements |
| 5 | +To run these examples with IPEX-LLM on Intel GPUs, we have some recommended requirements for your machine, please refer to [here](../../../README.md#requirements) for more information. |
| 6 | + |
| 7 | +## Example: Predict Tokens using `generate()` API |
| 8 | +In the example [generate.py](./generate.py), we show a basic use case for a Llama3.1 model to predict the next N tokens using `generate()` API, with IPEX-LLM INT4 optimizations on Intel GPUs. |
| 9 | +### 1. Install |
| 10 | +#### 1.1 Installation on Linux |
| 11 | +We suggest using conda to manage environment: |
| 12 | +```bash |
| 13 | +conda create -n llm python=3.11 |
| 14 | +conda activate llm |
| 15 | +# below command will install intel_extension_for_pytorch==2.1.10+xpu as default |
| 16 | +pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ |
| 17 | + |
| 18 | +# transformers>=4.43.1 is required for Llama3.1 with IPEX-LLM optimizations |
| 19 | +pip install transformers==4.43.1 |
| 20 | +pip install trl |
| 21 | +``` |
| 22 | + |
| 23 | +#### 1.2 Installation on Windows |
| 24 | +We suggest using conda to manage environment: |
| 25 | +```bash |
| 26 | +conda create -n llm python=3.11 libuv |
| 27 | +conda activate llm |
| 28 | + |
| 29 | +# below command will install intel_extension_for_pytorch==2.1.10+xpu as default |
| 30 | +pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ |
| 31 | + |
| 32 | +# transformers>=4.43.1 is required for Llama3.1 with IPEX-LLM optimizations |
| 33 | +pip install transformers==4.43.1 |
| 34 | +pip install trl |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Configures OneAPI environment variables for Linux |
| 38 | + |
| 39 | +> [!NOTE] |
| 40 | +> Skip this step if you are running on Windows. |
| 41 | +
|
| 42 | +This is a required step on Linux for APT or offline installed oneAPI. Skip this step for PIP-installed oneAPI. |
| 43 | + |
| 44 | +```bash |
| 45 | +source /opt/intel/oneapi/setvars.sh |
| 46 | +``` |
| 47 | + |
| 48 | +### 3. Runtime Configurations |
| 49 | +For optimal performance, it is recommended to set several environment variables. Please check out the suggestions based on your device. |
| 50 | +#### 3.1 Configurations for Linux |
| 51 | +<details> |
| 52 | + |
| 53 | +<summary>For Intel Arc™ A-Series Graphics and Intel Data Center GPU Flex Series</summary> |
| 54 | + |
| 55 | +```bash |
| 56 | +export USE_XETLA=OFF |
| 57 | +export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1 |
| 58 | +export SYCL_CACHE_PERSISTENT=1 |
| 59 | +``` |
| 60 | + |
| 61 | +</details> |
| 62 | + |
| 63 | +<details> |
| 64 | + |
| 65 | +<summary>For Intel Data Center GPU Max Series</summary> |
| 66 | + |
| 67 | +```bash |
| 68 | +export LD_PRELOAD=${LD_PRELOAD}:${CONDA_PREFIX}/lib/libtcmalloc.so |
| 69 | +export SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=1 |
| 70 | +export SYCL_CACHE_PERSISTENT=1 |
| 71 | +export ENABLE_SDP_FUSION=1 |
| 72 | +``` |
| 73 | +> Note: Please note that `libtcmalloc.so` can be installed by `conda install -c conda-forge -y gperftools=2.10`. |
| 74 | +</details> |
| 75 | +
|
| 76 | +<details> |
| 77 | + |
| 78 | +<summary>For Intel iGPU</summary> |
| 79 | + |
| 80 | +```bash |
| 81 | +export SYCL_CACHE_PERSISTENT=1 |
| 82 | +export BIGDL_LLM_XMX_DISABLED=1 |
| 83 | +``` |
| 84 | + |
| 85 | +</details> |
| 86 | + |
| 87 | +#### 3.2 Configurations for Windows |
| 88 | +<details> |
| 89 | + |
| 90 | +<summary>For Intel iGPU</summary> |
| 91 | + |
| 92 | +```cmd |
| 93 | +set SYCL_CACHE_PERSISTENT=1 |
| 94 | +set BIGDL_LLM_XMX_DISABLED=1 |
| 95 | +``` |
| 96 | + |
| 97 | +</details> |
| 98 | + |
| 99 | +<details> |
| 100 | + |
| 101 | +<summary>For Intel Arc™ A-Series Graphics</summary> |
| 102 | + |
| 103 | +```cmd |
| 104 | +set SYCL_CACHE_PERSISTENT=1 |
| 105 | +``` |
| 106 | + |
| 107 | +</details> |
| 108 | + |
| 109 | +> [!NOTE] |
| 110 | +> 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. |
| 111 | +### 4. Running examples |
| 112 | + |
| 113 | +``` |
| 114 | +python ./generate.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --prompt PROMPT --n-predict N_PREDICT |
| 115 | +``` |
| 116 | + |
| 117 | +Arguments info: |
| 118 | +- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the Llama3.1 model (e.g. `meta-llama/Meta-Llama-3.1-8B-Instruct`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'meta-llama/Meta-Llama-3.1-8B-Instruct'`. |
| 119 | +- `--prompt PROMPT`: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be `'What is AI?'`. |
| 120 | +- `--n-predict N_PREDICT`: argument defining the max number of tokens to predict. It is default to be `32`. |
| 121 | + |
| 122 | +#### Sample Output |
| 123 | +#### [meta-llama/Meta-Llama-3.1-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) |
| 124 | +```log |
| 125 | +Inference time: xxxx s |
| 126 | +-------------------- Prompt -------------------- |
| 127 | +<|begin_of_text|><|start_header_id|>user<|end_header_id|> |
| 128 | +
|
| 129 | +What is AI?<|eot_id|><|start_header_id|>assistant<|end_header_id|> |
| 130 | +
|
| 131 | +
|
| 132 | +-------------------- Output (skip_special_tokens=False) -------------------- |
| 133 | +<|begin_of_text|><|begin_of_text|><|start_header_id|>user<|end_header_id|> |
| 134 | +
|
| 135 | +What is AI?<|eot_id|><|start_header_id|>assistant<|end_header_id|> |
| 136 | +
|
| 137 | +AI, or Artificial Intelligence, refers to the development of computer systems that can perform tasks that typically require human intelligence, such as: |
| 138 | +
|
| 139 | +1. **Learning**: AI |
| 140 | +``` |
0 commit comments