Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- client.py +3 -0
- notebooks/grpo_aegis_hf_space_unsloth.ipynb +335 -0
client.py
CHANGED
|
@@ -51,6 +51,9 @@ class AegisEnv(
|
|
| 51 |
rubric=obs_data.get("rubric", ""),
|
| 52 |
max_score=float(obs_data.get("max_score", 1.0)),
|
| 53 |
student_answer=obs_data.get("student_answer", ""),
|
|
|
|
|
|
|
|
|
|
| 54 |
grading_info=obs_data.get("grading_info") or {},
|
| 55 |
done=payload.get("done", False),
|
| 56 |
reward=payload.get("reward"),
|
|
|
|
| 51 |
rubric=obs_data.get("rubric", ""),
|
| 52 |
max_score=float(obs_data.get("max_score", 1.0)),
|
| 53 |
student_answer=obs_data.get("student_answer", ""),
|
| 54 |
+
current_stage=str(obs_data.get("current_stage") or "arbiter"),
|
| 55 |
+
refinement_loops_taken=int(obs_data.get("refinement_loops_taken") or 0),
|
| 56 |
+
pipeline_history=str(obs_data.get("pipeline_history") or ""),
|
| 57 |
grading_info=obs_data.get("grading_info") or {},
|
| 58 |
done=payload.get("done", False),
|
| 59 |
reward=payload.get("reward"),
|
notebooks/grpo_aegis_hf_space_unsloth.ipynb
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# GRPO fine-tuning (Unsloth) — AEGIS-Env against your Hugging Face Space\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"This notebook trains a **small language model** with **GRPO** using rewards from **[AEGIS-Env](https://huggingface.co/spaces/NishithP2004/aegis-env)**.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"- **Remote environment:** OpenEnv WebSocket API at `…/openenv` on the Space.\n",
|
| 12 |
+
"- **Three difficulty tiers:** `easy`, `medium`, `hard` → same mapping as the server (`mohler` / `asap-sas` / `ricechem` datasets).\n",
|
| 13 |
+
"- **Reward signal:** After a **fixed deterministic replay** through arbiter → scrutinizer → validator, the model’s completion is parsed as a **mentor-stage** JSON action (`AegisAction`); the Space returns the **final step reward** in `[0, 1]`.\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"**Requirements:** GPU runtime (e.g. T4). The Space must be awake (cold start can take a minute). The clone step pulls `client.py`, `models.py`, and `inference.py` from your Space repo for `build_user_prompt` / `_parse_action`.\n"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"cell_type": "code",
|
| 20 |
+
"execution_count": null,
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"outputs": [],
|
| 23 |
+
"source": [
|
| 24 |
+
"# %%capture\n",
|
| 25 |
+
"import os, importlib.util\n",
|
| 26 |
+
"!pip install -qqq uv nest_asyncio\n",
|
| 27 |
+
"if importlib.util.find_spec(\"torch\") is None or \"COLAB_\" in \"\".join(os.environ.keys()):\n",
|
| 28 |
+
" try:\n",
|
| 29 |
+
" import numpy\n",
|
| 30 |
+
" get_numpy = f\"numpy=={numpy.__version__}\"\n",
|
| 31 |
+
" except Exception:\n",
|
| 32 |
+
" get_numpy = \"numpy\"\n",
|
| 33 |
+
" !uv pip install -qqq \\\n",
|
| 34 |
+
" \"torch>=2.8.0\" \"triton>=3.4.0\" {get_numpy} torchvision bitsandbytes \"transformers==4.56.2\" trackio \\\n",
|
| 35 |
+
" \"unsloth_zoo[base] @ git+https://github.com/unslothai/unsloth-zoo\" \\\n",
|
| 36 |
+
" \"unsloth[base] @ git+https://github.com/unslothai/unsloth\" \\\n",
|
| 37 |
+
" git+https://github.com/triton-lang/triton.git@0add68262ab0a2e33b84524346cb27cbb2787356#subdirectory=python/triton_kernels\n",
|
| 38 |
+
"elif importlib.util.find_spec(\"unsloth\") is None:\n",
|
| 39 |
+
" !uv pip install -qqq unsloth trackio\n",
|
| 40 |
+
"!uv pip install --upgrade --no-deps transformers==4.56.2 tokenizers trl==0.22.2 unsloth unsloth_zoo\n",
|
| 41 |
+
"!uv pip install -qqq \"openenv-core[core]>=0.2.2\" openai pydantic\n"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "code",
|
| 46 |
+
"execution_count": null,
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"outputs": [],
|
| 49 |
+
"source": [
|
| 50 |
+
"import os\n",
|
| 51 |
+
"# AEGIS-Env code (client + prompts). Override with your Git repo URL if preferred.\n",
|
| 52 |
+
"AEGIS_REPO = os.environ.get(\n",
|
| 53 |
+
" \"AEGIS_CODE_CLONE_URL\",\n",
|
| 54 |
+
" \"https://huggingface.co/spaces/NishithP2004/aegis-env\",\n",
|
| 55 |
+
")\n",
|
| 56 |
+
"!rm -rf /content/aegis-env-space && git clone --depth 1 {AEGIS_REPO} /content/aegis-env-space\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"import sys\n",
|
| 59 |
+
"sys.path.insert(0, \"/content/aegis-env-space\")\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"import nest_asyncio\n",
|
| 62 |
+
"nest_asyncio.apply()\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"AEGIS_OPENENV_BASE = os.environ.get(\n",
|
| 65 |
+
" \"AEGIS_OPENENV_BASE\",\n",
|
| 66 |
+
" \"https://huggingface.co/spaces/NishithP2004/aegis-env/openenv\",\n",
|
| 67 |
+
")\n",
|
| 68 |
+
"print(\"OpenEnv WS/HTTP base:\", AEGIS_OPENENV_BASE)\n"
|
| 69 |
+
]
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"cell_type": "code",
|
| 73 |
+
"execution_count": null,
|
| 74 |
+
"id": "7e9a6ebc",
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"outputs": [],
|
| 77 |
+
"source": [
|
| 78 |
+
"import asyncio\n",
|
| 79 |
+
"from typing import Any, List\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"from datasets import Dataset\n",
|
| 82 |
+
"from transformers import TextStreamer\n",
|
| 83 |
+
"from trl import GRPOConfig, GRPOTrainer\n",
|
| 84 |
+
"from unsloth import FastLanguageModel\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"from models import AegisAction, AegisObservation\n",
|
| 87 |
+
"from client import AegisEnv\n",
|
| 88 |
+
"from inference import build_user_prompt, _parse_action, _strip_markdown_json_fence\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"async def replay_to_mentor_obs(env: AegisEnv, seed: int, task_name: str) -> AegisObservation:\n",
|
| 92 |
+
" r = await env.reset(seed=seed, task_name=task_name)\n",
|
| 93 |
+
" obs = r.observation\n",
|
| 94 |
+
" ms = float(obs.max_score or 1.0)\n",
|
| 95 |
+
" mid = max(0.0, min(ms, ms * 0.5))\n",
|
| 96 |
+
" a1 = AegisAction(\n",
|
| 97 |
+
" proposed_score=mid,\n",
|
| 98 |
+
" agent_reasoning=\"Arbiter: preliminary assessment aligned with the rubric.\",\n",
|
| 99 |
+
" routing_decision=\"proceed\",\n",
|
| 100 |
+
" )\n",
|
| 101 |
+
" obs = (await env.step(a1)).observation\n",
|
| 102 |
+
" a2 = AegisAction(\n",
|
| 103 |
+
" proposed_score=mid,\n",
|
| 104 |
+
" agent_reasoning=\"Scrutinizer: refined criterion-by-criterion review.\",\n",
|
| 105 |
+
" routing_decision=\"proceed\",\n",
|
| 106 |
+
" )\n",
|
| 107 |
+
" obs = (await env.step(a2)).observation\n",
|
| 108 |
+
" a3 = AegisAction(\n",
|
| 109 |
+
" proposed_score=mid,\n",
|
| 110 |
+
" agent_reasoning=\"Validator: consistency check passed; proceed to mentor.\",\n",
|
| 111 |
+
" routing_decision=\"proceed\",\n",
|
| 112 |
+
" )\n",
|
| 113 |
+
" obs = (await env.step(a3)).observation\n",
|
| 114 |
+
" if str(getattr(obs, \"current_stage\", \"\") or \"\") != \"mentor\":\n",
|
| 115 |
+
" raise RuntimeError(f\"Expected mentor stage, got {getattr(obs, 'current_stage', None)}\")\n",
|
| 116 |
+
" return obs\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"\n",
|
| 119 |
+
"def _run(coro):\n",
|
| 120 |
+
" return asyncio.get_event_loop().run_until_complete(coro)\n",
|
| 121 |
+
"\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"async def build_one_row(task_name: str, seed: int, base_url: str) -> dict[str, Any]:\n",
|
| 124 |
+
" env = AegisEnv(base_url=base_url)\n",
|
| 125 |
+
" await env.connect()\n",
|
| 126 |
+
" try:\n",
|
| 127 |
+
" obs = await replay_to_mentor_obs(env, seed, task_name)\n",
|
| 128 |
+
" user_text = build_user_prompt(4, None, 0.0, [], obs)\n",
|
| 129 |
+
" return {\n",
|
| 130 |
+
" \"prompt\": [{\"role\": \"user\", \"content\": user_text}],\n",
|
| 131 |
+
" \"answer\": 0,\n",
|
| 132 |
+
" \"task_name\": task_name,\n",
|
| 133 |
+
" \"episode_seed\": int(seed),\n",
|
| 134 |
+
" \"mentor_max_score\": float(obs.max_score or 1.0),\n",
|
| 135 |
+
" }\n",
|
| 136 |
+
" finally:\n",
|
| 137 |
+
" await env.close()\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"def build_dataset(\n",
|
| 141 |
+
" base_url: str,\n",
|
| 142 |
+
" n_per_tier: int = 40,\n",
|
| 143 |
+
") -> Dataset:\n",
|
| 144 |
+
" rows: List[dict] = []\n",
|
| 145 |
+
" tier_seeds = {\"easy\": 1000, \"medium\": 11000, \"hard\": 21000}\n",
|
| 146 |
+
" for task_name in (\"easy\", \"medium\", \"hard\"):\n",
|
| 147 |
+
" base_seed = tier_seeds[task_name]\n",
|
| 148 |
+
" for i in range(n_per_tier):\n",
|
| 149 |
+
" seed = base_seed + i\n",
|
| 150 |
+
" row = _run(build_one_row(task_name, seed, base_url))\n",
|
| 151 |
+
" rows.append(row)\n",
|
| 152 |
+
" print(\"built\", task_name, i, \"max_score\", row[\"mentor_max_score\"])\n",
|
| 153 |
+
" return Dataset.from_list(rows)\n"
|
| 154 |
+
]
|
| 155 |
+
},
|
| 156 |
+
{
|
| 157 |
+
"cell_type": "code",
|
| 158 |
+
"execution_count": null,
|
| 159 |
+
"metadata": {},
|
| 160 |
+
"outputs": [],
|
| 161 |
+
"source": [
|
| 162 |
+
"# Build prompts against the live Space (sequential; Space allows limited concurrency)\n",
|
| 163 |
+
"train_ds = build_dataset(AEGIS_OPENENV_BASE, n_per_tier=40)\n",
|
| 164 |
+
"sample = train_ds[0][\"prompt\"]\n",
|
| 165 |
+
"print(train_ds)\n"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "code",
|
| 170 |
+
"execution_count": null,
|
| 171 |
+
"metadata": {},
|
| 172 |
+
"outputs": [],
|
| 173 |
+
"source": [
|
| 174 |
+
"MODEL_NAME = \"unsloth/Llama-3.2-3B-Instruct-bnb-4bit\"\n",
|
| 175 |
+
"max_seq_length = 4096\n",
|
| 176 |
+
"lora_rank = 8\n",
|
| 177 |
+
"\n",
|
| 178 |
+
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
|
| 179 |
+
" model_name=MODEL_NAME,\n",
|
| 180 |
+
" load_in_4bit=True,\n",
|
| 181 |
+
" max_seq_length=max_seq_length,\n",
|
| 182 |
+
")\n",
|
| 183 |
+
"model = FastLanguageModel.get_peft_model(\n",
|
| 184 |
+
" model,\n",
|
| 185 |
+
" r=lora_rank,\n",
|
| 186 |
+
" target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"],\n",
|
| 187 |
+
" lora_alpha=lora_rank * 2,\n",
|
| 188 |
+
" use_gradient_checkpointing=\"unsloth\",\n",
|
| 189 |
+
" random_state=3407,\n",
|
| 190 |
+
")\n",
|
| 191 |
+
"\n",
|
| 192 |
+
"maximum_length = len(tokenizer.apply_chat_template(sample, add_generation_prompt=True))\n",
|
| 193 |
+
"print(\"Prompt tokens (sample):\", maximum_length)\n"
|
| 194 |
+
]
|
| 195 |
+
},
|
| 196 |
+
{
|
| 197 |
+
"cell_type": "code",
|
| 198 |
+
"execution_count": null,
|
| 199 |
+
"metadata": {},
|
| 200 |
+
"outputs": [],
|
| 201 |
+
"source": [
|
| 202 |
+
"async def mentor_env_reward_one(\n",
|
| 203 |
+
" completion_text: str,\n",
|
| 204 |
+
" seed: int,\n",
|
| 205 |
+
" task_name: str,\n",
|
| 206 |
+
" mentor_max_score: float,\n",
|
| 207 |
+
" base_url: str,\n",
|
| 208 |
+
") -> float:\n",
|
| 209 |
+
" env = AegisEnv(base_url=base_url)\n",
|
| 210 |
+
" await env.connect()\n",
|
| 211 |
+
" try:\n",
|
| 212 |
+
" obs = await replay_to_mentor_obs(env, seed, task_name)\n",
|
| 213 |
+
" ms = float(obs.max_score or mentor_max_score or 1.0)\n",
|
| 214 |
+
" action = _parse_action(_strip_markdown_json_fence(completion_text), ms)\n",
|
| 215 |
+
" sr = await env.step(action)\n",
|
| 216 |
+
" r = float(sr.reward if sr.reward is not None else 0.0)\n",
|
| 217 |
+
" return r * 20.0\n",
|
| 218 |
+
" except Exception as e:\n",
|
| 219 |
+
" print(\"mentor_env_reward error:\", e)\n",
|
| 220 |
+
" return -8.0\n",
|
| 221 |
+
" finally:\n",
|
| 222 |
+
" await env.close()\n",
|
| 223 |
+
"\n",
|
| 224 |
+
"\n",
|
| 225 |
+
"def format_valid(prompts, completions, mentor_max_score, **kwargs):\n",
|
| 226 |
+
" scores = []\n",
|
| 227 |
+
" for c, ms in zip(completions, mentor_max_score):\n",
|
| 228 |
+
" try:\n",
|
| 229 |
+
" _parse_action(_strip_markdown_json_fence(c[0][\"content\"]), float(ms))\n",
|
| 230 |
+
" scores.append(1.0)\n",
|
| 231 |
+
" except Exception:\n",
|
| 232 |
+
" scores.append(-1.5)\n",
|
| 233 |
+
" return scores\n",
|
| 234 |
+
"\n",
|
| 235 |
+
"\n",
|
| 236 |
+
"def no_markdown_fence(prompts, completions, **kwargs):\n",
|
| 237 |
+
" scores = []\n",
|
| 238 |
+
" for c in completions:\n",
|
| 239 |
+
" t = c[0][\"content\"].strip()\n",
|
| 240 |
+
" scores.append(-0.5 if t.startswith(\"```\") else 0.2)\n",
|
| 241 |
+
" return scores\n",
|
| 242 |
+
"\n",
|
| 243 |
+
"\n",
|
| 244 |
+
"def mentor_success(prompts, completions, episode_seed, task_name, mentor_max_score, **kwargs):\n",
|
| 245 |
+
" scores = []\n",
|
| 246 |
+
" for c, seed, task, ms in zip(completions, episode_seed, task_name, mentor_max_score):\n",
|
| 247 |
+
" text = c[0][\"content\"]\n",
|
| 248 |
+
" coro = mentor_env_reward_one(text, int(seed), str(task), float(ms), AEGIS_OPENENV_BASE)\n",
|
| 249 |
+
" scores.append(float(_run(coro)))\n",
|
| 250 |
+
" return scores\n"
|
| 251 |
+
]
|
| 252 |
+
},
|
| 253 |
+
{
|
| 254 |
+
"cell_type": "code",
|
| 255 |
+
"execution_count": null,
|
| 256 |
+
"metadata": {},
|
| 257 |
+
"outputs": [],
|
| 258 |
+
"source": [
|
| 259 |
+
"text = tokenizer.apply_chat_template(sample, tokenize=False, add_generation_prompt=True)\n",
|
| 260 |
+
"_ = model.generate(\n",
|
| 261 |
+
" **tokenizer(text, return_tensors=\"pt\").to(\"cuda\"),\n",
|
| 262 |
+
" temperature=0.7,\n",
|
| 263 |
+
" max_new_tokens=512,\n",
|
| 264 |
+
" streamer=TextStreamer(tokenizer, skip_prompt=False),\n",
|
| 265 |
+
")\n"
|
| 266 |
+
]
|
| 267 |
+
},
|
| 268 |
+
{
|
| 269 |
+
"cell_type": "code",
|
| 270 |
+
"execution_count": null,
|
| 271 |
+
"metadata": {},
|
| 272 |
+
"outputs": [],
|
| 273 |
+
"source": [
|
| 274 |
+
"max_prompt_length = min(maximum_length + 8, max_seq_length // 2)\n",
|
| 275 |
+
"max_completion_length = max_seq_length - max_prompt_length\n",
|
| 276 |
+
"\n",
|
| 277 |
+
"training_args = GRPOConfig(\n",
|
| 278 |
+
" temperature=0.8,\n",
|
| 279 |
+
" learning_rate=2e-4,\n",
|
| 280 |
+
" weight_decay=0.001,\n",
|
| 281 |
+
" warmup_ratio=0.1,\n",
|
| 282 |
+
" lr_scheduler_type=\"linear\",\n",
|
| 283 |
+
" optim=\"adamw_8bit\",\n",
|
| 284 |
+
" logging_steps=1,\n",
|
| 285 |
+
" per_device_train_batch_size=1,\n",
|
| 286 |
+
" gradient_accumulation_steps=2,\n",
|
| 287 |
+
" num_generations=2,\n",
|
| 288 |
+
" max_prompt_length=max_prompt_length,\n",
|
| 289 |
+
" max_completion_length=max_completion_length,\n",
|
| 290 |
+
" max_steps=200,\n",
|
| 291 |
+
" save_steps=50,\n",
|
| 292 |
+
" report_to=\"trackio\",\n",
|
| 293 |
+
" output_dir=\"outputs_grpo_aegis_hf\",\n",
|
| 294 |
+
")\n",
|
| 295 |
+
"\n",
|
| 296 |
+
"trainer = GRPOTrainer(\n",
|
| 297 |
+
" model=model,\n",
|
| 298 |
+
" processing_class=tokenizer,\n",
|
| 299 |
+
" reward_funcs=[format_valid, no_markdown_fence, mentor_success],\n",
|
| 300 |
+
" args=training_args,\n",
|
| 301 |
+
" train_dataset=train_ds,\n",
|
| 302 |
+
")\n",
|
| 303 |
+
"\n",
|
| 304 |
+
"trainer.train()\n"
|
| 305 |
+
]
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"cell_type": "markdown",
|
| 309 |
+
"metadata": {},
|
| 310 |
+
"source": [
|
| 311 |
+
"## Notes\n",
|
| 312 |
+
"\n",
|
| 313 |
+
"- **Latency:** `mentor_success` calls your Space once per completion; keep `n_per_tier`, `max_steps`, and `num_generations` modest while iterating.\n",
|
| 314 |
+
"- **WebSocket client:** Use an `AegisEnv` / `client.py` that maps `current_stage`, `pipeline_history`, and `refinement_loops_taken` from observations (updated in this repo’s `client.py` — redeploy the Space to pick it up).\n",
|
| 315 |
+
"- **Overrides:** Set `AEGIS_OPENENV_BASE` or `AEGIS_CODE_CLONE_URL` instead of editing cells.\n"
|
| 316 |
+
]
|
| 317 |
+
}
|
| 318 |
+
],
|
| 319 |
+
"metadata": {
|
| 320 |
+
"accelerator": "GPU",
|
| 321 |
+
"colab": {
|
| 322 |
+
"gpuType": "T4",
|
| 323 |
+
"provenance": []
|
| 324 |
+
},
|
| 325 |
+
"kernelspec": {
|
| 326 |
+
"display_name": "Python 3",
|
| 327 |
+
"name": "python3"
|
| 328 |
+
},
|
| 329 |
+
"language_info": {
|
| 330 |
+
"name": "python"
|
| 331 |
+
}
|
| 332 |
+
},
|
| 333 |
+
"nbformat": 4,
|
| 334 |
+
"nbformat_minor": 5
|
| 335 |
+
}
|