-
Notifications
You must be signed in to change notification settings - Fork 0
/
autogptq_bench.py
84 lines (67 loc) · 2.96 KB
/
autogptq_bench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from optimum.habana.transformers.modeling_utils import adapt_transformers_to_gaudi
import torch
import time
from optimum.habana.utils import get_hpu_memory_stats
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--use_hpu_graphs",
action="store_true",
help="Whether to use HPU graphs or not. Using HPU graphs should give better latencies.",
)
parser.add_argument(
"--model_name_or_path",
default="TheBloke/Llama-2-7b-Chat-GPTQ",
type=str,
required=True,
help="Path to pre-trained model (on the HF Hub or locally).",
)
args = parser.parse_args()
adapt_transformers_to_gaudi()
#model_name_or_path = "meta-llama/Llama-2-7b-chat-hf"
# To use a different branch, change revision
# For example: revision="gptq-4bit-64g-actorder_True"
model = AutoModelForCausalLM.from_pretrained(args.model_name_or_path,
device_map="auto",
trust_remote_code=False,
revision="main",
torch_dtype = torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(args.model_name_or_path, use_fast=True)
prompt = "Tell me about AI"
prompt_template=f'''[INST] <<SYS>>
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
<</SYS>>
{prompt}[/INST]
'''
print("\n\n*** Generate:")
model = model.eval().to("hpu")
input_ids = tokenizer(prompt_template, return_tensors='pt').input_ids.to("hpu")
if args.use_hpu_graphs:
from habana_frameworks.torch.hpu import wrap_in_hpu_graph
model = wrap_in_hpu_graph(model)
print(f"input_ids.device={input_ids.device}")
for i in range(3) :
output = model.generate(inputs=input_ids, temperature=0.7, do_sample=True, top_p=0.95, top_k=40, max_new_tokens=128)
start = time.time()
for i in range(10) :
output = model.generate(inputs=input_ids, temperature=0.7, do_sample=True, top_p=0.95, top_k=40, max_new_tokens=128)
print(tokenizer.decode(output[0]))
end = time.time() - start
print(f"time = {end}s")
mem_stats = get_hpu_memory_stats("hpu:0")
print(f"memory_usage={mem_stats}")
# Inference can also be done using transformers' pipeline
# print("*** Pipeline:")
# pipe = pipeline(
# "text-generation",
# model=model,
# tokenizer=tokenizer,
# max_new_tokens=512,
# do_sample=True,
# temperature=0.7,
# top_p=0.95,
# top_k=40,
# repetition_penalty=1.1,
# )
#print(pipe(prompt_template)[0]['generated_text'])