darrenphodgson76 commited on
Commit
274e76a
·
verified ·
1 Parent(s): 12a4ede

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +37 -56
train.py CHANGED
@@ -1,87 +1,68 @@
1
- # === Required early import ===
2
- import unsloth
 
 
 
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
- # === Model and training config ===
12
- MODEL_NAME = "unsloth/SmolLM2-1.7B-Instruct" # Change if using another model
13
- BATCH_SIZE = 2
14
- EPOCHS = 3
15
- LR = 2e-4
16
- MAX_SEQ_LENGTH = 2048
17
- USE_4BIT = True
18
 
19
- # === Load model ===
20
  model, tokenizer = FastLanguageModel.from_pretrained(
21
- model_name = MODEL_NAME,
22
- max_seq_length = MAX_SEQ_LENGTH,
23
  dtype = torch.float16,
24
- load_in_4bit = USE_4BIT,
25
  )
26
 
27
- # === Load dataset ===
28
- dataset = load_dataset("csv", data_files="data.csv")["train"] # Replace with your CSV if needed
29
-
30
- # === Create 'text' column from instruction + response ===
31
- def add_text_column(example):
32
- example["text"] = example["instruction"] + "\n" + example["response"]
33
- return example
 
34
 
35
- dataset = dataset.map(add_text_column)
 
 
36
 
37
- # === Formatting function for trainer ===
38
- def formatting_func(example):
39
- return [example["text"]]
40
 
41
- # === TrainingArguments ===
42
  training_args = TrainingArguments(
43
- output_dir = "output",
44
- num_train_epochs = EPOCHS,
45
- per_device_train_batch_size = BATCH_SIZE,
46
- gradient_accumulation_steps = 1,
47
- gradient_checkpointing = True,
48
- optim = "paged_adamw_8bit",
49
  logging_steps = 10,
50
- save_strategy = "epoch",
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
- # === Trainer setup ===
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
- # === Save model ===
76
- save_dir = "output"
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
- # === Copy to visible directory ===
83
  try:
84
- copytree(save_dir, final_dir, dirs_exist_ok=True)
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))