ipex-llm/python/llm/example/GPU/PyTorch-Models/Model/qwen-vl/chat.py
Jin Qiao 440cfe18ed LLM: GPU Example Updates for Windows (#9992)
* modify aquila

* modify aquila2

* add baichuan

* modify baichuan2

* modify blue-lm

* modify chatglm3

* modify chinese-llama2

* modiy codellama

* modify distil-whisper

* modify dolly-v1

* modify dolly-v2

* modify falcon

* modify flan-t5

* modify gpt-j

* modify internlm

* modify llama2

* modify mistral

* modify mixtral

* modify mpt

* modify phi-1_5

* modify qwen

* modify qwen-vl

* modify replit

* modify solar

* modify starcoder

* modify vicuna

* modify voiceassistant

* modify whisper

* modify yi

* modify aquila2

* modify baichuan

* modify baichuan2

* modify blue-lm

* modify chatglm2

* modify chatglm3

* modify codellama

* modify distil-whisper

* modify dolly-v1

* modify dolly-v2

* modify flan-t5

* modify llama2

* modify llava

* modify mistral

* modify mixtral

* modify phi-1_5

* modify qwen-vl

* modify replit

* modify solar

* modify starcoder

* modify yi

* correct the comments

* remove cpu_embedding in code for whisper and distil-whisper

* remove comment

* remove cpu_embedding for voice assistant

* revert modify voice assistant

* modify for voice assistant

* add comment for voice assistant

* fix comments

* fix comments
2024-01-29 11:25:11 +08:00

101 lines
4 KiB
Python

#
# Copyright 2016 The BigDL Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig
from bigdl.llm import optimize_model
torch.manual_seed(1234)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Predict Tokens using `chat()` API for Qwen-VL model')
parser.add_argument('--repo-id-or-model-path', type=str, default="Qwen/Qwen-VL-Chat",
help='The huggingface repo id for the Qwen-VL model to be downloaded'
', or the path to the huggingface checkpoint folder')
parser.add_argument('--n-predict', type=int, default=32, help='Max tokens to predict')
current_path = os.path.dirname(os.path.abspath(__file__))
args = parser.parse_args()
model_path = args.repo_id_or_model_path
# Load model
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cpu", trust_remote_code=True)
# With only one line to enable BigDL-LLM optimization on model
# For successful BigDL-LLM optimization on Qwen-VL-Chat, skip the 'c_fc' and 'out_proj' modules during optimization
# When running LLMs on Intel iGPUs for Windows users, we recommend setting `cpu_embedding=True` in the optimize_model function.
# This will allow the memory-intensive embedding layer to utilize the CPU instead of iGPU.
model = optimize_model(model,
low_bit='sym_int4',
modules_to_not_convert=['c_fc', 'out_proj'])
model = model.to('xpu')
# Due to issue https://github.com/intel/intel-extension-for-pytorch/issues/454,
# currently put interpolation execution into cpu
def to_cpu(module, input, output):
return output.to("cpu")
def to_xpu(module, input):
return (input[0].to("xpu"),)
model.transformer.visual.ln_pre.register_forward_hook(to_cpu)
model.transformer.visual.transformer.register_forward_pre_hook(to_xpu)
# Specify hyperparameters for generation (No need to do this if you are using transformers>=4.32.0)
model.generation_config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# Session ID
session_id = 1
while True:
print('-'*20, 'Session %d' % session_id, '-'*20)
image_input = input(f' Please input a picture: ')
if image_input.lower() == 'exit' : # type 'exit' to quit the dialouge
break
text_input = input(f' Please enter the text: ')
if text_input.lower() == 'exit' : # type 'exit' to quit the dialouge
break
if session_id == 1:
history = None
all_input = [{'image': image_input}, {'text': text_input}]
input_list = [_input for _input in all_input if list(_input.values())[0] != '']
if len(input_list) == 0:
print("Input list should not be empty. Please try again with valid input.")
continue
query = tokenizer.from_list_format(input_list)
response, history = model.chat(tokenizer, query = query, history = history)
torch.xpu.synchronize()
print('-'*10, 'Response', '-'*10)
print(response, '\n')
image = tokenizer.draw_bbox_on_latest_picture(response, history)
if image is not None:
image.save(os.path.join(current_path, f'Session_{session_id}.png'), )
session_id += 1