File size: 6,036 Bytes
5fed0fc |
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 |
"""Run single simulations without shelling out to the CLI."""
import importlib.util
import json
import os
import sys
import tempfile
import uuid
from dataclasses import dataclass
from pathlib import Path
from types import ModuleType
from typing import Type
# Common directory (where this file lives)
COMMON_DIR = os.path.dirname(os.path.abspath(__file__))
SIM_ROOT = os.path.join(COMMON_DIR, "cant-be-late-simulator")
if SIM_ROOT not in sys.path:
sys.path.insert(0, SIM_ROOT)
try: # pragma: no cover - provides no-op wandb in minimal environments
import wandb # type: ignore
except ModuleNotFoundError: # pragma: no cover - exercised in evaluator environment
class _WandbStub:
run = None
class _Config:
def update(self, *args, **kwargs):
return None
config = _Config()
@staticmethod
def init(*args, **kwargs): # noqa: D401
"""No-op replacement for wandb.init."""
return None
@staticmethod
def log(*args, **kwargs):
return None
wandb = _WandbStub()
sys.modules["wandb"] = wandb
if not hasattr(wandb, "run"):
wandb.run = None
from sky_spot import simulate
from sky_spot.env import TraceEnv
from sky_spot.task import SingleTask
from sky_spot.strategies import strategy as strategy_lib
_OUTPUT_BASE = Path(
os.environ.get(
"GEPA_EVAL_TMPDIR",
os.path.join(tempfile.gettempdir(), "gepa_evaluator_runs"),
)
)
_OUTPUT_BASE.mkdir(parents=True, exist_ok=True)
_STRATEGY_CACHE: dict[str, Type[strategy_lib.Strategy]] = {}
@dataclass
class SimulationFailure(Exception):
error_msg: str
def __str__(self) -> str: # pragma: no cover - repr helper
return self.error_msg
def _load_strategy_class(module_path: str) -> Type[strategy_lib.Strategy]:
module_path = os.path.abspath(module_path)
cached = _STRATEGY_CACHE.get(module_path)
if cached is not None:
return cached
spec = importlib.util.spec_from_file_location(Path(module_path).stem, module_path)
if spec is None or spec.loader is None:
raise SimulationFailure(f"Could not create module spec for {module_path}")
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except Exception as exc: # pragma: no cover - surface to caller
raise SimulationFailure(f"Failed to import strategy module: {exc}") from exc
strategy_cls = _first_strategy_class(module, module_path)
_STRATEGY_CACHE[module_path] = strategy_cls
return strategy_cls
def _first_strategy_class(
module: ModuleType, module_path: str
) -> Type[strategy_lib.Strategy]:
# Prefer 'Solution' class if it exists and is a Strategy subclass
if hasattr(module, "Solution"):
solution_cls = module.Solution
if isinstance(solution_cls, type) and issubclass(
solution_cls, strategy_lib.Strategy
):
return solution_cls
# Fallback: find any Strategy subclass
for attr in module.__dict__.values():
if (
isinstance(attr, type)
and issubclass(attr, strategy_lib.Strategy)
and attr not in {strategy_lib.Strategy, strategy_lib.MultiRegionStrategy}
):
return attr
raise SimulationFailure(f"No Solution or Strategy subclass found in {module_path}")
def run_single_simulation(program_path: str, trace_file: str, config: dict):
"""Run a single simulation inside the worker process.
Returns:
Tuple[bool, float, str]: success flag, cost, error message
"""
program_path = os.path.abspath(program_path)
trace_file = os.path.abspath(trace_file)
try:
# Load the Solution class
strategy_cls = _load_strategy_class(program_path)
# Write config to a temp spec file
spec_data = {
"deadline": config["deadline"],
"duration": config["duration"],
"overhead": config["overhead"],
"trace_file": trace_file,
}
spec_fd, spec_path = tempfile.mkstemp(suffix=".json", prefix="spec_")
try:
with os.fdopen(spec_fd, "w") as f:
json.dump(spec_data, f)
# Create instance via solve(spec_path)
strategy = strategy_cls.__new__(strategy_cls)
strategy.solve(spec_path)
finally:
try:
os.remove(spec_path)
except OSError:
pass
# Get config values for simulation
deadline_hours = float(config["deadline"])
duration_hours = float(config["duration"])
overhead_hours = float(config["overhead"])
envs = TraceEnv.create_env(trace_file, env_start_hours=0.0)
task = SingleTask({"duration": duration_hours, "checkpoint_size_gb": 50.0})
output_dir = str(_OUTPUT_BASE)
os.makedirs(output_dir, exist_ok=True)
temp_name = f"eval_{os.getpid()}_{uuid.uuid4().hex}.json"
stats = simulate.simulate(
envs=envs,
strategy=strategy,
task=task,
trace_file=trace_file,
deadline_hours=deadline_hours,
restart_overhead_hours=[overhead_hours],
env_start_hours=0.0,
output_dir=output_dir,
kwargs={},
output_filename=temp_name,
silent=True,
dump_history=False,
)
try:
os.remove(os.path.join(output_dir, temp_name))
except OSError:
pass
costs = stats.get("costs", [])
if not costs:
raise SimulationFailure("Simulation produced no costs")
avg_cost = float(sum(costs) / len(costs))
return True, avg_cost, ""
except SimulationFailure as exc: # pragma: no cover - surfaced to caller
return False, 0.0, str(exc)
except Exception as exc: # pragma: no cover - unexpected path
return False, 0.0, f"Error on trace {os.path.basename(trace_file)}: {exc}"
|