-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathfinetune_qlora.py
369 lines (328 loc) · 11.5 KB
/
finetune_qlora.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
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import json
import os
from dataclasses import dataclass, field
from typing import Optional, List
import logging
import torch
import transformers
import argparse
from transformers import (
set_seed,
Trainer,
)
try:
from transformers import LlamaTokenizerFast as LlamaTokenizer
print("Using fast tokenizer")
except:
from transformers import LlamaTokenizer
print("Using slow tokenizer")
from transformers import AutoTokenizer
from qlora_utils import (
SavePeftModelCallback,
print_trainable_parameters,
get_last_checkpoint,
DEFAULT_PAD_TOKEN,
)
from data_utils_sft import (
make_sft_data_module,
IGNORE_INDEX,
)
from qlora_model import get_accelerate_model
torch.backends.cuda.matmul.allow_tf32 = True
logger = logging.getLogger(__name__)
@dataclass
class ModelArguments:
model_name_or_path: Optional[str] = field(default="EleutherAI/pythia-12b")
trust_remote_code: Optional[bool] = field(
default=False,
metadata={
"help": "Enable unpickling of arbitrary code in AutoModelForCausalLM#from_pretrained."
},
)
@dataclass
class DataArguments:
eval_dataset_size: int = field(
default=1024, metadata={"help": "Size of validation dataset."}
)
max_train_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of training examples to this "
"value if set."
},
)
max_eval_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
"value if set."
},
)
source_max_len: int = field(
default=1024,
metadata={
"help": "Maximum source sequence length. Sequences will be right padded (and possibly truncated)."
},
)
target_max_len: int = field(
default=256,
metadata={
"help": "Maximum target sequence length. Sequences will be right padded (and possibly truncated)."
},
)
dataset: str = field(
default="alpaca",
metadata={"help": "Which dataset to finetune on. See datamodule for options."},
)
dataset_format: Optional[str] = field(
default=None,
metadata={
"help": "Which dataset format is used. [alpaca|chip2|self-instruct|hh-rlhf]"
},
)
meta_prompt_pattern: Optional[str] = field(
default=None, metadata={"help": "Which meta prompt pattern to use."}
)
add_eos_to_target: bool = field(
default=True, metadata={"help": "Whether to add an EOS token to the target."}
)
@dataclass
class TrainingArguments(transformers.Seq2SeqTrainingArguments):
cache_dir: Optional[str] = field(default=None)
train_on_source: Optional[bool] = field(
default=False,
metadata={
"help": "Whether to train on the input in addition to the target text."
},
)
full_finetune: bool = field(
default=False, metadata={"help": "Finetune the entire model without adapters."}
)
adam8bit: bool = field(default=False, metadata={"help": "Use 8-bit adam."})
double_quant: bool = field(
default=True,
metadata={
"help": "Compress the quantization statistics through double quantization."
},
)
quant_type: str = field(
default="nf4",
metadata={
"help": "Quantization data type to use. Should be one of `fp4` or `nf4`."
},
)
bits: int = field(default=4, metadata={"help": "How many bits to use."})
lora_modules: Optional[List[str]] = field(
default=None,
metadata={
"help": "Which modules to use LoRA on. If None, will use all linear layers."
},
)
lora_r: int = field(default=64, metadata={"help": "Lora R dimension."})
lora_alpha: float = field(default=16, metadata={"help": " Lora alpha."})
lora_dropout: float = field(default=0.0, metadata={"help": "Lora dropout."})
max_memory_MB: int = field(default=80000, metadata={"help": "Free memory per gpu."})
report_to: str = field(
default="none",
metadata={"help": "To use wandb or something else for reporting."},
)
resume_dir: Optional[str] = field(
default=None,
metadata={"help": "Path to the directory containing the checkpoint to resume."},
)
output_dir: str = field(
default="./output", metadata={"help": "The output dir for logs and checkpoints"}
)
optim: str = field(
default="paged_adamw_32bit", metadata={"help": "The optimizer to be used"}
)
per_device_train_batch_size: int = field(
default=1,
metadata={
"help": "The training batch size per GPU. Increase for better speed."
},
)
gradient_accumulation_steps: int = field(
default=16,
metadata={
"help": "How many gradients to accumulate before to perform an optimizer step"
},
)
weight_decay: float = field(
default=0.0, metadata={"help": "The L2 weight decay rate of AdamW"}
) # use lora dropout instead for regularization if needed
learning_rate: float = field(default=0.0002, metadata={"help": "The learnign rate"})
remove_unused_columns: bool = field(
default=False,
metadata={"help": "Removed unused columns. Needed to make this codebase work."},
)
max_grad_norm: float = field(
default=0.3,
metadata={
"help": "Gradient clipping max norm. This is tuned and works well for all models tested."
},
)
gradient_checkpointing: bool = field(
default=True,
metadata={"help": "Use gradient checkpointing. You want to use this."},
)
do_train: bool = field(
default=True,
metadata={"help": "To train or not to train, that is the question?"},
)
lr_scheduler_type: str = field(
default="constant",
metadata={
"help": "Learning rate schedule. Constant a bit better than cosine, and has advantage for analysis"
},
)
warmup_ratio: float = field(
default=0.03, metadata={"help": "Fraction of steps to do a warmup for"}
)
logging_steps: int = field(
default=10,
metadata={"help": "The frequency of update steps after which to log the loss"},
)
group_by_length: bool = field(
default=True,
metadata={
"help": "Group sequences into batches with same length. Saves memory and speeds up training considerably."
},
)
save_strategy: str = field(
default="steps", metadata={"help": "When to save checkpoints"}
)
save_steps: int = field(default=250, metadata={"help": "How often to save a model"})
save_total_limit: int = field(
default=40,
metadata={
"help": "How many checkpoints to save before the oldest is overwritten"
},
)
resume_from_training: bool = field(
default=False, metadata={"help": "Resume from training"}
)
def rank0_print(*args):
local_rank = int(os.environ.get("LOCAL_RANK", 0))
if local_rank == 0:
print(*args)
def train():
hfparser = transformers.HfArgumentParser(
(ModelArguments, DataArguments, TrainingArguments)
)
(
model_args,
data_args,
training_args,
extra_args,
) = hfparser.parse_args_into_dataclasses(return_remaining_strings=True)
args = argparse.Namespace(
**vars(model_args), **vars(data_args), **vars(training_args)
)
if args.resume_dir is not None:
checkpoint_dir, completed_training = args.resume_dir, False
else:
checkpoint_dir, completed_training = get_last_checkpoint(args.output_dir)
if completed_training:
rank0_print("Detected that training was already completed!")
if checkpoint_dir is None:
rank0_print("Training from scratch.")
else:
rank0_print("Loading from checkpoint:", checkpoint_dir)
if args.resume_from_training:
rank0_print("Resuming from training not supported yet. Exiting.")
exit(1)
use_llama_base_model = (
"dromedary" in args.model_name_or_path.lower()
or "llama" in args.model_name_or_path.lower()
or "llemma" in args.model_name_or_path.lower()
)
if use_llama_base_model:
tokenizer_model_name = (
"TheBloke/dromedary-65b-lora-HF" # a random llama-based model
)
TokenizerClass = LlamaTokenizer
else:
tokenizer_model_name = args.model_name_or_path
TokenizerClass = AutoTokenizer
left_truncated_tokenizer = TokenizerClass.from_pretrained(
tokenizer_model_name,
cache_dir=args.cache_dir,
truncation_side="left",
padding_side="right",
# use_fast=False, # Fast tokenizer giving issues.
)
# Tokenizer
tokenizer = TokenizerClass.from_pretrained(
tokenizer_model_name,
cache_dir=args.cache_dir,
truncation_side="right",
padding_side="right",
# use_fast=False, # Fast tokenizer giving issues.
)
if use_llama_base_model:
if tokenizer._pad_token is None:
left_truncated_tokenizer.pad_token_id = (
0 # unk. we want this to be different from the eos token
)
tokenizer.pad_token_id = (
0 # unk. we want this to be different from the eos token
)
else:
raise NotImplementedError
data_module = make_sft_data_module(
left_truncated_tokenizer=left_truncated_tokenizer,
tokenizer=tokenizer,
args=args,
)
model = get_accelerate_model(args, checkpoint_dir)
model.config.use_cache = False
print_trainable_parameters(args, model)
print("loaded model")
set_seed(args.seed)
if args.do_train:
training_data = data_module["train_dataset"]
rank0_print("Training data size:", len(training_data))
rank0_print("Training data example:")
for i in range(min(3, len(training_data))):
rank0_print(training_data[i])
trainer = Trainer(
model=model,
tokenizer=tokenizer,
args=training_args,
**{k: v for k, v in data_module.items() if k != "predict_dataset"},
)
# Callbacks
if not args.full_finetune:
trainer.add_callback(SavePeftModelCallback)
# Verifying the datatypes.
dtypes = {}
for _, p in model.named_parameters():
dtype = p.dtype
if dtype not in dtypes:
dtypes[dtype] = 0
dtypes[dtype] += p.numel()
total = 0
for k, v in dtypes.items():
total += v
for k, v in dtypes.items():
print(k, v, v / total)
all_metrics = {"run_name": args.run_name}
# Training
if args.do_train:
logger.info("*** Train ***")
# Note: `resume_from_checkpoint` not supported for adapter checkpoints by HF.
# Currently adapter checkpoint is reloaded as expected but optimizer/scheduler states are not.
train_result = trainer.train()
metrics = train_result.metrics
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
all_metrics.update(metrics)
if args.do_train or args.do_eval or args.do_predict:
with open(os.path.join(args.output_dir, "metrics.json"), "w") as fout:
fout.write(json.dumps(all_metrics))
if __name__ == "__main__":
train()