Skip to content

Commit

Permalink
remove llama2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ATMxsp01 committed Dec 19, 2024
1 parent 897e6fa commit 2fce358
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
17 changes: 3 additions & 14 deletions python/llm/example/GPU/HuggingFace/LLM/llama2/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Llama2
In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on Llama2 models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [meta-llama/Llama-2-7b-chat-hf](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) and [meta-llama/Llama-2-13b-chat-hf](https://huggingface.co/meta-llama/Llama-2-13b-chat-hf) (or [shakechen/Llama-2-7b-chat-hf](https://www.modelscope.cn/models/shakechen/Llama-2-7b-chat-hf) and [ydyajyA/Llama-2-13b-chat-hf](https://www.modelscope.cn/models/ydyajyA/Llama-2-13b-chat-hf) for ModelScope) as reference Llama2 models.
In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on Llama2 models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [meta-llama/Llama-2-7b-chat-hf](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) and [meta-llama/Llama-2-13b-chat-hf](https://huggingface.co/meta-llama/Llama-2-13b-chat-hf) as reference Llama2 models.

## 0. Requirements
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.
Expand All @@ -14,9 +14,6 @@ conda create -n llm python=3.11
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/

# [optional] only needed if you would like to use ModelScope as model hub
pip install modelscope==1.11.0
```

#### 1.2 Installation on Windows
Expand All @@ -27,9 +24,6 @@ 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/

# [optional] only needed if you would like to use ModelScope as model hub
pip install modelscope==1.11.0
```

### 2. Configures OneAPI environment variables for Linux
Expand Down Expand Up @@ -97,19 +91,14 @@ set SYCL_CACHE_PERSISTENT=1
> 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.
### 4. Running examples

```bash
# for Hugging Face model hub
```
python ./generate.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --prompt PROMPT --n-predict N_PREDICT

# for ModelScope model hub
python ./generate.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --prompt PROMPT --n-predict N_PREDICT --modelscope
```

Arguments info:
- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the **Hugging Face** or **ModelScope** 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 checkpoint folder. It is default to be `'meta-llama/Llama-2-7b-chat-hf'` for **Hugging Face** and `'shakechen/Llama-2-7b-chat-hf'` for **ModelScope**.
- `--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 `'What is AI?'`.
- `--n-predict N_PREDICT`: argument defining the max number of tokens to predict. It is default to be `32`.
- `--modelscope`: using **ModelScope** as model hub instead of **Hugging Face**.

#### Sample Output
#### [meta-llama/Llama-2-7b-chat-hf](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf)
Expand Down
27 changes: 7 additions & 20 deletions python/llm/example/GPU/HuggingFace/LLM/llama2/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import argparse

from ipex_llm.transformers import AutoModelForCausalLM
from transformers import LlamaTokenizer

# you could tune the prompt based on your own model,
# here the prompt tuning refers to https://huggingface.co/georgesung/llama2_7b_chat_uncensored#prompt-style
Expand All @@ -40,29 +41,16 @@ def get_prompt(message: str, chat_history: list[tuple[str, str]],

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Predict Tokens using `generate()` API for Llama2 model')
parser.add_argument('--repo-id-or-model-path', type=str,
help='The Hugging Face or ModelScope repo id for the Llama2 (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 checkpoint folder')
parser.add_argument('--repo-id-or-model-path', type=str, default="meta-llama/Llama-2-7b-chat-hf",
help='The huggingface repo id for the Llama2 (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')
parser.add_argument('--prompt', type=str, default="What is AI?",
help='Prompt to infer')
parser.add_argument('--n-predict', type=int, default=32,
help='Max tokens to predict')
parser.add_argument('--modelscope', action="store_true", default=False,
help="Use models from modelscope")

args = parser.parse_args()

if args.modelscope:
from modelscope import AutoTokenizer
Tokenizer = AutoTokenizer
model_hub = 'modelscope'
else:
from transformers import LlamaTokenizer
Tokenizer = LlamaTokenizer
model_hub = 'huggingface'

model_path = args.repo_id_or_model_path if args.repo_id_or_model_path else \
("shakechen/Llama-2-7b-chat-hf" if args.modelscope else "meta-llama/Llama-2-7b-chat-hf")
model_path = args.repo_id_or_model_path

# Load model in 4 bit,
# which convert the relevant layers in the model into INT4 format
Expand All @@ -72,12 +60,11 @@ def get_prompt(message: str, chat_history: list[tuple[str, str]],
load_in_4bit=True,
optimize_model=True,
trust_remote_code=True,
use_cache=True,
model_hub=model_hub)
use_cache=True)
model = model.half().to('xpu')

# Load tokenizer
tokenizer = Tokenizer.from_pretrained(model_path, trust_remote_code=True)
tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True)

# Generate predicted tokens
with torch.inference_mode():
Expand Down

0 comments on commit 2fce358

Please sign in to comment.