forked from SafeAILab/EAGLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_ea_answer_mix.py
476 lines (423 loc) · 15.5 KB
/
gen_ea_answer_mix.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
"""Generate answers with local models.
Usage:
python3 gen_model_answer.py --model-path lmsys/fastchat-t5-3b-v1.0 --model-id fastchat-t5-3b-v1.0
"""
import argparse
import json
import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "6,7"
import time
import shortuuid
from fastchat.llm_judge.common import load_questions
from fastchat.model import get_conversation_template
from tqdm import tqdm
from model.ea_model import EaModel
from model.kv_cache import initialize_past_key_values
from model.utils import *
from model.choices import *
def ea_forward(input_ids, model, tokenizer, tree_choices, logits_processor=None, max_steps=512):
assert input_ids.shape[0] == 1, "Only support batch size 1 for now!!"
# Avoid modifying the input_ids in-place
input_ids = input_ids.clone()
model.ea_layer.reset_kv()
if hasattr(model, "tree_choices") and model.tree_choices == tree_choices:
tree_buffers = model.tree_buffers
else:
tree_buffers = generate_tree_buffers(
tree_choices, device=model.base_model.model.layers[-1].self_attn.q_proj.weight.device
)
tree_buffers["retrieve_indices_head"] = tree_buffers["retrieve_indices"].to(
model.base_model.lm_head.weight.device)
model.tree_buffers = tree_buffers
model.tree_choices = tree_choices
# Initialize the past key and value states
if hasattr(model, "past_key_values"):
past_key_values = model.past_key_values
past_key_values_data = model.past_key_values_data
current_length_data = model.current_length_data
# Reset the past key and value states
current_length_data.zero_()
else:
(
past_key_values,
past_key_values_data,
current_length_data,
) = initialize_past_key_values(model.base_model)
model.past_key_values = past_key_values
model.past_key_values_data = past_key_values_data
model.current_length_data = current_length_data
input_len = input_ids.shape[1]
reset_tree_mode(model)
tree_logits, logits, hidden_state, sample_token = initialize_tree(
input_ids, model, tree_buffers["tree_attn_mask"], past_key_values, logits_processor
)
new_token = 0
for idx in range(max_steps):
candidates, cart_candidates_prob, tree_candidates = generate_candidates(
tree_logits,
tree_buffers["tree_indices"],
tree_buffers["retrieve_indices"],
sample_token,
logits_processor
)
logits, hidden_state_new, outputs = tree_decoding(
model,
tree_candidates,
past_key_values,
tree_buffers["tree_position_ids"],
input_ids,
tree_buffers["retrieve_indices_head"],
)
best_candidate, accept_length, sample_p = evaluate_posterior(
logits, candidates, logits_processor, cart_candidates_prob, tree_logits[2], tree_buffers["p_indices"],
tree_candidates, tree_buffers["b_indices"]
)
input_ids, tree_logits, new_token, hidden_state, sample_token = update_inference_inputs(
input_ids,
candidates,
best_candidate,
accept_length,
tree_buffers["retrieve_indices"],
logits_processor,
logits,
tree_logits,
new_token,
past_key_values_data,
current_length_data,
model,
hidden_state,
hidden_state_new,
sample_p
)
if tokenizer.eos_token_id in input_ids[0, input_len:].tolist():
break
if new_token > 1024:
break
if input_ids.shape[1] > 1960:
break
return input_ids, new_token, idx
def run_eval(
base_model_path,
ea_model_path,
model_id,
question_file,
question_begin,
question_end,
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
num_gpus_total,
max_gpu_memory,
temperature,
tree_choices,
):
questions = load_questions(question_file, question_begin, question_end)
# random shuffle the questions to balance the loading
# random.shuffle(questions)
shuffled_ids = [q["question_id"] for q in questions]
# with open(f"data/{args.bench_name}/model_ids/{args.model_id}.shuffled_ids", "w") as fout:
# json.dump(shuffled_ids, fout)
# Split the question file into `num_gpus` files
assert num_gpus_total % num_gpus_per_model == 0
use_ray = num_gpus_total // num_gpus_per_model > 1
if use_ray:
get_answers_func = ray.remote(num_gpus=num_gpus_per_model)(
get_model_answers
).remote
else:
get_answers_func = get_model_answers
chunk_size = len(questions) // (num_gpus_total // num_gpus_per_model) # // 2
ans_handles = []
for i in range(0, len(questions), chunk_size):
ans_handles.append(
get_answers_func(
base_model_path,
ea_model_path,
model_id,
questions[i: i + chunk_size],
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
max_gpu_memory,
temperature,
tree_choices,
)
)
if use_ray:
ray.get(ans_handles)
@torch.inference_mode()
def get_model_answers(
base_model_path,
ea_model_path,
model_id,
questions,
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
max_gpu_memory,
temperature,
tree_choices,
):
# temperature = 0.0
model = EaModel.from_pretrained(
base_model_path=base_model_path,
ea_model_path=ea_model_path,
Type="Mixtral",
torch_dtype=torch.float16,
low_cpu_mem_usage=True,
# load_in_8bit=True,
device_map="auto"
)
tokenizer = model.get_tokenizer()
if temperature > 1e-5:
logits_processor = prepare_logits_processor(temperature=temperature)
else:
logits_processor = None
model.eval()
print('Check model training state:', model.training)
cuda_visible_devices = os.environ.get('CUDA_VISIBLE_DEVICES')
print('CUDA VISIBLE DEVICES:', cuda_visible_devices)
question = questions[0]
# warmup
for _ in range(3):
torch.manual_seed(0)
conv = get_conversation_template("llama-2-chat")
conv.system_message = ''
conv.sep2 = "</s>"
turns = []
idxs = []
new_tokens = []
wall_time = []
for j in range(len(question["turns"])):
qs = question["turns"][j]
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer([prompt]).input_ids
# try:
torch.cuda.synchronize()
start_time = time.time()
output_ids, new_token, idx = ea_forward(
torch.as_tensor(input_ids).cuda(),
model,
tokenizer,
tree_choices,
logits_processor,
)
torch.cuda.synchronize()
total_time = time.time() - start_time
output_ids = output_ids[0][len(input_ids[0]):]
# be consistent with the template's stop_token_ids
if conv.stop_token_ids:
stop_token_ids_index = [
i
for i, id in enumerate(output_ids)
if id in conv.stop_token_ids
]
if len(stop_token_ids_index) > 0:
output_ids = output_ids[: stop_token_ids_index[0]]
output = tokenizer.decode(
output_ids,
spaces_between_special_tokens=False,
)
conv.stop_str = "</s>"
if conv.stop_str and output.find(conv.stop_str) > 0:
output = output[: output.find(conv.stop_str)]
for special_token in tokenizer.special_tokens_map.values():
if isinstance(special_token, list):
for special_tok in special_token:
output = output.replace(special_tok, "")
else:
output = output.replace(special_token, "")
output = output.strip()
if conv.name == "xgen" and output.startswith("Assistant:"):
output = output.replace("Assistant:", "", 1).strip()
turns.append(output)
idxs.append(int(idx))
new_tokens.append(int(new_token))
wall_time.append(total_time)
conv.messages[-1][-1] = output
print('Warmup done')
# questions=questions[6:]
for question in tqdm(questions):
choices = []
for i in range(num_choices):
torch.manual_seed(i)
conv = get_conversation_template("llama-2-chat")
conv.system_message = ''
conv.sep2 = "</s>"
turns = []
idxs = []
new_tokens = []
wall_time = []
for j in range(len(question["turns"])):
qs = question["turns"][j]
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = tokenizer([prompt]).input_ids
try:
torch.cuda.synchronize()
start_time = time.time()
output_ids, new_token, idx = ea_forward(
torch.as_tensor(input_ids).cuda(),
model,
tokenizer,
tree_choices,
logits_processor,
)
torch.cuda.synchronize()
total_time = time.time() - start_time
output_ids = output_ids[0][len(input_ids[0]):]
if conv.stop_token_ids:
stop_token_ids_index = [
i
for i, id in enumerate(output_ids)
if id in conv.stop_token_ids
]
if len(stop_token_ids_index) > 0:
output_ids = output_ids[: stop_token_ids_index[0]]
output = tokenizer.decode(
output_ids,
spaces_between_special_tokens=False,
)
if conv.stop_str and output.find(conv.stop_str) > 0:
output = output[: output.find(conv.stop_str)]
for special_token in tokenizer.special_tokens_map.values():
if isinstance(special_token, list):
for special_tok in special_token:
output = output.replace(special_tok, "")
else:
output = output.replace(special_token, "")
output = output.strip()
if conv.name == "xgen" and output.startswith("Assistant:"):
output = output.replace("Assistant:", "", 1).strip()
except RuntimeError as e:
print("ERROR question ID: ", question["question_id"])
output = "ERROR"
turns.append(output)
idxs.append(int(idx))
new_tokens.append(int(new_token))
wall_time.append(total_time)
conv.messages[-1][-1] = output
# torch.cuda.empty_cache()
choices.append({"index": i, "turns": turns, "idxs": idxs, "new_tokens": new_tokens, "wall_time": wall_time})
# Dump answers
os.makedirs(os.path.dirname(answer_file), exist_ok=True)
with open(os.path.expanduser(answer_file), "a") as fout:
ans_json = {
"question_id": question["question_id"],
"answer_id": shortuuid.uuid(),
"model_id": model_id,
"choices": choices,
"tstamp": time.time(),
}
fout.write(json.dumps(ans_json) + "\n")
def reorg_answer_file(answer_file):
"""Sort by question id and de-duplication"""
answers = {}
with open(answer_file, "r") as fin:
for l in fin:
qid = json.loads(l)["question_id"]
answers[qid] = l
qids = sorted(list(answers.keys()))
with open(answer_file, "w") as fout:
for qid in qids:
fout.write(answers[qid])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--ea-model-path",
type=str,
default="/home/lyh/weights/hf/eagle/mix/8x7B/",
help="The path to the weights. This can be a local folder or a Hugging Face repo ID.",
)
parser.add_argument("--base-model-path", type=str, default="/home/lyh/weights/hf/Mixtral-Instruct/8x7B/",
help="1")
parser.add_argument(
"--load-in-8bit", action="store_false", help="Use 8-bit quantization"
)
parser.add_argument("--model-id", type=str, default="ess-llama-2-chat-70b-fp16")
parser.add_argument(
"--bench-name",
type=str,
default="mt_bench",
help="The name of the benchmark question set.",
)
parser.add_argument(
"--question-begin",
type=int,
help="A debug option. The begin index of questions.",
)
parser.add_argument(
"--question-end", type=int, help="A debug option. The end index of questions."
)
parser.add_argument("--answer-file", type=str, help="The output answer file.")
parser.add_argument(
"--max-new-token",
type=int,
default=1024,
help="The maximum number of new generated tokens.",
)
parser.add_argument(
"--num-choices",
type=int,
default=1,
help="How many completion choices to generate.",
)
parser.add_argument(
"--num-gpus-per-model",
type=int,
default=1,
help="The number of GPUs per model.",
)
parser.add_argument(
"--num-gpus-total", type=int, default=1, help="The total number of GPUs."
)
parser.add_argument(
"--max-gpu-memory",
type=str,
help="Maxmum GPU memory used for model weights per GPU.",
)
parser.add_argument(
"--temperature",
type=float,
default=1.0,
)
parser.add_argument(
"--tree-choices",
type=str,
default="mc_sim_7b_63",
)
args = parser.parse_args()
args.model_id = args.model_id + "-temperature-" + str(args.temperature)
args.tree_choices = eval(args.tree_choices)
if args.num_gpus_total // args.num_gpus_per_model > 1:
import ray
ray.init()
question_file = f"data/{args.bench_name}/question.jsonl"
if args.answer_file:
answer_file = args.answer_file
else:
answer_file = f"data/{args.bench_name}/model_answer/{args.model_id}.jsonl"
print(f"Output to {answer_file}")
run_eval(
args.base_model_path,
args.ea_model_path,
args.model_id,
question_file,
args.question_begin,
args.question_end,
answer_file,
args.max_new_token,
args.num_choices,
args.num_gpus_per_model,
args.num_gpus_total,
args.max_gpu_memory,
args.temperature,
args.tree_choices,
)
reorg_answer_file(answer_file)