-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathrun_train.py
executable file
·353 lines (306 loc) · 13.9 KB
/
run_train.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
#!/usr/bin/env python
# coding=utf-8
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
# Copyright (C) 2024. Huawei Technologies Co., Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# https://github.com/huggingface/alignment-handbook/blob/main/scripts/run_dpo.py
import logging
import random
import sys
import os
import torch
import transformers
import datasets
import re
from transformers import AutoModelForCausalLM, AutoConfig, set_seed, TrainingArguments
import wandb
from src.alignment import (
DataArguments,
H4ArgumentParser,
ModelArguments,
apply_chat_template,
decontaminate_humaneval,
get_checkpoint,
get_datasets,
get_kbit_device_map,
get_peft_config,
get_quantization_config,
get_tokenizer,
is_adapter_model,
)
from transformers.trainer_utils import get_last_checkpoint
from peft import PeftConfig, PeftModel
from src.trainers import (
MAPOConfig, MAPOTrainer,
SparseConfig, SparseTrainer,
)
from src.alignment.utils import get_mapo_model, get_sparse_pipeline
logger = logging.getLogger(__name__)
def main():
if '--pref_optim=mapo' in sys.argv:
parser = H4ArgumentParser((ModelArguments, DataArguments, MAPOConfig))
elif '--pref_optim=sparse' in sys.argv:
parser = H4ArgumentParser((ModelArguments, DataArguments, SparseConfig))
else:
raise ValueError('Invalid preference optimization flag!')
model_args, data_args, training_args = parser.parse()
training_args.logging_dir = os.path.join(training_args.output_dir, 'runs', training_args.logging_dir.split('/')[-1])
#######
# Setup
#######
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
log_level = training_args.get_process_log_level()
logger.setLevel(log_level)
transformers.utils.logging.set_verbosity(log_level)
transformers.utils.logging.enable_default_handler()
transformers.utils.logging.enable_explicit_format()
# Log on each process the small summary:
logger.info(f"Model parameters {model_args}")
logger.info(f"Data parameters {data_args}")
logger.info(f"Training/evaluation parameters {training_args}")
# Set seed for reproducibility
set_seed(training_args.seed)
#####################################
# Load tokenizer and process datasets
#####################################
if 'mbpp_new' not in list(data_args.dataset_mixer.keys())[0]:
data_args.truncation_side = "left" # Truncate from left to ensure we don't lose labels in final turn
tokenizer = get_tokenizer(model_args, data_args)
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = 'left' # to prevent errors with FA
tokenizer.truncation_side = 'left' # to prevent cutting off last generation
tokenizer.chat_template = "{% for message in messages %}\n{% if message['role'] == 'user' %}\n{{ '<|user|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'system' %}\n{{ '<|system|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'assistant' %}\n{{ '<|assistant|>\n' + message['content'] + eos_token }}\n{% endif %}\n{% if loop.last and add_generation_prompt %}\n{{ '<|assistant|>' }}\n{% endif %}\n{% endfor %}"
else:
data_args.truncation_side = "left" # Truncate from left to ensure we don't lose labels in final turn
tokenizer = get_tokenizer(model_args, data_args)
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = 'left' # to prevent errors with FA
tokenizer.truncation_side = 'left' # to prevent cutting off last generation
tokenizer.bos_token = None
tokenizer.bos_token_id = None
###############
# Load datasets
###############
def filter_dataset(examples):
query = examples['prompt']
prompt_length = tokenizer.apply_chat_template([{'content': query, 'role': 'user'}], tokenize=True, add_generation_prompt=True, return_tensors='pt').size(-1)
if model_args.pref_optim == 'sft':
return prompt_length < 1024
else:
return all([prompt_length < 1024,
examples['chosen'] != examples['rejected'],
examples['chosen'][-1]['content'] != "",
examples['rejected'][-1]['content'] != "", ])
raw_datasets = get_datasets(
data_args,
splits=data_args.dataset_splits,
configs=data_args.dataset_configs,
columns_to_keep=["messages", "chosen", "rejected", "prompt", "completion", "label"],
)
logger.info(raw_datasets)
# Filter based on prompt length
num_raw_train_samples = len(raw_datasets["train"])
if 'mbpp_new' not in list(data_args.dataset_mixer.keys())[0]:
raw_datasets = raw_datasets.filter(filter_dataset)
num_filtered_train_samples = num_raw_train_samples - len(raw_datasets["train"])
logger.info(
f"Filtered based on prompt length + none {num_filtered_train_samples} ({num_filtered_train_samples/num_raw_train_samples * 100:.2f}%) samples from the training set."
)
logger.info(raw_datasets)
logger.info(
f"Training on the following splits: {[split + ' : ' + str(dset.num_rows) for split, dset in raw_datasets.items()]}"
)
column_names = list(raw_datasets["train"].features)
#####################
# Apply chat template
#####################
if 'mbpp_new' not in list(data_args.dataset_mixer.keys())[0]:
raw_datasets = raw_datasets.map(
apply_chat_template,
fn_kwargs={
"tokenizer": tokenizer,
"task": model_args.pref_optim,
"auto_insert_empty_system_msg": data_args.auto_insert_empty_system_msg,
},
num_proc=data_args.preprocessing_num_workers,
remove_columns=column_names,
desc="Formatting comparisons with prompt template",
)
else:
raw_datasets = raw_datasets.rename_column('prompt', "text_prompt")
raw_datasets = raw_datasets.rename_column('chosen', "text_chosen")
raw_datasets = raw_datasets.rename_column('rejected', "text_rejected")
##########################
# Decontaminate benchmarks
##########################
num_raw_train_samples = len(raw_datasets["train"])
raw_datasets['train'] = raw_datasets['train'].filter(
decontaminate_humaneval,
fn_kwargs={"text_column": "text_chosen"} if model_args.pref_optim != 'sft' else {"text_column": "text"},
batched=True,
batch_size=10_000,
num_proc=1,
desc="Decontaminating HumanEval samples",
)
num_filtered_train_samples = num_raw_train_samples - len(raw_datasets["train"])
logger.info(
f"Decontaminated {num_filtered_train_samples} ({num_filtered_train_samples/num_raw_train_samples * 100:.2f}%) samples from the training set."
)
logger.info(raw_datasets)
if model_args.pref_optim != "sft":
# Replace column names with what TRL needs, text_chosen -> chosen and text_rejected -> rejected
raw_datasets = raw_datasets.rename_columns(
{"text_prompt": "prompt", "text_chosen": "chosen", "text_rejected": "rejected"}
)
# Log a few random samples from the training set:
for index in random.sample(range(len(raw_datasets["train"])), 3):
logger.info(f"Prompt sample {index} of the raw training set:\n\n{raw_datasets['train'][index]['prompt']}")
logger.info(f"Chosen sample {index} of the raw training set:\n\n{raw_datasets['train'][index]['chosen']}")
logger.info(f"Rejected sample {index} of the raw training set:\n\n{raw_datasets['train'][index]['rejected']}")
else:
for index in random.sample(range(len(raw_datasets["train"])), 3):
logger.info(f"Sample {index} of the raw training set:\n\n{raw_datasets['train'][index]['text']}")
########
# MODEL
########
torch_dtype = (
model_args.torch_dtype if model_args.torch_dtype in ["auto", None] else getattr(torch, model_args.torch_dtype)
)
quantization_config = get_quantization_config(model_args)
model_kwargs = dict(
revision=model_args.model_revision,
trust_remote_code=model_args.trust_remote_code,
use_flash_attention_2=model_args.use_flash_attention_2,
torch_dtype=torch_dtype,
use_cache=False if training_args.gradient_checkpointing else True,
device_map=get_kbit_device_map() if quantization_config is not None else None,
quantization_config=quantization_config,
)
config = AutoConfig.from_pretrained(
model_args.model_name_or_path,
revision=model_args.model_revision,
trust_remote_code=model_args.trust_remote_code,
)
model = None
if model_args.pref_optim == "sparse":
model = get_sparse_pipeline(model_args.model_name_or_path, config, training_args, model_kwargs)
elif model_args.pref_optim == "mapo":
model = get_mapo_model(model_args.model_name_or_path, config.architectures, model_kwargs)
peft_config = get_peft_config(model_args)
ref_model = None
if peft_config is None:
if model_args.pref_optim != "mapo":
ref_model = AutoModelForCausalLM.from_pretrained(model_args.model_name_or_path, **model_kwargs)
else:
ref_model = get_mapo_model(model_args.model_name_or_path, config.architectures, model_kwargs)
ref_model_kwargs = model_kwargs
else:
logger.info(peft_config)
ref_model = None
ref_model_kwargs = None
#########################
# Instantiate DPO trainer
#########################
logger.info(model)
if model_args.pref_optim == "mapo":
logger.info(f'********** Using {model_args.pref_optim.upper()} loss with beta = {training_args.beta} **********')
trainer = MAPOTrainer(
model,
ref_model,
args=training_args,
train_dataset=raw_datasets["train"],
eval_dataset=raw_datasets["test"],
tokenizer=tokenizer,
peft_config=get_peft_config(model_args),
)
elif model_args.pref_optim == "sparse":
logger.info(f'********** Using {model_args.pref_optim.upper()} loss beta = {training_args.beta} **********')
trainer = SparseTrainer(
model,
ref_model,
args=training_args,
train_dataset=raw_datasets["train"],
eval_dataset=raw_datasets["test"],
tokenizer=tokenizer,
peft_config=get_peft_config(model_args),
)
logger.info("[MODEL AFTER TRAINER] check...")
logger.info(trainer.model)
###############
# Training loop
###############
# Detecting last checkpoint.
checkpoint = None
if training_args.resume_from_checkpoint is not None:
checkpoint = training_args.resume_from_checkpoint
else:
last_checkpoint = None
if os.path.isdir(training_args.output_dir):
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
checkpoint = last_checkpoint
#
train_result = trainer.train(resume_from_checkpoint=checkpoint)
metrics = train_result.metrics
metrics["train_samples"] = len(raw_datasets["train"])
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
logger.info("*** Training complete ***")
##################################
# Save model and create model card
##################################
logger.info("*** Save model ***")
trainer.save_model(training_args.output_dir)
logger.info(f"Model saved to {training_args.output_dir}")
# Save everything else on main process
kwargs = {
"finetuned_from": model_args.model_name_or_path,
"dataset": list(data_args.dataset_mixer.keys()),
"dataset_tags": list(data_args.dataset_mixer.keys()),
"tags": [model_args.pref_optim],
}
if trainer.accelerator.is_main_process:
trainer.create_model_card(**kwargs)
# Restore k,v cache for fast inference
trainer.model.config.use_cache = True
trainer.model.config.save_pretrained(training_args.output_dir)
##########
# Evaluate
##########
if training_args.do_eval:
logger.info("*** Evaluate ***")
metrics = trainer.evaluate()
metrics["eval_samples"] = len(raw_datasets["test"])
metrics = {k:v for k,v in metrics.items() if "_hist" not in k}
trainer.log_metrics("eval", metrics)
trainer.save_metrics("eval", metrics)
logger.info("*** Final evaluation complete! ***")
if __name__ == "__main__":
main()