Skip to content

Commit 58a5132

Browse files
refactor: refactor the run auto search scripts to support file-based target execution
1 parent 4dbd692 commit 58a5132

2 files changed

Lines changed: 68 additions & 20 deletions

File tree

MaxKernel/auto_search/run_batch_search.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@ async def process_problem(
1818
) -> Tuple[str, str]:
1919
"""Executes the search algorithm for a single benchmark problem in the batch."""
2020
async with sem:
21+
problem_id = os.path.basename(os.path.normpath(problem_dir))
2122
try:
23+
reference_file_path = os.path.join(problem_dir, "reference.py")
24+
if not os.path.exists(reference_file_path):
25+
error_msg = (
26+
f"Missing reference file in {problem_dir}. "
27+
"Please ensure your file to be optimized is named 'reference.py'."
28+
)
29+
logger.error(error_msg)
30+
return problem_id, f"Failed: {error_msg}"
31+
32+
optimized_file_path = os.path.join(
33+
problem_dir, f"optimized_{algorithm}.py"
34+
)
2235
return await run_search(
23-
problem_dir=problem_dir,
36+
reference_file_path=reference_file_path,
37+
optimized_file_path=optimized_file_path,
2438
algorithm=algorithm,
39+
problem_id=problem_id,
2540
**kwargs,
2641
)
2742
except Exception as e:
28-
problem_id = os.path.basename(os.path.normpath(problem_dir))
2943
logger.error(
3044
f"Error executing search on {problem_id}: {e}", exc_info=True
3145
)

MaxKernel/auto_search/run_search.py

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ def get_orchestrator(
7272

7373
def save_optimized_kernel(
7474
orchestrator: SearchOrchestrator,
75-
problem_dir: str,
76-
algorithm: str,
75+
optimized_file_path: str,
7776
) -> Optional[Tuple[str, float]]:
78-
"""Saves the best kernel code found during search to the problem directory."""
77+
"""Saves the best kernel code found during search to the optimized file path."""
7978
best_id = orchestrator.graph.best_node_id
8079
if not best_id or best_id not in orchestrator.graph.nodes:
8180
return None
@@ -84,30 +83,44 @@ def save_optimized_kernel(
8483
if not best_node.code:
8584
return None
8685

87-
output_file = os.path.join(problem_dir, f"optimized_{algorithm}.py")
88-
with open(output_file, "w") as f:
86+
os.makedirs(
87+
os.path.dirname(os.path.abspath(optimized_file_path)), exist_ok=True
88+
)
89+
with open(optimized_file_path, "w") as f:
8990
f.write(best_node.code)
9091

91-
logger.info(f"Saved best kernel ({best_id}) to {output_file}")
92+
logger.info(f"Saved best kernel ({best_id}) to {optimized_file_path}")
9293
latency = best_node.evaluation.latency_ms or -1.0
9394
return best_id, latency
9495

9596

9697
async def run_search(
97-
problem_dir: str,
98+
reference_file_path: str,
99+
optimized_file_path: Optional[str] = None,
98100
algorithm: str = "parallel",
99101
graph_db_path: Optional[str] = None,
102+
problem_id: Optional[str] = None,
100103
**kwargs: Any,
101104
) -> Tuple[str, str]:
102-
"""Executes the search algorithm asynchronously for a single problem directory."""
103-
problem_id = os.path.basename(os.path.normpath(problem_dir))
104-
reference_file = os.path.join(problem_dir, "reference.py")
105+
"""Executes the search algorithm asynchronously for a single reference file."""
106+
problem_dir = os.path.dirname(os.path.abspath(reference_file_path))
107+
default_problem_id, ext = os.path.splitext(
108+
os.path.basename(reference_file_path)
109+
)
110+
if not problem_id:
111+
problem_id = default_problem_id
105112

106-
if not os.path.exists(reference_file):
107-
logger.error(f"reference.py not found in {problem_dir}")
108-
return problem_id, "Failed: reference.py missing"
113+
if not optimized_file_path:
114+
optimized_file_path = os.path.join(
115+
problem_dir, f"{problem_id}_optimized_{algorithm}{ext}"
116+
)
117+
logger.info(f"No optimized file path provided. Using {optimized_file_path}")
109118

110-
with open(reference_file, "r") as f:
119+
if not os.path.exists(reference_file_path):
120+
logger.error(f"{reference_file_path} not found")
121+
return problem_id, f"Failed: {reference_file_path} missing"
122+
123+
with open(reference_file_path, "r") as f:
111124
reference_code = f.read()
112125

113126
run_subdir = None
@@ -144,7 +157,7 @@ async def run_search(
144157

145158
await orchestrator.run()
146159

147-
best_result = save_optimized_kernel(orchestrator, problem_dir, algorithm)
160+
best_result = save_optimized_kernel(orchestrator, optimized_file_path)
148161

149162
if run_subdir:
150163
import shutil
@@ -174,10 +187,29 @@ def parse_args() -> argparse.Namespace:
174187
"Arguments shared across all algorithms and orchestrators.",
175188
)
176189
orch_group.add_argument(
177-
"--problem_dir",
190+
"--reference_file_path",
178191
type=str,
179192
required=True,
180-
help="Path to problem directory containing reference.py",
193+
help="Path to the reference kernel file",
194+
)
195+
orch_group.add_argument(
196+
"--optimized_file_path",
197+
type=str,
198+
default=None,
199+
help=(
200+
"Path to save the optimized kernel. "
201+
"Defaults to <problem_id>_optimized_{algorithm}.py"
202+
"in the reference file's directory."
203+
),
204+
)
205+
orch_group.add_argument(
206+
"--problem_id",
207+
type=str,
208+
default=None,
209+
help=(
210+
"Explicitly assign a problem ID to this search. "
211+
"Defaults to the reference file name."
212+
),
181213
)
182214
orch_group.add_argument(
183215
"--algorithm",
@@ -336,9 +368,11 @@ def main():
336368

337369
prob_id, status = asyncio.run(
338370
run_search(
339-
problem_dir=args.problem_dir,
371+
reference_file_path=args.reference_file_path,
372+
optimized_file_path=args.optimized_file_path,
340373
algorithm=args.algorithm,
341374
graph_db_path=args.graph_db_path,
375+
problem_id=args.problem_id,
342376
**kwargs,
343377
)
344378
)

0 commit comments

Comments
 (0)