ipex-llm/python/llm/example/NPU/HF-Transformers-AutoModels/Model/llama2
2024-06-19 18:07:28 +08:00
..
generate.py Add NPU HF example (#11358) 2024-06-19 18:07:28 +08:00
README.md Add NPU HF example (#11358) 2024-06-19 18:07:28 +08:00

Run LLama2 on Intel NPU

In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on Llama2 models on Intel NPUs. For illustration purposes, we utilize the meta-llama/Llama-2-7b-chat-hf as reference Llama2 models.

0. Requirements

To run these examples with IPEX-LLM on Intel NPUs, make sure to install the newest driver version of Intel NPU. Go to https://www.intel.com/content/www/us/en/download/794734/intel-npu-driver-windows.html to download and unzip the driver. Then go to Device Manager, find Neural Processors -> Intel(R) AI Boost. Right click and select Update Driver. And then manually select the folder unzipped from the driver.

Example: Predict Tokens using generate() API

In the example generate.py, we show a basic use case for a Llama2 model to predict the next N tokens using generate() API, with IPEX-LLM INT4 optimizations on Intel NPUs.

1. Install

1.1 Installation on Windows

We suggest using conda to manage environment:

conda create -n llm python=3.10 libuv
conda activate llm

# below command will install intel_extension_for_pytorch==2.1.10+xpu as default
pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/

# below command will install intel_npu_acceleration_library
conda install cmake
git clone https://github.com/intel/intel-npu-acceleration-library npu-library
cd npu-library
git checkout bcb1315
python setup.py bdist_wheel
pip install dist\intel_npu_acceleration_library-1.2.0-cp310-cp310-win_amd64.whl

2. Runtime Configurations

For optimal performance, it is recommended to set several environment variables. Please check out the suggestions based on your device.

2.1 Configurations for Windows

set BIGDL_USE_NPU=1

3. Running examples

python ./generate.py

Arguments info:

  • --repo-id-or-model-path REPO_ID_OR_MODEL_PATH: argument defining the huggingface repo id for the Llama2 model (e.g. meta-llama/Llama-2-7b-chat-hf and meta-llama/Llama-2-13b-chat-hf) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be 'meta-llama/Llama-2-7b-chat-hf'.
  • --prompt PROMPT: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be 'Once upon a time, there existed a little girl who liked to have adventures. She wanted to go to places and meet new people, and have fun'.
  • --n-predict N_PREDICT: argument defining the max number of tokens to predict. It is default to be 32.
  • --load_in_low_bit: argument defining the load_in_low_bit format used. It is default to be sym_int8, sym_int4 can also be used.

Sample Output

meta-llama/Llama-2-7b-chat-hf

Inference time: xxxx s
-------------------- Output --------------------
<s> Once upon a time, there existed a little girl who liked to have adventures. She wanted to go to places and meet new people, and have fun. But her parents were always telling her to stay at home and be careful. They were worried about her safety, and they didn't want her to
--------------------------------------------------------------------------------
done