Update train.py
Browse files
train.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# β
Final train.py with
|
2 |
import unsloth # must be first
|
3 |
import pandas as pd
|
4 |
import torch
|
@@ -7,75 +7,92 @@ from transformers import TrainingArguments
|
|
7 |
from unsloth import FastLanguageModel
|
8 |
from trl import SFTTrainer
|
9 |
import os
|
10 |
-
import shutil
|
11 |
import zipfile
|
12 |
|
13 |
-
# Load Unsloth model
|
14 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
15 |
-
model_name
|
16 |
-
max_seq_length
|
17 |
-
dtype
|
18 |
-
load_in_4bit
|
19 |
)
|
20 |
|
21 |
-
#
|
22 |
-
eos_token = tokenizer.eos_token
|
|
|
|
|
23 |
|
24 |
-
# Load
|
25 |
df = pd.read_csv("data.csv")
|
26 |
-
df["text"] = df.apply(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
dataset = Dataset.from_pandas(df[["text"]])
|
28 |
|
29 |
-
# Apply LoRA without task_type
|
30 |
model = FastLanguageModel.get_peft_model(
|
31 |
model,
|
32 |
-
r
|
33 |
-
lora_alpha
|
34 |
-
lora_dropout
|
35 |
-
bias
|
36 |
)
|
37 |
|
38 |
-
# Tokenize
|
39 |
def tokenize(example):
|
40 |
-
return tokenizer(
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
tokenized_dataset = dataset.map(tokenize, batched=True)
|
43 |
|
44 |
-
# Set up training
|
45 |
training_args = TrainingArguments(
|
46 |
-
output_dir
|
47 |
-
per_device_train_batch_size
|
48 |
-
num_train_epochs
|
49 |
-
learning_rate
|
50 |
-
logging_steps
|
51 |
-
save_steps
|
52 |
-
fp16
|
53 |
)
|
54 |
|
55 |
-
#
|
56 |
trainer = SFTTrainer(
|
57 |
-
model
|
58 |
-
tokenizer
|
59 |
-
args
|
60 |
-
train_dataset
|
|
|
61 |
)
|
62 |
|
|
|
63 |
trainer.train()
|
64 |
|
65 |
-
# Save the fine
|
66 |
output_dir = "./output_model"
|
67 |
os.makedirs(output_dir, exist_ok=True)
|
68 |
model.save_pretrained(output_dir)
|
69 |
|
70 |
-
#
|
71 |
zip_path = "/home/user/app/model.zip"
|
72 |
try:
|
73 |
-
with zipfile.ZipFile(zip_path,
|
74 |
for root, _, files in os.walk(output_dir):
|
75 |
-
for
|
76 |
-
|
77 |
-
|
78 |
-
zipf.write(
|
79 |
print(f"β
Zipped model to {zip_path}")
|
80 |
except Exception as e:
|
81 |
-
print(f"β Failed to zip model: {e}")
|
|
|
1 |
+
# β
Final train.py with EOS-as-pad and stop_sequences
|
2 |
import unsloth # must be first
|
3 |
import pandas as pd
|
4 |
import torch
|
|
|
7 |
from unsloth import FastLanguageModel
|
8 |
from trl import SFTTrainer
|
9 |
import os
|
|
|
10 |
import zipfile
|
11 |
|
12 |
+
# 1) Load Unsloth model + tokenizer
|
13 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
14 |
+
model_name="HuggingFaceTB/SmolLM2-1.7B",
|
15 |
+
max_seq_length=2048,
|
16 |
+
dtype=torch.float16,
|
17 |
+
load_in_4bit=True,
|
18 |
)
|
19 |
|
20 |
+
# 2) Reuse the existing eos_token as pad_token so generation will stop
|
21 |
+
eos_token = tokenizer.eos_token # should be "<|endoftext|>"
|
22 |
+
tokenizer.pad_token = eos_token
|
23 |
+
model.config.pad_token_id = tokenizer.eos_token_id
|
24 |
|
25 |
+
# 3) Load & format your dataset, always ending responses with the EOS token
|
26 |
df = pd.read_csv("data.csv")
|
27 |
+
df["text"] = df.apply(
|
28 |
+
lambda row: (
|
29 |
+
"### Instruction:\n"
|
30 |
+
+ row["instruction"].strip()
|
31 |
+
+ "\n\n### Response:\n"
|
32 |
+
+ row["response"].strip()
|
33 |
+
+ eos_token
|
34 |
+
),
|
35 |
+
axis=1
|
36 |
+
)
|
37 |
dataset = Dataset.from_pandas(df[["text"]])
|
38 |
|
39 |
+
# 4) Apply LoRA without task_type
|
40 |
model = FastLanguageModel.get_peft_model(
|
41 |
model,
|
42 |
+
r=8,
|
43 |
+
lora_alpha=32,
|
44 |
+
lora_dropout=0.05,
|
45 |
+
bias="none",
|
46 |
)
|
47 |
|
48 |
+
# 5) Tokenize
|
49 |
def tokenize(example):
|
50 |
+
return tokenizer(
|
51 |
+
example["text"],
|
52 |
+
truncation=True,
|
53 |
+
padding="max_length",
|
54 |
+
max_length=512,
|
55 |
+
)
|
56 |
|
57 |
tokenized_dataset = dataset.map(tokenize, batched=True)
|
58 |
|
59 |
+
# 6) Set up training arguments
|
60 |
training_args = TrainingArguments(
|
61 |
+
output_dir="./output_model",
|
62 |
+
per_device_train_batch_size=2,
|
63 |
+
num_train_epochs=3,
|
64 |
+
learning_rate=2e-4,
|
65 |
+
logging_steps=10,
|
66 |
+
save_steps=100,
|
67 |
+
fp16=True,
|
68 |
)
|
69 |
|
70 |
+
# 7) Initialize SFTTrainer with stop_sequences
|
71 |
trainer = SFTTrainer(
|
72 |
+
model=model,
|
73 |
+
tokenizer=tokenizer,
|
74 |
+
args=training_args,
|
75 |
+
train_dataset=tokenized_dataset,
|
76 |
+
stop_sequences=[eos_token],
|
77 |
)
|
78 |
|
79 |
+
# 8) Train!
|
80 |
trainer.train()
|
81 |
|
82 |
+
# 9) Save the fineβtuned LoRA adapter
|
83 |
output_dir = "./output_model"
|
84 |
os.makedirs(output_dir, exist_ok=True)
|
85 |
model.save_pretrained(output_dir)
|
86 |
|
87 |
+
# π§ Zip the model for download
|
88 |
zip_path = "/home/user/app/model.zip"
|
89 |
try:
|
90 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
91 |
for root, _, files in os.walk(output_dir):
|
92 |
+
for fname in files:
|
93 |
+
full = os.path.join(root, fname)
|
94 |
+
rel = os.path.relpath(full, output_dir)
|
95 |
+
zipf.write(full, rel)
|
96 |
print(f"β
Zipped model to {zip_path}")
|
97 |
except Exception as e:
|
98 |
+
print(f"β Failed to zip model: {e}")
|