-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_model.py
381 lines (347 loc) · 13.6 KB
/
train_model.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
from dataclasses import dataclass, field
from typing import Optional
from typing import cast
from transformers import HfArgumentParser
from transformers import TrainerCallback, TrainingArguments, TrainerState, TrainerControl
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, TaskType
from peft import AutoPeftModelForCausalLM
from trl import SFTTrainer, DataCollatorForCompletionOnlyLM
from datasets import load_dataset
import torch
import gdown
import os
# Arguments
@dataclass
class ScriptArgs:
"""
Arguments that are not part of the training args
"""
model_id: str = field(
metadata={
"help": "The model that you want to train from the Hugging Face hub."
},
)
dataset_path: Optional[str] = field(
metadata={"help": "The path or URI of the dataset to use."},
)
file_id: Optional[str] = field(
metadata={"help": "The dataset file id of the dataset in google drive."},
)
lora_alpha: Optional[int] = field(default=256)
lora_dropout: Optional[float] = field(default=0.1)
lora_r: Optional[int] = field(default=128)
use_flash_attn: Optional[bool] = field(
default=True,
metadata={"help": "Enables Flash attention for training."},
)
merge_adapters: bool = field(
metadata={"help": "Wether to merge weights for LoRA."},
default=True,
)
save_model: bool = field(
metadata={"help": "Wether to save the model."},
default=True,
)
upload_to_hub: bool = field(
metadata={"help": "Wether to upload the model to the hub."},
default=True,
)
model_name_in_hub: Optional[str] = field(
metadata={"help": "The model name in the hub."},
default="MrezaPRZ/sql-model",
)
load_in_4bit: bool = field(
metadata={"help": "Wether to load the model in 4bit."},
default = False,
)
use_lora: bool = field(
metadata={"help": "Wether to use LoRA."},
default = False,
)
response_template: str = field(
metadata={"help": "The response template."},
default = "[/INST]",
)
@dataclass
class TrainingArgs:
"""
Arguments that are part of the training args
"""
output_dir: str = field(
metadata={"help": "The output directory where the model predictions and checkpoints will be written."},
default="./fine_tuned_model"
)
overwrite_output_dir: bool = field(
metadata={"help": "Overwrite the content of the output directory."},
default=True,
)
num_train_epochs: int = field(
metadata={"help": "The number of epochs to train."},
default=3,
)
bf16: bool = field(
metadata={"help": "Wether to use bfloat16."},
default = True,
)
tf32: bool = field(
metadata={"help": "Wether to use tf32."},
default = False,
)
per_device_train_batch_size: int = field(
metadata={"help": "The batch size per GPU for training."},
default=1,
)
gradient_accumulation_steps: int = field(
metadata={"help": "The number of gradient accumulation steps."},
default=16,
)
gradient_checkpointing: bool = field(
metadata={"help": "Wether to use gradient checkpointing."},
default=True,
)
learning_rate: float = field(
metadata={"help": "The learning rate."},
default=5e-5,
)
weight_decay: float = field(
metadata={"help": "The weight decay."},
default=0.01,
)
lr_scheduler_type: str = field(
metadata={"help": "The learning rate scheduler type."},
default="cosine",
)
max_grad_norm: float = field(
metadata={"help": "The maximum gradient norm."},
default=0.3,
)
group_by_length: bool = field(
metadata={"help": "Wether to group by length."},
default=True,
)
save_steps: int = field(
metadata={"help": "The number of steps to save the model."},
default = 100,
)
logging_steps: int = field(
metadata={"help": "The number of steps to log the model."},
default = 100,
)
save_total_limit: int = field(
metadata={"help": "The total number of models to save."},
default = 1,
)
max_seq_length: int = field(
metadata={"help": "The maximum sequence length."},
default = 2048,
)
deepspeed: str = field(
metadata={"help": "The deepspeed config."},
default = "deepspeed_config.json",
)
# ------------------------------------------
# helper class
class SaveDeepSpeedPeftModelCallback(TrainerCallback):
def __init__(self, trainer, save_steps=500):
self.trainer = trainer
self.save_steps = save_steps
def on_step_end(
self,
args: TrainingArguments,
state: TrainerState,
control: TrainerControl,
**kwargs,
):
if (state.global_step + 1) % self.save_steps == 0:
self.trainer.accelerator.wait_for_everyone()
state_dict = self.trainer.accelerator.get_state_dict(self.trainer.deepspeed)
unwrapped_model = self.trainer.accelerator.unwrap_model(self.trainer.deepspeed)
if self.trainer.accelerator.is_main_process:
unwrapped_model.save_pretrained(args.output_dir, state_dict=state_dict)
self.trainer.accelerator.wait_for_everyone()
return control
# helper functions
def download_dataset(script_args:ScriptArgs):
url = 'https://drive.google.com/uc?id=' + script_args.file_id
output = script_args.dataset_path
gdown.download(url, output, quiet=False)
def find_all_linear_names(model):
cls = torch.nn.Linear
lora_module_names = set()
for name, module in model.named_modules():
if isinstance(module, cls):
names = name.split(".")
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
if "lm_head" in lora_module_names: # needed for 16-bit
lora_module_names.remove("lm_head")
return list(lora_module_names)
def create_and_prepare_model(script_args:ScriptArgs, training_args:TrainingArgs):
if script_args.load_in_4bit:
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype = torch.bfloat16,
bnb_4bit_quant_type='nf4',
bnb_4bit_use_double_quant = True
)
if script_args.use_flash_attn:
print("Loading model with 4bit quantization and flash attention")
model = AutoModelForCausalLM.from_pretrained(
script_args.model_id,
use_cache=not training_args.gradient_checkpointing,
quantization_config=bnb_config,
attn_implementation="flash_attention_2"
)
else:
print("Loading model with 4bit quantization and without flash attention")
model = AutoModelForCausalLM.from_pretrained(
script_args.model_id,
use_cache=not training_args.gradient_checkpointing,
quantization_config=bnb_config,
)
else:
if script_args.use_flash_attn:
print("Loading model with flash attention")
model = AutoModelForCausalLM.from_pretrained(
script_args.model_id,
use_cache=not training_args.gradient_checkpointing,
attn_implementation="flash_attention_2",
torch_dtype = torch.bfloat16
)
else:
print("Loading model without flash attention")
model = AutoModelForCausalLM.from_pretrained(
script_args.model_id,
use_cache=not training_args.gradient_checkpointing,
torch_dtype = torch.bfloat16
)
model.config.use_cache = False
print("model loaded")
tokenizer = AutoTokenizer.from_pretrained(script_args.model_id)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.pad_token_id = tokenizer.eos_token_id
tokenizer.padding_side = "right"
return model, tokenizer
def set_training_args(training_args:TrainingArgs):
return TrainingArguments(
output_dir=training_args.output_dir,
overwrite_output_dir=training_args.overwrite_output_dir,
num_train_epochs=training_args.num_train_epochs,
per_device_train_batch_size=training_args.per_device_train_batch_size,
gradient_accumulation_steps=training_args.gradient_accumulation_steps,
gradient_checkpointing=training_args.gradient_checkpointing,
learning_rate=training_args.learning_rate,
weight_decay=training_args.weight_decay,
bf16=training_args.bf16,
tf32=training_args.tf32,
lr_scheduler_type=training_args.lr_scheduler_type,
max_grad_norm=training_args.max_grad_norm,
group_by_length=training_args.group_by_length,
save_steps=training_args.save_steps,
logging_steps=training_args.logging_steps,
save_total_limit=training_args.save_total_limit,
deepspeed=training_args.deepspeed,
)
# ------------------------------------------
def training_function(script_args:ScriptArgs, training_args:TrainingArgs):
# loading the dataset
data_files = {"train": script_args.dataset_path}
if not os.path.exists(script_args.dataset_path):
download_dataset(script_args)
dataset = load_dataset('csv', data_files=data_files)
print(f"Dataset loaded: {dataset} with columns {dataset['train'].features}")
model, tokenizer = create_and_prepare_model(script_args, training_args)
def formatting_prompts_func(training_dataset):
output_texts = []
for i in range(len(training_dataset['question'])):
question = training_dataset['question'][i]
query = training_dataset['query'][i]
database_schema = training_dataset['db_schema'][i]
user_message = f"""Given the following SQL tables, your job is to generate a correct SQL query given the user's question.
Put your answer inside the ```sql and ``` tags.
{database_schema}
###
Question: {question}
"""
assitant_message = f"""
```sql
{query} ;
```
"""
messages = [
{"role": "user", "content": user_message},
{"role": "assistant", "content": assitant_message},
]
text = tokenizer.apply_chat_template(messages, tokenize=False)
output_texts.append(text)
return output_texts
response_template = script_args.response_template
collator = DataCollatorForCompletionOnlyLM(response_template, tokenizer=tokenizer)
if script_args.use_lora:
target_modules = find_all_linear_names(model)
peft_config = LoraConfig(
lora_alpha=script_args.lora_alpha,
lora_dropout=script_args.lora_dropout,
r=script_args.lora_r,
bias="none",
task_type=TaskType.CAUSAL_LM,
target_modules=target_modules,
)
if training_args.gradient_checkpointing:
model.gradient_checkpointing_enable()
trainer = SFTTrainer(
model=model,
train_dataset=dataset['train'],
args=set_training_args(training_args),
peft_config=peft_config if script_args.use_lora else None,
formatting_func=formatting_prompts_func,
data_collator=collator,
tokenizer=tokenizer,
max_seq_length=training_args.max_seq_length,
)
trainer.add_callback(SaveDeepSpeedPeftModelCallback(trainer, save_steps=training_args.save_steps))
trainer.accelerator.print("Start training")
trainer.train()
trainer.accelerator.wait_for_everyone()
state_dict = trainer.accelerator.get_state_dict(trainer.deepspeed)
unwrapped_model = trainer.accelerator.unwrap_model(trainer.deepspeed)
trainer.accelerator.print("Saving training")
if trainer.accelerator.is_main_process:
unwrapped_model.save_pretrained(training_args.output_dir, state_dict=state_dict)
trainer.accelerator.wait_for_everyone()
if trainer.args.process_index == 0:
if script_args.merge_adapters and script_args.use_lora:
trainer.model.save_pretrained(training_args.output_dir, safe_serialization=False)
del model
del trainer
torch.cuda.empty_cache()
model = AutoPeftModelForCausalLM.from_pretrained(
training_args.output_dir,
torch_dtype=torch.bfloat16,
)
model = model.merge_and_unload()
if script_args.save_model:
model.save_pretrained(
training_args.output_dir, safe_serialization=True, max_shard_size="8GB"
)
tokenizer.save_pretrained(training_args.output_dir)
if script_args.upload_to_hub:
model.push_to_hub(script_args.model_name_in_hub)
tokenizer.push_to_hub(script_args.model_name_in_hub)
else:
if script_args.save_model:
trainer.model.save_pretrained(
training_args.output_dir, safe_serialization=True
)
tokenizer.save_pretrained(training_args.output_dir)
if script_args.upload_to_hub:
trainer.model.push_to_hub(script_args.model_name_in_hub)
tokenizer.push_to_hub(script_args.model_name_in_hub)
def main():
parser = HfArgumentParser([ScriptArgs,TrainingArgs])
script_args, training_args = parser.parse_args_into_dataclasses()
script_args = cast(ScriptArgs, script_args)
training_args = cast(TrainingArgs, training_args)
training_function(script_args, training_args)
if __name__ == "__main__":
main()