forked from unslothai/unsloth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunsloth-cli.py
More file actions
473 lines (435 loc) · 15.3 KB
/
unsloth-cli.py
File metadata and controls
473 lines (435 loc) · 15.3 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env python3
"""
🦥 Starter Script for Fine-Tuning FastLanguageModel with Unsloth
This script is designed as a starting point for fine-tuning your models using unsloth.
It includes configurable options for model loading, PEFT parameters, training arguments,
and model saving/pushing functionalities.
You will likely want to customize this script to suit your specific use case
and requirements.
Here are a few suggestions for customization:
- Modify the dataset loading and preprocessing steps to match your data.
- Customize the model saving and pushing configurations.
Usage: (most of the options have valid default values this is an extended example for demonstration purposes)
python unsloth-cli.py --model_name "unsloth/llama-3-8b" --max_seq_length 8192 --dtype None --load_in_4bit \
--r 64 --lora_alpha 32 --lora_dropout 0.1 --bias "none" --use_gradient_checkpointing "unsloth" \
--random_state 3407 --use_rslora --per_device_train_batch_size 4 --gradient_accumulation_steps 8 \
--warmup_steps 5 --max_steps 400 --learning_rate 2e-6 --logging_steps 1 --optim "adamw_8bit" \
--weight_decay 0.005 --lr_scheduler_type "linear" --seed 3407 --output_dir "outputs" \
--report_to "tensorboard" --save_model --save_path "model" --quantization_method "f16" \
--push_model --hub_path "hf/model" --hub_token "your_hf_token"
To see a full list of configurable options, use:
python unsloth-cli.py --help
Happy fine-tuning!
"""
import argparse
import os
def run(args):
from unsloth import FastLanguageModel
from datasets import load_dataset
from transformers.utils import strtobool
from trl import SFTTrainer, SFTConfig
from unsloth import is_bfloat16_supported
from unsloth.models.loader_utils import prepare_device_map
import logging
from unsloth import RawTextDataLoader
logging.getLogger("hf-to-gguf").setLevel(logging.WARNING)
# Load model and tokenizer
device_map, distributed = prepare_device_map()
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = args.model_name,
max_seq_length = args.max_seq_length,
dtype = args.dtype,
load_in_4bit = args.load_in_4bit,
device_map = device_map,
)
# Configure PEFT model
model = FastLanguageModel.get_peft_model(
model,
r = args.r,
target_modules = [
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
],
lora_alpha = args.lora_alpha,
lora_dropout = args.lora_dropout,
bias = args.bias,
use_gradient_checkpointing = args.use_gradient_checkpointing,
random_state = args.random_state,
use_rslora = args.use_rslora,
loftq_config = args.loftq_config,
)
alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### 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):
text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
texts.append(text)
return {"text": texts}
def load_dataset_smart(args):
from transformers.utils import strtobool
if args.raw_text_file:
# Use raw text loader
loader = RawTextDataLoader(tokenizer, args.chunk_size, args.stride)
dataset = loader.load_from_file(args.raw_text_file)
elif args.dataset.endswith((".txt", ".md", ".json", ".jsonl")):
# Auto-detect local raw text files
loader = RawTextDataLoader(tokenizer)
dataset = loader.load_from_file(args.dataset)
else:
# Check for modelscope usage
use_modelscope = strtobool(
os.environ.get("UNSLOTH_USE_MODELSCOPE", "False")
)
if use_modelscope:
from modelscope import MsDataset
dataset = MsDataset.load(args.dataset, split = "train")
else:
# Existing HuggingFace dataset logic
dataset = load_dataset(args.dataset, split = "train")
# Apply formatting for structured datasets
dataset = dataset.map(formatting_prompts_func, batched = True)
return dataset
# Load dataset using smart loader
dataset = load_dataset_smart(args)
print("Data is formatted and ready!")
# Configure training arguments
training_args = SFTConfig(
per_device_train_batch_size = args.per_device_train_batch_size,
per_device_eval_batch_size = args.per_device_eval_batch_size,
gradient_accumulation_steps = args.gradient_accumulation_steps,
warmup_steps = args.warmup_steps,
max_steps = args.max_steps,
learning_rate = args.learning_rate,
fp16 = not is_bfloat16_supported(),
bf16 = is_bfloat16_supported(),
logging_steps = args.logging_steps,
optim = args.optim,
weight_decay = args.weight_decay,
lr_scheduler_type = args.lr_scheduler_type,
seed = args.seed,
output_dir = args.output_dir,
report_to = args.report_to,
max_length = args.max_seq_length,
dataset_num_proc = 2,
ddp_find_unused_parameters = False if distributed else None,
packing = args.packing,
)
# Initialize trainer
trainer = SFTTrainer(
model = model,
processing_class = tokenizer,
train_dataset = dataset,
args = training_args,
)
trainer.train()
# Save model
if args.save_model:
# if args.quantization_method is a list, we will save the model for each quantization method
if args.save_gguf:
if isinstance(args.quantization, list):
for quantization_method in args.quantization:
print(
f"Saving model with quantization method: {quantization_method}"
)
model.save_pretrained_gguf(
args.save_path,
tokenizer,
quantization_method = quantization_method,
)
if args.push_model:
model.push_to_hub_gguf(
hub_path = args.hub_path,
hub_token = args.hub_token,
quantization_method = quantization_method,
)
else:
print(f"Saving model with quantization method: {args.quantization}")
model.save_pretrained_gguf(
args.save_path,
tokenizer,
quantization_method = args.quantization,
)
if args.push_model:
model.push_to_hub_gguf(
hub_path = args.hub_path,
hub_token = args.hub_token,
quantization_method = args.quantization,
)
else:
model.save_pretrained_merged(args.save_path, tokenizer, args.save_method)
if args.push_model:
model.push_to_hub_merged(args.save_path, tokenizer, args.hub_token)
else:
print("Warning: The model is not saved!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = "🦥 Fine-tune your llm faster using unsloth!"
)
model_group = parser.add_argument_group("🤖 Model Options")
model_group.add_argument(
"--model_name",
type = str,
default = "unsloth/llama-3-8b",
help = "Model name to load",
)
model_group.add_argument(
"--max_seq_length",
type = int,
default = 2048,
help = "Maximum sequence length, default is 2048. We auto support RoPE Scaling internally!",
)
model_group.add_argument(
"--dtype",
type = str,
default = None,
help = "Data type for model (None for auto detection)",
)
model_group.add_argument(
"--load_in_4bit",
action = "store_true",
help = "Use 4bit quantization to reduce memory usage",
)
model_group.add_argument(
"--dataset",
type = str,
default = "yahma/alpaca-cleaned",
help = "Huggingface dataset to use for training",
)
lora_group = parser.add_argument_group(
"🧠 LoRA Options",
"These options are used to configure the LoRA model.",
)
lora_group.add_argument(
"--r",
type = int,
default = 16,
help = "Rank for Lora model, default is 16. (common values: 8, 16, 32, 64, 128)",
)
lora_group.add_argument(
"--lora_alpha",
type = int,
default = 16,
help = "LoRA alpha parameter, default is 16. (common values: 8, 16, 32, 64, 128)",
)
lora_group.add_argument(
"--lora_dropout",
type = float,
default = 0.0,
help = "LoRA dropout rate, default is 0.0 which is optimized.",
)
lora_group.add_argument(
"--bias",
type = str,
default = "none",
help = "Bias setting for LoRA",
)
lora_group.add_argument(
"--use_gradient_checkpointing",
type = str,
default = "unsloth",
help = "Use gradient checkpointing",
)
lora_group.add_argument(
"--random_state",
type = int,
default = 3407,
help = "Random state for reproducibility, default is 3407.",
)
lora_group.add_argument(
"--use_rslora",
action = "store_true",
help = "Use rank stabilized LoRA",
)
lora_group.add_argument(
"--loftq_config",
type = str,
default = None,
help = "Configuration for LoftQ",
)
training_group = parser.add_argument_group("🎓 Training Options")
training_group.add_argument(
"--per_device_train_batch_size",
type = int,
default = 2,
help = "Batch size per device during training, default is 2.",
)
training_group.add_argument(
"--per_device_eval_batch_size",
type = int,
default = 4,
help = "Batch size per device during evaluation, default is 4.",
)
training_group.add_argument(
"--gradient_accumulation_steps",
type = int,
default = 4,
help = "Number of gradient accumulation steps, default is 4.",
)
training_group.add_argument(
"--warmup_steps",
type = int,
default = 5,
help = "Number of warmup steps, default is 5.",
)
training_group.add_argument(
"--max_steps",
type = int,
default = 400,
help = "Maximum number of training steps.",
)
training_group.add_argument(
"--learning_rate",
type = float,
default = 2e-4,
help = "Learning rate, default is 2e-4.",
)
training_group.add_argument(
"--optim",
type = str,
default = "adamw_8bit",
help = "Optimizer type.",
)
training_group.add_argument(
"--weight_decay",
type = float,
default = 0.01,
help = "Weight decay, default is 0.01.",
)
training_group.add_argument(
"--lr_scheduler_type",
type = str,
default = "linear",
help = "Learning rate scheduler type, default is 'linear'.",
)
training_group.add_argument(
"--seed",
type = int,
default = 3407,
help = "Seed for reproducibility, default is 3407.",
)
training_group.add_argument(
"--packing",
action = "store_true",
help = "Enable padding-free sample packing via TRL's bin packer.",
)
report_group = parser.add_argument_group("📊 Report Options")
report_group.add_argument(
"--report_to",
type = str,
default = "tensorboard",
choices = [
"azure_ml",
"clearml",
"codecarbon",
"comet_ml",
"dagshub",
"dvclive",
"flyte",
"mlflow",
"neptune",
"tensorboard",
"wandb",
"all",
"none",
],
help = (
"The list of integrations to report the results and logs to. Supported platforms are:\n\t\t "
"'azure_ml', 'clearml', 'codecarbon', 'comet_ml', 'dagshub', 'dvclive', 'flyte', "
"'mlflow', 'neptune', 'tensorboard', and 'wandb'. Use 'all' to report to all integrations "
"installed, 'none' for no integrations."
),
)
report_group.add_argument(
"--logging_steps",
type = int,
default = 1,
help = "Logging steps, default is 1",
)
save_group = parser.add_argument_group("💾 Save Model Options")
save_group.add_argument(
"--output_dir",
type = str,
default = "outputs",
help = "Output directory",
)
save_group.add_argument(
"--save_model",
action = "store_true",
help = "Save the model after training",
)
save_group.add_argument(
"--save_method",
type = str,
default = "merged_16bit",
choices = ["merged_16bit", "merged_4bit", "lora"],
help = "Save method for the model, default is 'merged_16bit'",
)
save_group.add_argument(
"--save_gguf",
action = "store_true",
help = "Convert the model to GGUF after training",
)
save_group.add_argument(
"--save_path",
type = str,
default = "model",
help = "Path to save the model",
)
save_group.add_argument(
"--quantization",
type = str,
default = "q8_0",
nargs = "+",
help = (
"Quantization method for saving the model. common values ('f16', 'q4_k_m', 'q8_0'), "
"Check our wiki for all quantization methods https://github.com/unslothai/unsloth/wiki#saving-to-gguf"
),
)
push_group = parser.add_argument_group("🚀 Push Model Options")
push_group.add_argument(
"--push_model",
action = "store_true",
help = "Push the model to Hugging Face hub after training",
)
push_group.add_argument(
"--push_gguf",
action = "store_true",
help = "Push the model as GGUF to Hugging Face hub after training",
)
push_group.add_argument(
"--hub_path",
type = str,
default = "hf/model",
help = "Path on Hugging Face hub to push the model",
)
push_group.add_argument(
"--hub_token",
type = str,
help = "Token for pushing the model to Hugging Face hub",
)
parser.add_argument(
"--raw_text_file", type = str, help = "Path to raw text file for training"
)
parser.add_argument(
"--chunk_size", type = int, default = 2048, help = "Size of text chunks for training"
)
parser.add_argument(
"--stride", type = int, default = 512, help = "Overlap between chunks"
)
args = parser.parse_args()
run(args)