darrenphodgson76 commited on
Commit
8ba34e8
Β·
verified Β·
1 Parent(s): b6a51b0

Update train.py

Browse files
Files changed (1) hide show
  1. train.py +56 -39
train.py CHANGED
@@ -1,4 +1,4 @@
1
- # βœ… Final train.py with ZIP logic added
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 = "HuggingFaceTB/SmolLM2-1.7B",
16
- max_seq_length = 2048,
17
- dtype = torch.float16,
18
- load_in_4bit = True,
19
  )
20
 
21
- # Get eos_token after tokenizer is loaded
22
- eos_token = tokenizer.eos_token or "</s>"# Fallback if eos_token is None
 
 
23
 
24
- # Load and format your dataset
25
  df = pd.read_csv("data.csv")
26
- df["text"] = df.apply(lambda row: f"### Instruction:\n{row['instruction']}\n\n### Response:\n{row['response']} {eos_token}", axis=1)
 
 
 
 
 
 
 
 
 
27
  dataset = Dataset.from_pandas(df[["text"]])
28
 
29
- # Apply LoRA without task_type
30
  model = FastLanguageModel.get_peft_model(
31
  model,
32
- r = 8,
33
- lora_alpha = 32,
34
- lora_dropout = 0.05,
35
- bias = "none",
36
  )
37
 
38
- # Tokenize text
39
  def tokenize(example):
40
- return tokenizer(example["text"], truncation=True, padding="max_length", max_length=512)
 
 
 
 
 
41
 
42
  tokenized_dataset = dataset.map(tokenize, batched=True)
43
 
44
- # Set up training
45
  training_args = TrainingArguments(
46
- output_dir = "./output_model",
47
- per_device_train_batch_size = 2,
48
- num_train_epochs = 3,
49
- learning_rate = 2e-4,
50
- logging_steps = 10,
51
- save_steps = 100,
52
- fp16 = True,
53
  )
54
 
55
- # Train
56
  trainer = SFTTrainer(
57
- model = model,
58
- tokenizer = tokenizer,
59
- args = training_args,
60
- train_dataset = tokenized_dataset,
 
61
  )
62
 
 
63
  trainer.train()
64
 
65
- # Save the fine-tuned LoRA adapter
66
  output_dir = "./output_model"
67
  os.makedirs(output_dir, exist_ok=True)
68
  model.save_pretrained(output_dir)
69
 
70
- # βœ… Zip it for download
71
  zip_path = "/home/user/app/model.zip"
72
  try:
73
- with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
74
  for root, _, files in os.walk(output_dir):
75
- for file in files:
76
- full_path = os.path.join(root, file)
77
- rel_path = os.path.relpath(full_path, output_dir)
78
- zipf.write(full_path, rel_path)
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}")