-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_tod.py
173 lines (139 loc) · 5.53 KB
/
eval_tod.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import torch
import argparse
import json
import termcolor
from utils import set_seed, get_injected_tokens, setup_multi_gpu, get_loader, is_primary, gather
from eval_qa import set_config, add_params
import modules
from tqdm import tqdm
from eval_utils import *
def do_eval(model, tokenizer, args, is_stg):
output_file = _get_result_fpath(args.device_id)
output_inj_file = None
is_ensemble = is_stg or args.inj_scheme is not None
if is_ensemble:
output_inj_file = _get_result_fpath(args.device_id, postfix='inj')
print(f"output_file:{output_file}")
print(f"output_inj_file:{output_inj_file}")
loader = get_loader(tokenizer, args, mode='test')
iter = tqdm(range(len(loader.loader)))
outputs = []
output_injections = []
ids = []
for k in iter:
with torch.no_grad():
batch = loader.get_batch()
_, _, aux_data = batch
(_, _, _, _, data_id) = aux_data
samples, injection_acts = generate_sample(model, tokenizer, batch, args, is_stg=is_stg, do_sample=True)
utters, injections = [], []
for i, utter in enumerate(samples):
if is_ensemble:
injected_str = get_injected_tokens(tokenizer, injection_acts[i], config.vocab_size)
injections.append(injected_str)
# print(termcolor.colored('[' + injected_str.strip() + ']', 'red'))
next_utterance = tokenizer.decode(utter)
cl_idx = next_utterance.find(tokenizer.eos_token)
next_utterance = next_utterance[:cl_idx].strip().lower()
utters.append(next_utterance)
# print(termcolor.colored(next_utterance, 'green'))
outputs.append(utters)
output_injections.append(injections)
ids.append(data_id.cuda())
json.dump(outputs, open(output_file, 'w'), indent=2)
if is_ensemble:
json.dump(output_injections, open(output_inj_file, 'w'), indent=2)
ids = torch.cat(ids)
tot_ids = gather(ids)
if is_primary():
ids = torch.cat(tot_ids)
print("gatherd:", ids.shape)
result_path = _get_result_fpath(None)
result_inj_path = None
if is_ensemble:
result_inj_path = _get_result_fpath(None, postfix='inj')
print(result_path, result_inj_path)
overall_result = []
overall_inj_result = []
for _id in range(args.world_size):
fpath = _get_result_fpath(_id)
with open(fpath, 'r') as fin:
result_data = json.load(fin)
overall_result.extend(result_data)
os.remove(fpath)
if is_ensemble:
fpath = _get_result_fpath(_id, postfix='inj')
with open(fpath, 'r') as fin:
result_data = json.load(fin)
overall_inj_result.extend(result_data)
os.remove(fpath)
ids = ids.detach().cpu().numpy()
result_data = {}
for i, data_id in enumerate(ids):
result_data[int(data_id)] = overall_result[i]
if is_ensemble:
result_inj_data = {}
for i, data_id in enumerate(ids):
result_inj_data[int(data_id)] = overall_inj_result[i]
json.dump(result_data, open(result_path, 'w'), indent=2)
if is_ensemble:
json.dump(result_inj_data, open(result_inj_path, 'w'), indent=2)
bleu, err = eval_tod(args.domain, result_path, verbose=1)
score_dict = {
'seed': args.seed,
'scheme': args.scheme,
'PLM_temperature': args.temp,
'Calib_temperature': args.temp2,
'topk': args.top_k,
'n_sample': args.n_sample,
'BLEU': "{0:.2f}".format(bleu * 100),
'ERR': "{0:.2f}".format(err)
}
if args.inj_scheme is not None:
score_dict['inj_scheme'] = args.inj_scheme
print(result_path)
print(score_dict)
if __name__ == '__main__':
ap = argparse.ArgumentParser()
add_params(ap)
ap.add_argument('-d', '--domain', default=None, type=str, required=False)
ap.add_argument('-td', '--target_domain', type=str, default=None)
ap.add_argument('--inj_test', type=str2bool, default=False)
ap.add_argument('--seed_test', type=str2bool, default=False)
args = ap.parse_args()
args.exp = 'tod'
args.use_train_score = False
setup_multi_gpu(args)
model, tokenizer, config = load_model(args)
if config is not None:
args.domain = config.domain
output_dir = f'tod/log_dir/results/{args.domain}'
os.makedirs(output_dir, exist_ok=True)
is_stg = False
if config is not None:
is_stg = config.is_stg
def _get_result_fpath(device_id=None, postfix=None):
prefix = None
if args.mode == 'rl':
prefix = f'genSEED-{args.seed}'
return get_result_fpath(args, output_dir, device_id=device_id, postfix=postfix, prefix=prefix)
def do_test(args):
if args.inj_test:
inj_schemes = ['max', 'mix']
for scheme in inj_schemes:
config.inj_scheme = scheme
args.inj_scheme = scheme
do_eval(model, tokenizer, args, is_stg)
else:
do_eval(model, tokenizer, args, is_stg)
if args.seed_test:
seeds = [9, 99, 999, 9999, 99999]
for seed in seeds:
set_seed(seed)
args.seed = seed
config.seed = seed
do_test(args)
else:
set_seed(args.seed)
do_test(args)