|
| 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 torch |
| 18 | +import time |
| 19 | +import argparse |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +from transformers import AutoTokenizer |
| 23 | + |
| 24 | + |
| 25 | +if __name__ == '__main__': |
| 26 | + parser = argparse.ArgumentParser(description='Qwen2-7B-Instruct') |
| 27 | + parser.add_argument('--repo-id-or-model-path', type=str, default="Qwen/Qwen2-7B-Instruct", |
| 28 | + help='The huggingface repo id for the Qwen2 model to be downloaded' |
| 29 | + ', or the path to the huggingface checkpoint folder') |
| 30 | + parser.add_argument('--prompt', type=str, default="AI是什么?", |
| 31 | + help='Prompt to infer') |
| 32 | + parser.add_argument('--n-predict', type=int, default=32, |
| 33 | + help='Max tokens to predict') |
| 34 | + |
| 35 | + args = parser.parse_args() |
| 36 | + model_path = args.repo_id_or_model_path |
| 37 | + |
| 38 | + |
| 39 | + from transformers import AutoModelForCausalLM |
| 40 | + model = AutoModelForCausalLM.from_pretrained(model_path, |
| 41 | + trust_remote_code=True, |
| 42 | + torch_dtype='auto', |
| 43 | + low_cpu_mem_usage=True, |
| 44 | + use_cache=True) |
| 45 | + |
| 46 | + |
| 47 | + # Load tokenizer |
| 48 | + tokenizer = AutoTokenizer.from_pretrained(model_path, |
| 49 | + trust_remote_code=True) |
| 50 | + from ipex_llm import optimize_model |
| 51 | + model = optimize_model(model) |
| 52 | + |
| 53 | + prompt = args.prompt |
| 54 | + # Generate predicted tokens |
| 55 | + with torch.inference_mode(): |
| 56 | + # The following code for generation is adapted from https://huggingface.co/Qwen/Qwen2-7B-Instruct#quickstart |
| 57 | + messages = [ |
| 58 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 59 | + {"role": "user", "content": prompt} |
| 60 | + ] |
| 61 | + text = tokenizer.apply_chat_template( |
| 62 | + messages, |
| 63 | + tokenize=False, |
| 64 | + add_generation_prompt=True |
| 65 | + ) |
| 66 | + model_inputs = tokenizer([text], return_tensors="pt") |
| 67 | + st = time.time() |
| 68 | + generated_ids = model.generate( |
| 69 | + model_inputs.input_ids, |
| 70 | + max_new_tokens=args.n_predict |
| 71 | + ) |
| 72 | + end = time.time() |
| 73 | + generated_ids = [ |
| 74 | + output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids) |
| 75 | + ] |
| 76 | + |
| 77 | + response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] |
| 78 | + print(f'Inference time: {end-st} s') |
| 79 | + print('-'*20, 'Prompt', '-'*20) |
| 80 | + print(prompt) |
| 81 | + print('-'*20, 'Output', '-'*20) |
| 82 | + print(response) |
0 commit comments