Spaces:
Sleeping
Sleeping
File size: 10,757 Bytes
eb408af | 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | """
Shared runtime for the Gradio Space: mock compiler env + Deliverable 2 formatting.
Sourced from `compiler_optimization_grpo.ipynb` and
`role2_deliverable3_training_loop (2) (1) (1).ipynb`.
"""
from __future__ import annotations
import copy
import json
import re
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Tuple
# --- Deliverable 2 (LLM-facing pseudo-asm + pass-array parsing) -----------------
class Deliverable2_Formatter:
@staticmethod
def translate_state(raw_json: list) -> str:
"""Translate raw JSON IR into compact pseudo-assembly."""
if not isinstance(raw_json, list) or not raw_json:
return "; (empty program — 0 instructions)"
pseudo_assembly: list[str] = []
for i, instruction in enumerate(raw_json):
if not isinstance(instruction, dict):
pseudo_assembly.append(f"{i}. NOP")
continue
op = str(instruction.get("op", "UNKNOWN")).upper()
args = ", ".join(str(arg) for arg in instruction.get("args", []))
dest = instruction.get("dest", "")
if dest:
line = f"{i}. {dest} = {op} {args}".rstrip()
else:
line = f"{i}. {op} {args}".rstrip()
pseudo_assembly.append(line)
return "\n".join(pseudo_assembly)
@staticmethod
def extract_action_array(llm_output: str) -> list:
"""Best-effort extraction of JSON pass arrays from noisy LLM output."""
text = (llm_output or "").strip()
if not text:
raise ValueError("Invalid JSON format")
try:
parsed = json.loads(text)
if isinstance(parsed, list):
return parsed
except json.JSONDecodeError:
pass
cleaned = re.sub(r"```(?:json)?", "", text, flags=re.IGNORECASE).replace("```", "").strip()
if cleaned != text:
try:
parsed = json.loads(cleaned)
if isinstance(parsed, list):
return parsed
except json.JSONDecodeError:
pass
match = re.search(r"\[.*?\]", text, re.DOTALL)
if match:
candidate = match.group(0)
try:
parsed = json.loads(candidate)
if isinstance(parsed, list):
return parsed
except json.JSONDecodeError:
try:
parsed = json.loads(candidate.replace("'", '"'))
if isinstance(parsed, list):
return parsed
except json.JSONDecodeError:
pass
raise ValueError("Invalid JSON format")
# --- OpenEnv-style compiler environment (mock engine) --------------------------
class MCPEnvironment:
"""Minimal stub. In production: `from openenv import MCPEnvironment`."""
def reset(self, *args, **kwargs):
raise NotImplementedError
def step(self, *args, **kwargs):
raise NotImplementedError
def state(self):
raise NotImplementedError
@dataclass
class StepResult:
observation: str
reward: float
done: bool
info: Dict[str, Any] = field(default_factory=dict)
@dataclass
class EpisodeStats:
steps_taken: int = 0
total_reward: float = 0.0
passes_applied: List[str] = field(default_factory=list)
invalid_actions: int = 0
no_ops: int = 0
baseline_cycles: int = 0
final_cycles: int = 0
@property
def total_improvement_pct(self) -> float:
if self.baseline_cycles == 0:
return 0.0
return ((self.baseline_cycles - self.final_cycles) / self.baseline_cycles) * 100.0
class CompilerOptimizationEnv(MCPEnvironment):
TIME_TAX: float = 1.0
NO_OP_PENALTY: float = -2.0
INVALID_ACTION_PENALTY: float = -5.0
MAX_INVALID_ACTIONS: int = 3
TERMINAL_BONUS_SCALE: float = 0.5
def __init__(
self,
role1_engine,
role3_passes: Dict[str, Any],
max_steps: int = 10,
curriculum_level: int = 1,
):
self.engine = role1_engine
self.passes = role3_passes
self.max_steps = max_steps
self.curriculum_level = curriculum_level
self._valid_actions = frozenset(self.passes.keys())
self._stats: Optional[EpisodeStats] = None
self.original_program = None
self.current_program = None
self.previous_cycles = 0
self._consecutive_invalid = 0
def reset(self, new_program_json: List[Dict]) -> str:
self.original_program = copy.deepcopy(new_program_json)
self.current_program = copy.deepcopy(new_program_json)
self.previous_cycles = self._safe_count_cycles(self.current_program)
self._consecutive_invalid = 0
self._stats = EpisodeStats(
baseline_cycles=self.previous_cycles,
final_cycles=self.previous_cycles,
)
return self.state()
def state(self) -> str:
assert self.current_program is not None
return self._program_to_pseudoasm(self.current_program)
def step(self, action_string: str) -> StepResult:
assert self._stats is not None, "Call reset() before step()."
self._stats.steps_taken += 1
if action_string not in self._valid_actions:
return self._handle_invalid_action(action_string)
candidate_program = self.passes[action_string](copy.deepcopy(self.current_program))
is_valid = self.engine.verify_equivalence(self.original_program, candidate_program)
if not is_valid:
return self._handle_semantic_violation()
new_cycles = self._safe_count_cycles(candidate_program)
reward, info = self._compute_reward(action_string, new_cycles)
self.current_program = candidate_program
self.previous_cycles = new_cycles
self._stats.final_cycles = new_cycles
self._stats.total_reward += reward
self._stats.passes_applied.append(action_string)
self._consecutive_invalid = 0
done = self._stats.steps_taken >= self.max_steps
if done:
terminal_bonus = self._terminal_bonus()
reward += terminal_bonus
info["terminal_bonus"] = terminal_bonus
info["reason"] = "max_steps_reached"
info["episode_stats"] = self._episode_summary()
return StepResult(self.state(), reward, done, info)
def _compute_reward(self, action: str, new_cycles: int) -> Tuple[float, Dict]:
info: Dict[str, Any] = {"action": action}
if self.previous_cycles == 0:
return -self.TIME_TAX, {**info, "note": "zero_baseline"}
old_cycles = self.previous_cycles
delta_pct = ((old_cycles - new_cycles) / old_cycles) * 100.0
if new_cycles == old_cycles:
reward = self.NO_OP_PENALTY
if self._stats is not None:
self._stats.no_ops += 1
info["no_op"] = True
else:
reward = delta_pct - self.TIME_TAX
info["delta_pct"] = round(delta_pct, 3)
info["prev_cycles"] = old_cycles
info["new_cycles"] = new_cycles
return reward, info
def _terminal_bonus(self) -> float:
if self._stats is None:
return 0.0
return max(0.0, self._stats.total_improvement_pct * self.TERMINAL_BONUS_SCALE)
def _compute_crash_penalty(self) -> float:
return -2.0 * (100.0 * self.max_steps)
def _handle_invalid_action(self, action: str) -> StepResult:
self._consecutive_invalid += 1
if self._stats is not None:
self._stats.invalid_actions += 1
done = self._consecutive_invalid >= self.MAX_INVALID_ACTIONS
info = {
"error": f"Unknown action: '{action}'",
"valid_actions": sorted(self._valid_actions),
"consecutive_invalid": self._consecutive_invalid,
}
if done:
info["reason"] = "too_many_invalid_actions"
info["episode_stats"] = self._episode_summary()
return StepResult(self.state(), self.INVALID_ACTION_PENALTY, done, info)
def _handle_semantic_violation(self) -> StepResult:
return StepResult(
self.state(),
self._compute_crash_penalty(),
True,
{
"error": "Semantic equivalence check FAILED.",
"reason": "semantic_violation",
"episode_stats": self._episode_summary(),
},
)
@staticmethod
def _program_to_pseudoasm(program: List[Dict]) -> str:
if not program:
return "; (empty program)"
lines = []
for i, instr in enumerate(program):
op = instr.get("op", "NOP")
args = instr.get("args", [])
dest = instr.get("dest")
typ = instr.get("type", "")
arg_str = ", ".join(str(a) for a in args)
type_hint = f":{typ}" if typ else ""
if dest:
lines.append(f" {i:>3}: {dest}{type_hint} = {op} {arg_str}")
else:
lines.append(f" {i:>3}: {op} {arg_str}")
return "\n".join(lines)
def _safe_count_cycles(self, program: List[Dict]) -> int:
return max(0, int(self.engine.execute_and_count_cycles(program)))
def _episode_summary(self) -> Dict:
s = self._stats
if s is None:
return {}
return {
"steps": s.steps_taken,
"total_reward": round(s.total_reward, 3),
"passes_applied": s.passes_applied,
"invalid_actions": s.invalid_actions,
"no_ops": s.no_ops,
"baseline_cycles": s.baseline_cycles,
"final_cycles": s.final_cycles,
"total_improvement_pct": round(s.total_improvement_pct, 3),
}
def available_actions(self) -> List[str]:
return sorted(self._valid_actions)
class MockEngine:
"""Stub engine: cycles = instruction count, all programs semantically valid."""
def execute_and_count_cycles(self, program):
return len(program)
def verify_equivalence(self, original, candidate):
return True
MOCK_PASSES = {
"constant_folding": lambda p: p[:-1] if len(p) > 1 else p,
"dead_code_elimination": lambda p: p[:-1] if len(p) > 2 else p,
"loop_unrolling": lambda p: p,
}
SAMPLE_PROGRAM = [
{"op": "const", "dest": "x", "args": ["5"], "type": "int"},
{"op": "const", "dest": "y", "args": ["3"], "type": "int"},
{"op": "add", "dest": "z", "args": ["x", "y"], "type": "int"},
{"op": "mul", "dest": "w", "args": ["z", "x"], "type": "int"},
{"op": "ret", "args": ["w"]},
]
|