Skip to content

Commit

Permalink
glm-edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ATMxsp01 committed Dec 20, 2024
1 parent f1641c9 commit 7803eda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 14 additions & 3 deletions python/llm/example/GPU/HuggingFace/LLM/glm-edge/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# GLM-Edge
In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on GLM-Edge models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [THUDM/glm-edge-1.5b-chat](https://huggingface.co/THUDM/glm-edge-1.5b-chat) and [THUDM/glm-edge-4b-chat](https://huggingface.co/THUDM/glm-edge-4b-chat) as reference GLM-Edge models.
In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on GLM-Edge models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [THUDM/glm-edge-1.5b-chat](https://huggingface.co/THUDM/glm-edge-1.5b-chat) and [THUDM/glm-edge-4b-chat](https://huggingface.co/THUDM/glm-edge-4b-chat) (or [ZhipuAI/glm-edge-1.5b-chat](https://www.modelscope.cn/models/ZhipuAI/glm-edge-1.5b-chat) and [ZhipuAI/glm-edge-4b-chat](https://www.modelscope.cn/models/ZhipuAI/glm-edge-4b-chat) for ModelScope) as reference GLM-Edge 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 @@ -17,6 +17,9 @@ pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-exte
pip install transformers==4.47.0
pip install accelerate==0.33.0
pip install "trl<0.12.0"

# [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 @@ -32,6 +35,9 @@ pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-exte
pip install transformers==4.47.0
pip install accelerate==0.33.0
pip install "trl<0.12.0"

# [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 @@ -102,14 +108,19 @@ set SYCL_CACHE_PERSISTENT=1
### Example 1: Predict Tokens using `generate()` API
In the example [generate.py](./generate.py), we show a basic use case for a GLM-Edge model to predict the next N tokens using `generate()` API, with IPEX-LLM INT4 optimizations on Intel GPUs.

```
```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 huggingface repo id for the GLM-Edge model (e.g. `THUDM/glm-edge-1.5b-chat` or `THUDM/glm-edge-4b-chat`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'THUDM/glm-edge-4b-chat'`.
- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the GLM-Edge model (e.g. `THUDM/glm-edge-1.5b-chat` or `THUDM/glm-edge-4b-chat`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'THUDM/glm-edge-4b-chat'` for **Hugging Face** or `'ZhipuAI/glm-edge-4b-chat'` for **ModelScope**.
- `--prompt PROMPT`: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be `'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
#### [THUDM/glm-edge-1.5b-chat](https://huggingface.co/THUDM/glm-edge-1.5b-chat)
Expand Down
24 changes: 18 additions & 6 deletions python/llm/example/GPU/HuggingFace/LLM/glm-edge/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,32 @@
import argparse

from ipex_llm.transformers import AutoModelForCausalLM
from transformers import AutoTokenizer


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Predict Tokens using `generate()` API for GLM-Edge model')
parser.add_argument('--repo-id-or-model-path', type=str, default="THUDM/glm-edge-4b-chat",
help='The huggingface repo id for the GLM-Edge model to be downloaded'
', or the path to the huggingface checkpoint folder')
parser.add_argument('--repo-id-or-model-path', type=str,
help='The Hugging Face or ModelScope repo id for the GLM-Edge model to be downloaded'
', or the path to the checkpoint folder')
parser.add_argument('--prompt', type=str, default="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()
model_path = args.repo_id_or_model_path

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

model_path = args.repo_id_or_model_path if args.repo_id_or_model_path else \
("ZhipuAI/glm-edge-4b-chat" if args.modelscope else "THUDM/glm-edge-4b-chat")

# Load model in 4 bit,
# which convert the relevant layers in the model into INT4 format
Expand All @@ -43,7 +54,8 @@
load_in_4bit=True,
optimize_model=True,
trust_remote_code=True,
use_cache=True)
use_cache=True,
model_hub=model_hub)
model = model.half().to("xpu")

# Load tokenizer
Expand Down

0 comments on commit 7803eda

Please sign in to comment.