-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_model.py
More file actions
194 lines (150 loc) · 4.97 KB
/
train_model.py
File metadata and controls
194 lines (150 loc) · 4.97 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
# =======================
# STAGE 1 TRAINING
# =======================
from datasets import Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
import torch
import os, shutil
from google.colab import files
os.environ["WANDB_DISABLED"] = "true"
round = 3 # Replace with the current stage
# ======= CONFIG =======
comment_file = f"stage_{round}.txt"
model_name = f"./stage{round-1}" # Replace with 'Qwen/Qwen2.5-0.5B-Instruct' for stage 1
save_dir_stage1 = f"./stage_{round}-comments"
max_length = 128
# ======================
# ======= LOAD & CLEAN TEXT =======
def load_text(file_path):
with open(file_path, "r", encoding="utf-8") as f:
text = f.read().strip()
samples = [s.strip() for s in text.split("\n") if s.strip()]
return samples
print("Loading datasets...")
comment_samples = load_text(comment_file)
comment_dataset = Dataset.from_dict({"text": comment_samples})
# ======= TOKENIZER & MODEL =======
print("Loading tokenizer and model...")
# Load tokenizer from *same model folder* or fallback to base model
try:
tokenizer = AutoTokenizer.from_pretrained(model_name)
except:
print("Tokenizer not found in stage folder → using base tokenizer.")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = AutoModelForCausalLM.from_pretrained(model_name)
if torch.cuda.is_available():
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
else:
print("Training on CPU - slower")
# ======= TOKENIZE =======
def tokenize_fn(examples):
tokens = tokenizer(
examples["text"],
truncation=True,
padding="max_length",
max_length=max_length,
)
tokens["labels"] = tokens["input_ids"].copy()
return tokens
print("Tokenizing datasets...")
tokenized_comments = comment_dataset.map(tokenize_fn, batched=True)
# ======= TRAINING ARGS =======
training_args_1 = TrainingArguments(
output_dir=save_dir_stage1,
per_device_train_batch_size=2,
num_train_epochs=2,
learning_rate=2e-5,
logging_steps=50,
save_total_limit=1,
warmup_steps=20,
weight_decay=0.01,
fp16=torch.cuda.is_available(),
)
trainer_1 = Trainer(
model=model,
args=training_args_1,
train_dataset=tokenized_comments,
)
print("\nStage 1: Training on single comments...")
trainer_1.train()
# ======= SAVE MODEL =======
trainer_1.save_model(save_dir_stage1)
tokenizer.save_pretrained(save_dir_stage1)
print(f"Stage 1 complete. Saved at {save_dir_stage1}")
# =======================
# STAGE 2 TRAINING
# =======================
os.environ["WANDB_DISABLED"] = "true"
# ======= CONFIG =======
qa_file = f"stage_{round}_c.txt"
prevstage_model_dir = f"./stage_{round}-comments" # must exist
save_dir_stage2 = f"./stage_{round}-qa"
max_length = 128
# ======================
# ======= LOAD & CLEAN TEXT =======
def load_text(file_path):
with open(file_path, "r", encoding="utf-8") as f:
text = f.read().strip()
if "{---}" in text:
samples = [s.strip() for s in text.split("{---}") if s.strip()]
else:
samples = [line.strip() for line in text.split("\n") if line.strip()]
return samples
print("Loading QA dataset...")
qa_samples = load_text(qa_file)
qa_dataset = Dataset.from_dict({"text": qa_samples})
# ======= LOAD MODEL FROM STAGE 1 =======
print("Loading Stage 1 model...")
# Load tokenizer from *same model folder* or fallback to base model
try:
tokenizer = AutoTokenizer.from_pretrained(prevstage_model_dir)
except:
print("Tokenizer not found in stage folder → using base tokenizer.")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = AutoModelForCausalLM.from_pretrained(
prevstage_model_dir,
device_map="auto",
trust_remote_code=True
)
if torch.cuda.is_available():
print(f"Using GPU: {torch.cuda.get_device_name(0)}")
else:
print("Training on CPU - slower")
# ======= TOKENIZE =======
def tokenize_fn(examples):
tokens = tokenizer(
examples["text"],
truncation=True,
padding="max_length",
max_length=max_length,
)
tokens["labels"] = tokens["input_ids"].copy()
return tokens
print("Tokenizing QA dataset...")
tokenized_qa = qa_dataset.map(tokenize_fn, batched=True)
# ======= TRAINING =======
training_args_2 = TrainingArguments(
output_dir=save_dir_stage2,
per_device_train_batch_size=2,
num_train_epochs=2,
learning_rate=2e-5,
logging_steps=50,
save_total_limit=1,
warmup_steps=20,
weight_decay=0.01,
fp16=torch.cuda.is_available(),
)
trainer_2 = Trainer(
model=model,
args=training_args_2,
train_dataset=tokenized_qa,
)
print("\nStage 2: Fine-tuning on QA conversations...")
trainer_2.train()
# ======= SAVE FINAL MODEL =======
trainer_2.save_model(save_dir_stage2)
tokenizer.save_pretrained(save_dir_stage2)
print(f"Final model saved at {save_dir_stage2}")
# ======= ZIP & DOWNLOAD =======
zip_path = f"stage{round}_final_model"
shutil.make_archive(zip_path, 'zip', save_dir_stage2)