Put train.py
Browse files
train.py
CHANGED
|
@@ -17,6 +17,7 @@ import re
|
|
| 17 |
|
| 18 |
import torch
|
| 19 |
from datasets import load_dataset
|
|
|
|
| 20 |
from peft import LoraConfig
|
| 21 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 22 |
from trl import SFTConfig, SFTTrainer
|
|
@@ -65,23 +66,20 @@ def main():
|
|
| 65 |
ap.add_argument("--grad_accum", type=int, default=4)
|
| 66 |
ap.add_argument("--lr", type=float, default=5e-5)
|
| 67 |
ap.add_argument("--gc", action="store_true", help="enable gradient checkpointing")
|
| 68 |
-
ap.add_argument("--report_lengths", action="store_true")
|
| 69 |
ap.add_argument("--output", default="victor/functiongemma-270m-agent-sft-lora")
|
| 70 |
args = ap.parse_args()
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if not args.smoke:
|
| 73 |
# Fail fast if the push token is missing/invalid.
|
| 74 |
-
from huggingface_hub import HfApi, create_repo
|
| 75 |
-
|
| 76 |
HfApi().whoami(token=os.environ["HF_TOKEN"])
|
| 77 |
create_repo(args.output, token=os.environ["HF_TOKEN"], exist_ok=True)
|
| 78 |
print("auth OK; output repo ready:", args.output)
|
| 79 |
|
| 80 |
-
tokenizer = AutoTokenizer.from_pretrained(BASE, trust_remote_code=True)
|
| 81 |
-
if tokenizer.pad_token is None:
|
| 82 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 83 |
-
print("pad_token:", tokenizer.pad_token, "| pad_id:", tokenizer.pad_token_id)
|
| 84 |
-
|
| 85 |
ds = load_dataset("victor/functiongemma-agent-sft", split="train")
|
| 86 |
print("total rows:", len(ds))
|
| 87 |
if args.smoke:
|
|
@@ -152,10 +150,21 @@ def main():
|
|
| 152 |
print("SMOKE OK")
|
| 153 |
return
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
|
|
|
| 17 |
|
| 18 |
import torch
|
| 19 |
from datasets import load_dataset
|
| 20 |
+
from huggingface_hub import HfApi, create_repo
|
| 21 |
from peft import LoraConfig
|
| 22 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 23 |
from trl import SFTConfig, SFTTrainer
|
|
|
|
| 66 |
ap.add_argument("--grad_accum", type=int, default=4)
|
| 67 |
ap.add_argument("--lr", type=float, default=5e-5)
|
| 68 |
ap.add_argument("--gc", action="store_true", help="enable gradient checkpointing")
|
|
|
|
| 69 |
ap.add_argument("--output", default="victor/functiongemma-270m-agent-sft-lora")
|
| 70 |
args = ap.parse_args()
|
| 71 |
|
| 72 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE, trust_remote_code=True)
|
| 73 |
+
if tokenizer.pad_token is None:
|
| 74 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 75 |
+
print("pad_token:", tokenizer.pad_token, "| pad_id:", tokenizer.pad_token_id)
|
| 76 |
+
|
| 77 |
if not args.smoke:
|
| 78 |
# Fail fast if the push token is missing/invalid.
|
|
|
|
|
|
|
| 79 |
HfApi().whoami(token=os.environ["HF_TOKEN"])
|
| 80 |
create_repo(args.output, token=os.environ["HF_TOKEN"], exist_ok=True)
|
| 81 |
print("auth OK; output repo ready:", args.output)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
ds = load_dataset("victor/functiongemma-agent-sft", split="train")
|
| 84 |
print("total rows:", len(ds))
|
| 85 |
if args.smoke:
|
|
|
|
| 150 |
print("SMOKE OK")
|
| 151 |
return
|
| 152 |
|
| 153 |
+
token = os.environ["HF_TOKEN"]
|
| 154 |
+
# Remove files from the earlier buggy run that pushed the UNTRAINED base
|
| 155 |
+
# model into the output repo, so the repo holds only the real adapter.
|
| 156 |
+
for f in ["model.safetensors", "config.json", "generation_config.json", "README.md"]:
|
| 157 |
+
try:
|
| 158 |
+
HfApi().delete_file(path_in_repo=f, repo_id=args.output, token=token)
|
| 159 |
+
print("deleted stale file:", f)
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print("skip delete", f, "->", e)
|
| 162 |
+
|
| 163 |
+
# Push the TRAINED LoRA adapter (trainer.model is the PEFT-wrapped model).
|
| 164 |
+
trainer.model.save_pretrained("./fg-adapter", safe_serialization=True)
|
| 165 |
+
trainer.model.push_to_hub(args.output, token=token)
|
| 166 |
+
tokenizer.push_to_hub(args.output, token=token)
|
| 167 |
+
print("Pushed trained LoRA adapter to", args.output)
|
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__":
|