|
| 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 | +from bigdl.llm.transformers import AutoModelForCausalLM |
| 21 | +from transformers import LlamaTokenizer |
| 22 | + |
| 23 | +# you could tune the prompt based on your own model, |
| 24 | +# here the prompt tuning refers to https://huggingface.co/georgesung/llama2_7b_chat_uncensored#prompt-style |
| 25 | +LLAMA2_PROMPT_FORMAT = """### HUMAN: |
| 26 | +{prompt} |
| 27 | +
|
| 28 | +### RESPONSE: |
| 29 | +""" |
| 30 | + |
| 31 | +if __name__ == '__main__': |
| 32 | + parser = argparse.ArgumentParser(description='Example of saving and loading the optimized model') |
| 33 | + parser.add_argument('--repo-id-or-model-path', type=str, default="meta-llama/Llama-2-7b-chat-hf", |
| 34 | + help='The huggingface repo id for the Llama2 (e.g. `meta-llama/Llama-2-7b-chat-hf` and `meta-llama/Llama-2-13b-chat-hf`) to be downloaded' |
| 35 | + ', or the path to the huggingface checkpoint folder') |
| 36 | + parser.add_argument('--save-path', type=str, default=None, |
| 37 | + help='The path to save the low-bit model.') |
| 38 | + parser.add_argument('--load-path', type=str, default=None, |
| 39 | + help='The path to load the low-bit model.') |
| 40 | + parser.add_argument('--prompt', type=str, default="What is AI?", |
| 41 | + help='Prompt to infer') |
| 42 | + parser.add_argument('--n-predict', type=int, default=32, |
| 43 | + help='Max tokens to predict') |
| 44 | + args = parser.parse_args() |
| 45 | + model_path = args.repo_id_or_model_path |
| 46 | + load_path = args.load_path |
| 47 | + if load_path: |
| 48 | + model = AutoModelForCausalLM.load_low_bit(load_path, trust_remote_code=True) |
| 49 | + tokenizer = LlamaTokenizer.from_pretrained(load_path) |
| 50 | + else: |
| 51 | + model = AutoModelForCausalLM.from_pretrained(model_path, |
| 52 | + load_in_4bit=True, |
| 53 | + trust_remote_code=True) |
| 54 | + tokenizer = LlamaTokenizer.from_pretrained(model_path, trust_remote_code=True) |
| 55 | + |
| 56 | + save_path = args.save_path |
| 57 | + if save_path: |
| 58 | + model.save_low_bit(save_path) |
| 59 | + tokenizer.save_pretrained(save_path) |
| 60 | + print(f"Model and tokenizer are saved to {save_path}") |
| 61 | + |
| 62 | + # please save/load model before you run it on GPU |
| 63 | + model = model.to('xpu') |
| 64 | + |
| 65 | + # Generate predicted tokens |
| 66 | + with torch.inference_mode(): |
| 67 | + prompt = LLAMA2_PROMPT_FORMAT.format(prompt=args.prompt) |
| 68 | + input_ids = tokenizer.encode(prompt, return_tensors="pt").to('xpu') |
| 69 | + # ipex model needs a warmup, then inference time can be accurate |
| 70 | + output = model.generate(input_ids, |
| 71 | + max_new_tokens=args.n_predict) |
| 72 | + |
| 73 | + st = time.time() |
| 74 | + output = model.generate(input_ids, |
| 75 | + max_new_tokens=args.n_predict) |
| 76 | + torch.xpu.synchronize() |
| 77 | + end = time.time() |
| 78 | + output = output.cpu() |
| 79 | + output_str = tokenizer.decode(output[0], skip_special_tokens=True) |
| 80 | + print(f'Inference time: {end-st} s') |
| 81 | + print('-'*20, 'Output', '-'*20) |
| 82 | + print(output_str) |
0 commit comments