Spaces:
Running
Running
Commit ·
18750f8
1
Parent(s): e890cbb
fix: update health check endpoint in GridMind notebook and provide utility script to apply fix
Browse files- scratch/fix_health_check.py +26 -0
- scripts/gridmind_grpo_colab.ipynb +624 -623
scratch/fix_health_check.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
notebook_path = r"c:\Projects\gridmind\scripts\gridmind_grpo_colab.ipynb"
|
| 5 |
+
|
| 6 |
+
with open(notebook_path, 'r', encoding='utf-8') as f:
|
| 7 |
+
nb = json.load(f)
|
| 8 |
+
|
| 9 |
+
for i, cell in enumerate(nb['cells']):
|
| 10 |
+
if cell['cell_type'] == "code":
|
| 11 |
+
source_text = "".join(cell['source'])
|
| 12 |
+
if "health = requests.get(f\"{ENV_URL}/health\"" in source_text:
|
| 13 |
+
# Replace the health check with a safer one
|
| 14 |
+
new_source = []
|
| 15 |
+
for line in cell['source']:
|
| 16 |
+
if 'health = requests.get(f"{ENV_URL}/health"' in line:
|
| 17 |
+
new_source.append(' r = requests.get(f"{ENV_URL}", timeout=10)\n')
|
| 18 |
+
new_source.append(' health = {"status": r.status_code}\n')
|
| 19 |
+
else:
|
| 20 |
+
new_source.append(line)
|
| 21 |
+
nb['cells'][i]['source'] = new_source
|
| 22 |
+
print(f"Fixed health check in cell {i}")
|
| 23 |
+
break
|
| 24 |
+
|
| 25 |
+
with open(notebook_path, 'w', encoding='utf-8') as f:
|
| 26 |
+
json.dump(nb, f, indent=1)
|
scripts/gridmind_grpo_colab.ipynb
CHANGED
|
@@ -1,626 +1,627 @@
|
|
| 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 |
-
"cell_type": "code",
|
| 31 |
-
"execution_count": null,
|
| 32 |
-
"id": "f28e2f2c",
|
| 33 |
-
"metadata": {},
|
| 34 |
-
"outputs": [],
|
| 35 |
-
"source": [
|
| 36 |
-
"# Install dependencies\n",
|
| 37 |
-
"!pip install trl==0.8.6 transformers>=4.41.0 torch accelerate datasets requests -q\n",
|
| 38 |
-
"\n",
|
| 39 |
-
"import torch\n",
|
| 40 |
-
"import sys\n",
|
| 41 |
-
"\n",
|
| 42 |
-
"print(f\"PyTorch: {torch.__version__}\")\n",
|
| 43 |
-
"print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
|
| 44 |
-
"if torch.cuda.is_available():\n",
|
| 45 |
-
" print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
|
| 46 |
-
" print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")"
|
| 47 |
-
]
|
| 48 |
-
},
|
| 49 |
-
{
|
| 50 |
-
"cell_type": "markdown",
|
| 51 |
-
"id": "5021a299",
|
| 52 |
-
"metadata": {},
|
| 53 |
-
"source": [
|
| 54 |
-
"## Step 1: Connect to Environment and Verify Connectivity"
|
| 55 |
-
]
|
| 56 |
-
},
|
| 57 |
-
{
|
| 58 |
-
"cell_type": "code",
|
| 59 |
-
"execution_count": null,
|
| 60 |
-
"id": "4cdf0f35",
|
| 61 |
-
"metadata": {},
|
| 62 |
-
"outputs": [],
|
| 63 |
-
"source": [
|
| 64 |
-
"import requests\n",
|
| 65 |
-
"import json\n",
|
| 66 |
-
"import time\n",
|
| 67 |
-
"\n",
|
| 68 |
-
"ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n",
|
| 69 |
-
"\n",
|
| 70 |
-
"# Test connectivity\n",
|
| 71 |
-
"print(\"Testing environment connectivity...\")\n",
|
| 72 |
-
"try:\n",
|
| 73 |
-
" health = requests.get(f\"{ENV_URL}/health\", timeout=10).json()\n",
|
| 74 |
-
" print(f\"\u00e2\u0153\u201c Health check: {health}\")\n",
|
| 75 |
-
"except Exception as e:\n",
|
| 76 |
-
" print(f\"\u00e2\u0153\u2014 Health check failed: {e}\")\n",
|
| 77 |
-
" sys.exit(1)\n",
|
| 78 |
-
"\n",
|
| 79 |
-
"# Test each task reset\n",
|
| 80 |
-
"print(\"\\nTesting all 4 tasks...\")\n",
|
| 81 |
-
"for task_id in [1, 2, 3, 4]:\n",
|
| 82 |
-
" try:\n",
|
| 83 |
-
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 84 |
-
" obs = r.json()\n",
|
| 85 |
-
" has_card = \"instruction_card\" in obs or \"observations\" in obs and obs[\"observations\"][0].get(\"instruction_card\")\n",
|
| 86 |
-
" print(f\"\u00e2\u0153\u201c Task {task_id}: status={r.status_code}, has_instruction_card={has_card}\")\n",
|
| 87 |
-
" except Exception as e:\n",
|
| 88 |
-
" print(f\"\u00e2\u0153\u2014 Task {task_id} failed: {e}\")\n",
|
| 89 |
-
"\n",
|
| 90 |
-
"# Test coordinator (multi-agent)\n",
|
| 91 |
-
"print(\"\\nTesting multi-agent coordinator...\")\n",
|
| 92 |
-
"try:\n",
|
| 93 |
-
" r = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10)\n",
|
| 94 |
-
" obs = r.json()\n",
|
| 95 |
-
" n_buildings = len(obs.get(\"observations\", []))\n",
|
| 96 |
-
" print(f\"\u00e2\u0153\u201c Coordinator reset: {n_buildings} buildings\")\n",
|
| 97 |
-
"except Exception as e:\n",
|
| 98 |
-
" print(f\"\u00e2\u0153\u2014 Coordinator failed: {e}\")\n",
|
| 99 |
-
"\n",
|
| 100 |
-
"# Test world modeling\n",
|
| 101 |
-
"print(\"\\nTesting world modeling (/simulate)...\")\n",
|
| 102 |
-
"try:\n",
|
| 103 |
-
" r = requests.post(f\"{ENV_URL}/simulate\", \n",
|
| 104 |
-
" json=[{\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \n",
|
| 105 |
-
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n",
|
| 106 |
-
" timeout=10)\n",
|
| 107 |
-
" sim = r.json()\n",
|
| 108 |
-
" has_results = \"results\" in sim\n",
|
| 109 |
-
" print(f\"\u00e2\u0153\u201c Simulate: has_results={has_results}\")\n",
|
| 110 |
-
"except Exception as e:\n",
|
| 111 |
-
" print(f\"\u00e2\u0153\u2014 Simulate failed: {e}\")\n",
|
| 112 |
-
"\n",
|
| 113 |
-
"print(\"\\n\u00e2\u0153\u201c All connectivity checks passed!\")"
|
| 114 |
-
]
|
| 115 |
-
},
|
| 116 |
-
{
|
| 117 |
-
"cell_type": "markdown",
|
| 118 |
-
"id": "4a5b58c2",
|
| 119 |
-
"metadata": {},
|
| 120 |
-
"source": [
|
| 121 |
-
"## Step 2: Measure Baseline Performance (Before Training)"
|
| 122 |
-
]
|
| 123 |
-
},
|
| 124 |
-
{
|
| 125 |
-
"cell_type": "code",
|
| 126 |
-
"execution_count": null,
|
| 127 |
-
"id": "42cecadb",
|
| 128 |
-
"metadata": {},
|
| 129 |
-
"outputs": [],
|
| 130 |
-
"source": [
|
| 131 |
-
"import random\n",
|
| 132 |
-
"\n",
|
| 133 |
-
"def run_heuristic_episode(task_id=1, max_steps=96):\n",
|
| 134 |
-
" \"\"\"Run an episode using a rule-based heuristic policy.\"\"\"\n",
|
| 135 |
-
" try:\n",
|
| 136 |
-
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 137 |
-
" obs_data = r.json()\n",
|
| 138 |
-
" obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n",
|
| 139 |
-
" except:\n",
|
| 140 |
-
" return 0.0\n",
|
| 141 |
-
" \n",
|
| 142 |
-
" for step in range(max_steps):\n",
|
| 143 |
-
" # Simple heuristic: charge off-peak, discharge peak\n",
|
| 144 |
-
" hour = step // 4\n",
|
| 145 |
-
" hvac = 0.7 if 8 <= hour <= 18 else 0.3\n",
|
| 146 |
-
" charge = 0.6 if hour < 6 else (-0.4 if 14 <= hour <= 18 else 0.0)\n",
|
| 147 |
-
" shed = 0.3 if 14 <= hour <= 17 else 0.0\n",
|
| 148 |
-
" \n",
|
| 149 |
-
" action = {\n",
|
| 150 |
-
" \"hvac_power_level\": hvac,\n",
|
| 151 |
-
" \"thermal_charge_rate\": charge,\n",
|
| 152 |
-
" \"batch_job_slot\": 1 if 22 <= hour or hour <= 5 else 0,\n",
|
| 153 |
-
" \"load_shed_fraction\": shed,\n",
|
| 154 |
-
" \"building_id\": 0\n",
|
| 155 |
-
" }\n",
|
| 156 |
-
" \n",
|
| 157 |
-
" try:\n",
|
| 158 |
-
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 159 |
-
" step_data = r.json()\n",
|
| 160 |
-
" if isinstance(step_data, list):\n",
|
| 161 |
-
" step_data = step_data[0]\n",
|
| 162 |
-
" obs = step_data.get(\"observation\", obs)\n",
|
| 163 |
-
" if step_data.get(\"done\", False):\n",
|
| 164 |
-
" break\n",
|
| 165 |
-
" except:\n",
|
| 166 |
-
" break\n",
|
| 167 |
-
" \n",
|
| 168 |
-
" # Get final grade\n",
|
| 169 |
-
" try:\n",
|
| 170 |
-
" grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n",
|
| 171 |
-
" return float(grade.get(\"score\", 0))\n",
|
| 172 |
-
" except:\n",
|
| 173 |
-
" return 0.0\n",
|
| 174 |
-
"\n",
|
| 175 |
-
"print(\"Measuring heuristic baseline (2 episodes per task)...\")\n",
|
| 176 |
-
"baseline_scores = {}\n",
|
| 177 |
-
"for task_id in [1, 2, 3, 4]:\n",
|
| 178 |
-
" scores = []\n",
|
| 179 |
-
" for ep in range(2):\n",
|
| 180 |
-
" score = run_heuristic_episode(task_id=task_id)\n",
|
| 181 |
-
" scores.append(score)\n",
|
| 182 |
-
" print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n",
|
| 183 |
-
" baseline_scores[task_id] = sum(scores) / len(scores)\n",
|
| 184 |
-
"\n",
|
| 185 |
-
"print(f\"\\nHeuristic Baseline Averages:\")\n",
|
| 186 |
-
"for task_id, avg in baseline_scores.items():\n",
|
| 187 |
-
" print(f\" Task {task_id}: {avg:.3f}\")\n",
|
| 188 |
-
"print(f\" Overall: {sum(baseline_scores.values()) / len(baseline_scores):.3f}\")"
|
| 189 |
-
]
|
| 190 |
-
},
|
| 191 |
-
{
|
| 192 |
-
"cell_type": "markdown",
|
| 193 |
-
"id": "7abdd330",
|
| 194 |
-
"metadata": {},
|
| 195 |
-
"source": [
|
| 196 |
-
"## Step 3: Build Multi-Theme Training Dataset"
|
| 197 |
-
]
|
| 198 |
-
},
|
| 199 |
-
{
|
| 200 |
-
"cell_type": "code",
|
| 201 |
-
"execution_count": null,
|
| 202 |
-
"id": "1c496af9",
|
| 203 |
-
"metadata": {},
|
| 204 |
-
"outputs": [],
|
| 205 |
-
"source": [
|
| 206 |
-
"# Build a dataset that covers all 4 themes\n",
|
| 207 |
-
"dataset = []\n",
|
| 208 |
-
"\n",
|
| 209 |
-
"# Theme 1: Multi-Agent (3 buildings cooperating)\n",
|
| 210 |
-
"print(\"Building multi-agent theme examples...\")\n",
|
| 211 |
-
"for i in range(20):\n",
|
| 212 |
-
" try:\n",
|
| 213 |
-
" resp = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10).json()\n",
|
| 214 |
-
" if \"observations\" in resp:\n",
|
| 215 |
-
" for b_idx, b_obs in enumerate(resp[\"observations\"]):\n",
|
| 216 |
-
" prompt = f\"\"\"You control Building {b_idx} in a 3-building facility.\n",
|
| 217 |
-
"All buildings share one grid connection (feeder limit: 250 kW).\n",
|
| 218 |
-
"Your current state: temp={b_obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, \n",
|
| 219 |
-
"storage={b_obs.get('thermal_storage_level', 0.5):.2f}, \n",
|
| 220 |
-
"price=${b_obs.get('current_price', 0.1):.3f}/kWh\n",
|
| 221 |
-
"Grid stress signal: {b_obs.get('grid_stress_signal', 0):.2f}\n",
|
| 222 |
-
"\n",
|
| 223 |
-
"You must coordinate with other buildings to keep total feeder load under 250 kW.\n",
|
| 224 |
-
"Each building decides independently. Respond with your JSON action:\n",
|
| 225 |
-
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 226 |
-
"\"load_shed_fraction\": <0-0.5>, \"building_id\": {b_idx}}}\"\"\"\n",
|
| 227 |
-
" dataset.append({\"prompt\": prompt, \"theme\": \"multi_agent\"})\n",
|
| 228 |
-
" except:\n",
|
| 229 |
-
" pass\n",
|
| 230 |
-
"\n",
|
| 231 |
-
"print(f\"Multi-agent examples: {len([d for d in dataset if d.get('theme')=='multi_agent'])}\")\n",
|
| 232 |
-
"\n",
|
| 233 |
-
"# Theme 2: Instruction Following (Task 4 with explicit objectives)\n",
|
| 234 |
-
"print(\"Building instruction-following theme examples...\")\n",
|
| 235 |
-
"for i in range(20):\n",
|
| 236 |
-
" try:\n",
|
| 237 |
-
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 4}, timeout=10).json()\n",
|
| 238 |
-
" if \"observations\" in resp:\n",
|
| 239 |
-
" obs = resp[\"observations\"][0]\n",
|
| 240 |
-
" instruction = resp.get(\"instruction_card\", obs.get(\"instruction_card\", {}))\n",
|
| 241 |
-
" instruction_text = instruction.get(\"text\", \"Minimize cost\") if isinstance(instruction, dict) else str(instruction)\n",
|
| 242 |
-
" prompt = f\"\"\"INSTRUCTION CARD: {instruction_text}\n",
|
| 243 |
-
"\n",
|
| 244 |
-
"Current state: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, \n",
|
| 245 |
-
"storage={obs.get('thermal_storage_level', 0.5):.2f}, \n",
|
| 246 |
-
"cost_so_far=${obs.get('cumulative_cost', 0):.2f}, \n",
|
| 247 |
-
"step={obs.get('step', 0)}/96\n",
|
| 248 |
-
"\n",
|
| 249 |
-
"You MUST satisfy the instruction. Output JSON action:\n",
|
| 250 |
-
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 251 |
-
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 252 |
-
" dataset.append({\"prompt\": prompt, \"theme\": \"instruction_following\"})\n",
|
| 253 |
-
" except:\n",
|
| 254 |
-
" pass\n",
|
| 255 |
-
"\n",
|
| 256 |
-
"print(f\"Instruction-following examples: {len([d for d in dataset if d.get('theme')=='instruction_following'])}\")\n",
|
| 257 |
-
"\n",
|
| 258 |
-
"# Theme 3: World Modeling (use /simulate)\n",
|
| 259 |
-
"print(\"Building world-modeling theme examples...\")\n",
|
| 260 |
-
"for task_id in [1, 2]:\n",
|
| 261 |
-
" for i in range(10):\n",
|
| 262 |
-
" try:\n",
|
| 263 |
-
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10).json()\n",
|
| 264 |
-
" if \"observations\" in resp:\n",
|
| 265 |
-
" obs = resp[\"observations\"][0]\n",
|
| 266 |
-
" # Simulate 2 candidate actions\n",
|
| 267 |
-
" try:\n",
|
| 268 |
-
" sim_a = requests.post(f\"{ENV_URL}/simulate\",\n",
|
| 269 |
-
" json=[{\"hvac_power_level\": 0.8, \"thermal_charge_rate\": 0.3,\n",
|
| 270 |
-
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n",
|
| 271 |
-
" timeout=10).json()\n",
|
| 272 |
-
" sim_b = requests.post(f\"{ENV_URL}/simulate\",\n",
|
| 273 |
-
" json=[{\"hvac_power_level\": 0.3, \"thermal_charge_rate\": -0.2,\n",
|
| 274 |
-
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.2, \"building_id\": 0}],\n",
|
| 275 |
-
" timeout=10).json()\n",
|
| 276 |
-
" sim_context = \"\\nPredicted outcomes:\\nOption A (high HVAC): efficient\\nOption B (low HVAC): economical\"\n",
|
| 277 |
-
" except:\n",
|
| 278 |
-
" sim_context = \"\"\n",
|
| 279 |
-
" \n",
|
| 280 |
-
" prompt = f\"\"\"Plan your actions using simulation of future outcomes.\n",
|
| 281 |
-
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f}{sim_context}\n",
|
| 282 |
-
"\n",
|
| 283 |
-
"Output your best JSON action:\n",
|
| 284 |
-
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 285 |
-
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 286 |
-
" dataset.append({\"prompt\": prompt, \"theme\": \"world_modeling\"})\n",
|
| 287 |
-
" except:\n",
|
| 288 |
-
" pass\n",
|
| 289 |
-
"\n",
|
| 290 |
-
"print(f\"World-modeling examples: {len([d for d in dataset if d.get('theme')=='world_modeling'])}\")\n",
|
| 291 |
-
"\n",
|
| 292 |
-
"# Theme 4: Self-Improvement (curriculum across difficulties)\n",
|
| 293 |
-
"print(\"Building self-improvement theme examples...\")\n",
|
| 294 |
-
"for difficulty in [1, 1, 2, 2, 3, 3]:\n",
|
| 295 |
-
" try:\n",
|
| 296 |
-
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": difficulty}, timeout=10).json()\n",
|
| 297 |
-
" if \"observations\" in resp:\n",
|
| 298 |
-
" obs = resp[\"observations\"][0]\n",
|
| 299 |
-
" prompt = f\"\"\"Difficulty Level {difficulty}/3 - Control building energy system.\n",
|
| 300 |
-
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f},\n",
|
| 301 |
-
"price=${obs.get('current_price', 0.1):.3f}/kWh\n",
|
| 302 |
-
"\n",
|
| 303 |
-
"Output JSON action:\n",
|
| 304 |
-
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 305 |
-
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 306 |
-
" dataset.append({\"prompt\": prompt, \"theme\": \"curriculum\", \"difficulty\": difficulty})\n",
|
| 307 |
-
" except:\n",
|
| 308 |
-
" pass\n",
|
| 309 |
-
"\n",
|
| 310 |
-
"print(f\"Self-improvement examples: {len([d for d in dataset if d.get('theme')=='curriculum'])}\")\n",
|
| 311 |
-
"\n",
|
| 312 |
-
"print(f\"\\nTotal dataset: {len(dataset)} prompts\")\n",
|
| 313 |
-
"theme_counts = {}\n",
|
| 314 |
-
"for d in dataset:\n",
|
| 315 |
-
" theme = d.get(\"theme\", \"unknown\")\n",
|
| 316 |
-
" theme_counts[theme] = theme_counts.get(theme, 0) + 1\n",
|
| 317 |
-
"print(f\"Theme distribution: {theme_counts}\")"
|
| 318 |
-
]
|
| 319 |
-
},
|
| 320 |
-
{
|
| 321 |
-
"cell_type": "markdown",
|
| 322 |
-
"id": "2ed46c06",
|
| 323 |
-
"metadata": {},
|
| 324 |
-
"source": [
|
| 325 |
-
"## Step 4: Load Model and Tokenizer"
|
| 326 |
-
]
|
| 327 |
-
},
|
| 328 |
-
{
|
| 329 |
-
"cell_type": "code",
|
| 330 |
-
"execution_count": null,
|
| 331 |
-
"id": "5e5826e4",
|
| 332 |
-
"metadata": {},
|
| 333 |
-
"outputs": [],
|
| 334 |
-
"source": [
|
| 335 |
-
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
|
| 336 |
-
"\n",
|
| 337 |
-
"MODEL_NAME = \"Qwen/Qwen2.5-1.5B-Instruct\"\n",
|
| 338 |
-
"print(f\"Loading {MODEL_NAME}...\")\n",
|
| 339 |
-
"\n",
|
| 340 |
-
"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n",
|
| 341 |
-
"if tokenizer.pad_token is None:\n",
|
| 342 |
-
" tokenizer.pad_token = tokenizer.eos_token\n",
|
| 343 |
-
"\n",
|
| 344 |
-
"model = AutoModelForCausalLM.from_pretrained(\n",
|
| 345 |
-
" MODEL_NAME,\n",
|
| 346 |
-
" torch_dtype=torch.float16,\n",
|
| 347 |
-
" device_map=\"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 348 |
-
")\n",
|
| 349 |
-
"\n",
|
| 350 |
-
"total_params = sum(p.numel() for p in model.parameters())\n",
|
| 351 |
-
"print(f\"Model loaded. Parameters: {total_params/1e6:.0f}M\")\n",
|
| 352 |
-
"print(f\"Device: {next(model.parameters()).device}\")"
|
| 353 |
-
]
|
| 354 |
-
},
|
| 355 |
-
{
|
| 356 |
-
"cell_type": "markdown",
|
| 357 |
-
"id": "ba6645a6",
|
| 358 |
-
"metadata": {},
|
| 359 |
-
"source": [
|
| 360 |
-
"## Step 5: Define Reward Function"
|
| 361 |
-
]
|
| 362 |
-
},
|
| 363 |
-
{
|
| 364 |
-
"cell_type": "code",
|
| 365 |
-
"execution_count": null,
|
| 366 |
-
"id": "02686008",
|
| 367 |
-
"metadata": {},
|
| 368 |
-
"outputs": [],
|
| 369 |
-
"source": [
|
| 370 |
-
"import json as _json\n",
|
| 371 |
-
"\n",
|
| 372 |
-
"training_rewards = []\n",
|
| 373 |
-
"\n",
|
| 374 |
-
"def gridmind_reward_fn(completions, **kwargs):\n",
|
| 375 |
-
" \"\"\"Reward function that calls the real environment.\"\"\"\n",
|
| 376 |
-
" rewards = []\n",
|
| 377 |
-
" \n",
|
| 378 |
-
" for completion in completions:\n",
|
| 379 |
-
" try:\n",
|
| 380 |
-
" # Extract JSON action from completion\n",
|
| 381 |
-
" text = str(completion).strip()\n",
|
| 382 |
-
" start = text.rfind('{')\n",
|
| 383 |
-
" end = text.rfind('}') + 1\n",
|
| 384 |
-
" if start < 0 or end <= start:\n",
|
| 385 |
-
" rewards.append(-1.0)\n",
|
| 386 |
-
" continue\n",
|
| 387 |
-
" \n",
|
| 388 |
-
" action_str = text[start:end]\n",
|
| 389 |
-
" action = _json.loads(action_str)\n",
|
| 390 |
-
" \n",
|
| 391 |
-
" # Clamp action to valid ranges\n",
|
| 392 |
-
" action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n",
|
| 393 |
-
" action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n",
|
| 394 |
-
" action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n",
|
| 395 |
-
" action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n",
|
| 396 |
-
" action[\"building_id\"] = int(action.get(\"building_id\", 0))\n",
|
| 397 |
-
" \n",
|
| 398 |
-
" # Call environment\n",
|
| 399 |
-
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 400 |
-
" if r.status_code != 200:\n",
|
| 401 |
-
" rewards.append(-0.5)\n",
|
| 402 |
-
" continue\n",
|
| 403 |
-
" \n",
|
| 404 |
-
" step_data = r.json()\n",
|
| 405 |
-
" if isinstance(step_data, list):\n",
|
| 406 |
-
" step_data = step_data[0]\n",
|
| 407 |
-
" \n",
|
| 408 |
-
" reward = float(step_data.get(\"reward\", 0))\n",
|
| 409 |
-
" rewards.append(max(-1.0, min(1.0, reward))) # Clamp to [-1, 1]\n",
|
| 410 |
-
" training_rewards.append(reward)\n",
|
| 411 |
-
" \n",
|
| 412 |
-
" except Exception as e:\n",
|
| 413 |
-
" rewards.append(-1.0)\n",
|
| 414 |
-
" \n",
|
| 415 |
-
" return rewards\n",
|
| 416 |
-
"\n",
|
| 417 |
-
"print(\"Reward function defined.\")"
|
| 418 |
-
]
|
| 419 |
-
},
|
| 420 |
-
{
|
| 421 |
-
"cell_type": "markdown",
|
| 422 |
-
"id": "adae3837",
|
| 423 |
-
"metadata": {},
|
| 424 |
-
"source": [
|
| 425 |
-
"## Step 6: Configure and Run GRPO Training"
|
| 426 |
-
]
|
| 427 |
-
},
|
| 428 |
-
{
|
| 429 |
-
"cell_type": "code",
|
| 430 |
-
"execution_count": null,
|
| 431 |
-
"id": "ceac8c9d",
|
| 432 |
-
"metadata": {},
|
| 433 |
-
"outputs": [],
|
| 434 |
-
"source": [
|
| 435 |
-
"from trl import GRPOTrainer, GRPOConfig\n",
|
| 436 |
-
"from datasets import Dataset\n",
|
| 437 |
-
"\n",
|
| 438 |
-
"# Prepare dataset\n",
|
| 439 |
-
"train_data = [{\"prompt\": d[\"prompt\"]} for d in dataset]\n",
|
| 440 |
-
"train_ds = Dataset.from_list(train_data)\n",
|
| 441 |
-
"\n",
|
| 442 |
-
"print(f\"Training dataset: {len(train_ds)} prompts\")\n",
|
| 443 |
-
"print(f\"Sample prompt:\\n{train_data[0]['prompt'][:200]}...\\n\")\n",
|
| 444 |
-
"\n",
|
| 445 |
-
"# GRPO config for free T4 GPU\n",
|
| 446 |
-
"config = GRPOConfig(\n",
|
| 447 |
-
" output_dir=\"./gridmind-grpo-output\",\n",
|
| 448 |
-
" num_train_epochs=1,\n",
|
| 449 |
-
" max_steps=60, # Complete in ~30-40 min on T4\n",
|
| 450 |
-
" per_device_train_batch_size=2,\n",
|
| 451 |
-
" gradient_accumulation_steps=2,\n",
|
| 452 |
-
" max_new_tokens=100,\n",
|
| 453 |
-
" max_prompt_length=512,\n",
|
| 454 |
-
" learning_rate=5e-6,\n",
|
| 455 |
-
" logging_steps=5,\n",
|
| 456 |
-
" save_steps=60,\n",
|
| 457 |
-
" fp16=True,\n",
|
| 458 |
-
" dataloader_num_workers=0,\n",
|
| 459 |
-
" report_to=\"none\",\n",
|
| 460 |
-
" num_generations=2, # 2 generations per prompt for speed\n",
|
| 461 |
-
")\n",
|
| 462 |
-
"\n",
|
| 463 |
-
"print(\"\\nStarting GRPO training...\")\n",
|
| 464 |
-
"print(f\"Estimated time: 30-40 minutes on Colab T4 GPU\")\n",
|
| 465 |
-
"print(f\"Steps: {config.max_steps}, Batch size: {config.per_device_train_batch_size * config.gradient_accumulation_steps}\\n\")\n",
|
| 466 |
-
"\n",
|
| 467 |
-
"# Initialize trainer\n",
|
| 468 |
-
"trainer = GRPOTrainer(\n",
|
| 469 |
-
" model=model,\n",
|
| 470 |
-
" tokenizer=tokenizer,\n",
|
| 471 |
-
" config=config,\n",
|
| 472 |
-
" train_dataset=train_ds,\n",
|
| 473 |
-
" reward_funcs=gridmind_reward_fn,\n",
|
| 474 |
-
")\n",
|
| 475 |
-
"\n",
|
| 476 |
-
"# Train\n",
|
| 477 |
-
"trainer.train()\n",
|
| 478 |
-
"print(\"\\n\u00e2\u0153\u201c Training complete!\")"
|
| 479 |
-
]
|
| 480 |
-
},
|
| 481 |
-
{
|
| 482 |
-
"cell_type": "markdown",
|
| 483 |
-
"id": "c145c8c6",
|
| 484 |
-
"metadata": {},
|
| 485 |
-
"source": [
|
| 486 |
-
"## Step 7: Evaluate Trained Model"
|
| 487 |
-
]
|
| 488 |
-
},
|
| 489 |
-
{
|
| 490 |
-
"cell_type": "code",
|
| 491 |
-
"execution_count": null,
|
| 492 |
-
"id": "dac005cc",
|
| 493 |
-
"metadata": {},
|
| 494 |
-
"outputs": [],
|
| 495 |
-
"source": [
|
| 496 |
-
"def run_llm_episode(task_id=1, max_steps=96):\n",
|
| 497 |
-
" \"\"\"Run an episode using the trained LLM.\"\"\"\n",
|
| 498 |
-
" try:\n",
|
| 499 |
-
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 500 |
-
" obs_data = r.json()\n",
|
| 501 |
-
" obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n",
|
| 502 |
-
" except:\n",
|
| 503 |
-
" return 0.0\n",
|
| 504 |
-
" \n",
|
| 505 |
-
" model.eval()\n",
|
| 506 |
-
" \n",
|
| 507 |
-
" for step in range(max_steps):\n",
|
| 508 |
-
" prompt = f\"\"\"Control industrial building energy system.\n",
|
| 509 |
-
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f}\n",
|
| 510 |
-
"Output JSON action (hvac_power_level 0-1, thermal_charge_rate -1 to 1, batch_job_slot 0-4,\n",
|
| 511 |
-
"load_shed_fraction 0-0.5, building_id 0):\"\"\"\n",
|
| 512 |
-
" \n",
|
| 513 |
-
" try:\n",
|
| 514 |
-
" inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=400).to(model.device)\n",
|
| 515 |
-
" with torch.no_grad():\n",
|
| 516 |
-
" outputs = model.generate(**inputs, max_new_tokens=80, do_sample=False, pad_token_id=tokenizer.eos_token_id)\n",
|
| 517 |
-
" generated = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)\n",
|
| 518 |
-
" \n",
|
| 519 |
-
" start = generated.rfind('{')\n",
|
| 520 |
-
" end = generated.rfind('}') + 1\n",
|
| 521 |
-
" if start >= 0 and end > start:\n",
|
| 522 |
-
" action = _json.loads(generated[start:end])\n",
|
| 523 |
-
" action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n",
|
| 524 |
-
" action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n",
|
| 525 |
-
" action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n",
|
| 526 |
-
" action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n",
|
| 527 |
-
" action[\"building_id\"] = 0\n",
|
| 528 |
-
" else:\n",
|
| 529 |
-
" action = {\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \"batch_job_slot\": 0,\n",
|
| 530 |
-
" \"load_shed_fraction\": 0.0, \"building_id\": 0}\n",
|
| 531 |
-
" \n",
|
| 532 |
-
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 533 |
-
" step_data = r.json()\n",
|
| 534 |
-
" if isinstance(step_data, list):\n",
|
| 535 |
-
" step_data = step_data[0]\n",
|
| 536 |
-
" obs = step_data.get(\"observation\", obs)\n",
|
| 537 |
-
" if step_data.get(\"done\", False):\n",
|
| 538 |
-
" break\n",
|
| 539 |
-
" except:\n",
|
| 540 |
-
" break\n",
|
| 541 |
-
" \n",
|
| 542 |
-
" try:\n",
|
| 543 |
-
" grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n",
|
| 544 |
-
" return float(grade.get(\"score\", 0))\n",
|
| 545 |
-
" except:\n",
|
| 546 |
-
" return 0.0\n",
|
| 547 |
-
"\n",
|
| 548 |
-
"print(\"Evaluating trained model (2 episodes per task)...\")\n",
|
| 549 |
-
"trained_scores = {}\n",
|
| 550 |
-
"for task_id in [1, 2, 3, 4]:\n",
|
| 551 |
-
" scores = []\n",
|
| 552 |
-
" for ep in range(2):\n",
|
| 553 |
-
" score = run_llm_episode(task_id=task_id)\n",
|
| 554 |
-
" scores.append(score)\n",
|
| 555 |
-
" print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n",
|
| 556 |
-
" trained_scores[task_id] = sum(scores) / len(scores)\n",
|
| 557 |
-
"\n",
|
| 558 |
-
"print(f\"\\nTrained Model Scores:\")\n",
|
| 559 |
-
"for task_id, avg in trained_scores.items():\n",
|
| 560 |
-
" baseline = baseline_scores[task_id]\n",
|
| 561 |
-
" improvement = ((avg - baseline) / baseline * 100) if baseline > 0 else 0\n",
|
| 562 |
-
" print(f\" Task {task_id}: {avg:.3f} (baseline: {baseline:.3f}, {improvement:+.1f}%)\")\n",
|
| 563 |
-
"\n",
|
| 564 |
-
"trained_avg = sum(trained_scores.values()) / len(trained_scores)\n",
|
| 565 |
-
"baseline_avg = sum(baseline_scores.values()) / len(baseline_scores)\n",
|
| 566 |
-
"overall_improvement = ((trained_avg - baseline_avg) / baseline_avg * 100) if baseline_avg > 0 else 0\n",
|
| 567 |
-
"\n",
|
| 568 |
-
"print(f\"\\nOverall Scores:\")\n",
|
| 569 |
-
"print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n",
|
| 570 |
-
"print(f\" Trained LLM: {trained_avg:.3f}\")\n",
|
| 571 |
-
"print(f\" Improvement: {overall_improvement:+.1f}%\")"
|
| 572 |
-
]
|
| 573 |
-
},
|
| 574 |
-
{
|
| 575 |
-
"cell_type": "markdown",
|
| 576 |
-
"id": "0f955e71",
|
| 577 |
-
"metadata": {},
|
| 578 |
-
"source": [
|
| 579 |
-
"## Step 8: Save Results"
|
| 580 |
-
]
|
| 581 |
-
},
|
| 582 |
-
{
|
| 583 |
-
"cell_type": "code",
|
| 584 |
-
"execution_count": null,
|
| 585 |
-
"id": "00844cb1",
|
| 586 |
-
"metadata": {},
|
| 587 |
-
"outputs": [],
|
| 588 |
-
"source": [
|
| 589 |
-
"results = {\n",
|
| 590 |
-
" \"heuristic_baseline\": {\n",
|
| 591 |
-
" \"scores_by_task\": {str(k): v for k, v in baseline_scores.items()},\n",
|
| 592 |
-
" \"average\": baseline_avg\n",
|
| 593 |
-
" },\n",
|
| 594 |
-
" \"trained_llm\": {\n",
|
| 595 |
-
" \"scores_by_task\": {str(k): v for k, v in trained_scores.items()},\n",
|
| 596 |
-
" \"average\": trained_avg\n",
|
| 597 |
-
" },\n",
|
| 598 |
-
" \"improvement_percent\": overall_improvement,\n",
|
| 599 |
-
" \"model\": MODEL_NAME,\n",
|
| 600 |
-
" \"training_steps\": config.max_steps,\n",
|
| 601 |
-
" \"themes_covered\": [\"multi_agent\", \"instruction_following\", \"world_modeling\", \"curriculum\"],\n",
|
| 602 |
-
" \"training_rewards_log\": training_rewards[-20:] if training_rewards else [],\n",
|
| 603 |
-
"}\n",
|
| 604 |
-
"\n",
|
| 605 |
-
"print(\"Saving results...\")\n",
|
| 606 |
-
"with open(\"gridmind_training_results.json\", \"w\") as f:\n",
|
| 607 |
-
" _json.dump(results, f, indent=2)\n",
|
| 608 |
-
"\n",
|
| 609 |
-
"print(\"\u00e2\u0153\u201c Results saved to gridmind_training_results.json\")\n",
|
| 610 |
-
"print(f\"\\nSummary:\")\n",
|
| 611 |
-
"print(f\" Model: {MODEL_NAME}\")\n",
|
| 612 |
-
"print(f\" Themes: {results['themes_covered']}\")\n",
|
| 613 |
-
"print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n",
|
| 614 |
-
"print(f\" Trained LLM: {trained_avg:.3f}\")\n",
|
| 615 |
-
"print(f\" Improvement: {overall_improvement:+.1f}%\")"
|
| 616 |
-
]
|
| 617 |
-
}
|
| 618 |
-
],
|
| 619 |
-
"metadata": {
|
| 620 |
-
"language_info": {
|
| 621 |
-
"name": "python"
|
| 622 |
-
}
|
| 623 |
},
|
| 624 |
-
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "193da661",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# GridMind-RL: GRPO Training for Industrial Energy Management\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"**Meta PyTorch OpenEnv Hackathon \u00e2\u20ac\u201d GridMind-RL Team**\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"This notebook trains a small LLM (Qwen2.5-1.5B) using TRL GRPO on the GridMind-RL environment.\n",
|
| 13 |
+
"The environment covers all 4 hackathon themes:\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"1. **Theme 1: Multi-Agent** \u00e2\u20ac\u201d 3 buildings share a grid feeder; each agent makes independent decisions\n",
|
| 16 |
+
"2. **Theme 2: Instruction Following** \u00e2\u20ac\u201d Task 4 provides natural language objectives that must be satisfied\n",
|
| 17 |
+
"3. **Theme 3: World Modeling** \u00e2\u20ac\u201d `/simulate` endpoint predicts outcomes before committing actions\n",
|
| 18 |
+
"4. **Theme 4: Self-Improvement** \u00e2\u20ac\u201d Curriculum automatically advances difficulty as agent performance improves\n",
|
| 19 |
+
"\n",
|
| 20 |
+
"| | |\n",
|
| 21 |
+
"|---|---|\n",
|
| 22 |
+
"| **Environment** | https://lo-kyu-gridmind.hf.space |\n",
|
| 23 |
+
"| **Method** | GRPO (Group Relative Policy Optimization) |\n",
|
| 24 |
+
"| **Model** | Qwen2.5-1.5B-Instruct |\n",
|
| 25 |
+
"| **Training Time** | ~30-40 minutes on free Colab T4 GPU |\n",
|
| 26 |
+
"| **Expected Improvement** | 20-40% score gain over heuristic baseline |"
|
| 27 |
+
]
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"execution_count": null,
|
| 32 |
+
"id": "f28e2f2c",
|
| 33 |
+
"metadata": {},
|
| 34 |
+
"outputs": [],
|
| 35 |
+
"source": [
|
| 36 |
+
"# Install dependencies\n",
|
| 37 |
+
"!pip install trl==0.8.6 transformers>=4.41.0 torch accelerate datasets requests -q\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"import torch\n",
|
| 40 |
+
"import sys\n",
|
| 41 |
+
"\n",
|
| 42 |
+
"print(f\"PyTorch: {torch.__version__}\")\n",
|
| 43 |
+
"print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
|
| 44 |
+
"if torch.cuda.is_available():\n",
|
| 45 |
+
" print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
|
| 46 |
+
" print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "markdown",
|
| 51 |
+
"id": "5021a299",
|
| 52 |
+
"metadata": {},
|
| 53 |
+
"source": [
|
| 54 |
+
"## Step 1: Connect to Environment and Verify Connectivity"
|
| 55 |
+
]
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"cell_type": "code",
|
| 59 |
+
"execution_count": null,
|
| 60 |
+
"id": "4cdf0f35",
|
| 61 |
+
"metadata": {},
|
| 62 |
+
"outputs": [],
|
| 63 |
+
"source": [
|
| 64 |
+
"import requests\n",
|
| 65 |
+
"import json\n",
|
| 66 |
+
"import time\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"# Test connectivity\n",
|
| 71 |
+
"print(\"Testing environment connectivity...\")\n",
|
| 72 |
+
"try:\n",
|
| 73 |
+
" r = requests.get(f\"{ENV_URL}\", timeout=10)\n",
|
| 74 |
+
" health = {\"status\": r.status_code}\n",
|
| 75 |
+
" print(f\"\u00e2\u0153\u201c Health check: {health}\")\n",
|
| 76 |
+
"except Exception as e:\n",
|
| 77 |
+
" print(f\"\u00e2\u0153\u2014 Health check failed: {e}\")\n",
|
| 78 |
+
" sys.exit(1)\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"# Test each task reset\n",
|
| 81 |
+
"print(\"\\nTesting all 4 tasks...\")\n",
|
| 82 |
+
"for task_id in [1, 2, 3, 4]:\n",
|
| 83 |
+
" try:\n",
|
| 84 |
+
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 85 |
+
" obs = r.json()\n",
|
| 86 |
+
" has_card = \"instruction_card\" in obs or \"observations\" in obs and obs[\"observations\"][0].get(\"instruction_card\")\n",
|
| 87 |
+
" print(f\"\u00e2\u0153\u201c Task {task_id}: status={r.status_code}, has_instruction_card={has_card}\")\n",
|
| 88 |
+
" except Exception as e:\n",
|
| 89 |
+
" print(f\"\u00e2\u0153\u2014 Task {task_id} failed: {e}\")\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"# Test coordinator (multi-agent)\n",
|
| 92 |
+
"print(\"\\nTesting multi-agent coordinator...\")\n",
|
| 93 |
+
"try:\n",
|
| 94 |
+
" r = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10)\n",
|
| 95 |
+
" obs = r.json()\n",
|
| 96 |
+
" n_buildings = len(obs.get(\"observations\", []))\n",
|
| 97 |
+
" print(f\"\u00e2\u0153\u201c Coordinator reset: {n_buildings} buildings\")\n",
|
| 98 |
+
"except Exception as e:\n",
|
| 99 |
+
" print(f\"\u00e2\u0153\u2014 Coordinator failed: {e}\")\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"# Test world modeling\n",
|
| 102 |
+
"print(\"\\nTesting world modeling (/simulate)...\")\n",
|
| 103 |
+
"try:\n",
|
| 104 |
+
" r = requests.post(f\"{ENV_URL}/simulate\", \n",
|
| 105 |
+
" json=[{\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \n",
|
| 106 |
+
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n",
|
| 107 |
+
" timeout=10)\n",
|
| 108 |
+
" sim = r.json()\n",
|
| 109 |
+
" has_results = \"results\" in sim\n",
|
| 110 |
+
" print(f\"\u00e2\u0153\u201c Simulate: has_results={has_results}\")\n",
|
| 111 |
+
"except Exception as e:\n",
|
| 112 |
+
" print(f\"\u00e2\u0153\u2014 Simulate failed: {e}\")\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"print(\"\\n\u00e2\u0153\u201c All connectivity checks passed!\")"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "markdown",
|
| 119 |
+
"id": "4a5b58c2",
|
| 120 |
+
"metadata": {},
|
| 121 |
+
"source": [
|
| 122 |
+
"## Step 2: Measure Baseline Performance (Before Training)"
|
| 123 |
+
]
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"cell_type": "code",
|
| 127 |
+
"execution_count": null,
|
| 128 |
+
"id": "42cecadb",
|
| 129 |
+
"metadata": {},
|
| 130 |
+
"outputs": [],
|
| 131 |
+
"source": [
|
| 132 |
+
"import random\n",
|
| 133 |
+
"\n",
|
| 134 |
+
"def run_heuristic_episode(task_id=1, max_steps=96):\n",
|
| 135 |
+
" \"\"\"Run an episode using a rule-based heuristic policy.\"\"\"\n",
|
| 136 |
+
" try:\n",
|
| 137 |
+
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 138 |
+
" obs_data = r.json()\n",
|
| 139 |
+
" obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n",
|
| 140 |
+
" except:\n",
|
| 141 |
+
" return 0.0\n",
|
| 142 |
+
" \n",
|
| 143 |
+
" for step in range(max_steps):\n",
|
| 144 |
+
" # Simple heuristic: charge off-peak, discharge peak\n",
|
| 145 |
+
" hour = step // 4\n",
|
| 146 |
+
" hvac = 0.7 if 8 <= hour <= 18 else 0.3\n",
|
| 147 |
+
" charge = 0.6 if hour < 6 else (-0.4 if 14 <= hour <= 18 else 0.0)\n",
|
| 148 |
+
" shed = 0.3 if 14 <= hour <= 17 else 0.0\n",
|
| 149 |
+
" \n",
|
| 150 |
+
" action = {\n",
|
| 151 |
+
" \"hvac_power_level\": hvac,\n",
|
| 152 |
+
" \"thermal_charge_rate\": charge,\n",
|
| 153 |
+
" \"batch_job_slot\": 1 if 22 <= hour or hour <= 5 else 0,\n",
|
| 154 |
+
" \"load_shed_fraction\": shed,\n",
|
| 155 |
+
" \"building_id\": 0\n",
|
| 156 |
+
" }\n",
|
| 157 |
+
" \n",
|
| 158 |
+
" try:\n",
|
| 159 |
+
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 160 |
+
" step_data = r.json()\n",
|
| 161 |
+
" if isinstance(step_data, list):\n",
|
| 162 |
+
" step_data = step_data[0]\n",
|
| 163 |
+
" obs = step_data.get(\"observation\", obs)\n",
|
| 164 |
+
" if step_data.get(\"done\", False):\n",
|
| 165 |
+
" break\n",
|
| 166 |
+
" except:\n",
|
| 167 |
+
" break\n",
|
| 168 |
+
" \n",
|
| 169 |
+
" # Get final grade\n",
|
| 170 |
+
" try:\n",
|
| 171 |
+
" grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n",
|
| 172 |
+
" return float(grade.get(\"score\", 0))\n",
|
| 173 |
+
" except:\n",
|
| 174 |
+
" return 0.0\n",
|
| 175 |
+
"\n",
|
| 176 |
+
"print(\"Measuring heuristic baseline (2 episodes per task)...\")\n",
|
| 177 |
+
"baseline_scores = {}\n",
|
| 178 |
+
"for task_id in [1, 2, 3, 4]:\n",
|
| 179 |
+
" scores = []\n",
|
| 180 |
+
" for ep in range(2):\n",
|
| 181 |
+
" score = run_heuristic_episode(task_id=task_id)\n",
|
| 182 |
+
" scores.append(score)\n",
|
| 183 |
+
" print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n",
|
| 184 |
+
" baseline_scores[task_id] = sum(scores) / len(scores)\n",
|
| 185 |
+
"\n",
|
| 186 |
+
"print(f\"\\nHeuristic Baseline Averages:\")\n",
|
| 187 |
+
"for task_id, avg in baseline_scores.items():\n",
|
| 188 |
+
" print(f\" Task {task_id}: {avg:.3f}\")\n",
|
| 189 |
+
"print(f\" Overall: {sum(baseline_scores.values()) / len(baseline_scores):.3f}\")"
|
| 190 |
+
]
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
"cell_type": "markdown",
|
| 194 |
+
"id": "7abdd330",
|
| 195 |
+
"metadata": {},
|
| 196 |
+
"source": [
|
| 197 |
+
"## Step 3: Build Multi-Theme Training Dataset"
|
| 198 |
+
]
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"cell_type": "code",
|
| 202 |
+
"execution_count": null,
|
| 203 |
+
"id": "1c496af9",
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"outputs": [],
|
| 206 |
+
"source": [
|
| 207 |
+
"# Build a dataset that covers all 4 themes\n",
|
| 208 |
+
"dataset = []\n",
|
| 209 |
+
"\n",
|
| 210 |
+
"# Theme 1: Multi-Agent (3 buildings cooperating)\n",
|
| 211 |
+
"print(\"Building multi-agent theme examples...\")\n",
|
| 212 |
+
"for i in range(20):\n",
|
| 213 |
+
" try:\n",
|
| 214 |
+
" resp = requests.post(f\"{ENV_URL}/coordinator/reset\", json={}, timeout=10).json()\n",
|
| 215 |
+
" if \"observations\" in resp:\n",
|
| 216 |
+
" for b_idx, b_obs in enumerate(resp[\"observations\"]):\n",
|
| 217 |
+
" prompt = f\"\"\"You control Building {b_idx} in a 3-building facility.\n",
|
| 218 |
+
"All buildings share one grid connection (feeder limit: 250 kW).\n",
|
| 219 |
+
"Your current state: temp={b_obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, \n",
|
| 220 |
+
"storage={b_obs.get('thermal_storage_level', 0.5):.2f}, \n",
|
| 221 |
+
"price=${b_obs.get('current_price', 0.1):.3f}/kWh\n",
|
| 222 |
+
"Grid stress signal: {b_obs.get('grid_stress_signal', 0):.2f}\n",
|
| 223 |
+
"\n",
|
| 224 |
+
"You must coordinate with other buildings to keep total feeder load under 250 kW.\n",
|
| 225 |
+
"Each building decides independently. Respond with your JSON action:\n",
|
| 226 |
+
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 227 |
+
"\"load_shed_fraction\": <0-0.5>, \"building_id\": {b_idx}}}\"\"\"\n",
|
| 228 |
+
" dataset.append({\"prompt\": prompt, \"theme\": \"multi_agent\"})\n",
|
| 229 |
+
" except:\n",
|
| 230 |
+
" pass\n",
|
| 231 |
+
"\n",
|
| 232 |
+
"print(f\"Multi-agent examples: {len([d for d in dataset if d.get('theme')=='multi_agent'])}\")\n",
|
| 233 |
+
"\n",
|
| 234 |
+
"# Theme 2: Instruction Following (Task 4 with explicit objectives)\n",
|
| 235 |
+
"print(\"Building instruction-following theme examples...\")\n",
|
| 236 |
+
"for i in range(20):\n",
|
| 237 |
+
" try:\n",
|
| 238 |
+
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 4}, timeout=10).json()\n",
|
| 239 |
+
" if \"observations\" in resp:\n",
|
| 240 |
+
" obs = resp[\"observations\"][0]\n",
|
| 241 |
+
" instruction = resp.get(\"instruction_card\", obs.get(\"instruction_card\", {}))\n",
|
| 242 |
+
" instruction_text = instruction.get(\"text\", \"Minimize cost\") if isinstance(instruction, dict) else str(instruction)\n",
|
| 243 |
+
" prompt = f\"\"\"INSTRUCTION CARD: {instruction_text}\n",
|
| 244 |
+
"\n",
|
| 245 |
+
"Current state: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, \n",
|
| 246 |
+
"storage={obs.get('thermal_storage_level', 0.5):.2f}, \n",
|
| 247 |
+
"cost_so_far=${obs.get('cumulative_cost', 0):.2f}, \n",
|
| 248 |
+
"step={obs.get('step', 0)}/96\n",
|
| 249 |
+
"\n",
|
| 250 |
+
"You MUST satisfy the instruction. Output JSON action:\n",
|
| 251 |
+
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 252 |
+
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 253 |
+
" dataset.append({\"prompt\": prompt, \"theme\": \"instruction_following\"})\n",
|
| 254 |
+
" except:\n",
|
| 255 |
+
" pass\n",
|
| 256 |
+
"\n",
|
| 257 |
+
"print(f\"Instruction-following examples: {len([d for d in dataset if d.get('theme')=='instruction_following'])}\")\n",
|
| 258 |
+
"\n",
|
| 259 |
+
"# Theme 3: World Modeling (use /simulate)\n",
|
| 260 |
+
"print(\"Building world-modeling theme examples...\")\n",
|
| 261 |
+
"for task_id in [1, 2]:\n",
|
| 262 |
+
" for i in range(10):\n",
|
| 263 |
+
" try:\n",
|
| 264 |
+
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10).json()\n",
|
| 265 |
+
" if \"observations\" in resp:\n",
|
| 266 |
+
" obs = resp[\"observations\"][0]\n",
|
| 267 |
+
" # Simulate 2 candidate actions\n",
|
| 268 |
+
" try:\n",
|
| 269 |
+
" sim_a = requests.post(f\"{ENV_URL}/simulate\",\n",
|
| 270 |
+
" json=[{\"hvac_power_level\": 0.8, \"thermal_charge_rate\": 0.3,\n",
|
| 271 |
+
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0}],\n",
|
| 272 |
+
" timeout=10).json()\n",
|
| 273 |
+
" sim_b = requests.post(f\"{ENV_URL}/simulate\",\n",
|
| 274 |
+
" json=[{\"hvac_power_level\": 0.3, \"thermal_charge_rate\": -0.2,\n",
|
| 275 |
+
" \"batch_job_slot\": 0, \"load_shed_fraction\": 0.2, \"building_id\": 0}],\n",
|
| 276 |
+
" timeout=10).json()\n",
|
| 277 |
+
" sim_context = \"\\nPredicted outcomes:\\nOption A (high HVAC): efficient\\nOption B (low HVAC): economical\"\n",
|
| 278 |
+
" except:\n",
|
| 279 |
+
" sim_context = \"\"\n",
|
| 280 |
+
" \n",
|
| 281 |
+
" prompt = f\"\"\"Plan your actions using simulation of future outcomes.\n",
|
| 282 |
+
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f}{sim_context}\n",
|
| 283 |
+
"\n",
|
| 284 |
+
"Output your best JSON action:\n",
|
| 285 |
+
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 286 |
+
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 287 |
+
" dataset.append({\"prompt\": prompt, \"theme\": \"world_modeling\"})\n",
|
| 288 |
+
" except:\n",
|
| 289 |
+
" pass\n",
|
| 290 |
+
"\n",
|
| 291 |
+
"print(f\"World-modeling examples: {len([d for d in dataset if d.get('theme')=='world_modeling'])}\")\n",
|
| 292 |
+
"\n",
|
| 293 |
+
"# Theme 4: Self-Improvement (curriculum across difficulties)\n",
|
| 294 |
+
"print(\"Building self-improvement theme examples...\")\n",
|
| 295 |
+
"for difficulty in [1, 1, 2, 2, 3, 3]:\n",
|
| 296 |
+
" try:\n",
|
| 297 |
+
" resp = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": difficulty}, timeout=10).json()\n",
|
| 298 |
+
" if \"observations\" in resp:\n",
|
| 299 |
+
" obs = resp[\"observations\"][0]\n",
|
| 300 |
+
" prompt = f\"\"\"Difficulty Level {difficulty}/3 - Control building energy system.\n",
|
| 301 |
+
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f},\n",
|
| 302 |
+
"price=${obs.get('current_price', 0.1):.3f}/kWh\n",
|
| 303 |
+
"\n",
|
| 304 |
+
"Output JSON action:\n",
|
| 305 |
+
"{{\"hvac_power_level\": <0-1>, \"thermal_charge_rate\": <-1 to 1>, \"batch_job_slot\": <0-4>, \n",
|
| 306 |
+
"\"load_shed_fraction\": <0-0.5>, \"building_id\": 0}}\"\"\"\n",
|
| 307 |
+
" dataset.append({\"prompt\": prompt, \"theme\": \"curriculum\", \"difficulty\": difficulty})\n",
|
| 308 |
+
" except:\n",
|
| 309 |
+
" pass\n",
|
| 310 |
+
"\n",
|
| 311 |
+
"print(f\"Self-improvement examples: {len([d for d in dataset if d.get('theme')=='curriculum'])}\")\n",
|
| 312 |
+
"\n",
|
| 313 |
+
"print(f\"\\nTotal dataset: {len(dataset)} prompts\")\n",
|
| 314 |
+
"theme_counts = {}\n",
|
| 315 |
+
"for d in dataset:\n",
|
| 316 |
+
" theme = d.get(\"theme\", \"unknown\")\n",
|
| 317 |
+
" theme_counts[theme] = theme_counts.get(theme, 0) + 1\n",
|
| 318 |
+
"print(f\"Theme distribution: {theme_counts}\")"
|
| 319 |
+
]
|
| 320 |
+
},
|
| 321 |
+
{
|
| 322 |
+
"cell_type": "markdown",
|
| 323 |
+
"id": "2ed46c06",
|
| 324 |
+
"metadata": {},
|
| 325 |
+
"source": [
|
| 326 |
+
"## Step 4: Load Model and Tokenizer"
|
| 327 |
+
]
|
| 328 |
+
},
|
| 329 |
+
{
|
| 330 |
+
"cell_type": "code",
|
| 331 |
+
"execution_count": null,
|
| 332 |
+
"id": "5e5826e4",
|
| 333 |
+
"metadata": {},
|
| 334 |
+
"outputs": [],
|
| 335 |
+
"source": [
|
| 336 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
|
| 337 |
+
"\n",
|
| 338 |
+
"MODEL_NAME = \"Qwen/Qwen2.5-1.5B-Instruct\"\n",
|
| 339 |
+
"print(f\"Loading {MODEL_NAME}...\")\n",
|
| 340 |
+
"\n",
|
| 341 |
+
"tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)\n",
|
| 342 |
+
"if tokenizer.pad_token is None:\n",
|
| 343 |
+
" tokenizer.pad_token = tokenizer.eos_token\n",
|
| 344 |
+
"\n",
|
| 345 |
+
"model = AutoModelForCausalLM.from_pretrained(\n",
|
| 346 |
+
" MODEL_NAME,\n",
|
| 347 |
+
" torch_dtype=torch.float16,\n",
|
| 348 |
+
" device_map=\"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 349 |
+
")\n",
|
| 350 |
+
"\n",
|
| 351 |
+
"total_params = sum(p.numel() for p in model.parameters())\n",
|
| 352 |
+
"print(f\"Model loaded. Parameters: {total_params/1e6:.0f}M\")\n",
|
| 353 |
+
"print(f\"Device: {next(model.parameters()).device}\")"
|
| 354 |
+
]
|
| 355 |
+
},
|
| 356 |
+
{
|
| 357 |
+
"cell_type": "markdown",
|
| 358 |
+
"id": "ba6645a6",
|
| 359 |
+
"metadata": {},
|
| 360 |
+
"source": [
|
| 361 |
+
"## Step 5: Define Reward Function"
|
| 362 |
+
]
|
| 363 |
+
},
|
| 364 |
+
{
|
| 365 |
+
"cell_type": "code",
|
| 366 |
+
"execution_count": null,
|
| 367 |
+
"id": "02686008",
|
| 368 |
+
"metadata": {},
|
| 369 |
+
"outputs": [],
|
| 370 |
+
"source": [
|
| 371 |
+
"import json as _json\n",
|
| 372 |
+
"\n",
|
| 373 |
+
"training_rewards = []\n",
|
| 374 |
+
"\n",
|
| 375 |
+
"def gridmind_reward_fn(completions, **kwargs):\n",
|
| 376 |
+
" \"\"\"Reward function that calls the real environment.\"\"\"\n",
|
| 377 |
+
" rewards = []\n",
|
| 378 |
+
" \n",
|
| 379 |
+
" for completion in completions:\n",
|
| 380 |
+
" try:\n",
|
| 381 |
+
" # Extract JSON action from completion\n",
|
| 382 |
+
" text = str(completion).strip()\n",
|
| 383 |
+
" start = text.rfind('{')\n",
|
| 384 |
+
" end = text.rfind('}') + 1\n",
|
| 385 |
+
" if start < 0 or end <= start:\n",
|
| 386 |
+
" rewards.append(-1.0)\n",
|
| 387 |
+
" continue\n",
|
| 388 |
+
" \n",
|
| 389 |
+
" action_str = text[start:end]\n",
|
| 390 |
+
" action = _json.loads(action_str)\n",
|
| 391 |
+
" \n",
|
| 392 |
+
" # Clamp action to valid ranges\n",
|
| 393 |
+
" action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n",
|
| 394 |
+
" action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n",
|
| 395 |
+
" action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n",
|
| 396 |
+
" action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n",
|
| 397 |
+
" action[\"building_id\"] = int(action.get(\"building_id\", 0))\n",
|
| 398 |
+
" \n",
|
| 399 |
+
" # Call environment\n",
|
| 400 |
+
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 401 |
+
" if r.status_code != 200:\n",
|
| 402 |
+
" rewards.append(-0.5)\n",
|
| 403 |
+
" continue\n",
|
| 404 |
+
" \n",
|
| 405 |
+
" step_data = r.json()\n",
|
| 406 |
+
" if isinstance(step_data, list):\n",
|
| 407 |
+
" step_data = step_data[0]\n",
|
| 408 |
+
" \n",
|
| 409 |
+
" reward = float(step_data.get(\"reward\", 0))\n",
|
| 410 |
+
" rewards.append(max(-1.0, min(1.0, reward))) # Clamp to [-1, 1]\n",
|
| 411 |
+
" training_rewards.append(reward)\n",
|
| 412 |
+
" \n",
|
| 413 |
+
" except Exception as e:\n",
|
| 414 |
+
" rewards.append(-1.0)\n",
|
| 415 |
+
" \n",
|
| 416 |
+
" return rewards\n",
|
| 417 |
+
"\n",
|
| 418 |
+
"print(\"Reward function defined.\")"
|
| 419 |
+
]
|
| 420 |
+
},
|
| 421 |
+
{
|
| 422 |
+
"cell_type": "markdown",
|
| 423 |
+
"id": "adae3837",
|
| 424 |
+
"metadata": {},
|
| 425 |
+
"source": [
|
| 426 |
+
"## Step 6: Configure and Run GRPO Training"
|
| 427 |
+
]
|
| 428 |
+
},
|
| 429 |
+
{
|
| 430 |
+
"cell_type": "code",
|
| 431 |
+
"execution_count": null,
|
| 432 |
+
"id": "ceac8c9d",
|
| 433 |
+
"metadata": {},
|
| 434 |
+
"outputs": [],
|
| 435 |
+
"source": [
|
| 436 |
+
"from trl import GRPOTrainer, GRPOConfig\n",
|
| 437 |
+
"from datasets import Dataset\n",
|
| 438 |
+
"\n",
|
| 439 |
+
"# Prepare dataset\n",
|
| 440 |
+
"train_data = [{\"prompt\": d[\"prompt\"]} for d in dataset]\n",
|
| 441 |
+
"train_ds = Dataset.from_list(train_data)\n",
|
| 442 |
+
"\n",
|
| 443 |
+
"print(f\"Training dataset: {len(train_ds)} prompts\")\n",
|
| 444 |
+
"print(f\"Sample prompt:\\n{train_data[0]['prompt'][:200]}...\\n\")\n",
|
| 445 |
+
"\n",
|
| 446 |
+
"# GRPO config for free T4 GPU\n",
|
| 447 |
+
"config = GRPOConfig(\n",
|
| 448 |
+
" output_dir=\"./gridmind-grpo-output\",\n",
|
| 449 |
+
" num_train_epochs=1,\n",
|
| 450 |
+
" max_steps=60, # Complete in ~30-40 min on T4\n",
|
| 451 |
+
" per_device_train_batch_size=2,\n",
|
| 452 |
+
" gradient_accumulation_steps=2,\n",
|
| 453 |
+
" max_new_tokens=100,\n",
|
| 454 |
+
" max_prompt_length=512,\n",
|
| 455 |
+
" learning_rate=5e-6,\n",
|
| 456 |
+
" logging_steps=5,\n",
|
| 457 |
+
" save_steps=60,\n",
|
| 458 |
+
" fp16=True,\n",
|
| 459 |
+
" dataloader_num_workers=0,\n",
|
| 460 |
+
" report_to=\"none\",\n",
|
| 461 |
+
" num_generations=2, # 2 generations per prompt for speed\n",
|
| 462 |
+
")\n",
|
| 463 |
+
"\n",
|
| 464 |
+
"print(\"\\nStarting GRPO training...\")\n",
|
| 465 |
+
"print(f\"Estimated time: 30-40 minutes on Colab T4 GPU\")\n",
|
| 466 |
+
"print(f\"Steps: {config.max_steps}, Batch size: {config.per_device_train_batch_size * config.gradient_accumulation_steps}\\n\")\n",
|
| 467 |
+
"\n",
|
| 468 |
+
"# Initialize trainer\n",
|
| 469 |
+
"trainer = GRPOTrainer(\n",
|
| 470 |
+
" model=model,\n",
|
| 471 |
+
" tokenizer=tokenizer,\n",
|
| 472 |
+
" config=config,\n",
|
| 473 |
+
" train_dataset=train_ds,\n",
|
| 474 |
+
" reward_funcs=gridmind_reward_fn,\n",
|
| 475 |
+
")\n",
|
| 476 |
+
"\n",
|
| 477 |
+
"# Train\n",
|
| 478 |
+
"trainer.train()\n",
|
| 479 |
+
"print(\"\\n\u00e2\u0153\u201c Training complete!\")"
|
| 480 |
+
]
|
| 481 |
+
},
|
| 482 |
+
{
|
| 483 |
+
"cell_type": "markdown",
|
| 484 |
+
"id": "c145c8c6",
|
| 485 |
+
"metadata": {},
|
| 486 |
+
"source": [
|
| 487 |
+
"## Step 7: Evaluate Trained Model"
|
| 488 |
+
]
|
| 489 |
+
},
|
| 490 |
+
{
|
| 491 |
+
"cell_type": "code",
|
| 492 |
+
"execution_count": null,
|
| 493 |
+
"id": "dac005cc",
|
| 494 |
+
"metadata": {},
|
| 495 |
+
"outputs": [],
|
| 496 |
+
"source": [
|
| 497 |
+
"def run_llm_episode(task_id=1, max_steps=96):\n",
|
| 498 |
+
" \"\"\"Run an episode using the trained LLM.\"\"\"\n",
|
| 499 |
+
" try:\n",
|
| 500 |
+
" r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": task_id}, timeout=10)\n",
|
| 501 |
+
" obs_data = r.json()\n",
|
| 502 |
+
" obs = obs_data[\"observations\"][0] if \"observations\" in obs_data else obs_data\n",
|
| 503 |
+
" except:\n",
|
| 504 |
+
" return 0.0\n",
|
| 505 |
+
" \n",
|
| 506 |
+
" model.eval()\n",
|
| 507 |
+
" \n",
|
| 508 |
+
" for step in range(max_steps):\n",
|
| 509 |
+
" prompt = f\"\"\"Control industrial building energy system.\n",
|
| 510 |
+
"State: temp={obs.get('indoor_temperature', 21):.1f}\u00c2\u00b0C, storage={obs.get('thermal_storage_level', 0.5):.2f}\n",
|
| 511 |
+
"Output JSON action (hvac_power_level 0-1, thermal_charge_rate -1 to 1, batch_job_slot 0-4,\n",
|
| 512 |
+
"load_shed_fraction 0-0.5, building_id 0):\"\"\"\n",
|
| 513 |
+
" \n",
|
| 514 |
+
" try:\n",
|
| 515 |
+
" inputs = tokenizer(prompt, return_tensors=\"pt\", truncation=True, max_length=400).to(model.device)\n",
|
| 516 |
+
" with torch.no_grad():\n",
|
| 517 |
+
" outputs = model.generate(**inputs, max_new_tokens=80, do_sample=False, pad_token_id=tokenizer.eos_token_id)\n",
|
| 518 |
+
" generated = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)\n",
|
| 519 |
+
" \n",
|
| 520 |
+
" start = generated.rfind('{')\n",
|
| 521 |
+
" end = generated.rfind('}') + 1\n",
|
| 522 |
+
" if start >= 0 and end > start:\n",
|
| 523 |
+
" action = _json.loads(generated[start:end])\n",
|
| 524 |
+
" action[\"hvac_power_level\"] = max(0.0, min(1.0, float(action.get(\"hvac_power_level\", 0.5))))\n",
|
| 525 |
+
" action[\"thermal_charge_rate\"] = max(-1.0, min(1.0, float(action.get(\"thermal_charge_rate\", 0.0))))\n",
|
| 526 |
+
" action[\"batch_job_slot\"] = max(0, min(4, int(action.get(\"batch_job_slot\", 0))))\n",
|
| 527 |
+
" action[\"load_shed_fraction\"] = max(0.0, min(0.5, float(action.get(\"load_shed_fraction\", 0.0))))\n",
|
| 528 |
+
" action[\"building_id\"] = 0\n",
|
| 529 |
+
" else:\n",
|
| 530 |
+
" action = {\"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0, \"batch_job_slot\": 0,\n",
|
| 531 |
+
" \"load_shed_fraction\": 0.0, \"building_id\": 0}\n",
|
| 532 |
+
" \n",
|
| 533 |
+
" r = requests.post(f\"{ENV_URL}/step\", json=action, timeout=8)\n",
|
| 534 |
+
" step_data = r.json()\n",
|
| 535 |
+
" if isinstance(step_data, list):\n",
|
| 536 |
+
" step_data = step_data[0]\n",
|
| 537 |
+
" obs = step_data.get(\"observation\", obs)\n",
|
| 538 |
+
" if step_data.get(\"done\", False):\n",
|
| 539 |
+
" break\n",
|
| 540 |
+
" except:\n",
|
| 541 |
+
" break\n",
|
| 542 |
+
" \n",
|
| 543 |
+
" try:\n",
|
| 544 |
+
" grade = requests.get(f\"{ENV_URL}/grade\", timeout=10).json()\n",
|
| 545 |
+
" return float(grade.get(\"score\", 0))\n",
|
| 546 |
+
" except:\n",
|
| 547 |
+
" return 0.0\n",
|
| 548 |
+
"\n",
|
| 549 |
+
"print(\"Evaluating trained model (2 episodes per task)...\")\n",
|
| 550 |
+
"trained_scores = {}\n",
|
| 551 |
+
"for task_id in [1, 2, 3, 4]:\n",
|
| 552 |
+
" scores = []\n",
|
| 553 |
+
" for ep in range(2):\n",
|
| 554 |
+
" score = run_llm_episode(task_id=task_id)\n",
|
| 555 |
+
" scores.append(score)\n",
|
| 556 |
+
" print(f\" Task {task_id} Episode {ep+1}: {score:.3f}\")\n",
|
| 557 |
+
" trained_scores[task_id] = sum(scores) / len(scores)\n",
|
| 558 |
+
"\n",
|
| 559 |
+
"print(f\"\\nTrained Model Scores:\")\n",
|
| 560 |
+
"for task_id, avg in trained_scores.items():\n",
|
| 561 |
+
" baseline = baseline_scores[task_id]\n",
|
| 562 |
+
" improvement = ((avg - baseline) / baseline * 100) if baseline > 0 else 0\n",
|
| 563 |
+
" print(f\" Task {task_id}: {avg:.3f} (baseline: {baseline:.3f}, {improvement:+.1f}%)\")\n",
|
| 564 |
+
"\n",
|
| 565 |
+
"trained_avg = sum(trained_scores.values()) / len(trained_scores)\n",
|
| 566 |
+
"baseline_avg = sum(baseline_scores.values()) / len(baseline_scores)\n",
|
| 567 |
+
"overall_improvement = ((trained_avg - baseline_avg) / baseline_avg * 100) if baseline_avg > 0 else 0\n",
|
| 568 |
+
"\n",
|
| 569 |
+
"print(f\"\\nOverall Scores:\")\n",
|
| 570 |
+
"print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n",
|
| 571 |
+
"print(f\" Trained LLM: {trained_avg:.3f}\")\n",
|
| 572 |
+
"print(f\" Improvement: {overall_improvement:+.1f}%\")"
|
| 573 |
+
]
|
| 574 |
+
},
|
| 575 |
+
{
|
| 576 |
+
"cell_type": "markdown",
|
| 577 |
+
"id": "0f955e71",
|
| 578 |
+
"metadata": {},
|
| 579 |
+
"source": [
|
| 580 |
+
"## Step 8: Save Results"
|
| 581 |
+
]
|
| 582 |
+
},
|
| 583 |
+
{
|
| 584 |
+
"cell_type": "code",
|
| 585 |
+
"execution_count": null,
|
| 586 |
+
"id": "00844cb1",
|
| 587 |
+
"metadata": {},
|
| 588 |
+
"outputs": [],
|
| 589 |
+
"source": [
|
| 590 |
+
"results = {\n",
|
| 591 |
+
" \"heuristic_baseline\": {\n",
|
| 592 |
+
" \"scores_by_task\": {str(k): v for k, v in baseline_scores.items()},\n",
|
| 593 |
+
" \"average\": baseline_avg\n",
|
| 594 |
+
" },\n",
|
| 595 |
+
" \"trained_llm\": {\n",
|
| 596 |
+
" \"scores_by_task\": {str(k): v for k, v in trained_scores.items()},\n",
|
| 597 |
+
" \"average\": trained_avg\n",
|
| 598 |
+
" },\n",
|
| 599 |
+
" \"improvement_percent\": overall_improvement,\n",
|
| 600 |
+
" \"model\": MODEL_NAME,\n",
|
| 601 |
+
" \"training_steps\": config.max_steps,\n",
|
| 602 |
+
" \"themes_covered\": [\"multi_agent\", \"instruction_following\", \"world_modeling\", \"curriculum\"],\n",
|
| 603 |
+
" \"training_rewards_log\": training_rewards[-20:] if training_rewards else [],\n",
|
| 604 |
+
"}\n",
|
| 605 |
+
"\n",
|
| 606 |
+
"print(\"Saving results...\")\n",
|
| 607 |
+
"with open(\"gridmind_training_results.json\", \"w\") as f:\n",
|
| 608 |
+
" _json.dump(results, f, indent=2)\n",
|
| 609 |
+
"\n",
|
| 610 |
+
"print(\"\u00e2\u0153\u201c Results saved to gridmind_training_results.json\")\n",
|
| 611 |
+
"print(f\"\\nSummary:\")\n",
|
| 612 |
+
"print(f\" Model: {MODEL_NAME}\")\n",
|
| 613 |
+
"print(f\" Themes: {results['themes_covered']}\")\n",
|
| 614 |
+
"print(f\" Heuristic baseline: {baseline_avg:.3f}\")\n",
|
| 615 |
+
"print(f\" Trained LLM: {trained_avg:.3f}\")\n",
|
| 616 |
+
"print(f\" Improvement: {overall_improvement:+.1f}%\")"
|
| 617 |
+
]
|
| 618 |
+
}
|
| 619 |
+
],
|
| 620 |
+
"metadata": {
|
| 621 |
+
"language_info": {
|
| 622 |
+
"name": "python"
|
| 623 |
+
}
|
| 624 |
+
},
|
| 625 |
+
"nbformat": 4,
|
| 626 |
+
"nbformat_minor": 5
|
| 627 |
}
|