-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinference.py
92 lines (80 loc) · 4.34 KB
/
inference.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
import re
from utils import *
model_list = ['gpt2']
datasets = ['analytic_entailment/task.json']
p_ts = ['closed', 'closed-adv', 'open']
seeds = [2266, 105, 86379]
device = 'cuda'
for pretrained in model_list:
tokenizer = AutoTokenizer.from_pretrained(pretrained, padding_side='left')
if pretrained in ['gpt-2']:
model = GPT2LMHeadModel.from_pretrained(pretrained).to(device)
elif pretrained in ['t5-small', 'google/flan-t5-small', 't5-large', 'google/flan-t5-large']:
model = T5ForConditionalGeneration.from_pretrained(pretrained).to(device)
else:
model = AutoModelForCausalLM.from_pretrained(pretrained, torch_dtype=torch.float16).to(device)
for dataset in datasets:
if dataset == 'causal_judgment/task.json':
n_o_s = [0, 2]
elif dataset == 'strange_stories/task.json':
n_o_s = [0, 4]
else:
n_o_s = [0, 5]
for prompt_type in p_ts:
for number_of_shots in n_o_s:
for seed in seeds:
config = {
'run_id': time.strftime('%Y%m%d_%H%M%S', time.localtime()),
'filename': dataset,
'number_of_data': None, ## check here
'model': pretrained,
'prompt_type': prompt_type,
'number_of_shots': number_of_shots,
'temperature': 0.01,
'max_new_tokens': 'adaptive', ## check here
'batch_size': 16, ## check here
'pad_token': pad_tokens[pretrained],
'pad_token_id': pad_ids[pretrained],
'eos_token_id': eos_ids[pretrained],
'seed': seed,
'device': torch.cuda.get_device_name(torch.cuda.current_device())
}
torch.cuda.manual_seed_all(config['seed'])
name = config['filename'].replace('/task.json', '')
try:
with open(os.path.join('data/', config['filename'])) as file:
data = json.loads(file.read())['examples']
except Exception as e:
with open(os.path.join('data/', config['filename'])) as file:
data = json.loads(file.read())
inputs_targets = prepare_data_bigbench(data, config['prompt_type'], config['number_of_shots'], name)
config['number_of_data'] = len(inputs_targets)
loader = prepare_loader(inputs_targets, tokenizer, config['pad_token'], batch_size=config['batch_size'], shuffle=False, device=device)
results = defaultdict(list)
with tqdm(total=len(loader)) as t:
for input_ids, attention_mask, raw_inputs, targets, options, longest_sequence in loader:
max_new_tokens = config['max_new_tokens']
if config['max_new_tokens'] == 'adaptive':
max_new_tokens = tokenizer(list(longest_sequence), padding=True, return_tensors='pt').input_ids.shape[1] + 8
outputs = batch_predict(
input_ids,
attention_mask,
model,
tokenizer,
max_new_tokens,
pad_token_id=config['pad_token_id'],
eos_token_id=config['eos_token_id'],
temperature=config['temperature'],
device=device
)
for i in range(len(outputs)):
results['input'].append(raw_inputs[i])
results['target'].append(targets[i])
results['options'].append(options[i])
results['prediction'].append(outputs[i].replace(raw_inputs[i], '').lstrip('</s>'))
t.update(1)
log_and_save(results, config)
time.sleep(2)
del model
torch.cuda.empty_cache()
time.sleep(2)