-
Notifications
You must be signed in to change notification settings - Fork 97
/
inference_ptuning.py
64 lines (53 loc) · 2.05 KB
/
inference_ptuning.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
'''
Author: lihaitao
Date: 2023-05-20 15:06:50
LastEditors: Do not edit
LastEditTime: 2023-05-20 19:36:14
'''
from transformers import AutoModel
import torch
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
from modeling_chatglm import ChatGLMForConditionalGeneration
from tokenization_chatglm import ChatGLMTokenizer
import torch
from peft import PeftModel
import argparse
def generate(model,tokenizer,text):
with torch.no_grad():
input_text = text
ids = tokenizer.encode(input_text)
input_ids = torch.LongTensor([ids]).cuda()
output = model.generate(
input_ids=input_ids,
min_length=20,
max_length=512,
do_sample=False,
temperature=0.7,
num_return_sequences=1
)[0]
output = tokenizer.decode(output)
# answer = output.split(input_text)[-1]
return output.strip()
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("--base_model", type=str, default="model/LexiLaw")
argparser.add_argument("--ptuning", type=str, default="ptuning/pytorch_model.bin")
argparser.add_argument("--interactive", default=True)
args = argparser.parse_args()
config = AutoConfig.from_pretrained(args.base_model, trust_remote_code=True)
config.pre_seq_len = 128
config.prefix_projection = True
model = ChatGLMForConditionalGeneration.from_pretrained(args.base_model, trust_remote_code=True)
tokenizer = ChatGLMTokenizer.from_pretrained("model/LexiLaw", trust_remote_code=True)
prefix_state_dict = torch.load(args.ptuning)
new_prefix_state_dict = {}
for k, v in prefix_state_dict.items():
if k.startswith("transformer.prefix_encoder."):
new_prefix_state_dict[k[len("transformer.prefix_encoder."):]] = v
model.transformer.prefix_encoder.load_state_dict(new_prefix_state_dict)
model.transformer.prefix_encoder.float()
model = model.half().cuda().eval()
while True:
text = input("Input: ")
print(generate(peft_model,tokenizer,text))