A single KernelBench problem.
| 46 | |
| 47 | @dataclass |
| 48 | class KernelBenchProblem: |
| 49 | """A single KernelBench problem.""" |
| 50 | |
| 51 | level: int |
| 52 | problem_id: int |
| 53 | name: str |
| 54 | source_code: str |
| 55 | |
| 56 | @property |
| 57 | def uid(self) -> str: |
| 58 | return f"L{self.level}_P{self.problem_id:03d}" |
| 59 | |
| 60 | @property |
| 61 | def cache_path(self) -> Path: |
| 62 | return KB_CACHE_DIR / f"level{self.level}" / f"{self.problem_id}.py" |
| 63 | |
| 64 | # ----- Cache persistence ----- |
| 65 | |
| 66 | def save_to_cache(self) -> None: |
| 67 | self.cache_path.parent.mkdir(parents=True, exist_ok=True) |
| 68 | self.cache_path.write_text(self.source_code, encoding="utf-8") |
| 69 | meta_path = self.cache_path.with_suffix(".json") |
| 70 | meta = { |
| 71 | "level": self.level, |
| 72 | "problem_id": self.problem_id, |
| 73 | "name": self.name, |
| 74 | } |
| 75 | meta_path.write_text(json.dumps(meta, indent=2), encoding="utf-8") |
| 76 | |
| 77 | @classmethod |
| 78 | def load_from_cache(cls, level: int, problem_id: int) -> Optional["KernelBenchProblem"]: |
| 79 | cache_path = KB_CACHE_DIR / f"level{level}" / f"{problem_id}.py" |
| 80 | meta_path = cache_path.with_suffix(".json") |
| 81 | if not cache_path.exists(): |
| 82 | return None |
| 83 | source = cache_path.read_text(encoding="utf-8") |
| 84 | name = f"problem_{problem_id}" |
| 85 | if meta_path.exists(): |
| 86 | meta = json.loads(meta_path.read_text(encoding="utf-8")) |
| 87 | name = meta.get("name", name) |
| 88 | return cls(level=level, problem_id=problem_id, name=name, source_code=source) |
| 89 | |
| 90 | # ----- Analysis ----- |
| 91 | |
| 92 | def analyze(self) -> Dict[str, Any]: |
| 93 | """Identify operations, shapes, parameter usage, and estimate difficulty.""" |
| 94 | analysis: Dict[str, Any] = { |
| 95 | "operations": [], |
| 96 | "estimated_difficulty": "unknown", |
| 97 | "has_parameters": False, |
| 98 | "input_shapes": [], |
| 99 | "forward_lines": 0, |
| 100 | } |
| 101 | |
| 102 | try: |
| 103 | ast.parse(self.source_code) |
| 104 | except SyntaxError: |
| 105 | return analysis |
no outgoing calls
no test coverage detected