From f1641c95545c940d0cb62fbd0eebc6170a38ee7a Mon Sep 17 00:00:00 2001 From: ATMxsp01 Date: Thu, 19 Dec 2024 17:06:21 +0800 Subject: [PATCH 1/3] Add --modelscope option for glm-v4 and MiniCPM-V-2_6 --- .../Multimodal/MiniCPM-V-2_6/README.md | 35 +++++++++++++++---- .../Multimodal/MiniCPM-V-2_6/chat.py | 26 ++++++++++---- .../HuggingFace/Multimodal/glm-4v/README.md | 19 +++++++--- .../HuggingFace/Multimodal/glm-4v/generate.py | 24 +++++++++---- 4 files changed, 81 insertions(+), 23 deletions(-) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/README.md b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/README.md index 0af1ba5c9cf..bbcc0ee7a72 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/README.md +++ b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/README.md @@ -1,5 +1,5 @@ # MiniCPM-V-2_6 -In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on MiniCPM-V-2_6 model on [Intel GPUs](../../../README.md). For illustration purposes, we utilize [openbmb/MiniCPM-V-2_6](https://huggingface.co/openbmb/MiniCPM-V-2_6) as reference MiniCPM-V-2_6 model. +In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on MiniCPM-V-2_6 model on [Intel GPUs](../../../README.md). For illustration purposes, we utilize [openbmb/MiniCPM-V-2_6](https://huggingface.co/openbmb/MiniCPM-V-2_6) (or [OpenBMB/MiniCPM-V-2_6](https://www.modelscope.cn/models/OpenBMB/MiniCPM-V-2_6) for ModelScope) as reference MiniCPM-V-2_6 model. ## 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. @@ -16,6 +16,9 @@ conda activate llm pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ pip install transformers==4.40.0 "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 @@ -28,6 +31,9 @@ conda activate llm pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ pip install transformers==4.40.0 "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 @@ -96,31 +102,48 @@ set SYCL_CACHE_PERSISTENT=1 ### 4. Running examples - chat without streaming mode: - ``` + ```bash + # for Hugging Face model hub python ./chat.py --prompt 'What is in the image?' + + # for ModelScope model hub + python ./chat.py --prompt 'What is in the image?' --modelscope ``` - chat in streaming mode: - ``` + ```bash + # for Hugging Face model hub python ./chat.py --prompt 'What is in the image?' --stream + + # for ModelScope model hub + python ./chat.py --prompt 'What is in the image?' --stream --modelscope ``` - save model with low-bit optimization (if `LOWBIT_MODEL_PATH` does not exist) - ``` + ```bash + # for Hugging Face model hub python ./chat.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --lowbit-path LOWBIT_MODEL_PATH --prompt 'What is in the image?' + + # for ModelScope model hub + python ./chat.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --lowbit-path LOWBIT_MODEL_PATH --prompt 'What is in the image?' --modelscope ``` - chat with saved model with low-bit optimization (if `LOWBIT_MODEL_PATH` exists): - ``` + ```bash + # for Hugging Face model hub python ./chat.py --lowbit-path LOWBIT_MODEL_PATH --prompt 'What is in the image?' + + # for ModelScope model hub + python ./chat.py --lowbit-path LOWBIT_MODEL_PATH --prompt 'What is in the image?' --modelscope ``` > [!TIP] > For chatting in streaming mode, it is recommended to set the environment variable `PYTHONUNBUFFERED=1`. Arguments info: -- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the MiniCPM-V-2_6 (e.g. `openbmb/MiniCPM-V-2_6`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'openbmb/MiniCPM-V-2_6'`. +- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the **Hugging Face** or **ModelScope** repo id for the MiniCPM-V-2_6 (e.g. `openbmb/MiniCPM-V-2_6`) to be downloaded, or the path to the checkpoint folder. It is default to be `'openbmb/MiniCPM-V-2_6'` for **Hugging Face** or `'OpenBMB/MiniCPM-V-2_6'` for **ModelScope**. - `--lowbit-path LOWBIT_MODEL_PATH`: argument defining the path to save/load the model with IPEX-LLM low-bit optimization. If it is an empty string, the original pretrained model specified by `REPO_ID_OR_MODEL_PATH` will be loaded. If it is an existing path, the saved model with low-bit optimization in `LOWBIT_MODEL_PATH` will be loaded. If it is a non-existing path, the original pretrained model specified by `REPO_ID_OR_MODEL_PATH` will be loaded, and the optimized low-bit model will be saved into `LOWBIT_MODEL_PATH`. It is default to be `''`, i.e. an empty string. - `--image-url-or-path IMAGE_URL_OR_PATH`: argument defining the image to be infered. It is default to be `'http://farm6.staticflickr.com/5268/5602445367_3504763978_z.jpg'`. - `--prompt PROMPT`: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be `'What is in the image?'`. - `--stream`: flag to chat in streaming mode +- `--modelscope`: using **ModelScope** as model hub instead of **Hugging Face**. #### Sample Output diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py index cad68239fd5..bcfc4fda40f 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py +++ b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py @@ -22,14 +22,13 @@ import torch from PIL import Image from ipex_llm.transformers import AutoModel -from transformers import AutoTokenizer, AutoProcessor if __name__ == '__main__': parser = argparse.ArgumentParser(description='Predict Tokens using `chat()` API for openbmb/MiniCPM-V-2_6 model') - parser.add_argument('--repo-id-or-model-path', type=str, default="openbmb/MiniCPM-V-2_6", - help='The huggingface repo id for the openbmb/MiniCPM-V-2_6 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 MiniCPM-V-2_6 model to be downloaded' + ', or the path to the checkpoint folder') parser.add_argument("--lowbit-path", type=str, default="", help="The path to the saved model folder with IPEX-LLM low-bit optimization. " @@ -44,9 +43,20 @@ help='Prompt to infer') parser.add_argument('--stream', action='store_true', help='Whether to chat in streaming mode') + 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, AutoProcessor + model_hub = 'modelscope' + else: + from transformers import AutoTokenizer, AutoProcessor + model_hub = 'huggingface' + + model_path = args.repo_id_or_model_path if args.repo_id_or_model_path else \ + ("OpenBMB/MiniCPM-V-2_6" if args.modelscope else "openbmb/MiniCPM-V-2_6") image_path = args.image_url_or_path lowbit_path = args.lowbit_path @@ -61,7 +71,8 @@ optimize_model=True, trust_remote_code=True, use_cache=True, - modules_to_not_convert=["vpm", "resampler"]) + modules_to_not_convert=["vpm", "resampler"], + model_hub=model_hub) tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) @@ -70,7 +81,8 @@ optimize_model=True, trust_remote_code=True, use_cache=True, - modules_to_not_convert=["vpm", "resampler"]) + modules_to_not_convert=["vpm", "resampler"], + model_hub=model_hub) tokenizer = AutoTokenizer.from_pretrained(lowbit_path, trust_remote_code=True) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/README.md b/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/README.md index 7464c7e7751..3720b05983b 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/README.md +++ b/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/README.md @@ -1,5 +1,5 @@ # GLM-4V -In this directory, you will find examples on how you could apply IPEX-LLM FP8 optimizations on GLM-4V models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [THUDM/glm-4v-9b](https://huggingface.co/THUDM/glm-4v-9b) as a reference GLM-4V model. +In this directory, you will find examples on how you could apply IPEX-LLM FP8 optimizations on GLM-4V models on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [THUDM/glm-4v-9b](https://huggingface.co/THUDM/glm-4v-9b) (or [ZhipuAI/glm-4v-9b](https://www.modelscope.cn/models/ZhipuAI/glm-4v-9b) for ModelScope) as a reference GLM-4V model. ## 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. @@ -16,6 +16,9 @@ conda activate llm pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ pip install tiktoken transformers==4.42.4 "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 @@ -28,6 +31,9 @@ conda activate llm pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ pip install tiktoken transformers==4.42.4 "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 @@ -95,15 +101,20 @@ 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 -``` -python ./generate.py --prompt 'What is in the image?' +```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 --image-url-or-path IMAGE_URL_OR_PATH + +# for ModelScope model hub +python ./generate.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --prompt PROMPT --n-predict N_PREDICT --image-url-or-path IMAGE_URL_OR_PATH --modelscope ``` Arguments info: -- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the GLM-4V model (e.g. `THUDM/glm-4v-9b`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'THUDM/glm-4v-9b'`. +- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the **Hugging Face** or **ModelScope** repo id for the GLM-4V model (e.g. `THUDM/glm-4v-9b`) to be downloaded, or the path to the checkpoint folder. It is default to be `'THUDM/glm-4v-9b'` for **Hugging Face** or `'ZhipuAI/glm-4v-9b'` for **ModelScope**. - `--image-url-or-path IMAGE_URL_OR_PATH`: argument defining the image to be infered. It is default to be `'http://farm6.staticflickr.com/5268/5602445367_3504763978_z.jpg'`. - `--prompt PROMPT`: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be `'What is in the image?'`. - `--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-4v-9b](https://huggingface.co/THUDM/glm-4v-9b) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/generate.py b/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/generate.py index 6a1dd035e9e..1ac3cdbf690 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/generate.py +++ b/python/llm/example/GPU/HuggingFace/Multimodal/glm-4v/generate.py @@ -22,13 +22,12 @@ from PIL import Image from ipex_llm.transformers import AutoModelForCausalLM -from transformers import AutoTokenizer if __name__ == '__main__': parser = argparse.ArgumentParser(description='Predict Tokens using `generate()` API for THUDM/glm-4v-9b model') - parser.add_argument('--repo-id-or-model-path', type=str, default="THUDM/glm-4v-9b", - help='The huggingface repo id for the THUDM/glm-4v-9b 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-4v model to be downloaded' + ', or the path to the checkpoint folder') parser.add_argument('--image-url-or-path', type=str, default='http://farm6.staticflickr.com/5268/5602445367_3504763978_z.jpg', help='The URL or path to the image to infer') @@ -36,9 +35,20 @@ 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-4v-9b" if args.modelscope else "THUDM/glm-4v-9b") image_path = args.image_url_or_path # Load model in 4 bit, @@ -49,7 +59,9 @@ load_in_4bit=True, optimize_model=True, trust_remote_code=True, - use_cache=True).half().to('xpu') + use_cache=True, + model_hub=model_hub) + model = model.half().to('xpu') tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) From 7803eda430de780d459fa922f371c542658864ee Mon Sep 17 00:00:00 2001 From: ATMxsp01 Date: Fri, 20 Dec 2024 09:51:38 +0800 Subject: [PATCH 2/3] glm-edge --- .../GPU/HuggingFace/LLM/glm-edge/README.md | 17 ++++++++++--- .../GPU/HuggingFace/LLM/glm-edge/generate.py | 24 ++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/python/llm/example/GPU/HuggingFace/LLM/glm-edge/README.md b/python/llm/example/GPU/HuggingFace/LLM/glm-edge/README.md index a7038c0ef43..85744192261 100644 --- a/python/llm/example/GPU/HuggingFace/LLM/glm-edge/README.md +++ b/python/llm/example/GPU/HuggingFace/LLM/glm-edge/README.md @@ -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. @@ -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 @@ -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 @@ -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) diff --git a/python/llm/example/GPU/HuggingFace/LLM/glm-edge/generate.py b/python/llm/example/GPU/HuggingFace/LLM/glm-edge/generate.py index b02afa18860..8001345bd21 100644 --- a/python/llm/example/GPU/HuggingFace/LLM/glm-edge/generate.py +++ b/python/llm/example/GPU/HuggingFace/LLM/glm-edge/generate.py @@ -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 @@ -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 From 409f8f5e2e5b9e00b885f4097ed5f79a564935f1 Mon Sep 17 00:00:00 2001 From: ATMxsp01 Date: Fri, 20 Dec 2024 11:29:50 +0800 Subject: [PATCH 3/3] minicpm-v-2_6:don't use model_hub=modelscope when use lowbit; internvl2 --- .../Multimodal/MiniCPM-V-2_6/chat.py | 8 ++++---- .../HuggingFace/Multimodal/internvl2/chat.py | 19 +++++++++++++++---- .../Multimodal/internvl2/readme.md | 19 +++++++++++++++---- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py index bcfc4fda40f..68044d1be85 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py +++ b/python/llm/example/GPU/HuggingFace/Multimodal/MiniCPM-V-2_6/chat.py @@ -22,6 +22,7 @@ import torch from PIL import Image from ipex_llm.transformers import AutoModel +from transformers import AutoProcessor if __name__ == '__main__': @@ -49,10 +50,10 @@ args = parser.parse_args() if args.modelscope: - from modelscope import AutoTokenizer, AutoProcessor + from modelscope import AutoTokenizer model_hub = 'modelscope' else: - from transformers import AutoTokenizer, AutoProcessor + from transformers import AutoTokenizer model_hub = 'huggingface' model_path = args.repo_id_or_model_path if args.repo_id_or_model_path else \ @@ -81,8 +82,7 @@ optimize_model=True, trust_remote_code=True, use_cache=True, - modules_to_not_convert=["vpm", "resampler"], - model_hub=model_hub) + modules_to_not_convert=["vpm", "resampler"]) tokenizer = AutoTokenizer.from_pretrained(lowbit_path, trust_remote_code=True) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/chat.py b/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/chat.py index 22517d1bdd8..c2dceebee4a 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/chat.py +++ b/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/chat.py @@ -22,22 +22,32 @@ import torch from PIL import Image from ipex_llm.transformers import AutoModelForCausalLM -from transformers import AutoTokenizer, CLIPImageProcessor +from transformers import CLIPImageProcessor if __name__ == '__main__': parser = argparse.ArgumentParser(description='Predict Tokens using `chat()` API for OpenGVLab/InternVL2-4B model') parser.add_argument('--repo-id-or-model-path', type=str, default="OpenGVLab/InternVL2-4B", - help='The huggingface repo id for the OpenGVLab/InternVL2-4B model to be downloaded' - ', or the path to the huggingface checkpoint folder') + help='The Hugging Face or ModelScope repo id for the InternVL2 model to be downloaded' + ', or the path to the checkpoint folder') parser.add_argument('--image-url-or-path', type=str, default='https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg', help='The URL or path to the image to infer') parser.add_argument('--prompt', type=str, default="What is in the image?", help='Prompt to infer') parser.add_argument('--n-predict', type=int, default=64, 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 + model_hub = 'modelscope' + else: + from transformers import AutoTokenizer + model_hub = 'huggingface' + model_path = args.repo_id_or_model_path image_path = args.image_url_or_path n_predict = args.n_predict @@ -48,7 +58,8 @@ # This will allow the memory-intensive embedding layer to utilize the CPU instead of iGPU. model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, load_in_low_bit="sym_int4", - modules_to_not_convert=["vision_model"]) + modules_to_not_convert=["vision_model"], + model_hub=model_hub) model = model.half().to('xpu') tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) diff --git a/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/readme.md b/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/readme.md index ad5bf92207a..1b282592337 100644 --- a/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/readme.md +++ b/python/llm/example/GPU/HuggingFace/Multimodal/internvl2/readme.md @@ -1,5 +1,5 @@ # InternVL2 -In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on InternVL2 model on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [OpenGVLab/InternVL2-4B](https://huggingface.co/OpenGVLab/InternVL2-4B) as a reference InternVL2 model. +In this directory, you will find examples on how you could apply IPEX-LLM INT4 optimizations on InternVL2 model on [Intel GPUs](../../../README.md). For illustration purposes, we utilize the [OpenGVLab/InternVL2-4B](https://huggingface.co/OpenGVLab/InternVL2-4B) (or [OpenGVLab/InternVL2-4B](https://www.modelscope.cn/models/OpenGVLab/InternVL2-4B) for ModelScope) as a reference InternVL2 model. ## 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. @@ -17,6 +17,9 @@ pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-exte pip install einops timm +# [optional] only needed if you would like to use ModelScope as model hub +pip install modelscope==1.11.0 + ``` #### 1.2 Installation on Windows @@ -30,6 +33,9 @@ pip install --pre --upgrade ipex-llm[xpu] --extra-index-url https://pytorch-exte pip install einops timm +# [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 @@ -98,15 +104,20 @@ set SYCL_CACHE_PERSISTENT=1 ### 4. Running examples - chat with specified prompt: - ``` - python ./chat.py --prompt 'What is in the image?' + ```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 --image-url-or-path IMAGE_URL_OR_PATH + + # for ModelScope model hub + python ./generate.py --repo-id-or-model-path REPO_ID_OR_MODEL_PATH --prompt PROMPT --n-predict N_PREDICT --image-url-or-path IMAGE_URL_OR_PATH --modelscope ``` Arguments info: -- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the huggingface repo id for the InternVL2 (e.g. `OpenGVLab/InternVL2-4B`) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be `'OpenGVLab/InternVL2-4B'`. +- `--repo-id-or-model-path REPO_ID_OR_MODEL_PATH`: argument defining the **Hugging Face** or **ModelScope** repo id for the InternVL2 (e.g. `OpenGVLab/InternVL2-4B`) to be downloaded, or the path to the checkpoint folder. It is default to be `'OpenGVLab/InternVL2-4B'`. - `--image-url-or-path IMAGE_URL_OR_PATH`: argument defining the image to be infered. It is default to be `'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'`. - `--prompt PROMPT`: argument defining the prompt to be infered (with integrated prompt format for chat). It is default to be `'What is in the image?'`. - `--n-predict N_PREDICT`: argument defining the max number of tokens to predict. It is default to be `64`. +- `--modelscope`: using **ModelScope** as model hub instead of **Hugging Face**. #### Sample Output