-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinternlm2.py
64 lines (55 loc) · 2.39 KB
/
internlm2.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
from utils import *
from tqdm import tqdm
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import transformers
from vllm import LLM, SamplingParams
import torch
from transformers.generation import GenerationConfig
from fastchat.model import load_model, get_conversation_template, add_model_args
from transformers import pipeline, LlamaTokenizer, LlamaForCausalLM
import json
import os
import sys
import ipdb
import argparse
def parser_args():
parser = argparse.ArgumentParser(description='train parameters')
parser.add_argument('--output_dir', type=str, default='outputs')
parser.add_argument('--data_dir', type=str, default='../data/CriticBench')
parser.add_argument('--split', type=str, default='test')
parser.add_argument('--mode_name', type=str, default='feedback')
parser.add_argument('--set_name', type=str, default='translate')
parser.add_argument('--model_name', type=str, default='internlm/internlm2-chat-7b')
return parser.parse_args()
if __name__ == "__main__":
args = vars(parser_args())
# init the dataset
datasets = load_all_datasets(args['data_dir'])
## init the model, revise following codes for your LLMs to be evaluated
tokenizer = AutoTokenizer.from_pretrained(
args['model_name'],
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
args['model_name'],
device_map="auto",
trust_remote_code=True
).cuda().eval()
if os.path.exists(args['output_dir']) is False:
os.makedirs(args['output_dir'])
if os.path.exists(os.path.join(args['output_dir'], args['model_name'])) is False:
os.makedirs(os.path.join(args['output_dir'], args['model_name']))
folder_path = os.path.join(args['output_dir'], args['model_name'])
# inference and save the results
for abbr, dataset in tqdm(datasets.items()):
path = os.path.join(folder_path, abbr + ".json")
results = {}
for item in tqdm(dataset['dev']):
# If you want to inference other LLMs, please revise this line
response, history = model.chat(tokenizer, item['question'], history=[])
results[str(len(results))] = {
'origin_prompt': item['question'],
'prediction': response
}
with open(path, 'w') as f:
json.dump(results, f, ensure_ascii=False, indent=4)