Spaces:
Running
Running
feat: add GridMind GRPO training notebook using Unsloth and HF TRL
Browse files3 unmodified lines45678910111213144 unmodified lines192021222324253 unmodified lines293031323334353 unmodified lines394041424344452 unmodified lines4849505152535455565758596061626364656667686970717273747576777823 unmodified lines10210310410510610710810911011111211311411511 unmodified lines1271281291301311321331341351361371381391409 unmodified lines1501511521531541551565 unmodified lines1621631641651661671681691702 unmodified lines1731741751761771781791801811821831841851861871881891901911921931 unmodified line1951961971981992002012 unmodified lines20420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328 unmodified lines3123133143153163173183193203213223233243255 unmodified lines33133233333433533633716 unmodified lines3543553563573583593603613623633643 unmodified lines"cell_type": "markdown","metadata": {},"source": ["# ⚡ GridMind-RL: Training an LLM Energy Controller with Unsloth + GRPO\n","\n","This notebook fine-tunes **Qwen2.5-1.5B-Instruct** to manage industrial building energy\n","using Reinforcement Learning via the live **GridMind-RL OpenEnv** environment.\n","\n","| | |\n","|---|---|\n","| **Environment** | https://lo-kyu-gridmind.hf.space |\n",4 unmodified lines"### What does the agent learn?\n","- **Task 1**: Minimize energy cost by charging thermal storage off-peak\n","- **Task 2**: Maintain indoor temperature while minimizing cost\n","- **Task 3**: Full demand-response — cost + temperature + grid stress + batch scheduling + carbon"]},{3 unmodified lines"outputs": [],"source": ["%%capture\n","!pip install unsloth openenv-core\n","!pip install --no-deps bitsandbytes accelerate xformers peft trl triton\n","!pip install --no-deps cut_cross_entropy unsloth_zoo\n","!pip install \"datasets>=3.4.1,<4.0.0\" pandas matplotlib nest_asyncio"3 unmodified lines"cell_type": "markdown","metadata": {},"source": ["## Step 1 — Verify the Live Environment"]},{2 unmodified lines"metadata": {},"outputs": [],"source": ["from openenv.core import GenericEnvClient\n","import asyncio, nest_asyncio\n","nest_asyncio.apply()\n","\n","ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n","\n","async def verify_env():\n"," async with GenericEnvClient(base_url=ENV_URL) as env:\n"," r = await env.reset()\n"," print(\"✅ Environment live!\")\n"," print(\"Observation keys:\", list(r.observation.keys()))\n"," r2 = await env.step({\n"," \"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0,\n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0\n"," })\n"," print(f\"Step reward: {r2.reward:.3f}, done: {r2.done}\")\n","\n","asyncio.run(verify_env())"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 2 — Load Model with Unsloth 4-bit LoRA"]},{23 unmodified lines" use_gradient_checkpointing=\"unsloth\",\n"," random_state=42,\n",")\n","print(\"✅ Model loaded with Unsloth 4-bit LoRA\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 3 — Define Reward Functions\n","\n","We use a **composite reward** with three components:\n","\n",11 unmodified lines"metadata": {},"outputs": [],"source": ["import json, re\n","\n","def reward_valid_json(completions, **kwargs):\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r'\\{.*?\\}', text, re.DOTALL)\n"," if match:\n"," json.loads(match.group())\n"," rewards.append(0.3)\n",9 unmodified lines" for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r'\\{.*?\\}', text, re.DOTALL)\n"," if match:\n"," action = json.loads(match.group())\n"," rewards.append(0.3 if required.issubset(action.keys()) else 0.1)\n",5 unmodified lines"\n","def reward_env_interaction(completions, **kwargs):\n"," \"\"\"Reward 0.0-0.4 based on actual environment reward from live GridMind-RL HF Space.\"\"\"\n"," async def run_step(text):\n"," try:\n"," match = re.search(r'\\{.*?\\}', text, re.DOTALL)\n"," action = json.loads(match.group()) if match else {}\n"," step_action = {\n"," \"hvac_power_level\": float(max(0, min(1, action.get(\"hvac_power_level\", 0.5)))),\n",2 unmodified lines" \"load_shed_fraction\": float(max(0, min(0.5, action.get(\"load_shed_fraction\", 0.0)))),\n"," \"building_id\": 0\n"," }\n"," async with GenericEnvClient(base_url=ENV_URL) as env:\n"," await env.reset()\n"," result = await env.step(step_action)\n"," # Normalize raw env reward (~[-2, 3]) → (0.0, 0.4)\n"," return min(0.4, max(0.0, (result.reward + 2.0) * 0.08))\n"," except Exception:\n"," return 0.0\n","\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," rewards.append(asyncio.run(run_step(text)))\n"," return rewards\n","\n","print(\"✅ Reward functions defined\")\n","print(\" Total max reward per step: 1.0\")"]},1 unmodified line"cell_type": "markdown","metadata": {},"source": ["## Step 4 — Build Training Dataset & Start GRPO Training"]},{2 unmodified lines"metadata": {},"outputs": [],"source": ["from trl import GRPOTrainer, GRPOConfig\n","from datasets import Dataset\n","import pandas as pd, os\n","from transformers import TrainerCallback\n","\n","SYSTEM_PROMPT = \"\"\"You are an expert industrial building energy controller.\n","Each turn you receive the current building state and must respond with \n","ONLY a valid JSON action object.\n","\n","Action format:\n","{\"hvac_power_level\": <0.0-1.0>, \"thermal_charge_rate\": <-1.0 to 1.0>, \n"," \"batch_job_slot\": <0-4>, \"load_shed_fraction\": <0.0-0.5>, \"building_id\": 0}\n","\n","Strategy:\n","- Charge storage when price < $0.08/kWh (positive thermal_charge_rate)\n","- Discharge storage when price > $0.15/kWh (negative thermal_charge_rate) \n","- Shed load 0.3-0.5 when grid_stress_signal > 0.7\n","- Reduce HVAC during peak hours (8-12, 17-21)\n","- Keep temperature between 19-23°C\"\"\"\n","\n","def make_prompt(i):\n"," return [{\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n"," {\"role\": \"user\",\n"," \"content\": f\"Episode {i+1}: Building simulation starting. Output your first action as JSON.\"}]\n","\n","dataset = Dataset.from_dict({\"prompt\": [make_prompt(i) for i in range(300)]})\n","print(f\"✅ Dataset: {len(dataset)} training prompts\")\n","\n","# --- CSV Logger ---\n","log_history = []\n","class CSVLogger(TrainerCallback):\n"," def on_log(self, args, state, control, logs=None, **kwargs):\n"," if logs and \"loss\" in logs:\n"," entry = {**logs, \"step\": state.global_step}\n"," log_history.append(entry)\n"," os.makedirs(\"results\", exist_ok=True)\n"," pd.DataFrame(log_history).to_csv(\"results/training_log.csv\", index=False)\n","\n","training_args = GRPOConfig(\n"," output_dir=\"gridmind-grpo-unsloth\",\n"," num_train_epochs=1,\n"," per_device_train_batch_size=1,\n"," gradient_accumulation_steps=4,\n"," num_generations=4,\n"," max_prompt_length=256,\n"," max_completion_length=128,\n"," learning_rate=5e-6,\n"," lr_scheduler_type=\"cosine\",\n"," warmup_ratio=0.1,\n"," logging_steps=5,\n"," save_steps=100,\n"," fp16=True,\n"," report_to=\"none\",\n"," seed=42,\n",")\n","\n","trainer = GRPOTrainer(\n"," model=model,\n"," tokenizer=tokenizer,\n"," args=training_args,\n"," train_dataset=dataset,\n"," reward_funcs=[reward_valid_json, reward_has_required_keys, reward_env_interaction],\n"," callbacks=[CSVLogger()]\n",")\n","\n","print(\"🚀 Starting GRPO training...\")\n","trainer.train()"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 5 — Plot Training Curve\n","\n","This plot is the key **evidence of learning** for the hackathon judges."]28 unmodified lines"plt.tight_layout()\n","plt.savefig(\"results/training_curve.png\", dpi=200, bbox_inches='tight')\n","plt.show()\n","print(\"✅ Training curve saved to results/training_curve.png\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 6 — Before vs After Comparison\n","\n","Test the same scenario pre-training and post-training to show qualitative improvement."]5 unmodified lines"outputs": [],"source": ["test_state = (\n"," \"Building state: temp=24.5°C (too hot!), price=$0.18/kWh (peak), \"\n"," \"storage=0.7 (charged), grid_stress=0.85 (CRITICAL!), hour=18, step=60/95\\n\"\n"," \"Pending batch job deadlines: [12, 30]\\n\"\n"," \"Cumulative cost so far: $1.24\"\n",16 unmodified lines" )\n","\n","response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)\n","print(\"📋 Test Scenario:\")\n","print(\" \", test_state.replace(\"\\n\", \"\\n \"))\n","print(\"\\n🤖 Fine-tuned Model Response:\")\n","print(\" \", response)\n","print(\"\\n✅ Expected: load_shed_fraction > 0 (grid_stress=0.85), thermal_charge_rate < 0 (discharge at peak price)\")"]}],3 unmodified lines456789101112131415164 unmodified lines212223242526273 unmodified lines313233343536373 unmodified lines414243444546472 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828323 unmodified lines10710810911011111211311411511611711811912011 unmodified lines1321331341351361371381391401411421431441459 unmodified lines1551561571581591601615 unmodified lines1671681691701711721731741751761772 unmodified lines1801811821831841851861871881891901911921931941951961971981992002012021 unmodified line2042052062072082092102 unmodified lines21321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029128 unmodified lines3203213223233243253263273283293303313323335 unmodified lines33934034134234334434516 unmodified lines3623633643653663673683693703713723 unmodified lines"cell_type": "markdown","metadata": {},"source": ["# \u26a1 GridMind-RL: Training an LLM Energy Controller with Unsloth + GRPO\n","\n","This notebook fine-tunes **Qwen2.5-1.5B-Instruct** to manage industrial building energy\n","using Reinforcement Learning via the live **GridMind-RL OpenEnv** environment.\n","\n","**Hardware:** This notebook is designed to run on a **Hugging Face Space (ZeroGPU/A10G)** or Google Colab (T4/L4).\n","\n","| | |\n","|---|---|\n","| **Environment** | https://lo-kyu-gridmind.hf.space |\n",4 unmodified lines"### What does the agent learn?\n","- **Task 1**: Minimize energy cost by charging thermal storage off-peak\n","- **Task 2**: Maintain indoor temperature while minimizing cost\n","- **Task 3**: Full demand-response \u2014 cost + temperature + grid stress + batch scheduling + carbon"]},{3 unmodified lines"outputs": [],"source": ["%%capture\n","!pip install unsloth requests\n","!pip install --no-deps bitsandbytes accelerate xformers peft trl triton\n","!pip install --no-deps cut_cross_entropy unsloth_zoo\n","!pip install \"datasets>=3.4.1,<4.0.0\" pandas matplotlib nest_asyncio"3 unmodified lines"cell_type": "markdown","metadata": {},"source": ["## Step 1 \u2014 Verify the Live Environment"]},{2 unmodified lines"metadata": {},"outputs": [],"source": ["import requests\n","\n","ENV_URL = \"https://lo-kyu-gridmind.hf.space\"\n","\n","def verify_env():\n"," try:\n"," r = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 1, \"seed\": 42})\n"," r.raise_for_status()\n"," data = r.json()\n"," print(\"\u2705 Environment live!\")\n"," print(\"Observation keys:\", list(data.get(\"observations\", [{}])[0].keys()))\n"," r2 = requests.post(f\"{ENV_URL}/step\", json=[{\n"," \"hvac_power_level\": 0.5, \"thermal_charge_rate\": 0.0,\n"," \"batch_job_slot\": 0, \"load_shed_fraction\": 0.0, \"building_id\": 0\n"," }])\n"," res = r2.json().get(\"results\", [{}])[0]\n"," print(f\"Step reward: {res.get('reward', 0):.3f}, done: {res.get('done', False)}\")\n"," except Exception as e:\n"," print(f\"\u274c Environment verification failed: {e}\")\n","\n","verify_env()"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 2 \u2014 Load Model with Unsloth 4-bit LoRA"]},{23 unmodified lines" use_gradient_checkpointing=\"unsloth\",\n"," random_state=42,\n",")\n","print(\"\u2705 Model loaded with Unsloth 4-bit LoRA\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 3 \u2014 Define Reward Functions\n","\n","We use a **composite reward** with three components:\n","\n",11 unmodified lines"metadata": {},"outputs": [],"source": ["import json, re, requests\n","\n","def reward_valid_json(completions, **kwargs):\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," if match:\n"," json.loads(match.group())\n"," rewards.append(0.3)\n",9 unmodified lines" for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," if match:\n"," action = json.loads(match.group())\n"," rewards.append(0.3 if required.issubset(action.keys()) else 0.1)\n",5 unmodified lines"\n","def reward_env_interaction(completions, **kwargs):\n"," \"\"\"Reward 0.0-0.4 based on actual environment reward from live GridMind-RL HF Space.\"\"\"\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," action = json.loads(match.group()) if match else {}\n"," step_action = {\n"," \"hvac_power_level\": float(max(0, min(1, action.get(\"hvac_power_level\", 0.5)))),\n",2 unmodified lines" \"load_shed_fraction\": float(max(0, min(0.5, action.get(\"load_shed_fraction\", 0.0)))),\n"," \"building_id\": 0\n"," }\n"," r_reset = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 1, \"seed\": 42}, timeout=30)\n"," if r_reset.status_code != 200:\n"," rewards.append(0.0)\n"," continue\n"," r_step = requests.post(f\"{ENV_URL}/step\", json=[step_action], timeout=30)\n"," if r_step.status_code != 200:\n"," rewards.append(0.0)\n"," continue\n"," res = r_step.json().get(\"results\", [{}])[0]\n"," step_reward = float(res.get(\"reward\", 0.0))\n"," val = (step_reward + 2.0) * 0.08\n"," rewards.append(min(0.4, max(0.0, val)))\n"," except Exception:\n"," rewards.append(0.0)\n"," return rewards\n","\n","print(\"\u2705 Reward functions defined\")\n","print(\" Total max reward per step: 1.0\")"]},1 unmodified line"cell_type": "markdown","metadata": {},"source": ["## Step 4 \u2014 Build Training Dataset & Start GRPO Training"]},{2 unmodified lines"metadata": {},"outputs": [],"source": ["import json, re, requests\n","\n","def reward_valid_json(completions, **kwargs):\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," if match:\n"," json.loads(match.group())\n"," rewards.append(0.3)\n"," else:\n"," rewards.append(0.0)\n"," except Exception:\n"," rewards.append(0.0)\n"," return rewards\n","\n","def reward_has_required_keys(completions, **kwargs):\n"," required = {\"hvac_power_level\", \"thermal_charge_rate\", \"batch_job_slot\", \"load_shed_fraction\"}\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," if match:\n"," action = json.loads(match.group())\n"," rewards.append(0.3 if required.issubset(action.keys()) else 0.1)\n"," else:\n"," rewards.append(0.0)\n"," except Exception:\n"," rewards.append(0.0)\n"," return rewards\n","\n","def reward_env_interaction(completions, **kwargs):\n"," \"\"\"Reward 0.0-0.4 based on actual environment reward from live GridMind-RL HF Space.\"\"\"\n"," rewards = []\n"," for completion in completions:\n"," text = completion[0][\"content\"] if isinstance(completion, list) else completion\n"," try:\n"," match = re.search(r\"\\{.*?\\}\", text, re.DOTALL)\n"," action = json.loads(match.group()) if match else {}\n"," step_action = {\n"," \"hvac_power_level\": float(max(0, min(1, action.get(\"hvac_power_level\", 0.5)))),\n"," \"thermal_charge_rate\": float(max(-1, min(1, action.get(\"thermal_charge_rate\", 0.0)))),\n"," \"batch_job_slot\": int(max(0, min(4, action.get(\"batch_job_slot\", 0)))),\n"," \"load_shed_fraction\": float(max(0, min(0.5, action.get(\"load_shed_fraction\", 0.0)))),\n"," \"building_id\": 0\n"," }\n"," r_reset = requests.post(f\"{ENV_URL}/reset\", json={\"task_id\": 1, \"seed\": 42}, timeout=30)\n"," if r_reset.status_code != 200:\n"," rewards.append(0.0)\n"," continue\n"," r_step = requests.post(f\"{ENV_URL}/step\", json=[step_action], timeout=30)\n"," if r_step.status_code != 200:\n"," rewards.append(0.0)\n"," continue\n"," res = r_step.json().get(\"results\", [{}])[0]\n"," step_reward = float(res.get(\"reward\", 0.0))\n"," val = (step_reward + 2.0) * 0.08\n"," rewards.append(min(0.4, max(0.0, val)))\n"," except Exception:\n"," rewards.append(0.0)\n"," return rewards\n","\n","print(\"\u2705 Reward functions defined\")\n","print(\" Total max reward per step: 1.0\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 5 \u2014 Plot Training Curve\n","\n","This plot is the key **evidence of learning** for the hackathon judges."]28 unmodified lines"plt.tight_layout()\n","plt.savefig(\"results/training_curve.png\", dpi=200, bbox_inches='tight')\n","plt.show()\n","print(\"\u2705 Training curve saved to results/training_curve.png\")"]},{"cell_type": "markdown","metadata": {},"source": ["## Step 6 \u2014 Before vs After Comparison\n","\n","Test the same scenario pre-training and post-training to show qualitative improvement."]5 unmodified lines"outputs": [],"source": ["test_state = (\n"," \"Building state: temp=24.5\u00b0C (too hot!), price=$0.18/kWh (peak), \"\n"," \"storage=0.7 (charged), grid_stress=0.85 (CRITICAL!), hour=18, step=60/95\\n\"\n"," \"Pending batch job deadlines: [12, 30]\\n\"\n"," \"Cumulative cost so far: $1.24\"\n",16 unmodified lines" )\n","\n","response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)\n","print(\"\ud83d\udccb Test Scenario:\")\n","print(\" \", test_state.replace(\"\\n\", \"\\n \"))\n","print(\"\\n\ud83e\udd16 Fine-tuned Model Response:\")\n","print(\" \", response)\n","print(\"\\n\u2705 Expected: load_shed_fraction > 0 (grid_stress=0.85), thermal_charge_rate < 0 (discharge at peak price)\")"]}],
scripts/gridmind_grpo_colab.ipynb+115-107