-
Notifications
You must be signed in to change notification settings - Fork 2
/
finetune.py
193 lines (167 loc) · 6.77 KB
/
finetune.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
import json
import os
import sys
from typing import List
import datasets
import fire
import torch
import transformers
from datasets import load_dataset
from datasets import Dataset
from datasets import DatasetDict
from peft import LoraConfig
from peft import get_peft_model
from peft import get_peft_model_state_dict
from peft import prepare_model_for_int8_training
from peft import set_peft_model_state_dict
from transformers import LlamaForCausalLM
from transformers import LlamaTokenizer
from multi_turn_alpaca.utils.prompter import Prompter
from multi_turn_alpaca.common import common_path
def tokenize(prompt, tokenizer, cutoff_len, add_eos_token=True):
# there's probably a way to do this with the tokenizer settings
# but again, gotta move fast
result = tokenizer(
prompt,
truncation=True,
max_length=cutoff_len,
padding=False,
return_tensors=None,
)
if (
result["input_ids"][-1] != tokenizer.eos_token_id
and len(result["input_ids"]) < cutoff_len
and add_eos_token
):
result["input_ids"].append(tokenizer.eos_token_id)
result["attention_mask"].append(1)
result["labels"] = result["input_ids"].copy()
return result
def generate_and_tokenize_prompt_wrapper(prompter, train_on_inputs, tokenizer, cutoff_len):
def generate_and_tokenize_prompt(data_point):
full_prompt = prompter.generate_prompt(
data_point["instruction"],
data_point["input"],
data_point["output"],
)
tokenized_full_prompt = tokenize(full_prompt, tokenizer, cutoff_len)
if not train_on_inputs:
user_prompt = prompter.generate_prompt(
data_point["instruction"], data_point["input"]
)
tokenized_user_prompt = tokenize(user_prompt, tokenizer, cutoff_len, add_eos_token=False)
user_prompt_len = len(tokenized_user_prompt["input_ids"])
tokenized_full_prompt["labels"] = [
-100
] * user_prompt_len + tokenized_full_prompt["labels"][
user_prompt_len:
] # could be sped up, probably
return tokenized_full_prompt
return generate_and_tokenize_prompt
def train(
# model/data params
base_model: str = "decapoda-research/llama-7b-hf", # the only required argument
data_path: str = os.path.join(common_path.data_dir, 'training_data', 'training_data.txt'),
output_dir: str = "./multi-turn-alpaca",
# training hyperparams
batch_size: int = 128,
micro_batch_size: int = 16,
num_epochs: int = 1,
learning_rate: float = 3e-4,
cutoff_len: int = 1024,
val_set_size: int = 2000,
# lora hyperparams
lora_r: int = 8,
lora_alpha: int = 8,
lora_dropout: float = 0.05,
lora_target_modules: List[str] = ['q_proj', 'k_proj'],
# llm hyperparams
train_on_inputs: bool = False, # if False, masks out inputs in loss
group_by_length: bool = False, # faster, but produces an odd training loss curve
resume_from_checkpoint: str = None, # either training checkpoint or final adapter
prompt_template_name: str = "multi_turn", # The prompt template to use, will default to alpaca.
):
tokenizer = LlamaTokenizer.from_pretrained(base_model)
tokenizer.pad_token_id = (
0 # unk. we want this to be different from the eos token
)
tokenizer.padding_side = "left" # Allow batched inference
data = DatasetDict.from_json({'train': data_path})
prompter = Prompter(prompt_template_name)
if val_set_size > 0:
train_val = data["train"].train_test_split(
test_size=val_set_size, shuffle=True, seed=42
)
train_data = (
train_val["train"].shuffle().map(generate_and_tokenize_prompt_wrapper(prompter, train_on_inputs, tokenizer,
cutoff_len))
)
val_data = (
train_val["test"].shuffle().map(generate_and_tokenize_prompt_wrapper(prompter, train_on_inputs, tokenizer,
cutoff_len))
)
else:
train_data = data["train"].shuffle().map(generate_and_tokenize_prompt_wrapper(prompter, train_on_inputs, tokenizer,
cutoff_len))
val_data = None
device_map = "auto"
model = LlamaForCausalLM.from_pretrained(
base_model,
load_in_8bit=True,
torch_dtype=torch.float16,
device_map=device_map,
)
model = prepare_model_for_int8_training(model)
config = LoraConfig(
r=lora_r,
lora_alpha=lora_alpha,
target_modules=lora_target_modules,
lora_dropout=lora_dropout,
bias="none",
task_type="CAUSAL_LM",
)
model = get_peft_model(model, config)
model.print_trainable_parameters() # Be more transparent about the % of trainable params.
gradient_accumulation_steps = batch_size // micro_batch_size
trainer = transformers.Trainer(
model=model,
train_dataset=train_data,
eval_dataset=val_data,
args=transformers.TrainingArguments(
per_device_train_batch_size=micro_batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
warmup_steps=100,
num_train_epochs=num_epochs,
learning_rate=learning_rate,
fp16=True,
logging_steps=10,
optim="adamw_torch",
evaluation_strategy="steps" if val_set_size > 0 else "no",
save_strategy="steps",
eval_steps=200 if val_set_size > 0 else None,
save_steps=200,
output_dir=output_dir,
save_total_limit=3,
load_best_model_at_end=True if val_set_size > 0 else False,
group_by_length=group_by_length
),
data_collator=transformers.DataCollatorForSeq2Seq(
tokenizer, pad_to_multiple_of=8, return_tensors="pt", padding=True
),
)
model.config.use_cache = False
old_state_dict = model.state_dict
model.state_dict = (
lambda self, *_, **__: get_peft_model_state_dict(
self, old_state_dict()
)
).__get__(model, type(model))
if torch.__version__ >= "2" and sys.platform != "win32":
model = torch.compile(model)
trainer.train(resume_from_checkpoint=resume_from_checkpoint)
model.save_pretrained(output_dir)
print(
"\n If there's a warning about missing keys above, please disregard :)"
)
if __name__ == "__main__":
fire.Fire(train)