Spaces:
Sleeping
Sleeping
Update training/train_agent.py
Browse files- training/train_agent.py +243 -70
training/train_agent.py
CHANGED
|
@@ -2,41 +2,56 @@
|
|
| 2 |
training/train_agent.py β SQL Database Engineer Agent
|
| 3 |
Unsloth + GRPO training script.
|
| 4 |
Run on venue GPU (April 25-26) with compute credits.
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import os
|
| 9 |
import json
|
| 10 |
import requests
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# ββ Try importing Unsloth (GPU only) βββββββββββββββββββββββββ
|
| 14 |
try:
|
| 15 |
from unsloth import FastLanguageModel
|
| 16 |
from trl import GRPOTrainer, GRPOConfig
|
|
|
|
| 17 |
UNSLOTH_AVAILABLE = True
|
|
|
|
| 18 |
except ImportError:
|
| 19 |
UNSLOTH_AVAILABLE = False
|
| 20 |
-
print("
|
| 21 |
|
| 22 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
-
# CONFIG
|
| 24 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 25 |
|
| 26 |
ENV_URL = os.getenv("ENV_URL", "https://junaid0600-sql-db-engineer-agent.hf.space")
|
| 27 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 28 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "unsloth/Qwen2.5-
|
| 29 |
OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./sdea-trained")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
SYSTEM_PROMPT = """You are a senior database engineer.
|
| 32 |
Given the current database state with slow queries, choose the BEST action to improve performance.
|
| 33 |
Think step by step:
|
| 34 |
-
1. If you
|
| 35 |
-
2. If you
|
| 36 |
-
3. If you know which index is missing
|
| 37 |
-
4. If query can be rewritten better
|
| 38 |
-
5. If table is huge (1M+ rows)
|
| 39 |
-
6. When performance target is reached
|
| 40 |
|
| 41 |
Respond with JSON only β no explanation, no markdown:
|
| 42 |
{"action_type": "...", "payload": {...}}"""
|
|
@@ -46,37 +61,65 @@ Respond with JSON only β no explanation, no markdown:
|
|
| 46 |
# REWARD FUNCTION (calls live HF Space)
|
| 47 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def reward_fn(prompts, completions, **kwargs):
|
| 50 |
"""
|
| 51 |
-
GRPO reward function β calls /
|
| 52 |
Returns list of float rewards, one per completion.
|
|
|
|
| 53 |
"""
|
| 54 |
rewards = []
|
| 55 |
task_ids = kwargs.get("task_ids", ["easy_s001"] * len(prompts))
|
| 56 |
|
| 57 |
for i, (prompt, completion) in enumerate(zip(prompts, completions)):
|
| 58 |
try:
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
task_id = task_ids[i] if i < len(task_ids) else "easy_s001"
|
| 66 |
-
requests.post(f"{ENV_URL}/reset",
|
| 67 |
-
json={"task_id": task_id}, timeout=15)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
resp = requests.post(
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
except json.JSONDecodeError:
|
| 77 |
-
rewards.append(0.001)
|
| 78 |
except Exception as e:
|
| 79 |
-
print(f"
|
| 80 |
rewards.append(0.001)
|
| 81 |
|
| 82 |
return rewards
|
|
@@ -90,21 +133,29 @@ def build_dataset():
|
|
| 90 |
"""Build training examples from all 15 Round 2 scenarios."""
|
| 91 |
scenarios = []
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
try:
|
| 98 |
with open(fname) as f:
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
except FileNotFoundError:
|
| 101 |
print(f"{fname} not found, skipping")
|
| 102 |
|
| 103 |
if not scenarios:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
examples = []
|
| 110 |
for s in scenarios:
|
|
@@ -121,36 +172,109 @@ Current Database State:
|
|
| 121 |
What is your next action?"""
|
| 122 |
|
| 123 |
examples.append({
|
| 124 |
-
"prompt":
|
| 125 |
-
"task_id":
|
| 126 |
})
|
| 127 |
|
| 128 |
-
print(f"Built {len(examples)} training examples")
|
|
|
|
| 129 |
return Dataset.from_list(examples)
|
| 130 |
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 133 |
# MAIN TRAINING
|
| 134 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 135 |
|
| 136 |
def train():
|
| 137 |
if not UNSLOTH_AVAILABLE:
|
| 138 |
-
print("Cannot train β Unsloth not installed")
|
| 139 |
print("Run: pip install unsloth trl transformers datasets accelerate")
|
| 140 |
return
|
| 141 |
|
| 142 |
-
print(f"
|
| 143 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
# Load model
|
| 146 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 147 |
model_name = MODEL_NAME,
|
| 148 |
-
max_seq_length =
|
| 149 |
-
load_in_4bit = True,
|
|
|
|
| 150 |
token = HF_TOKEN or None,
|
| 151 |
)
|
|
|
|
| 152 |
|
| 153 |
-
#
|
| 154 |
model = FastLanguageModel.get_peft_model(
|
| 155 |
model,
|
| 156 |
r = 16,
|
|
@@ -160,48 +284,97 @@ def train():
|
|
| 160 |
lora_dropout = 0,
|
| 161 |
bias = "none",
|
| 162 |
use_gradient_checkpointing = "unsloth",
|
|
|
|
| 163 |
)
|
|
|
|
| 164 |
|
| 165 |
-
# Build dataset
|
|
|
|
| 166 |
dataset = build_dataset()
|
|
|
|
| 167 |
|
| 168 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
config = GRPOConfig(
|
| 170 |
output_dir = OUTPUT_DIR,
|
| 171 |
-
|
| 172 |
-
per_device_train_batch_size =
|
| 173 |
gradient_accumulation_steps = 8,
|
| 174 |
-
learning_rate = 5e-
|
| 175 |
max_completion_length = 256,
|
| 176 |
-
num_generations =
|
| 177 |
-
|
|
|
|
| 178 |
save_steps = 50,
|
|
|
|
| 179 |
warmup_ratio = 0.1,
|
| 180 |
report_to = "none",
|
|
|
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# Reward function wrapper
|
| 184 |
-
def reward_wrapper(prompts, completions, **kwargs):
|
| 185 |
-
task_ids = [ex.get("task_id", "easy_s001") for ex in kwargs.get("batch", [])]
|
| 186 |
-
return reward_fn(prompts, completions, task_ids=task_ids)
|
| 187 |
-
|
| 188 |
-
# Train
|
| 189 |
trainer = GRPOTrainer(
|
| 190 |
-
model
|
| 191 |
-
tokenizer
|
| 192 |
-
reward_funcs
|
| 193 |
-
args
|
| 194 |
train_dataset = dataset,
|
| 195 |
)
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
| 198 |
trainer.train()
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
model.save_pretrained(f"{OUTPUT_DIR}/final")
|
| 202 |
tokenizer.save_pretrained(f"{OUTPUT_DIR}/final")
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
train()
|
|
|
|
| 2 |
training/train_agent.py β SQL Database Engineer Agent
|
| 3 |
Unsloth + GRPO training script.
|
| 4 |
Run on venue GPU (April 25-26) with compute credits.
|
| 5 |
+
|
| 6 |
+
FREE T4 (Colab): MODEL_NAME=unsloth/Qwen2.5-1.5B-Instruct (default)
|
| 7 |
+
VENUE A100: set ENV_VAR MODEL_NAME=unsloth/Qwen2.5-7B-Instruct
|
| 8 |
"""
|
| 9 |
|
| 10 |
import os
|
| 11 |
import json
|
| 12 |
import requests
|
| 13 |
+
import sys
|
| 14 |
+
from pathlib import Path
|
| 15 |
|
| 16 |
# ββ Try importing Unsloth (GPU only) βββββββββββββββββββββββββ
|
| 17 |
try:
|
| 18 |
from unsloth import FastLanguageModel
|
| 19 |
from trl import GRPOTrainer, GRPOConfig
|
| 20 |
+
import torch
|
| 21 |
UNSLOTH_AVAILABLE = True
|
| 22 |
+
print("Unsloth + TRL loaded successfully")
|
| 23 |
except ImportError:
|
| 24 |
UNSLOTH_AVAILABLE = False
|
| 25 |
+
print("Unsloth not available. Run: pip install unsloth trl")
|
| 26 |
|
| 27 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
# CONFIG β change MODEL_NAME via env var at venue
|
| 29 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
|
| 31 |
ENV_URL = os.getenv("ENV_URL", "https://junaid0600-sql-db-engineer-agent.hf.space")
|
| 32 |
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 33 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "unsloth/Qwen2.5-1.5B-Instruct") # 1.5B for free T4
|
| 34 |
OUTPUT_DIR = os.getenv("OUTPUT_DIR", "./sdea-trained")
|
| 35 |
+
MAX_STEPS = int(os.getenv("MAX_STEPS", "100")) # increase to 300+ at venue
|
| 36 |
+
|
| 37 |
+
print(f"[CONFIG] Model: {MODEL_NAME}")
|
| 38 |
+
print(f"[CONFIG] Output: {OUTPUT_DIR}")
|
| 39 |
+
print(f"[CONFIG] Max steps: {MAX_STEPS}")
|
| 40 |
+
print(f"[CONFIG] ENV URL: {ENV_URL}")
|
| 41 |
+
|
| 42 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
+
# SYSTEM PROMPT
|
| 44 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
|
| 46 |
+
SYSTEM_PROMPT = """You are a senior database engineer.
|
| 47 |
Given the current database state with slow queries, choose the BEST action to improve performance.
|
| 48 |
Think step by step:
|
| 49 |
+
1. If you have not inspected queries yet -> use inspect_query
|
| 50 |
+
2. If you have not analyzed indexes -> use analyze_indexes
|
| 51 |
+
3. If you know which index is missing -> use create_index
|
| 52 |
+
4. If query can be rewritten better -> use rewrite_query
|
| 53 |
+
5. If table is huge (1M+ rows) -> use partition_table
|
| 54 |
+
6. When performance target is reached -> use submit_report
|
| 55 |
|
| 56 |
Respond with JSON only β no explanation, no markdown:
|
| 57 |
{"action_type": "...", "payload": {...}}"""
|
|
|
|
| 61 |
# REWARD FUNCTION (calls live HF Space)
|
| 62 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
|
| 64 |
+
def parse_action(text: str) -> dict:
|
| 65 |
+
"""Parse LLM output into action dict. Returns safe fallback on failure."""
|
| 66 |
+
try:
|
| 67 |
+
text = text.strip()
|
| 68 |
+
if "```" in text:
|
| 69 |
+
text = text.split("```")[1]
|
| 70 |
+
if text.startswith("json"):
|
| 71 |
+
text = text[4:]
|
| 72 |
+
text = text.strip()
|
| 73 |
+
data = json.loads(text)
|
| 74 |
+
if "action_type" in data:
|
| 75 |
+
return data
|
| 76 |
+
except Exception:
|
| 77 |
+
pass
|
| 78 |
+
# Safe fallback β never crashes
|
| 79 |
+
return {"action_type": "inspect_query", "payload": {"query_id": "q1"}}
|
| 80 |
+
|
| 81 |
+
|
| 82 |
def reward_fn(prompts, completions, **kwargs):
|
| 83 |
"""
|
| 84 |
+
GRPO reward function β calls /grader on live environment.
|
| 85 |
Returns list of float rewards, one per completion.
|
| 86 |
+
Score always between 0.001 and 0.999.
|
| 87 |
"""
|
| 88 |
rewards = []
|
| 89 |
task_ids = kwargs.get("task_ids", ["easy_s001"] * len(prompts))
|
| 90 |
|
| 91 |
for i, (prompt, completion) in enumerate(zip(prompts, completions)):
|
| 92 |
try:
|
| 93 |
+
# Get completion text
|
| 94 |
+
if isinstance(completion, list):
|
| 95 |
+
text = completion[0].get("content", "") if completion else ""
|
| 96 |
+
else:
|
| 97 |
+
text = str(completion)
|
| 98 |
+
|
| 99 |
+
# Parse into action
|
| 100 |
+
action = parse_action(text)
|
| 101 |
task_id = task_ids[i] if i < len(task_ids) else "easy_s001"
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Call grader endpoint
|
| 104 |
+
resp = requests.post(
|
| 105 |
+
f"{ENV_URL}/grader",
|
| 106 |
+
json={"task_id": task_id, "action": action},
|
| 107 |
+
timeout=15,
|
| 108 |
+
headers={"Content-Type": "application/json"}
|
| 109 |
+
)
|
| 110 |
+
if resp.status_code == 200:
|
| 111 |
+
score = resp.json().get("score", 0.001)
|
| 112 |
+
score = max(0.001, min(0.999, float(score)))
|
| 113 |
+
else:
|
| 114 |
+
score = 0.001
|
| 115 |
+
|
| 116 |
+
rewards.append(score)
|
| 117 |
+
print(f" [REWARD] task={task_id} | action={action.get('action_type')} | score={score:.3f}")
|
| 118 |
|
| 119 |
except json.JSONDecodeError:
|
| 120 |
+
rewards.append(0.001)
|
| 121 |
except Exception as e:
|
| 122 |
+
print(f" [REWARD] Error: {e}")
|
| 123 |
rewards.append(0.001)
|
| 124 |
|
| 125 |
return rewards
|
|
|
|
| 133 |
"""Build training examples from all 15 Round 2 scenarios."""
|
| 134 |
scenarios = []
|
| 135 |
|
| 136 |
+
for fname in [
|
| 137 |
+
"dataset/easy_scenarios.json",
|
| 138 |
+
"dataset/medium_scenarios.json",
|
| 139 |
+
"dataset/hard_scenarios.json"
|
| 140 |
+
]:
|
| 141 |
try:
|
| 142 |
with open(fname) as f:
|
| 143 |
+
data = json.load(f)
|
| 144 |
+
scenarios.extend(data)
|
| 145 |
+
print(f" Loaded {len(data)} scenarios from {fname}")
|
| 146 |
except FileNotFoundError:
|
| 147 |
print(f"{fname} not found, skipping")
|
| 148 |
|
| 149 |
if not scenarios:
|
| 150 |
+
print("No local scenarios found. Fetching from live environment...")
|
| 151 |
+
try:
|
| 152 |
+
resp = requests.get(f"{ENV_URL}/tasks", timeout=15)
|
| 153 |
+
tasks = resp.json().get("tasks", [])
|
| 154 |
+
scenarios = [{"id": t["id"], "description": t["description"]} for t in tasks]
|
| 155 |
+
print(f" Fetched {len(scenarios)} tasks from HF Space")
|
| 156 |
+
except Exception as e:
|
| 157 |
+
print(f"Could not fetch tasks: {e}")
|
| 158 |
+
sys.exit(1)
|
| 159 |
|
| 160 |
examples = []
|
| 161 |
for s in scenarios:
|
|
|
|
| 172 |
What is your next action?"""
|
| 173 |
|
| 174 |
examples.append({
|
| 175 |
+
"prompt": prompt,
|
| 176 |
+
"task_id": s.get("id", "easy_s001"),
|
| 177 |
})
|
| 178 |
|
| 179 |
+
print(f" Built {len(examples)} training examples total")
|
| 180 |
+
from datasets import Dataset
|
| 181 |
return Dataset.from_list(examples)
|
| 182 |
|
| 183 |
|
| 184 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 185 |
+
# INFERENCE TEST β run immediately after save
|
| 186 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 187 |
+
|
| 188 |
+
def test_inference(model, tokenizer):
|
| 189 |
+
"""
|
| 190 |
+
REQUIRED: Test inference immediately after saving.
|
| 191 |
+
If this fails, the model was not saved correctly.
|
| 192 |
+
"""
|
| 193 |
+
print("\n[INFERENCE TEST] Testing saved model...")
|
| 194 |
+
try:
|
| 195 |
+
FastLanguageModel.for_inference(model)
|
| 196 |
+
|
| 197 |
+
test_prompt = f"""{SYSTEM_PROMPT}
|
| 198 |
+
|
| 199 |
+
Current Database State:
|
| 200 |
+
- Scenario: easy_s001
|
| 201 |
+
- Description: User lookup query taking 2s on 10K users table
|
| 202 |
+
- Tables: [{{"name": "users", "rows": 10000, "indexes": ["PRIMARY"]}}]
|
| 203 |
+
- Slow Queries: [{{"id": "q1", "sql": "SELECT * FROM users WHERE email=?", "avg_ms": 2000}}]
|
| 204 |
+
- Performance Score: 8.0 / 100
|
| 205 |
+
- Target Score: 80.0
|
| 206 |
+
|
| 207 |
+
What is your next action?"""
|
| 208 |
+
|
| 209 |
+
inputs = tokenizer(
|
| 210 |
+
test_prompt,
|
| 211 |
+
return_tensors="pt",
|
| 212 |
+
truncation=True,
|
| 213 |
+
max_length=1024
|
| 214 |
+
).to(model.device)
|
| 215 |
+
|
| 216 |
+
with torch.no_grad():
|
| 217 |
+
outputs = model.generate(
|
| 218 |
+
**inputs,
|
| 219 |
+
max_new_tokens = 100,
|
| 220 |
+
temperature = 0.3,
|
| 221 |
+
do_sample = True,
|
| 222 |
+
pad_token_id = tokenizer.eos_token_id,
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
response = tokenizer.decode(
|
| 226 |
+
outputs[0][inputs["input_ids"].shape[1]:],
|
| 227 |
+
skip_special_tokens=True
|
| 228 |
+
).strip()
|
| 229 |
+
|
| 230 |
+
print(f"[INFERENCE TEST] Model output:\n {response}")
|
| 231 |
+
|
| 232 |
+
# Validate output
|
| 233 |
+
action = parse_action(response)
|
| 234 |
+
print(f"[INFERENCE TEST] Parsed action: {action}")
|
| 235 |
+
print("[INFERENCE TEST] PASSED β model saved correctly!")
|
| 236 |
+
return True
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
print(f"[INFERENCE TEST] FAILED: {e}")
|
| 240 |
+
print("[INFERENCE TEST] Check model save path. Do NOT proceed without fixing this.")
|
| 241 |
+
return False
|
| 242 |
+
|
| 243 |
+
|
| 244 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 245 |
# MAIN TRAINING
|
| 246 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 247 |
|
| 248 |
def train():
|
| 249 |
if not UNSLOTH_AVAILABLE:
|
| 250 |
+
print(" Cannot train β Unsloth not installed or no GPU found")
|
| 251 |
print("Run: pip install unsloth trl transformers datasets accelerate")
|
| 252 |
return
|
| 253 |
|
| 254 |
+
print(f"\n Loading model: {MODEL_NAME}")
|
| 255 |
+
print(f" Environment: {ENV_URL}\n")
|
| 256 |
+
|
| 257 |
+
# Verify environment is reachable
|
| 258 |
+
try:
|
| 259 |
+
r = requests.get(f"{ENV_URL}/health", timeout=10)
|
| 260 |
+
version = r.json().get("version", "?")
|
| 261 |
+
print(f" Environment reachable β version {version}")
|
| 262 |
+
except Exception as e:
|
| 263 |
+
print(f" Cannot reach environment at {ENV_URL}: {e}")
|
| 264 |
+
print("Check ENV_URL and make sure HF Space is running.")
|
| 265 |
+
sys.exit(1)
|
| 266 |
|
| 267 |
+
# ββ Load model βββββββββββββββββββββββββββββββββββββββββββ
|
| 268 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 269 |
model_name = MODEL_NAME,
|
| 270 |
+
max_seq_length = 2048,
|
| 271 |
+
load_in_4bit = True, # QLoRA β required for T4
|
| 272 |
+
dtype = None, # Auto detect
|
| 273 |
token = HF_TOKEN or None,
|
| 274 |
)
|
| 275 |
+
print(" Model loaded")
|
| 276 |
|
| 277 |
+
# ββ Apply LoRA adapters ββββββββββββββββββββββββββββββββββ
|
| 278 |
model = FastLanguageModel.get_peft_model(
|
| 279 |
model,
|
| 280 |
r = 16,
|
|
|
|
| 284 |
lora_dropout = 0,
|
| 285 |
bias = "none",
|
| 286 |
use_gradient_checkpointing = "unsloth",
|
| 287 |
+
random_state = 42,
|
| 288 |
)
|
| 289 |
+
print(" LoRA adapters applied")
|
| 290 |
|
| 291 |
+
# ββ Build dataset ββββββββββββββββββββββββββββββββββββββββ
|
| 292 |
+
print("\n[DATASET] Building training dataset...")
|
| 293 |
dataset = build_dataset()
|
| 294 |
+
print(f" Dataset ready: {len(dataset)} examples")
|
| 295 |
|
| 296 |
+
# ββ Reward wrapper βββββββββββββββββββββββββββββββββββββββ
|
| 297 |
+
def reward_wrapper(prompts, completions, **kwargs):
|
| 298 |
+
batch = kwargs.get("batch", [])
|
| 299 |
+
if batch and hasattr(batch[0], "get"):
|
| 300 |
+
task_ids = [b.get("task_id", "easy_s001") for b in batch]
|
| 301 |
+
else:
|
| 302 |
+
task_ids = ["easy_s001"] * len(prompts)
|
| 303 |
+
return reward_fn(prompts, completions, task_ids=task_ids)
|
| 304 |
+
|
| 305 |
+
# ββ GRPO config ββββββββββββββββββββββββββββββββββββββββββ
|
| 306 |
+
# NOTE: batch_size=1, num_generations=2 for free T4
|
| 307 |
+
# At venue A100: increase to batch_size=2, num_generations=4
|
| 308 |
config = GRPOConfig(
|
| 309 |
output_dir = OUTPUT_DIR,
|
| 310 |
+
max_steps = MAX_STEPS,
|
| 311 |
+
per_device_train_batch_size = 1, # 1 for T4, 2 for A100
|
| 312 |
gradient_accumulation_steps = 8,
|
| 313 |
+
learning_rate = 5e-6,
|
| 314 |
max_completion_length = 256,
|
| 315 |
+
num_generations = 2, # 2 for T4, 4 for A100
|
| 316 |
+
temperature = 0.8,
|
| 317 |
+
logging_steps = 5,
|
| 318 |
save_steps = 50,
|
| 319 |
+
save_total_limit = 2,
|
| 320 |
warmup_ratio = 0.1,
|
| 321 |
report_to = "none",
|
| 322 |
+
remove_unused_columns = False,
|
| 323 |
)
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
trainer = GRPOTrainer(
|
| 326 |
+
model = model,
|
| 327 |
+
tokenizer = tokenizer,
|
| 328 |
+
reward_funcs = reward_wrapper,
|
| 329 |
+
args = config,
|
| 330 |
train_dataset = dataset,
|
| 331 |
)
|
| 332 |
|
| 333 |
+
# ββ Train ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 334 |
+
print(f"\nποΈ Starting GRPO training β {MAX_STEPS} steps...")
|
| 335 |
+
print("Watch the 'reward' column β it should increase over time.\n")
|
| 336 |
trainer.train()
|
| 337 |
+
print("\n Training complete!")
|
| 338 |
+
|
| 339 |
+
# ββ Save β ADAPTER ONLY (correct way for QLoRA) ββββββββββ
|
| 340 |
+
# DO NOT call merge_and_unload() on 4-bit model
|
| 341 |
+
# DO NOT upcast to 16-bit and merge naively
|
| 342 |
+
# CORRECT: save adapter weights only, load with from_pretrained later
|
| 343 |
+
print(f"\n[SAVE] Saving adapter to {OUTPUT_DIR}/final ...")
|
| 344 |
+
Path(OUTPUT_DIR).mkdir(parents=True, exist_ok=True)
|
| 345 |
model.save_pretrained(f"{OUTPUT_DIR}/final")
|
| 346 |
tokenizer.save_pretrained(f"{OUTPUT_DIR}/final")
|
| 347 |
+
|
| 348 |
+
# Save config for reference
|
| 349 |
+
with open(f"{OUTPUT_DIR}/final/training_config.json", "w") as f:
|
| 350 |
+
json.dump({
|
| 351 |
+
"model_name": MODEL_NAME,
|
| 352 |
+
"max_steps": MAX_STEPS,
|
| 353 |
+
"save_method": "adapter_only_qlora",
|
| 354 |
+
"lora_r": 16,
|
| 355 |
+
"lora_alpha": 16,
|
| 356 |
+
}, f, indent=2)
|
| 357 |
+
print(f" Adapter saved to {OUTPUT_DIR}/final")
|
| 358 |
+
|
| 359 |
+
# ββ IMMEDIATE inference test (required) ββββββββββββββββββ
|
| 360 |
+
passed = test_inference(model, tokenizer)
|
| 361 |
+
|
| 362 |
+
# ββ Summary ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 363 |
+
print("\n" + "="*60)
|
| 364 |
+
print("TRAINING COMPLETE")
|
| 365 |
+
print("="*60)
|
| 366 |
+
print(f" Model: {MODEL_NAME}")
|
| 367 |
+
print(f" Steps: {MAX_STEPS}")
|
| 368 |
+
print(f" Saved to: {OUTPUT_DIR}/final")
|
| 369 |
+
print(f" Save method: Adapter only (QLoRA safe)")
|
| 370 |
+
print(f" Inference test: {' PASSED' if passed else ' FAILED'}")
|
| 371 |
+
print("="*60)
|
| 372 |
+
print("\nNext steps:")
|
| 373 |
+
print(" 1. python training/evaluate_agent.py")
|
| 374 |
+
print(" 2. Open reward_curve.png β show to judges")
|
| 375 |
+
print(" 3. git add reward_curve.png && git commit && git push")
|
| 376 |
+
print("="*60)
|
| 377 |
|
| 378 |
|
| 379 |
if __name__ == "__main__":
|
| 380 |
+
train()
|