Spaces:
Sleeping
Sleeping
Pranjal Parashar commited on
rl training improved
Browse files- train_sft.py +58 -15
train_sft.py
CHANGED
|
@@ -18,7 +18,7 @@ def parse_args() -> argparse.Namespace:
|
|
| 18 |
parser.add_argument("--dataset-path", default="outputs/sft_traces/traces.jsonl", help="JSONL trace file")
|
| 19 |
parser.add_argument("--model-id", default="Qwen/Qwen2.5-1.5B-Instruct", help="Base instruct model")
|
| 20 |
parser.add_argument("--output-dir", default="outputs/flowos-sft", help="Checkpoint output directory")
|
| 21 |
-
parser.add_argument("--learning-rate", type=float, default=
|
| 22 |
parser.add_argument("--num-epochs", type=int, default=1, help="Training epochs")
|
| 23 |
parser.add_argument("--per-device-train-batch-size", type=int, default=1, help="Per-device batch size")
|
| 24 |
parser.add_argument("--gradient-accumulation-steps", type=int, default=4, help="Gradient accumulation")
|
|
@@ -154,24 +154,24 @@ class FlowOSSFTDataset:
|
|
| 154 |
],
|
| 155 |
add_generation_prompt=True,
|
| 156 |
)
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
prompt_ids = self.tokenizer(
|
| 160 |
prompt_text,
|
| 161 |
-
truncation=True,
|
| 162 |
-
max_length=self.max_seq_length,
|
| 163 |
add_special_tokens=False,
|
| 164 |
)["input_ids"]
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
truncation=True,
|
| 168 |
-
max_length=self.max_seq_length,
|
| 169 |
add_special_tokens=False,
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
return {
|
| 177 |
"input_ids": torch.tensor(input_ids, dtype=torch.long),
|
|
@@ -180,6 +180,40 @@ class FlowOSSFTDataset:
|
|
| 180 |
}
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
class SFTCollator:
|
| 184 |
def __init__(self, tokenizer: Any) -> None:
|
| 185 |
self.tokenizer = tokenizer
|
|
@@ -229,6 +263,15 @@ def main() -> None:
|
|
| 229 |
tokenizer = AutoTokenizer.from_pretrained(args.model_id)
|
| 230 |
if tokenizer.pad_token is None:
|
| 231 |
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
if torch.cuda.is_available():
|
| 234 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|
|
|
|
| 18 |
parser.add_argument("--dataset-path", default="outputs/sft_traces/traces.jsonl", help="JSONL trace file")
|
| 19 |
parser.add_argument("--model-id", default="Qwen/Qwen2.5-1.5B-Instruct", help="Base instruct model")
|
| 20 |
parser.add_argument("--output-dir", default="outputs/flowos-sft", help="Checkpoint output directory")
|
| 21 |
+
parser.add_argument("--learning-rate", type=float, default=5e-6, help="Learning rate")
|
| 22 |
parser.add_argument("--num-epochs", type=int, default=1, help="Training epochs")
|
| 23 |
parser.add_argument("--per-device-train-batch-size", type=int, default=1, help="Per-device batch size")
|
| 24 |
parser.add_argument("--gradient-accumulation-steps", type=int, default=4, help="Gradient accumulation")
|
|
|
|
| 154 |
],
|
| 155 |
add_generation_prompt=True,
|
| 156 |
)
|
| 157 |
+
prompt_ids_full = self.tokenizer(
|
|
|
|
|
|
|
| 158 |
prompt_text,
|
|
|
|
|
|
|
| 159 |
add_special_tokens=False,
|
| 160 |
)["input_ids"]
|
| 161 |
+
target_ids = self.tokenizer(
|
| 162 |
+
sample["target_action"],
|
|
|
|
|
|
|
| 163 |
add_special_tokens=False,
|
| 164 |
+
)["input_ids"]
|
| 165 |
+
|
| 166 |
+
# Always preserve target tokens; truncate prompt first if needed.
|
| 167 |
+
if len(target_ids) >= self.max_seq_length:
|
| 168 |
+
target_ids = target_ids[: self.max_seq_length - 1]
|
| 169 |
+
|
| 170 |
+
available_prompt_tokens = max(1, self.max_seq_length - len(target_ids))
|
| 171 |
+
prompt_ids = prompt_ids_full[-available_prompt_tokens:]
|
| 172 |
+
input_ids = prompt_ids + target_ids
|
| 173 |
+
attention_mask = [1] * len(input_ids)
|
| 174 |
+
labels = ([-100] * len(prompt_ids)) + target_ids
|
| 175 |
|
| 176 |
return {
|
| 177 |
"input_ids": torch.tensor(input_ids, dtype=torch.long),
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
|
| 183 |
+
def summarize_truncation(
|
| 184 |
+
rows: list[dict[str, Any]],
|
| 185 |
+
tokenizer: Any,
|
| 186 |
+
max_seq_length: int,
|
| 187 |
+
) -> dict[str, int]:
|
| 188 |
+
stats = {
|
| 189 |
+
"prompt_truncated_rows": 0,
|
| 190 |
+
"target_truncated_rows": 0,
|
| 191 |
+
"empty_target_rows": 0,
|
| 192 |
+
"max_prompt_tokens": 0,
|
| 193 |
+
"max_target_tokens": 0,
|
| 194 |
+
}
|
| 195 |
+
for row in rows:
|
| 196 |
+
prompt_text = apply_chat_template(
|
| 197 |
+
tokenizer,
|
| 198 |
+
[
|
| 199 |
+
{"role": "system", "content": row["system_prompt"]},
|
| 200 |
+
{"role": "user", "content": row["user_prompt"]},
|
| 201 |
+
],
|
| 202 |
+
add_generation_prompt=True,
|
| 203 |
+
)
|
| 204 |
+
prompt_ids = tokenizer(prompt_text, add_special_tokens=False)["input_ids"]
|
| 205 |
+
target_ids = tokenizer(row["target_action"], add_special_tokens=False)["input_ids"]
|
| 206 |
+
stats["max_prompt_tokens"] = max(stats["max_prompt_tokens"], len(prompt_ids))
|
| 207 |
+
stats["max_target_tokens"] = max(stats["max_target_tokens"], len(target_ids))
|
| 208 |
+
if len(target_ids) == 0:
|
| 209 |
+
stats["empty_target_rows"] += 1
|
| 210 |
+
if len(target_ids) >= max_seq_length:
|
| 211 |
+
stats["target_truncated_rows"] += 1
|
| 212 |
+
elif len(prompt_ids) + len(target_ids) > max_seq_length:
|
| 213 |
+
stats["prompt_truncated_rows"] += 1
|
| 214 |
+
return stats
|
| 215 |
+
|
| 216 |
+
|
| 217 |
class SFTCollator:
|
| 218 |
def __init__(self, tokenizer: Any) -> None:
|
| 219 |
self.tokenizer = tokenizer
|
|
|
|
| 263 |
tokenizer = AutoTokenizer.from_pretrained(args.model_id)
|
| 264 |
if tokenizer.pad_token is None:
|
| 265 |
tokenizer.pad_token = tokenizer.eos_token
|
| 266 |
+
truncation_stats = summarize_truncation(rows, tokenizer, args.max_seq_length)
|
| 267 |
+
print(
|
| 268 |
+
"Truncation stats:",
|
| 269 |
+
truncation_stats,
|
| 270 |
+
)
|
| 271 |
+
if truncation_stats["empty_target_rows"] > 0:
|
| 272 |
+
raise SystemExit(
|
| 273 |
+
f"Found {truncation_stats['empty_target_rows']} rows with empty target_action. Fix the trace data first."
|
| 274 |
+
)
|
| 275 |
|
| 276 |
if torch.cuda.is_available():
|
| 277 |
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
|