-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstep04_run_decoding.py
347 lines (310 loc) · 14.2 KB
/
step04_run_decoding.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Ref: https://github.com/kojima-takeshi188/zero_shot_cot
import re
import os
import json
import random
import torch
import numpy as np
import transformers
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from tqdm import tqdm
import argparse
import pickle
from generation import LLM
os.environ["TOKENIZERS_PARALLELISM"] = "false"
transformers.logging.set_verbosity(40)
def num_tokens_from_message(message, llama2_tokenizer):
return len(llama2_tokenizer(message)['input_ids'])
def truncate_message(prompt1, prompt2, llama2_tokenizer):
if num_tokens_from_message(prompt1 + prompt2) > 2033:
truncation_length = 2033 - num_tokens_from_message(prompt2, llama2_tokenizer)
while num_tokens_from_message(prompt1) > truncation_length:
prompt1 = " ".join(prompt1.split(' ')[:-1])
prompt = prompt1 + prompt2
return prompt
data_context_names = {
'cnndm': 'Document',
'xsum': 'Article',
'nq': 'Document',
}
data_response_names = {
'cnndm': 'Summary',
'xsum': 'Summary',
'nq': 'Answer',
}
temperature_config = {
"writing": 0.7,
"roleplay": 0.7,
"extraction": 0.0,
"math": 0.0,
"coding": 0.0,
"reasoning": 0.0,
"stem": 0.1,
"humanities": 0.1,
"arena-hard-200": 0.0,
}
def load_nq_open(file_path, parallel=False, total_shard=8, shard_id=0, debug=False, data_type='nq_open', subsample=None):
list_data_dict = []
is_train = 'nq_train' in file_path
with open(file_path, 'r', encoding="utf-8") as f:
data = []
data_indices = []
data_index = 0
for line in f:
data.append(json.loads(line))
data_indices.append(data_index)
data_index += 1
if debug:
data = data[:10]
data_indices = data_indices[:10]
if subsample is not None:
# select data if idx%subsample == 0
data = [data[i] for i in range(len(data)) if i % subsample == 0]
data_indices = [data_indices[i] for i in range(len(data_indices)) if i % subsample == 0]
if parallel:
chunk_size = len(data) // total_shard
data = data[shard_id * chunk_size: (shard_id + 1) * chunk_size] if shard_id != total_shard - 1 else data[shard_id * chunk_size:]
data_indices = data_indices[shard_id * chunk_size: (shard_id + 1) * chunk_size] if shard_id != total_shard - 1 else data_indices[shard_id * chunk_size:]
for idx in range(len(data)):
data_index = data_indices[idx]
question = data[idx]['question']
# capitalize the first letter of the question, add the question mark if not present at the end
question = question[0].upper() + question[1:]
if question[-1] != '?':
question += '?'
answers = data[idx]['answers']
if is_train:
pos_ctxs = data[idx]['positive_ctxs']
neg_ctxs = data[idx]['negative_ctxs']
else:
ctxs = data[idx]['ctxs']
pos_ctxs = [ctx for ctx in ctxs if ctx['hasanswer']]
neg_ctxs = [ctx for ctx in ctxs if not ctx['hasanswer']]
assert len(pos_ctxs) > 0, "No positive context found."
assert len(neg_ctxs) >= 2, "At least two negative contexts are required."
context = f"#Document#: " + neg_ctxs[0]['text'] + '\n' + pos_ctxs[0]['text'] + '\n' + neg_ctxs[1]['text']
context += f"\n#Question#: {question}"
response = f"\n#Answer#:"
new_item = dict(
context=context,
response=response,
answer=answers[0],
data_index=data_index
)
list_data_dict.append(new_item)
return list_data_dict
def load_jsonl(file_path, parallel=False, total_shard=8, shard_id=0, debug=False, data_type='cnndm', subsample=None):
list_data_dict = []
with open(file_path, 'r', encoding="utf-8") as f:
data = []
data_indices = []
data_index = 0
for line in f:
data.append(json.loads(line))
data_indices.append(data_index)
data_index += 1
if debug:
data = data[:10]
data_indices = data_indices[:10]
if subsample is not None:
# select data if idx%subsample == 0
data = [data[i] for i in range(len(data)) if i % subsample == 0]
data_indices = [data_indices[i] for i in range(len(data_indices)) if i % subsample == 0]
if parallel:
chunk_size = len(data) // total_shard
data = data[shard_id * chunk_size: (shard_id + 1) * chunk_size] if shard_id != total_shard - 1 else data[shard_id * chunk_size:]
data_indices = data_indices[shard_id * chunk_size: (shard_id + 1) * chunk_size] if shard_id != total_shard - 1 else data_indices[shard_id * chunk_size:]
for idx in range(len(data)):
data_index = data_indices[idx]
if data_type == 'mt_bench':
context = data[idx]['document']
category = data[idx]['category']
else:
context = f"#{data_context_names[data_type]}#: " + data[idx]["document"]
new_item = dict(
context=context,
data_index=data_index,
category=category if data_type == 'mt_bench' else None
)
list_data_dict.append(new_item)
return list_data_dict
def dump_jsonl(data, output_path, append=False):
"""
Write list of objects to a JSON lines file.
"""
mode = 'a+' if append else 'w'
with open(output_path, mode, encoding='utf-8') as f:
json_record = json.dumps(data, ensure_ascii=False)
f.write(json_record + '\n')
def create_demo_text(data_type='cnndm'):
if data_type == 'cnndm':
return "Generate a summary based on the information in the document.\n\n"
elif data_type == 'nq':
return "Answer the question based on the information in the document. Explain your reasoning in the document step-by-step before providing the final answer.\n\n"
elif data_type == 'xsum':
return "Generate a summary comprising of 1 sentence for the given article.\n\n"
else:
return None
def build_prompt(context, response, data_type='cnndm', llama2_tokenizer=None):
demo = create_demo_text(data_type)
prompt = demo + context
if data_type == 'cnndm':
input_text_prompt = truncate_message(prompt, response, llama2_tokenizer)
else:
input_text_prompt = prompt + response
return input_text_prompt
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str, default="meta-llama/Llama-7b-chat-hf")
parser.add_argument("--num_gpus", type=str, default="1")
parser.add_argument("--device", type=str,
choices=["cuda", "cpu"], default="cuda")
parser.add_argument("--max_memory", type=int, default=45)
parser.add_argument("--auth_token", type=str, default=None)
parser.add_argument("--output_path", type=str, default="./output.jsonl")
# data
parser.add_argument("--data_type", type=str, default=None)
parser.add_argument("--data_path", type=str, default="./data/nq-open-10_total_documents_gold_at_4.jsonl")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--subsample", type=int, default=None)
# parallel mode (split the dataset into multiple parts, inference by separate processes)
parser.add_argument("--parallel", action="store_true")
parser.add_argument("--total_shard", type=int, default=8)
parser.add_argument("--shard_id", type=int, default=0)
# generation
parser.add_argument("--max_new_tokens", type=int, default=256)
parser.add_argument("--top_p", type=float, default=0.95) # only used when do_sample is True
parser.add_argument("--top_k", type=int, default=0) # only used when do_sample is True
parser.add_argument("--temperature", type=float, default=0.9)
parser.add_argument("--do_sample", action="store_true")
parser.add_argument("--seed", type=int, default=42)
# classifier model path
parser.add_argument("--guiding_classifier", type=str, default=None)
# chunk size
parser.add_argument("--chunk_size", type=int, default=5)
# num candidates
parser.add_argument("--num_candidates", type=int, default=8)
# conversion matrix
parser.add_argument("--conversion_matrix", type=str, default=None)
# feat_layer
parser.add_argument("--feat_layer", type=int, default=None)
args = parser.parse_args()
model_name = args.model_name
num_gpus = args.num_gpus
device = args.device
set_seed(args.seed)
forced_truncate = ('gpt2' in args.model_name)
if args.data_type is None:
if 'cnndm' in args.data_path or 'summ' in args.data_path:
args.data_type = 'cnndm'
elif 'nq-open' in args.data_path:
args.data_type = 'nq'
elif 'xsum' in args.data_path:
args.data_type = 'xsum'
elif 'mt_bench' in args.data_path:
args.data_type = 'mt_bench'
else:
raise ValueError("Please specify the data type.")
fp = args.data_path
if not os.path.exists(fp):
raise ValueError(f"Test file {fp} does not exist.")
if "nq-open" in fp:
list_data_dict = load_nq_open(fp, parallel=args.parallel, total_shard=args.total_shard, shard_id=args.shard_id, debug=args.debug, subsample=args.subsample)
else:
list_data_dict = load_jsonl(fp, parallel=args.parallel, total_shard=args.total_shard, shard_id=args.shard_id, debug=args.debug, data_type=args.data_type, subsample=args.subsample)
llm = LLM(
model_name, device, num_gpus,
auth_token=args.auth_token,
max_memory=args.max_memory)
stop_word_list = ["### User:", "Q:", "\end{code}", "#Document#:", "#Pondering#:", "#Question#:", "#Dialogue History#:"]
llm.set_stop_words(stop_word_list)
guiding_classifier = None
if args.guiding_classifier is not None:
if args.guiding_classifier == 'vectara/hallucination_evaluation_model':
from sentence_transformers import CrossEncoder
guiding_classifier = {}
model = CrossEncoder(args.guiding_classifier)
tokenizer = llm.tokenizer
guiding_classifier['model'] = model
guiding_classifier['tokenizer'] = tokenizer
guiding_classifier['is_cross_encoder'] = True
guiding_classifier['is_deberta'] = False
elif '.pkl' in args.guiding_classifier:
guiding_classifier = pickle.load(open(args.guiding_classifier, 'rb'))
guiding_classifier['is_cross_encoder'] = False
guiding_classifier['is_deberta'] = False
else: # is deberta nli model
nli_model = AutoModelForSequenceClassification.from_pretrained(args.guiding_classifier)
nli_tokenizer = AutoTokenizer.from_pretrained(args.guiding_classifier)
nli_model.to(device)
guiding_classifier = {'model': nli_model, 'tokenizer': nli_tokenizer, 'is_deberta': True, 'is_cross_encoder': False}
mode = "classifier_guided"
print("MODE: classifier guided decoding", flush=True)
else:
mode = "vanilla"
print("MODE: vanilla decoding", flush=True)
conversion_matrix = None
if args.conversion_matrix is not None:
conversion_matrix = pickle.load(open(args.conversion_matrix, 'rb'))
output_path = args.output_path+"_"+str(args.shard_id)+".jsonl"
done_indices = {}
if os.path.exists(output_path):
print("Try to resume from the existing output file.")
with open(output_path, 'r') as f:
for line in f:
data = json.loads(line)
for key, value in data.items():
done_indices[int(key)] = value
fw = open(output_path, 'a')
else:
fw = open(output_path, 'w')
if args.data_type == 'mt_bench':
extra_prompt_length = len(llm.tokenizer(f"\n\n### Assistant:")['input_ids'])
else:
extra_prompt_length = len(llm.tokenizer(f"\n#{data_response_names[args.data_type]}#:")['input_ids']) - 1
time_decoding = 0.0
for idx in tqdm(range(len(list_data_dict))):
sample = list_data_dict[idx]
if sample['data_index'] in done_indices:
continue
if args.data_type != 'mt_bench':
input_text = build_prompt(sample['context'], f"\n#{data_response_names[args.data_type]}#:", data_type=args.data_type, llama2_tokenizer=llm.tokenizer)
else:
input_text = sample['context']
generate_kwargs = dict(max_new_tokens=args.max_new_tokens,
do_sample=args.do_sample, top_p=args.top_p, top_k=args.top_k,
temperature=args.temperature,
mode=mode)
if args.data_type == 'mt_bench':
if sample["category"] in temperature_config:
temperature = temperature_config[sample["category"]]
else:
temperature = 0.7
if temperature < 1e-4:
do_sample = False
else:
do_sample = True
generate_kwargs['temperature'] = temperature
generate_kwargs['do_sample'] = do_sample
model_completion, gen_seq = llm.generate(
input_text, guiding_classifier=guiding_classifier, conversion_matrix=conversion_matrix,
extra_prompt_length=extra_prompt_length,
feat_layer=args.feat_layer,
chunk_size=args.chunk_size, num_candidates=args.num_candidates, **generate_kwargs)
cropped_model_completion = model_completion
for stop_word in stop_word_list:
length_to_remove = len(stop_word)
if model_completion[-length_to_remove:] == stop_word:
cropped_model_completion = model_completion[:-length_to_remove]
cropped_gen_seq = llm.tokenizer(model_completion)['input_ids'][1:]
return_dict = {
sample['data_index']: cropped_model_completion.strip()
}
fw.write(json.dumps(return_dict, ensure_ascii=False) + '\n')
fw.flush()
fw.close()