-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_task_vector_mt_bench.py
418 lines (372 loc) · 12.6 KB
/
merge_task_vector_mt_bench.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
import argparse
import copy
import gc
import os
import re
import shutil
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import optuna
import pandas as pd
import torch
from tqdm import tqdm
from transformers import AutoModelForCausalLM, AutoTokenizer
from FastChat.fastchat.llm_judge.gen_model_answer import (
run_eval_from_model_and_tokenizer,
)
from fastchat.llm_judge.common import (
NEED_REF_CATS,
check_data,
load_judge_prompts,
load_model_answers,
load_questions,
play_a_match_single,
)
from fastchat.llm_judge.gen_judgment import make_judge_single, make_match_single
def create_responses(trial_number, model):
model_id = "temp_model_mistral_" + str(trial_number)
answer_file = (
f"FastChat/fastchat/llm_judge/data/{bench_name}/model_answer/{model_id}.jsonl"
)
run_eval_from_model_and_tokenizer(
model=model,
tokenizer=tokenizer,
model_id=model_id,
question_file=question_file,
question_begin=None,
question_end=None,
answer_file=answer_file,
max_new_token=512,
num_choices=1,
num_gpus_per_model=1,
num_gpus_total=1,
)
def get_evaluate_result(trial_number):
score_df = pd.read_json(
f"FastChat/fastchat/llm_judge/data/{bench_name}/model_judgment/{judge_model_name}_single.jsonl",
orient="records",
lines=True,
)
model_id = "temp_model_mistral_" + str(trial_number)
score_df = score_df[score_df["model"] == model_id]
score = score_df["score"].mean()
return score
def evaluate_and_save_to_json(
judge_model, judge_tokenizer, models, mode="single", baseline_model=None, parallel=1
):
# Load questions
questions = load_questions(question_file, None, None)
# Load answers
model_answers = load_model_answers(answer_dir)
ref_answers = load_model_answers(ref_answer_dir)
# Load judge
judge_prompts = load_judge_prompts(judge_file)
if mode == "single":
judges = make_judge_single(judge_model_name, judge_prompts)
play_a_match_func = play_a_match_single
output_file = f"FastChat/fastchat/llm_judge/data/{bench_name}/model_judgment/{judge_model_name}_single.jsonl"
make_match_func = make_match_single
baseline_model = None
else:
# ここは未実装です。
raise ValueError
check_data(questions, model_answers, ref_answers, models, judges)
question_math = [q for q in questions if q["category"] in NEED_REF_CATS]
question_default = [q for q in questions if q["category"] not in NEED_REF_CATS]
# Make matches
matches = []
matches += make_match_func(
question_default, models, model_answers, judges["default"], baseline_model
)
matches += make_match_func(
question_math,
models,
model_answers,
judges["math"],
baseline_model,
ref_answers,
)
matches += make_match_func(
question_default,
models,
model_answers,
judges["default-mt"],
baseline_model,
multi_turn=True,
)
matches += make_match_func(
question_math,
models,
model_answers,
judges["math-mt"],
baseline_model,
ref_answers,
multi_turn=True,
)
match_stat = {}
match_stat["bench_name"] = bench_name
match_stat["mode"] = mode
match_stat["judge"] = judge_model_name
match_stat["baseline"] = baseline_model
match_stat["model_list"] = models
match_stat["total_num_questions"] = len(questions)
match_stat["total_num_matches"] = len(matches)
match_stat["output_path"] = output_file
# Play matches
if parallel == 1:
for match in tqdm(matches):
play_a_match_func(
match,
output_file=output_file,
judge_model=judge_model,
judge_tokenizer=judge_tokenizer,
)
else:
def play_a_match_wrapper(match):
play_a_match_func(match, output_file=output_file)
np.random.seed(0)
np.random.shuffle(matches)
with ThreadPoolExecutor(parallel) as executor:
for match in tqdm(
executor.map(play_a_match_wrapper, matches), total=len(matches)
):
pass
# 評価関数
def evaluate(trial_number):
model_id = "temp_model_mistral_" + str(trial_number)
models = [model_id]
evaluate_and_save_to_json(
judge_model,
judge_tokenizer,
models,
mode="single",
baseline_model=None,
parallel=parallel,
)
score = get_evaluate_result(trial_number)
return score
def update_model_parameters(
model, task_vectors, weights, num_params, optimize_mode, unique_params=None
):
if optimize_mode == "all":
for i, (k, v) in enumerate(model.state_dict().items()):
new_v = v + (weights[i] * task_vectors[k].to(v.device))
v.copy_(new_v)
elif optimize_mode == "layer":
for k, v in model.state_dict().items():
if k == "model.embed_tokens.weight":
new_v = v + (weights[0] * task_vectors[k].to(v.device))
v.copy_(new_v)
elif k == "model.norm.weight":
new_v = v + (weights[num_params - 2] * task_vectors[k].to(v.device))
v.copy_(new_v)
elif k == "lm_head.weight":
new_v = v + (weights[num_params - 1] * task_vectors[k].to(v.device))
v.copy_(new_v)
else:
layer_index = int(re.findall(r"\d+", k)[0])
new_v = v + (weights[layer_index + 1] * task_vectors[k].to(v.device))
v.copy_(new_v)
elif optimize_mode == "parameter":
for i, (k, v) in enumerate(model.state_dict().items()):
param_name = k.split(".weight")[0]
param_name = param_name.replace("model.", "")
param_name = (
param_name.split(".")[-1] if "layers" in param_name else param_name
)
param_index = unique_params.index(param_name)
new_v = v + (weights[param_index] * task_vectors[k].to(v.device))
v.copy_(new_v)
parser = argparse.ArgumentParser()
parser.add_argument(
"--target_model", type=str, required=True, help="マージ対象のモデル"
)
parser.add_argument(
"--base_model", type=str, required=True, help="ベクトル計算の元となるベースモデル"
)
parser.add_argument(
"--tuned_model",
type=str,
required=True,
help="ベクトル計算の元となるチューニング済みモデル",
)
parser.add_argument("--bench_name", type=str, required=True, help="ベンチマーク名")
parser.add_argument(
"--judge_model_name", type=str, default="gpt-4", help="評価モデル名"
)
parser.add_argument(
"--optimize_mode",
type=str,
choices=["all", "layer", "parameter"],
default="layer",
help="探索のモード",
)
parser.add_argument(
"--n_trials", type=int, default=30, help="Optunaによる探索の試行回数"
)
parser.add_argument(
"--cache_dir",
type=str,
default="./models",
help="モデルやトークナイザーをキャッシュするディレクトリのパス",
)
parser.add_argument(
"--judge_file",
type=str,
default="FastChat/fastchat/llm_judge/data/judge_prompts.jsonl",
help="評価用のプロンプトが含まれるファイルのパス",
)
parser.add_argument("--parallel", type=int, default=1, help="評価を並列で実行する数")
parser.add_argument(
"--optuna_sampler",
type=str,
choices=["CMA-ES", "TPE"],
default="TPE",
help="Optunaのサンプラー",
)
parser.add_argument(
"--weight_min", type=float, default=0, help="加算割合の探索範囲の最小値"
)
parser.add_argument(
"--weight_max", type=float, default=2, help="加算割合の探索範囲の最大値"
)
parser.add_argument(
"--output_dir", type=str, default="./merged_model", help="最終モデルの保存先"
)
parser.add_argument(
"--optuna_seed", type=int, default=42, help="Optunaのサンプラーのシード値"
)
args = parser.parse_args()
target_model = args.target_model
base_model = args.base_model
tuned_model = args.tuned_model
bench_name = args.bench_name
judge_model_name = args.judge_model_name
optimize_mode = args.optimize_mode
n_trials = args.n_trials
cache_dir = args.cache_dir
judge_file = args.judge_file
parallel = args.parallel
optuna_sampler = args.optuna_sampler
weight_min = args.weight_min
weight_max = args.weight_max
output_dir = args.output_dir
optuna_seed = args.optuna_seed
base_model = AutoModelForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.bfloat16,
device_map="cpu",
cache_dir=cache_dir,
)
tuned_model = AutoModelForCausalLM.from_pretrained(
tuned_model,
torch_dtype=torch.bfloat16,
device_map="cpu",
cache_dir=cache_dir,
)
task_vectors = {
k: tuned_model.state_dict()[k] - base_model.state_dict()[k]
for k in base_model.state_dict()
}
# 不要になるので削除し、メモリを解放
del base_model
del tuned_model
gc.collect()
# モデルのロード
model = AutoModelForCausalLM.from_pretrained(
target_model,
torch_dtype=torch.bfloat16,
device_map="cpu",
cache_dir=cache_dir,
)
tokenizer = AutoTokenizer.from_pretrained(
target_model,
cache_dir=cache_dir,
)
unique_params = None
if optimize_mode == "all":
num_params = len(model.state_dict().items())
elif optimize_mode == "layer":
num_params = model.config.num_hidden_layers + 3 # for mistral
elif optimize_mode == "parameter":
param_list = []
for k, v in model.state_dict().items():
param_name = k.split(".weight")[0]
param_name = param_name.replace("model.", "")
param_name = param_name.split(".")[-1] if "layers" in param_name else param_name
param_list.append(param_name)
unique_params = list(set(param_list))
num_params = len(unique_params)
if judge_model_name == "cohere":
judge_model = AutoModelForCausalLM.from_pretrained(
"CohereForAI/c4ai-command-r-plus-4bit",
cache_dir="./models",
device_map="cuda",
)
judge_tokenizer = AutoTokenizer.from_pretrained(
"CohereForAI/c4ai-command-r-plus",
cache_dir="./models",
device_map="cuda",
)
else:
judge_model = None
judge_tokenizer = None
question_file = f"FastChat/fastchat/llm_judge/data/{bench_name}/question.jsonl"
answer_dir = f"FastChat/fastchat/llm_judge/data/{bench_name}/model_answer"
ref_answer_dir = f"FastChat/fastchat/llm_judge/data/{bench_name}/reference_answer"
# 最適化する関数
def objective(trial):
model.load_state_dict(original_model_state)
weights = [
trial.suggest_float(f"weight_{i}", weight_min, weight_max)
for i in range(num_params)
]
update_model_parameters(
model,
task_vectors,
weights,
num_params,
optimize_mode,
unique_params,
)
# マージしたモデルでベンチマークのプロンプトに対し推論
# 高速化のため一度GPUに移動
model.to("cuda")
create_responses(trial.number, model)
model.to("cpu")
# モデルの評価
score = evaluate(trial.number) # この関数はユーザーが定義する必要があります。
return score
# 最適化プロセスの実行
if optuna_sampler == "CMA-ES":
sampler = optuna.samplers.CmaEsSampler(seed=optuna_seed)
elif optuna_sampler == "TPE":
sampler = optuna.samplers.TPESampler(seed=optuna_seed)
study = optuna.create_study(direction="maximize", sampler=sampler)
original_model_state = copy.deepcopy(model.state_dict())
study.optimize(objective, n_trials=n_trials, show_progress_bar=True)
# 最適化された比率を取得
optimal_weights = [study.best_params[f"weight_{i}"] for i in range(num_params)]
# この時点でのmodelは最後のtrialの加算の影響を受けているので、一度最初の状態に戻す
model.load_state_dict(original_model_state)
# 最適な比率でモデルをマージ
update_model_parameters(
model,
task_vectors,
optimal_weights,
num_params,
optimize_mode,
unique_params,
)
# メモリ不足で保存できない場合があるため、task_vectorsとoriginal_model_stateを削除しておく
del task_vectors
del original_model_state
gc.collect()
# マージされたモデルを保存
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
eval_output = f"FastChat/fastchat/llm_judge/data/{bench_name}/model_judgment/{judge_model_name}_single.jsonl"
if os.path.exists(eval_output):
os.remove(eval_output)
if os.path.exists(answer_dir) and os.path.isdir(answer_dir):
shutil.rmtree(answer_dir)