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