-
Notifications
You must be signed in to change notification settings - Fork 15
/
train.py
196 lines (174 loc) · 6.61 KB
/
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
import logging
from dataclasses import dataclass
from typing import Optional
import torch
from peft import LoraConfig
from datasets import disable_caching, load_dataset, concatenate_datasets
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
TrainingArguments,
HfArgumentParser,
BitsAndBytesConfig,
)
from trl import DataCollatorForCompletionOnlyLM, SFTTrainer
disable_caching()
logger = logging.getLogger(__name__)
@dataclass
class SFTTrainingArguments:
model_name_or_path: str
data_files: list[str]
eval_data_files: list[str] = None
tokenizer_name_or_path: Optional[str] = None
use_fast: bool = True
additional_special_tokens: list[str] = None
max_seq_length: int = 2048
load_in_8bit: bool = False
load_in_4bit: bool = False
use_flash_attention_2: bool = False
use_peft: bool = False
peft_target_model: Optional[str] = "llm-jp"
peft_target_modules: Optional[list[str]] = None
peft_lora_r: int = 8
peft_lora_alpha: int = 32
peft_lora_dropout: float = 0.05
def __post_init__(self):
if self.load_in_8bit and self.load_in_4bit:
raise ValueError("load_in_8bit and load_in_4bit are mutually exclusive")
if self.peft_target_model and self.peft_target_modules is None:
if self.peft_target_model == "llm-jp":
self.peft_target_modules = ["c_attn", "c_proj", "c_fc"]
elif self.peft_target_model == "llama":
# https://github.com/serp-ai/LLaMA-8bit-LoRA/blob/main/finetune_peft_8bit.py
self.peft_target_modules = [
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
]
elif self.peft_target_model == "llama-all":
# https://note.com/kan_hatakeyama/n/ncd09c52d26c7
self.peft_target_modules = [
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
"lm_head",
"embed_tokens",
]
else:
logger.warning(
f"peft_target_model '{self.peft_target_model}' is not supported, "
f"so peft_target_modules is set to None."
)
def from_pretrained_kwargs(self, training_args):
if self.load_in_8bit:
kwargs = {"load_in_8bit": True}
elif self.load_in_4bit:
kwargs = {
"quantization_config": BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
}
elif training_args.bf16:
kwargs = {"torch_dtype": torch.bfloat16}
else:
kwargs = {"torch_dtype": torch.float16}
kwargs["use_flash_attention_2"] = self.use_flash_attention_2
return kwargs
def load_datasets(data_files):
datasets = []
for data_file in data_files:
dataset = load_dataset("json", data_files=data_file)
dataset = dataset["train"]
dataset = dataset.select_columns("text")
datasets.append(dataset)
return concatenate_datasets(datasets)
def main() -> None:
parser = HfArgumentParser((TrainingArguments, SFTTrainingArguments))
training_args, sft_training_args = parser.parse_args_into_dataclasses()
tokenizer_name_or_path: str = (
sft_training_args.tokenizer_name_or_path or sft_training_args.model_name_or_path
)
logger.info(f"Loading tokenizer from {tokenizer_name_or_path}")
tokenizer = AutoTokenizer.from_pretrained(
tokenizer_name_or_path,
use_fast=sft_training_args.use_fast,
additional_special_tokens=sft_training_args.additional_special_tokens,
trust_remote_code=True,
)
logger.info("Loading data")
train_dataset = load_datasets(sft_training_args.data_files)
if sft_training_args.eval_data_files:
eval_dataset = load_datasets(sft_training_args.eval_data_files)
training_args.do_eval = True
else:
eval_dataset = None
logger.info("Formatting prompts")
instruction_ids = tokenizer.encode("\n\n### 指示:\n", add_special_tokens=False)[1:]
response_ids = tokenizer.encode("\n\n### 応答:\n", add_special_tokens=False)[1:]
collator = DataCollatorForCompletionOnlyLM(
instruction_template=instruction_ids,
response_template=response_ids,
tokenizer=tokenizer,
)
logger.info(f"Loading model from {sft_training_args.model_name_or_path}")
kwargs = sft_training_args.from_pretrained_kwargs(training_args)
logger.debug(
f"AutoModelForCausalLM.from_pretrained({sft_training_args.model_name_or_path}, trust_remote_code=True, **kwargs={kwargs})"
)
model = AutoModelForCausalLM.from_pretrained(
sft_training_args.model_name_or_path,
trust_remote_code=True,
**kwargs,
)
peft_config: Optional[LoraConfig] = None
if sft_training_args.use_peft:
logger.info("Setting up LoRA")
peft_config = LoraConfig(
r=sft_training_args.peft_lora_r,
target_modules=sft_training_args.peft_target_modules,
lora_alpha=sft_training_args.peft_lora_alpha,
lora_dropout=sft_training_args.peft_lora_dropout,
fan_in_fan_out=True,
bias="none",
task_type="CAUSAL_LM",
)
if training_args.gradient_checkpointing:
for param in model.parameters():
param.requires_grad = False
if param.ndim == 1:
param.data = param.data.to(torch.float32)
model.gradient_checkpointing_enable()
model.enable_input_require_grads()
logger.info("Setting up trainer")
trainer = SFTTrainer(
model,
args=training_args,
tokenizer=tokenizer,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
dataset_text_field="text",
data_collator=collator,
peft_config=peft_config,
max_seq_length=sft_training_args.max_seq_length,
)
logger.info("Training")
trainer.train()
logger.info("Saving model")
trainer.save_model()
if __name__ == "__main__":
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(name)s:%(lineno)d: %(levelname)s: %(message)s",
)
main()