-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsft_qwen3b.py
More file actions
268 lines (240 loc) · 9 KB
/
sft_qwen3b.py
File metadata and controls
268 lines (240 loc) · 9 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import shutil
import re
import time
import torch
import inspect
from typing import List
from datasets import load_dataset, load_from_disk
from transformers import AutoTokenizer, AutoModelForCausalLM
from trl import SFTTrainer, SFTConfig
def _find_subsequence_indices(sequence: List[int], subsequence: List[int]) -> List[int]:
if not subsequence or not sequence:
return []
sub_len = len(subsequence)
return [
i
for i in range(len(sequence) - sub_len + 1)
if sequence[i : i + sub_len] == subsequence
]
class CompletionOnlyDataCollator:
def __init__(self, tokenizer, response_template: str, max_length: int):
self.tokenizer = tokenizer
self.response_template_ids = tokenizer.encode(
response_template, add_special_tokens=False
)
self.role_start_ids = tokenizer.encode("<|im_start|>", add_special_tokens=False)
self.max_length = max_length
def __call__(self, features):
if "text" in features[0]:
texts = [f["text"] for f in features]
batch = self.tokenizer(
texts,
padding="max_length",
truncation=True,
max_length=self.max_length,
return_tensors="pt",
)
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
else:
truncated = []
for f in features:
item = dict(f)
if "input_ids" in item:
item["input_ids"] = item["input_ids"][: self.max_length]
if "attention_mask" in item:
item["attention_mask"] = item["attention_mask"][: self.max_length]
truncated.append(item)
batch = self.tokenizer.pad(
truncated,
padding="max_length",
max_length=self.max_length,
return_tensors="pt",
)
input_ids = batch["input_ids"]
attention_mask = batch["attention_mask"]
labels = torch.full_like(input_ids, fill_value=-100)
for i, seq in enumerate(input_ids.tolist()):
response_starts = _find_subsequence_indices(
seq, self.response_template_ids
)
if not response_starts:
continue
role_starts = _find_subsequence_indices(seq, self.role_start_ids)
for start_idx in response_starts:
content_start = start_idx + len(self.response_template_ids)
next_role = next(
(r for r in role_starts if r > start_idx), len(seq)
)
labels[i, content_start:next_role] = input_ids[i, content_start:next_role]
labels[attention_mask == 0] = -100
for i in range(labels.size(0)):
if (labels[i] != -100).any():
continue
valid_len = int(attention_mask[i].sum().item())
last_idx = max(valid_len - 1, 1)
labels[i, last_idx] = input_ids[i, last_idx]
batch["labels"] = labels
return batch
MODEL_ID = ""
DATA_PATH = ""
OUTPUT_DIR = ""
RESPONSE_TEMPLATE = "<|im_start|>assistant\n"
PREPROCESSED_DIR = ""
PREPROCESS_DONE = os.path.join(PREPROCESSED_DIR, ".done")
def train():
local_rank = int(os.environ.get("LOCAL_RANK", "0"))
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True, local_files_only=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
tokenizer.truncation_side = "left"
max_length = 2048
response_template_ids = tokenizer.encode(
RESPONSE_TEMPLATE, add_special_tokens=False
)
role_start_ids = tokenizer.encode("<|im_start|>", add_special_tokens=False)
tool_response_pattern = re.compile(
r"<tool_response>.*?</tool_response>",
flags=re.DOTALL | re.IGNORECASE,
)
def mask_tool_response(content: str) -> str:
return tool_response_pattern.sub(
"<tool_response>[TOOL_RESPONSE_OMITTED]</tool_response>",
content,
)
def formatting_prompts_func(example):
messages = []
for msg in example["messages"]:
if not isinstance(msg, dict):
messages.append(msg)
continue
content = msg.get("content")
if isinstance(content, str):
content = mask_tool_response(content)
msg = {**msg, "content": content}
if msg.get("role") == "tool":
msg = {**msg, "content": "<tool_response>[TOOL_RESPONSE_OMITTED]</tool_response>"}
messages.append(msg)
output_text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=False,
)
token_ids = tokenizer.encode(
output_text,
add_special_tokens=False,
truncation=True,
max_length=max_length,
)
if len(token_ids) < 2:
return {"text": output_text, "has_response": False}
response_starts = _find_subsequence_indices(token_ids, response_template_ids)
if not response_starts:
return {"text": output_text, "has_response": False}
role_starts = _find_subsequence_indices(token_ids, role_start_ids)
has_response_content = False
for start_idx in response_starts:
content_start = start_idx + len(response_template_ids)
next_role = next((r for r in role_starts if r > start_idx), len(token_ids))
if next_role > content_start:
has_response_content = True
break
return {
"text": output_text,
"has_response": has_response_content,
}
def build_and_save_dataset():
if os.path.exists(PREPROCESSED_DIR):
shutil.rmtree(PREPROCESSED_DIR, ignore_errors=True)
os.makedirs(PREPROCESSED_DIR, exist_ok=True)
raw_dataset = load_dataset("json", data_files=DATA_PATH, split="train")
dataset = raw_dataset.map(
formatting_prompts_func,
remove_columns=raw_dataset.column_names,
)
dataset = dataset.filter(lambda x: x["has_response"])
dataset = dataset.remove_columns(["has_response"])
dataset.save_to_disk(PREPROCESSED_DIR)
with open(PREPROCESS_DONE, "w", encoding="utf-8") as f:
f.write("done\n")
if local_rank == 0:
if os.path.exists(PREPROCESS_DONE):
try:
_ = load_from_disk(PREPROCESSED_DIR)
except Exception:
try:
os.remove(PREPROCESS_DONE)
except OSError:
pass
build_and_save_dataset()
else:
build_and_save_dataset()
else:
while not os.path.exists(PREPROCESS_DONE):
time.sleep(2)
dataset = None
for _ in range(5):
try:
dataset = load_from_disk(PREPROCESSED_DIR)
break
except Exception:
time.sleep(2)
if dataset is None:
raise RuntimeError(f"Failed to load dataset from {PREPROCESSED_DIR}")
split_dataset = dataset.train_test_split(test_size=0.05, seed=42)
train_dataset = split_dataset["train"]
eval_dataset = split_dataset["test"]
sft_kwargs = dict(
output_dir=OUTPUT_DIR,
dataset_text_field="text",
packing=False,
max_seq_length=max_length,
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
learning_rate=1e-5,
num_train_epochs=15,
lr_scheduler_type="cosine",
warmup_ratio=0.05,
bf16=True,
tf32=True,
gradient_checkpointing=False,
ddp_find_unused_parameters=True,
remove_unused_columns=False,
logging_steps=1,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
metric_for_best_model="eval_loss",
greater_is_better=False,
report_to="tensorboard",
)
sft_params = set(inspect.signature(SFTConfig).parameters.keys())
if "evaluation_strategy" not in sft_params and "eval_strategy" in sft_params:
sft_kwargs["eval_strategy"] = sft_kwargs["evaluation_strategy"]
sft_config = SFTConfig(**{k: v for k, v in sft_kwargs.items() if k in sft_params})
collator = CompletionOnlyDataCollator(
tokenizer=tokenizer,
response_template=RESPONSE_TEMPLATE,
max_length=max_length,
)
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16,
trust_remote_code=True,
local_files_only=True,
)
model.requires_grad_(True)
model.config.use_cache = False
trainer = SFTTrainer(
model=model,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
args=sft_config,
data_collator=collator,
processing_class=tokenizer,
)
trainer.train()
trainer.save_model(OUTPUT_DIR)
if __name__ == "__main__":
train()