@@ -72,10 +72,9 @@ def get_orchestrator(
7272
7373def 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
9697async 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