forked from unslothai/unsloth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
211 lines (176 loc) · 6.45 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from bitsandbytes.nn import Linear4bit as Bnb_Linear4bit
from peft.tuners.lora import Linear4bit as Peft_Linear4bit
from unsloth.models.sparsetral import FastSparsetralModel
import torch
from torch import nn
import math
from data_utils import make_supervised_data_module, SavePeftModelCallback
max_seq_length = 4096
dtype = torch.bfloat16
load_in_4bit = True
# 4bit pre quantized models we support for 4x faster downloading + no OOMs.
fourbit_models = [
"unsloth/mistral-7b-bnb-4bit",
"unsloth/mistral-7b-instruct-v0.1-bnb-4bit",
"unsloth/mistral-7b-instruct-v0.2-bnb-4bit",
"unsloth/llama-2-7b-bnb-4bit",
"unsloth/llama-2-13b-bnb-4bit",
"unsloth/codellama-34b-bnb-4bit",
"unsloth/tinyllama-bnb-4bit",
]
model, tokenizer = FastSparsetralModel.from_pretrained(
model_name="unsloth/mistral-7b-instruct-v0.2-bnb-4bit", # Choose ANY! eg teknium/OpenHermes-2.5-Mistral-7B
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
)
model = FastSparsetralModel.get_peft_model(
model,
r=64,
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
],
lora_alpha=16,
lora_dropout=0, # Supports any, but = 0 is optimized
bias="none", # Supports any, but = "none" is optimized
use_gradient_checkpointing=True,
random_state=741,
use_rslora=False, # We support rank stabilized LoRA
loftq_config=None, # And LoftQ
)
for name, module in model.named_modules():
if "adapter" in name or "router" in name:
if isinstance(module, (Bnb_Linear4bit, Peft_Linear4bit)):
# Create a new Linear module
new_module = (
torch.nn.Linear(module.in_features, module.out_features, bias=False)
.to(model.device)
.to(torch.bfloat16)
)
else:
new_module = module.to(torch.bfloat16)
# Get the attribute name to set the new module
parent_name, child_name = name.rsplit(".", 1)
parent_module = dict(model.named_modules())[parent_name]
# Replace the old module with the new one
setattr(parent_module, child_name, new_module)
# Zero Init
for n, p in model.named_parameters():
if "adapter_up" in n:
nn.init.zeros_(p)
if "adapter_down" in n:
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
if "router" in n:
nn.init.kaiming_uniform_(p, a=math.sqrt(5))
for n, p in model.named_parameters():
if "adapter" in n or "router" in n:
p.requires_grad = True
data_module = make_supervised_data_module(tokenizer, "data/openhermes2_5.json")
# from trl import SFTTrainer
from transformers import TrainingArguments, Trainer
tokenizer.model_max_length = max_seq_length
trainer = Trainer(
model=model,
tokenizer=tokenizer,
args=TrainingArguments(
per_device_train_batch_size=1,
gradient_accumulation_steps=128,
warmup_steps=200,
num_train_epochs=1,
# ddp_find_unused_parameters=False, If using DDP and you get an error, uncomment this line
learning_rate=2e-4,
fp16=not torch.cuda.is_bf16_supported(),
bf16=torch.cuda.is_bf16_supported(),
logging_steps=1,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=741,
output_dir="outputs",
save_strategy="steps",
save_steps=1000,
),
**data_module,
)
trainer.add_callback(SavePeftModelCallback)
# alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriat>
# ### Instruction:
# {}
# ### Input:
# {}
# ### Response:
# {}"""
# EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
# def formatting_prompts_func(examples):
# instructions = examples["instruction"]
# inputs = examples["input"]
# outputs = examples["output"]
# texts = []
# for instruction, input, output in zip(instructions, inputs, outputs):
# # Must add EOS_TOKEN, otherwise your generation will go on forever!
# text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
# texts.append(text)
# return {
# "text": texts,
# }
# pass
# from datasets import load_dataset
# dataset = load_dataset("yahma/alpaca-cleaned", split="train")
# dataset = dataset.map(
# formatting_prompts_func,
# batched=True,
# )
# from trl import SFTTrainer
# from transformers import TrainingArguments
# trainer = SFTTrainer(
# model=model,
# tokenizer=tokenizer,
# train_dataset=dataset,
# dataset_text_field="text",
# max_seq_length=max_seq_length,
# dataset_num_proc=2,
# packing=False, # Can make training 5x faster for short sequences.
# args=TrainingArguments(
# per_device_train_batch_size=8,
# gradient_accumulation_steps=4,
# warmup_steps=5,
# max_steps=50,
# learning_rate=2e-4,
# fp16=not torch.cuda.is_bf16_supported(),
# bf16=torch.cuda.is_bf16_supported(),
# logging_steps=1,
# optim="adamw_8bit",
# weight_decay=0.01,
# lr_scheduler_type="linear",
# seed=741,
# output_dir="outputs",
# ),
# )
# @title Show current memory stats
gpu_stats = torch.cuda.get_device_properties(0)
start_gpu_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)
print(f"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.")
print(f"{start_gpu_memory} GB of memory reserved.")
trainer_stats = trainer.train()
model.save_pretrained("outputs")
# @title Show final memory and time stats
used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
used_memory_for_lora = round(used_memory - start_gpu_memory, 3)
used_percentage = round(used_memory / max_memory * 100, 3)
lora_percentage = round(used_memory_for_lora / max_memory * 100, 3)
print(f"{trainer_stats.metrics['train_runtime']} seconds used for training.")
print(
f"{round(trainer_stats.metrics['train_runtime']/60, 2)} minutes used for training."
)
print(f"Peak reserved memory = {used_memory} GB.")
print(f"Peak reserved memory for training = {used_memory_for_lora} GB.")
print(f"Peak reserved memory % of max memory = {used_percentage} %.")
print(f"Peak reserved memory for training % of max memory = {lora_percentage} %.")