forked from SkyworkAI/Skywork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_demo.py
99 lines (83 loc) · 3.72 KB
/
cli_demo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig
from transformers import AutoConfig
import torch
import argparse
def load(tokenizer_path, checkpoint_path, use_cpu=False):
print('Loading tokenizer ...')
tokenizer = AutoTokenizer.from_pretrained(
tokenizer_path, use_fast=False, trust_remote_code=True, padding_side='left')
tokenizer.add_tokens("[USER]")
tokenizer.add_tokens("[BOT]")
tokenizer.add_tokens("[SEP]")
print('Loading model ...')
config = AutoConfig.from_pretrained(checkpoint_path, trust_remote_code=True)
if use_cpu:
device_map = "cpu"
else:
device_map = "balanced_low_0"
model = AutoModelForCausalLM.from_pretrained(
checkpoint_path, config=config, device_map=device_map, torch_dtype=torch.bfloat16, trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained(
checkpoint_path, trust_remote_code=True)
model.generation_config.do_sample = True
model.eval()
if tokenizer.pad_token_id is None:
if tokenizer.eos_token_id is not None:
tokenizer.pad_token_id = tokenizer.eos_token_id
else:
tokenizer.pad_token_id = 0
return model, tokenizer
def special_encode(prompt, tokenizer):
raw_str = "[USER]%s[SEP][BOT]" % prompt.strip().replace("\r", "")
bos_id = tokenizer.bos_token_id
eos_id = tokenizer.eos_token_id
sep_id = tokenizer.encode("[SEP]")[-1]
res_id = [eos_id, bos_id]
arr = raw_str.split("[SEP]")
for elem_idx in range(len(arr)):
elem = arr[elem_idx]
elem_id = tokenizer.encode(elem)[1:]
res_id += elem_id
if elem_idx < len(arr) - 1:
res_id.append(sep_id)
return res_id
def extract_res(response):
if "[BOT]" in response:
response = response.split("[BOT]")[1]
if "<s>" in response:
response = response.split("<s>")[-1]
if "</s>" in response:
response = response.split("</s>")[0]
if "[SEP]" in response:
response = response.split("[SEP]")[0]
return response[1:]
if __name__ == '__main__':
parser = argparse.ArgumentParser("Skywork-cli-demo")
parser.add_argument("-m", "--model_path", type=str, default="skywork-13b-chat")
parser.add_argument("-n", "--max_new_tokens", type=int, default=1000)
parser.add_argument("-t", "--temperature", type=float, default=0.95)
parser.add_argument("-p", "--top_p", type=float, default=0.8)
parser.add_argument("-k", "--top_k", type=int, default=5)
parser.add_argument("--cpu", action='store_true', help="inference with cpu")
args = parser.parse_args()
model, tokenizer = load(args.model_path, args.model_path, args.cpu)
while True:
doc = input("输入:")
input_tokens = special_encode(doc, tokenizer)
input_tokens = torch.tensor(input_tokens).to(model.device).reshape(1, -1)
response = model.generate(input_tokens,
max_new_tokens=args.max_new_tokens,
pad_token_id=tokenizer.pad_token_id,
do_sample=True,
top_p=args.top_p,
top_k=args.top_k,
temperature=args.temperature,
num_return_sequences=1,
repetition_penalty=1.1,
bos_token_id=1,
eos_token_id=2)
response = tokenizer.decode(response.cpu()[0], skip_special_tokens=True)
response = extract_res(response)
print("模型输出:")
print(response)