|
| 1 | +# |
| 2 | +# Copyright 2016 The BigDL Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import argparse |
| 18 | +from io import BytesIO |
| 19 | +from urllib.request import urlopen |
| 20 | +import librosa |
| 21 | +import torch |
| 22 | +from transformers import Qwen2AudioForConditionalGeneration, AutoProcessor |
| 23 | +from ipex_llm import optimize_model |
| 24 | + |
| 25 | +def main(args): |
| 26 | + model_path = args.repo_id_or_model_path |
| 27 | + max_length = args.max_length |
| 28 | + audio_url = args.audio_url |
| 29 | + |
| 30 | + processor = AutoProcessor.from_pretrained(model_path) |
| 31 | + model = Qwen2AudioForConditionalGeneration.from_pretrained(model_path) |
| 32 | + model = optimize_model(model, low_bit='sym_int4', optimize_llm=True) |
| 33 | + model = model.half().to('xpu') |
| 34 | + |
| 35 | + conversation = [ |
| 36 | + {"role": "user", "content": [ |
| 37 | + {"type": "audio", "audio_url": audio_url}, |
| 38 | + ]}, |
| 39 | + ] |
| 40 | + text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False) |
| 41 | + audios = [] |
| 42 | + for message in conversation: |
| 43 | + if isinstance(message["content"], list): |
| 44 | + for ele in message["content"]: |
| 45 | + if ele["type"] == "audio": |
| 46 | + audios.append(librosa.load( |
| 47 | + BytesIO(urlopen(ele['audio_url']).read()), |
| 48 | + sr=processor.feature_extractor.sampling_rate)[0] |
| 49 | + ) |
| 50 | + |
| 51 | + inputs = processor(text=text, audios=audios, return_tensors="pt", padding=True) |
| 52 | + inputs = inputs.to('xpu') |
| 53 | + |
| 54 | + with torch.inference_mode(): |
| 55 | + generate_ids = model.generate(**inputs, max_length=max_length) # warmup |
| 56 | + import time |
| 57 | + st = time.time() |
| 58 | + generate_ids = model.generate(**inputs, max_length=max_length) |
| 59 | + generate_ids = generate_ids[:, inputs.input_ids.size(1):] |
| 60 | + et = time.time() |
| 61 | + print(f'Inference time: {et-st} s') |
| 62 | + |
| 63 | + response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) |
| 64 | + print(response) |
| 65 | + |
| 66 | +if __name__=="__main__": |
| 67 | + parser = argparse.ArgumentParser(description="Qwen2-Audio") |
| 68 | + parser.add_argument('--repo-id-or-model-path', type=str, default="Qwen/Qwen2-Audio-7B-Instruct", |
| 69 | + help='The huggingface repo id for the Qwen2-Audio model checkpoint') |
| 70 | + parser.add_argument('--max-length', type=int, default=256, |
| 71 | + help='The max length of input text') |
| 72 | + parser.add_argument('--audio-url', type=str, default="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/translate_to_chinese.wav", |
| 73 | + help='The URL to the input audio file') |
| 74 | + args = parser.parse_args() |
| 75 | + main(args) |
0 commit comments