-
Notifications
You must be signed in to change notification settings - Fork 0
/
sft_lora.py
295 lines (250 loc) · 12.3 KB
/
sft_lora.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
import os
import torch
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
DataCollatorForLanguageModeling,
set_seed
)
from peft import LoraConfig, AutoPeftModelForCausalLM
from accelerate import Accelerator
from datasets import load_dataset, Dataset
import huggingface_hub
from typing import Literal
import re
import random
from trl import SFTTrainer
import argparse
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument("--hf_token", type=str, help="Required to upload models to hub.")
parser.add_argument("--model_name", type=str, default="mistralai/Mistral-7B-v0.1")
parser.add_argument("--dataset_name", type=str, default="ryan0712/ultra_no_robots")
parser.add_argument("--train_split", type=str, default="train")
parser.add_argument("--test_split", type=str, default="test")
parser.add_argument("--seq_length", type=int, default=2048)
parser.add_argument("--num_workers", type=int, default=None)
parser.add_argument("--do_sample", type=bool, default=False, help="Sample the dataset.")
parser.add_argument("--sample_size", type=int, default=None)
parser.add_argument("--num_epochs", type=int, default=1)
parser.add_argument("--logging_steps", type=int, default=100)
parser.add_argument("--save_strategy", type=str, default="epoch", help="You can choose the strategy of saving model.")
parser.add_argument("--save_steps", type=int, default=100)
parser.add_argument("--gradient_accumulation_steps", type=int, default=2)
parser.add_argument("--gradient_checkpointing", type=bool, default=True)
parser.add_argument("--per_device_train_batch_size", type=int, default=32)
parser.add_argument("--per_device_eval_batch_size", type=int, default=16)
parser.add_argument("--group_by_length", type=bool, default=False)
parser.add_argument("--packing", type=bool, default=False)
parser.add_argument("--use_flash_attention", type=bool, default=True)
parser.add_argument("--bf16", type=bool, default=True)
parser.add_argument("--lora_alpha", type=int, default=16)
parser.add_argument("--lora_dropout", type=float, default=0.05)
parser.add_argument("--lora_r", type=int, default=8)
parser.add_argument("--learning_rate", type=float, default=2e-5)
parser.add_argument("--lr_scheduler_type", type=str, default="cosine")
parser.add_argument("--warmup_ratio", type=float, default=0.1)
parser.add_argument("--weight_decay", type=float, default=0)
parser.add_argument("--wandb_project", type=str)
parser.add_argument("--wandb_run_name", type=str)
parser.add_argument(
"--output_dir",
type=str,
default="SFT/final_checkpoint"
)
parser.add_argument(
"--hf_hub_path",
type=str,
help="The hub path to upload the model"
)
return parser.parse_args()
def apply_chat_template(
example, tokenizer, task: Literal["sft", "generation", "rm", "dpo"] = "sft", assistant_prefix="<|assistant|>\n"
):
def _strip_prefix(s, pattern):
# Use re.escape to escape any special characters in the pattern
return re.sub(f"^{re.escape(pattern)}", "", s)
if task in ["sft", "generation"]:
messages = example["messages"]
# We add an empty system message if there is none
if messages[0]["role"] != "system":
messages.insert(0, {"role": "system", "content": ""})
example["text"] = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True if task == "generation" else False
)
elif task == "rm":
if all(k in example.keys() for k in ("chosen", "rejected")):
chosen_messages = example["chosen"]
rejected_messages = example["rejected"]
# We add an empty system message if there is none
if chosen_messages[0]["role"] != "system":
chosen_messages.insert(0, {"role": "system", "content": ""})
if rejected_messages[0]["role"] != "system":
rejected_messages.insert(0, {"role": "system", "content": ""})
example["text_chosen"] = tokenizer.apply_chat_template(chosen_messages, tokenize=False)
example["text_rejected"] = tokenizer.apply_chat_template(rejected_messages, tokenize=False)
else:
raise ValueError(
f"Could not format example as dialogue for `rm` task! Require `[chosen, rejected]` keys but found {list(example.keys())}"
)
elif task == "dpo":
if all(k in example.keys() for k in ("chosen", "rejected")):
# Compared to reward modeling, we filter out the prompt, so the text is everything after the last assistant token
prompt_messages = [[msg for msg in example["chosen"] if msg["role"] == "user"][0]]
# Insert system message
if example["chosen"][0]["role"] != "system":
prompt_messages.insert(0, {"role": "system", "content": ""})
else:
prompt_messages.insert(0, example["chosen"][0])
# TODO: handle case where chosen/rejected also have system messages
chosen_messages = example["chosen"][1:]
rejected_messages = example["rejected"][1:]
example["text_chosen"] = tokenizer.apply_chat_template(chosen_messages, tokenize=False)
example["text_rejected"] = tokenizer.apply_chat_template(rejected_messages, tokenize=False)
example["text_prompt"] = tokenizer.apply_chat_template(
prompt_messages, tokenize=False, add_generation_prompt=True
)
example["text_chosen"] = _strip_prefix(example["text_chosen"], assistant_prefix)
example["text_rejected"] = _strip_prefix(example["text_rejected"], assistant_prefix)
else:
raise ValueError(
f"Could not format example as dialogue for `dpo` task! Require `[chosen, rejected]` keys but found {list(example.keys())}"
)
return example
def apply_template(dataset, tokenizer):
formatted_dataset = []
for data in dataset:
messages = data["messages"]
# We add an empty system message if there is none
if messages[0]["role"] != "system":
messages.insert(0, {"role": "system", "content": ""})
data["text"] = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
formatted_dataset.append(data)
return Dataset.from_list(formatted_dataset)
def dataset_sampling(dataset, sample_size):
random_indices = random.sample(range(len(dataset)), sample_size)
random_samples = dataset.select(random_indices)
return random_samples
def create_datasets(dataset_name, split):
dataset = load_dataset(
dataset_name,
split=split,
)
if args.do_sample:
dataset = dataset_sampling(dataset, args.sample_size)
return dataset
if __name__ == "__main__":
set_seed(42)
args = arg_parse()
# device_map = "auto"
# world_size = int(os.environ.get("WORLD_SIZE", 1))
# ddp = world_size != 1
# if ddp:
# device_map = {"": int(os.environ.get("LOCAL_RANK") or 0)}
# gradient_accumulation_steps = args.gradient_accumulation_steps // world_size
# print("gradient_accumulation_steps: ", gradient_accumulation_steps)
# else:
# gradient_accumulation_steps = args.gradient_accumulation_steps
huggingface_hub.login(args.hf_token)
peft_config = LoraConfig(
r=args.lora_r,
lora_alpha=args.lora_alpha,
lora_dropout=args.lora_dropout,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "fc_in", "fc_out", "wte", "gate_proj", "down_proj", "up_proj"],
# target_modules=["gate_proj", "down_proj", "up_proj"],
bias="none",
task_type="CAUSAL_LM"
)
model = AutoModelForCausalLM.from_pretrained(
args.model_name,
# device_map={"": Accelerator().process_index}, # unavailable in deepspeed
trust_remote_code=True,
torch_dtype=torch.bfloat16,
use_cache=not args.gradient_checkpointing,
use_flash_attention_2=args.use_flash_attention,
)
model.enable_input_require_grads()
model.to('cuda')
if args.gradient_checkpointing:
model.gradient_checkpointing_enable()
# if not ddp and torch.cuda.device_count() > 1:
# # keeps Trainer from trying its own DataParallelism when more than 1 gpu is available
# model.is_parallelizable = True
# model.model_parallel = True
tokenizer = AutoTokenizer.from_pretrained(
args.model_name,
trust_remote_code=True,
)
special_tokens_dict = {"additional_special_tokens": ["<unk>", "<s>", "</s>"]}
num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)
model.resize_token_embeddings(len(tokenizer))
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
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 %}"
use_wandb = len(args.wandb_project) > 0 or (
"WANDB_PROJECT" in os.environ and len(os.environ["WANDB_PROJECT"]) > 0
)
# Only overwrite environ if wandb param passed
if len(args.wandb_project) > 0:
os.environ["WANDB_PROJECT"] = args.wandb_project
train_dataset = create_datasets(args.dataset_name, args.train_split)
eval_dataset = create_datasets(args.dataset_name, args.test_split)
# train_dataset = apply_template(train_dataset, tokenizer)
# eval_dataset = apply_template(eval_dataset, tokenizer)
original_columns = train_dataset.column_names
train_dataset = train_dataset.map(apply_chat_template, remove_columns=original_columns, fn_kwargs={"tokenizer": tokenizer, "task": "sft"})
eval_dataset = eval_dataset.map(apply_chat_template, remove_columns=original_columns, fn_kwargs={"tokenizer": tokenizer, "task": "sft"})
# print(f"Size of the train set: {len(train_dataset)}.")
print(f"Size of the train set: {len(train_dataset)}. Size of the validation set: {len(eval_dataset)}")
training_args = TrainingArguments(
output_dir=args.output_dir,
num_train_epochs=args.num_epochs,
per_device_train_batch_size=args.per_device_train_batch_size,
per_device_eval_batch_size=args.per_device_eval_batch_size if eval_dataset else None,
gradient_accumulation_steps=args.gradient_accumulation_steps,
gradient_checkpointing=args.gradient_checkpointing,
learning_rate=args.learning_rate,
logging_steps=args.logging_steps,
# optim="adamw_torch",
evaluation_strategy="epoch" if eval_dataset else "no",
save_strategy=args.save_strategy,
save_steps=args.save_steps,
save_total_limit=2,
group_by_length=args.group_by_length,
lr_scheduler_type=args.lr_scheduler_type,
warmup_ratio=args.warmup_ratio,
bf16=args.bf16, # True
remove_unused_columns=False,
report_to="wandb" if use_wandb else None,
run_name=args.wandb_run_name if use_wandb else None,
)
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)
trainer = SFTTrainer(
model=model,
peft_config=peft_config,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
dataset_text_field="text",
data_collator=data_collator,
packing=args.packing,
# packing=False,
max_seq_length=args.seq_length,
tokenizer=tokenizer,
neftune_noise_alpha=5,
args=training_args,
)
trainer.train()
trainer.model.save_pretrained(args.output_dir)
del model
torch.cuda.empty_cache()
model = AutoPeftModelForCausalLM.from_pretrained(args.output_dir, device_map="auto", torch_dtype=torch.bfloat16)
model = model.merge_and_unload()
if args.hf_hub_path:
model.push_to_hub(args.hf_hub_path)
tokenizer.push_to_hub(args.hf_hub_path)
else:
model.save_pretrained(args.output_dir)
tokenizer.save_pretrained(args.output_dir)