Run all (or selected) problems in a level.
(
level: int,
problem_ids: Optional[List[int]] = None,
quick: bool = False,
backend: str = "cuda",
)
| 180 | |
| 181 | |
| 182 | def run_level( |
| 183 | level: int, |
| 184 | problem_ids: Optional[List[int]] = None, |
| 185 | quick: bool = False, |
| 186 | backend: str = "cuda", |
| 187 | ) -> List[Dict[str, Any]]: |
| 188 | """Run all (or selected) problems in a level.""" |
| 189 | |
| 190 | # Discover available problems |
| 191 | level_dir = KB_CACHE_DIR / f"level{level}" |
| 192 | available_ids = [] |
| 193 | if level_dir.exists(): |
| 194 | for f in sorted(level_dir.glob("*.json")): |
| 195 | try: |
| 196 | meta = json.loads(f.read_text(encoding="utf-8")) |
| 197 | available_ids.append(meta["problem_id"]) |
| 198 | except (json.JSONDecodeError, KeyError): |
| 199 | continue |
| 200 | |
| 201 | if not available_ids: |
| 202 | print(f"No cached problems for Level {level}.") |
| 203 | print(f" Fetch first: uv run kernelbench/bridge.py fetch --source hf --level {level}") |
| 204 | return [] |
| 205 | |
| 206 | # Filter if specific IDs requested |
| 207 | if problem_ids: |
| 208 | run_ids = [pid for pid in problem_ids if pid in available_ids] |
| 209 | if not run_ids: |
| 210 | print(f"None of the requested problem IDs found in cache for Level {level}.") |
| 211 | return [] |
| 212 | else: |
| 213 | run_ids = available_ids |
| 214 | |
| 215 | print(f"=== KernelBench Scorer: Level {level} ({len(run_ids)} problems) ===\n") |
| 216 | |
| 217 | results = [] |
| 218 | scores = load_scores() |
| 219 | |
| 220 | for i, pid in enumerate(run_ids): |
| 221 | print(f"[{i + 1}/{len(run_ids)}] Problem L{level}_P{pid:03d}...", end=" ", flush=True) |
| 222 | t0 = time.time() |
| 223 | |
| 224 | result = run_single_problem(level, pid, quick=quick, backend=backend) |
| 225 | elapsed = time.time() - t0 |
| 226 | |
| 227 | result["elapsed_s"] = elapsed |
| 228 | results.append(result) |
| 229 | |
| 230 | # Save incrementally |
| 231 | key = f"L{level}_P{pid:03d}" |
| 232 | scores["problems"][key] = result |
| 233 | save_scores(scores) |
| 234 | |
| 235 | # Print one-line result |
| 236 | status = result["correctness"] |
| 237 | speedup = result["speedup"] |
| 238 | if result.get("error"): |
| 239 | print(f"ERROR ({result['error'][:60]})") |
no test coverage detected