-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_lora.py
108 lines (86 loc) · 2.92 KB
/
train_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
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments,BitsAndBytesConfig
from datasets import load_dataset
from trl import SFTTrainer
from peft import AutoPeftModelForCausalLM, LoraConfig, get_peft_model, prepare_model_for_kbit_training
from utils import find_all_linear_names, print_trainable_parameters
from dataclasses import field
import random
import pdb
from datasets import Dataset
output_dir="./Results/"
model_name ="NousResearch/Llama-2-7b-hf"
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--language", type=str, default="English")
parser.add_argument("--task", type=str, default="Wiki")
args = parser.parse_args()
print(args)
dataset = load_dataset("json", data_files="German_Wiki.json",split="train")
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
base_model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
base_model.config.use_cache = False
base_model = prepare_model_for_kbit_training(base_model)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right" # Fix weird overflow issue with fp16 training
# Change the LORA hyperparameters accordingly to fit your use case
peft_config = LoraConfig(
r=8,
lora_alpha=16,
target_modules = [
"q_proj",
"v_proj",
"o_proj",
"gate_proj",
"k_proj",
"down_proj",
"up_proj"
],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM",
)
base_model = get_peft_model(base_model, peft_config)
print_trainable_parameters(base_model)
def formatting_prompts_func(example):
output_texts = []
for i in range(len(example['prompt'])):
text = f"```{example['prompt'][i]}```{example['completion'][i]}"
# text = f"```{example['prompt'][i]}{example['completion'][i]}```"
output_texts.append(text)
return output_texts
# Parameters for training arguments details => https://github.com/huggingface/transformers/blob/main/src/transformers/training_args.py#L158
training_args = TrainingArguments(
per_device_train_batch_size=4,
gradient_accumulation_steps=2,
gradient_checkpointing =True,
max_grad_norm= 0.3,
num_train_epochs=3,
learning_rate=2e-4,
bf16=True,
save_total_limit=3,
logging_steps=10,
output_dir=output_dir,
optim="paged_adamw_32bit",
lr_scheduler_type="cosine",
warmup_ratio=0.05,
)
trainer = SFTTrainer(
base_model,
train_dataset=dataset,
tokenizer=tokenizer,
max_seq_length=1024,
formatting_func=formatting_prompts_func,
args=training_args
)
trainer.train()
trainer.save_model(output_dir)
output_dir = os.path.join(output_dir, f"{args.language}_{args.task}_lora")
trainer.model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)