Skip to content

Commit

Permalink
minicpm-v-2_6:don't use model_hub=modelscope when use lowbit; internvl2
Browse files Browse the repository at this point in the history
  • Loading branch information
ATMxsp01 committed Dec 20, 2024
1 parent 7803eda commit 409f8f5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import torch
from PIL import Image
from ipex_llm.transformers import AutoModel
from transformers import AutoProcessor


if __name__ == '__main__':
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 15 additions & 4 deletions python/llm/example/GPU/HuggingFace/Multimodal/internvl2/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions python/llm/example/GPU/HuggingFace/Multimodal/internvl2/readme.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 409f8f5

Please sign in to comment.