MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / generate_starter

Method generate_starter

kernelbench/bridge.py:206–303  ·  view source on GitHub ↗

Generate a starter kernel.py (ModelNew initially copies Model logic).

(self, backend: str = "cuda")

Source from the content-addressed store, hash-verified

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'''"""
212KernelBench Problem {self.uid}: {self.name}
213Level: {self.level} | Problem ID: {self.problem_id}
214Operations: {ops_str}
215Difficulty: {analysis["estimated_difficulty"]}
216
217Source: ScalingIntelligence/KernelBench
218Optimized with AutoKernel (https://github.com/RightNow-AI/autokernel)
219
220The agent optimizes ModelNew to outperform the PyTorch reference (Model).
221Edit ModelNew.forward() -- use CUDA C++ via compile_cuda() or Triton @jit.
222Run `uv run kernelbench/bench_kb.py` to evaluate correctness + speedup.
223"""
224
225KERNELBENCH_PROBLEM = {{
226 "level": {self.level},
227 "problem_id": {self.problem_id},
228 "name": {self.name!r},
229}}
230
231import torch
232import torch.nn as nn
233import 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>

Callers 1

setup_problemFunction · 0.80

Calls 2

analyzeMethod · 0.95

Tested by

no test coverage detected