|
| 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 ipex_llm.transformers import AutoModelForCausalLM |
| 23 | +from transformers import AutoTokenizer |
| 24 | + |
| 25 | +# you could tune the prompt based on your own model, |
| 26 | +# here the prompt tuning refers to https://hf-mirror.com/THUDM/glm-edge-1.5b-chat |
| 27 | + |
| 28 | + |
| 29 | +if __name__ == '__main__': |
| 30 | + parser = argparse.ArgumentParser(description='Predict Tokens using `generate()` API for GLM-Edge model') |
| 31 | + parser.add_argument('--repo-id-or-model-path', type=str, default="THUDM/glm-edge-1.5b-chat", |
| 32 | + help='The huggingface repo id for the GLM-Edge model to be downloaded' |
| 33 | + ', or the path to the huggingface checkpoint folder') |
| 34 | + parser.add_argument('--prompt', type=str, default="AI是什么?", |
| 35 | + help='Prompt to infer') |
| 36 | + parser.add_argument('--n-predict', type=int, default=32, |
| 37 | + help='Max tokens to predict') |
| 38 | + |
| 39 | + args = parser.parse_args() |
| 40 | + model_path = args.repo_id_or_model_path |
| 41 | + |
| 42 | + # Load model in 4 bit, |
| 43 | + # which convert the relevant layers in the model into INT4 format |
| 44 | + # When running LLMs on Intel iGPUs for Windows users, we recommend setting `cpu_embedding=True` in the from_pretrained function. |
| 45 | + # This will allow the memory-intensive embedding layer to utilize the CPU instead of iGPU. |
| 46 | + model = AutoModelForCausalLM.from_pretrained(model_path, |
| 47 | + load_in_4bit=True, |
| 48 | + optimize_model=True, |
| 49 | + trust_remote_code=True, |
| 50 | + use_cache=True) |
| 51 | + model = model.to("xpu") |
| 52 | + |
| 53 | + # Load tokenizer |
| 54 | + tokenizer = AutoTokenizer.from_pretrained(model_path, |
| 55 | + trust_remote_code=True) |
| 56 | + |
| 57 | + # Generate predicted tokens |
| 58 | + with torch.inference_mode(): |
| 59 | + message = [{"role": "user", "content": args.prompt}] |
| 60 | + |
| 61 | + inputs = tokenizer.apply_chat_template( |
| 62 | + message, |
| 63 | + return_tensors="pt", |
| 64 | + add_generation_prompt=True, |
| 65 | + return_dict=True, |
| 66 | + ).to(model.device) |
| 67 | + |
| 68 | + generate_kwargs = { |
| 69 | + "input_ids": inputs["input_ids"], |
| 70 | + "attention_mask": inputs["attention_mask"], |
| 71 | + "max_new_tokens": args.n_predict, |
| 72 | + "do_sample": False, |
| 73 | + } |
| 74 | + |
| 75 | + # ipex_llm model needs a warmup, then inference time can be accurate |
| 76 | + output = model.generate(**generate_kwargs) |
| 77 | + |
| 78 | + st = time.time() |
| 79 | + |
| 80 | + output = model.generate(**generate_kwargs) |
| 81 | + |
| 82 | + torch.xpu.synchronize() |
| 83 | + end = time.time() |
| 84 | + output_str = tokenizer.decode(output[0], skip_special_tokens=True) |
| 85 | + print(f'Inference time: {end-st} s') |
| 86 | + print('-'*20, 'Prompt', '-'*20) |
| 87 | + print(args.prompt) |
| 88 | + print('-'*20, 'Output', '-'*20) |
| 89 | + print(output_str) |
0 commit comments