-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathinfer.py
executable file
·82 lines (67 loc) · 2.72 KB
/
infer.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
import torch
from transformers import AutoTokenizer, AutoModel
from peft import get_peft_model, LoraConfig, TaskType
import json
# import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# from modeling_chatglm import ChatGLMForConditionalGeneration
def format_example(example: dict) -> dict:
context = f"Instruction: {example['instruction']}\n"
if example.get("input"):
context += f"Input: {example['input']}\n"
context += "Answer: "
target = example["output"]
return {"context": context, "target": target}
class ChatGLMPredictor:
def __init__(self, model_path, peft_path, device):
self.device = device
torch.set_default_tensor_type(torch.cuda.HalfTensor)
self.model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map='auto')
self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
peft_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
inference_mode=True,
r=8,
lora_alpha=32,
lora_dropout=0.1,
)
self.model = get_peft_model(self.model, peft_config)
self.model.load_state_dict(torch.load(peft_path), strict=False)
torch.set_default_tensor_type(torch.cuda.FloatTensor)
def predict(self, input_text):
ids = self.tokenizer.encode(input_text)
input_ids = torch.LongTensor([ids]).to(self.device)
out = self.model.generate(
input_ids=input_ids,
max_length=120,
do_sample=False,
temperature=0
)
out_text = self.tokenizer.decode(out[0])
return out_text
def run_prediction(self, instructions):
answers = []
with torch.no_grad():
for idx, item in enumerate(instructions):
input_text = format_example(item)['context']
answer = self.predict(input_text)
item['infer_answer'] = answer
answers.append({'index': idx, **item})
return answers
def main():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_path = "THUDM/chatglm-6b"
peft_path = "output_zh-data01/adapter_model.bin"
instructions_path = "data/zh-data01.json"
predictor = ChatGLMPredictor(model_path, peft_path, device)
instructions = json.load(open(instructions_path))
answers = predictor.run_prediction(instructions[12:18])
for idx, answer in enumerate(answers):
print(f"### {idx + 1}. Answer:\n", answer['infer_answer'], '\n\n')
## interact
while True:
input_text = input("User:")
out_text = predictor.predict(input_text)
print(out_text)
if __name__ == "__main__":
main()