Generate a starter kernel.py (ModelNew initially copies Model logic).
(self, backend: str = "cuda")
| 204 | # ----- Starter generation ----- |
| 205 | |
| 206 | def generate_starter(self, backend: str = "cuda") -> str: |
| 207 | """Generate a starter kernel.py (ModelNew initially copies Model logic).""" |
| 208 | analysis = self.analyze() |
| 209 | ops_str = ", ".join(analysis["operations"]) or "unknown" |
| 210 | |
| 211 | header = f'''""" |
| 212 | KernelBench Problem {self.uid}: {self.name} |
| 213 | Level: {self.level} | Problem ID: {self.problem_id} |
| 214 | Operations: {ops_str} |
| 215 | Difficulty: {analysis["estimated_difficulty"]} |
| 216 | |
| 217 | Source: ScalingIntelligence/KernelBench |
| 218 | Optimized with AutoKernel (https://github.com/RightNow-AI/autokernel) |
| 219 | |
| 220 | The agent optimizes ModelNew to outperform the PyTorch reference (Model). |
| 221 | Edit ModelNew.forward() -- use CUDA C++ via compile_cuda() or Triton @jit. |
| 222 | Run `uv run kernelbench/bench_kb.py` to evaluate correctness + speedup. |
| 223 | """ |
| 224 | |
| 225 | KERNELBENCH_PROBLEM = {{ |
| 226 | "level": {self.level}, |
| 227 | "problem_id": {self.problem_id}, |
| 228 | "name": {self.name!r}, |
| 229 | }} |
| 230 | |
| 231 | import torch |
| 232 | import torch.nn as nn |
| 233 | import torch.nn.functional as F |
| 234 | ''' |
| 235 | |
| 236 | # Deduplicate imports already in header |
| 237 | skip_imports = { |
| 238 | "import torch", |
| 239 | "import torch.nn as nn", |
| 240 | "import torch.nn.functional as F", |
| 241 | "from torch import nn", |
| 242 | } |
| 243 | filtered_lines = [] |
| 244 | for line in self.source_code.split("\n"): |
| 245 | if line.strip() in skip_imports: |
| 246 | continue |
| 247 | # Also skip `from torch.nn import functional as F` and similar |
| 248 | if re.match(r"^\s*import\s+torch\.nn\.functional\s+as\s+F\s*$", line.strip()): |
| 249 | continue |
| 250 | filtered_lines.append(line) |
| 251 | remaining_source = "\n".join(filtered_lines).strip() |
| 252 | |
| 253 | # Build ModelNew by copying Model class |
| 254 | model_new_source = self._extract_and_rename_model() |
| 255 | |
| 256 | compile_hint = "" |
| 257 | if backend == "cuda": |
| 258 | compile_hint = """ |
| 259 | # Optional: use AutoKernel's CUDA compilation utility for custom CUDA C++ kernels |
| 260 | # from kernels.cuda._compile import compile_cuda |
| 261 | # |
| 262 | # CUDA_SRC = r\""" |
| 263 | # #include <torch/extension.h> |
no test coverage detected