Update train.py
Browse files
train.py
CHANGED
@@ -1,87 +1,68 @@
|
|
1 |
-
#
|
2 |
-
import
|
|
|
|
|
|
|
3 |
from unsloth import FastLanguageModel
|
4 |
from trl import SFTTrainer
|
5 |
-
from transformers import TrainingArguments
|
6 |
-
from datasets import load_dataset
|
7 |
-
import torch
|
8 |
from shutil import copytree
|
9 |
import os
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
LR = 2e-4
|
16 |
-
MAX_SEQ_LENGTH = 2048
|
17 |
-
USE_4BIT = True
|
18 |
|
19 |
-
#
|
20 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
21 |
-
model_name =
|
22 |
-
max_seq_length =
|
23 |
dtype = torch.float16,
|
24 |
-
load_in_4bit =
|
25 |
)
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
def formatting_func(example):
|
39 |
-
return [example["text"]]
|
40 |
|
41 |
-
#
|
42 |
training_args = TrainingArguments(
|
43 |
-
output_dir = "
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
gradient_checkpointing = True,
|
48 |
-
optim = "paged_adamw_8bit",
|
49 |
logging_steps = 10,
|
50 |
-
|
51 |
-
learning_rate = LR,
|
52 |
-
bf16 = False,
|
53 |
fp16 = True,
|
54 |
-
max_grad_norm = 1.0,
|
55 |
-
warmup_ratio = 0.03,
|
56 |
-
lr_scheduler_type = "linear",
|
57 |
-
disable_tqdm = False,
|
58 |
-
report_to = "none",
|
59 |
)
|
60 |
|
61 |
-
#
|
62 |
trainer = SFTTrainer(
|
63 |
model = model,
|
64 |
tokenizer = tokenizer,
|
65 |
-
train_dataset = dataset,
|
66 |
-
dataset_text_field = "text",
|
67 |
-
formatting_func = formatting_func,
|
68 |
args = training_args,
|
|
|
69 |
)
|
70 |
|
71 |
-
# === Train ===
|
72 |
-
model = FastLanguageModel.prepare_for_training(model)
|
73 |
trainer.train()
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
final_dir = "/home/user/app/final_model"
|
78 |
-
|
79 |
-
model.save_pretrained(save_dir, safe_serialization=True)
|
80 |
-
tokenizer.save_pretrained(save_dir)
|
81 |
|
82 |
-
#
|
83 |
try:
|
84 |
-
copytree(
|
85 |
print("✅ Model saved to /home/user/app/final_model for download in UI.")
|
86 |
except Exception as e:
|
87 |
print("⚠️ Failed to copy model to visible folder:", str(e))
|
|
|
1 |
+
import unsloth # must be first
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from datasets import Dataset
|
5 |
+
from transformers import TrainingArguments
|
6 |
from unsloth import FastLanguageModel
|
7 |
from trl import SFTTrainer
|
|
|
|
|
|
|
8 |
from shutil import copytree
|
9 |
import os
|
10 |
|
11 |
+
# Load and format your dataset
|
12 |
+
df = pd.read_csv("data.csv")
|
13 |
+
df["text"] = df.apply(lambda row: f"### Instruction:\n{row['instruction']}\n\n### Response:\n{row['response']}\n", axis=1)
|
14 |
+
dataset = Dataset.from_pandas(df[["text"]])
|
|
|
|
|
|
|
15 |
|
16 |
+
# Load Unsloth model
|
17 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
18 |
+
model_name = "unsloth/Llama-3.2-3B-Instruct", # or another Unsloth-compatible model
|
19 |
+
max_seq_length = 2048,
|
20 |
dtype = torch.float16,
|
21 |
+
load_in_4bit = True,
|
22 |
)
|
23 |
|
24 |
+
# Apply LoRA without task_type
|
25 |
+
model = FastLanguageModel.get_peft_model(
|
26 |
+
model,
|
27 |
+
r = 8,
|
28 |
+
lora_alpha = 32,
|
29 |
+
lora_dropout = 0.05,
|
30 |
+
bias = "none",
|
31 |
+
)
|
32 |
|
33 |
+
# Tokenize
|
34 |
+
def tokenize(example):
|
35 |
+
return tokenizer(example["text"], truncation=True, padding="max_length", max_length=512)
|
36 |
|
37 |
+
tokenized_dataset = dataset.map(tokenize, batched=True)
|
|
|
|
|
38 |
|
39 |
+
# Define training args
|
40 |
training_args = TrainingArguments(
|
41 |
+
output_dir = "./lora-finetuned",
|
42 |
+
per_device_train_batch_size = 2,
|
43 |
+
num_train_epochs = 3,
|
44 |
+
learning_rate = 2e-4,
|
|
|
|
|
45 |
logging_steps = 10,
|
46 |
+
save_steps = 100,
|
|
|
|
|
47 |
fp16 = True,
|
|
|
|
|
|
|
|
|
|
|
48 |
)
|
49 |
|
50 |
+
# Train
|
51 |
trainer = SFTTrainer(
|
52 |
model = model,
|
53 |
tokenizer = tokenizer,
|
|
|
|
|
|
|
54 |
args = training_args,
|
55 |
+
train_dataset = tokenized_dataset,
|
56 |
)
|
57 |
|
|
|
|
|
58 |
trainer.train()
|
59 |
|
60 |
+
# Save model
|
61 |
+
model.save_pretrained("./lora-finetuned")
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Copy to visible folder
|
64 |
try:
|
65 |
+
copytree("./lora-finetuned", "/home/user/app/final_model", dirs_exist_ok=True)
|
66 |
print("✅ Model saved to /home/user/app/final_model for download in UI.")
|
67 |
except Exception as e:
|
68 |
print("⚠️ Failed to copy model to visible folder:", str(e))
|