2525)
2626from auto_agent .tools .search_api_tool import search_api_tool
2727
28+
2829# 1. Planner Agent
2930# This agent identifies parameters, creates the template, and defines the search space.
3031# It saves them to session state instead of calling the tool directly.
31- autotune_planner_agent = CustomLlmAgent (
32- name = "AutotunePlannerAgent" ,
33- model = MODEL_NAME ,
34- generate_content_config = model_config ,
35- planner = get_thinking_planner ("high" ),
36- instruction = autotune_prompt .PROMPT ,
37- description = "Prepares code template and search space for auto-tuning Pallas kernels." ,
38- tools = [filesystem_tool_r , write_autotune_specs_tool , search_api_tool ],
39- )
32+ def create_autotune_planner_agent (
33+ model_name : str = MODEL_NAME ,
34+ ) -> CustomLlmAgent :
35+ return CustomLlmAgent (
36+ name = "AutotunePlannerAgent" ,
37+ model = model_name ,
38+ generate_content_config = model_config ,
39+ planner = get_thinking_planner ("high" ),
40+ instruction = autotune_prompt .PROMPT ,
41+ description = "Prepares code template and search space for auto-tuning Pallas kernels." ,
42+ tools = [filesystem_tool_r , write_autotune_specs_tool , search_api_tool ],
43+ )
44+
45+
46+ autotune_planner_agent = create_autotune_planner_agent ()
4047
4148
4249# 2. Runner Agent
@@ -154,52 +161,86 @@ async def _run_async_impl(
154161 )
155162
156163
157- autotune_runner = AutotuneRunner (
158- name = "AutotuneRunner" ,
159- output_key = "autotune_results" ,
160- )
164+ def create_autotune_runner (model_name : str = MODEL_NAME ) -> AutotuneRunner :
165+ return AutotuneRunner (
166+ name = "AutotuneRunner" ,
167+ output_key = "autotune_results" ,
168+ )
169+
170+
171+ autotune_runner = create_autotune_runner ()
161172
162173
163174# 3. Apply Best Config Agent
164- apply_best_config_agent = CustomLlmAgent (
165- name = "ApplyBestConfigAgent" ,
166- model = MODEL_NAME ,
167- generate_content_config = model_config ,
168- planner = get_thinking_planner ("high" ),
169- instruction = apply_best_config_prompt .PROMPT ,
170- description = "Applies autotuning results to the optimized kernel file." ,
171- tools = [filesystem_tool_r , write_optimized_kernel_tool ],
172- )
175+ def create_apply_best_config_agent (
176+ model_name : str = MODEL_NAME ,
177+ ) -> CustomLlmAgent :
178+ return CustomLlmAgent (
179+ name = "ApplyBestConfigAgent" ,
180+ model = model_name ,
181+ generate_content_config = model_config ,
182+ planner = get_thinking_planner ("high" ),
183+ instruction = apply_best_config_prompt .PROMPT ,
184+ description = "Applies autotuning results to the optimized kernel file." ,
185+ tools = [filesystem_tool_r , write_optimized_kernel_tool ],
186+ )
187+
188+
189+ apply_best_config_agent = create_apply_best_config_agent ()
173190
174191
175192# 4. Summarizer Agent
176193# This agent reads results from state and talks to the user.
177- autotune_summary_agent = CustomLlmAgent (
178- name = "AutotuneSummaryAgent" ,
179- model = MODEL_NAME ,
180- generate_content_config = model_config ,
181- instruction = summary_prompt .PROMPT ,
182- description = "Summarizes autotuning results." ,
183- tools = [filesystem_tool_r ],
184- output_key = "autotuning_summary" ,
185- )
194+ def create_autotune_summary_agent (
195+ model_name : str = MODEL_NAME ,
196+ ) -> CustomLlmAgent :
197+ return CustomLlmAgent (
198+ name = "AutotuneSummaryAgent" ,
199+ model = model_name ,
200+ generate_content_config = model_config ,
201+ instruction = summary_prompt .PROMPT ,
202+ description = "Summarizes autotuning results." ,
203+ tools = [filesystem_tool_r ],
204+ output_key = "autotuning_summary" ,
205+ )
206+
207+
208+ autotune_summary_agent = create_autotune_summary_agent ()
186209
187210
188211class CombinedAutotuneAgent (BaseAgent ):
189212 """Chains autotuning steps and conditionally applies best config."""
190213
191- def __init__ (self , name : str ):
192- super ().__init__ (name = name )
214+ planner_agent : Optional [BaseAgent ] = None
215+ runner_agent : Optional [BaseAgent ] = None
216+ apply_config_agent : Optional [BaseAgent ] = None
217+ summary_agent : Optional [BaseAgent ] = None
218+
219+ def __init__ (
220+ self ,
221+ name : str ,
222+ planner_agent : BaseAgent ,
223+ runner_agent : BaseAgent ,
224+ apply_config_agent : BaseAgent ,
225+ summary_agent : BaseAgent ,
226+ ):
227+ super ().__init__ (
228+ name = name ,
229+ planner_agent = planner_agent ,
230+ runner_agent = runner_agent ,
231+ apply_config_agent = apply_config_agent ,
232+ summary_agent = summary_agent ,
233+ )
193234
194235 async def _run_async_impl (
195236 self , ctx : InvocationContext
196237 ) -> AsyncGenerator [Event , None ]:
197238 logging .info (f"[{ self .name } ] Running AutotunePlannerAgent..." )
198- async for event in autotune_planner_agent .run_async (ctx ):
239+ async for event in self . planner_agent .run_async (ctx ):
199240 yield event
200241
201242 logging .info (f"[{ self .name } ] Running AutotuneRunner..." )
202- async for event in autotune_runner .run_async (ctx ):
243+ async for event in self . runner_agent .run_async (ctx ):
203244 yield event
204245
205246 autotune_results = ctx .session .state .get ("autotune_results" , {})
@@ -209,7 +250,7 @@ async def _run_async_impl(
209250 and autotune_results .get ("best_time_ms" ) is not None
210251 ):
211252 logging .info (f"[{ self .name } ] Running ApplyBestConfigAgent..." )
212- async for event in apply_best_config_agent .run_async (ctx ):
253+ async for event in self . apply_config_agent .run_async (ctx ):
213254 yield event
214255 else :
215256 logging .warning (
@@ -218,10 +259,22 @@ async def _run_async_impl(
218259 )
219260
220261 logging .info (f"[{ self .name } ] Running AutotuneSummaryAgent..." )
221- async for event in autotune_summary_agent .run_async (ctx ):
262+ async for event in self . summary_agent .run_async (ctx ):
222263 yield event
223264
224265
225- autotune_agent = CombinedAutotuneAgent (name = "AutotuneAgent" )
266+ def create_autotune_agent (
267+ model_name : str = MODEL_NAME ,
268+ ) -> CombinedAutotuneAgent :
269+ return CombinedAutotuneAgent (
270+ name = "AutotuneAgent" ,
271+ planner_agent = create_autotune_planner_agent (model_name ),
272+ runner_agent = create_autotune_runner (model_name ),
273+ apply_config_agent = create_apply_best_config_agent (model_name ),
274+ summary_agent = create_autotune_summary_agent (model_name ),
275+ )
276+
277+
278+ autotune_agent = create_autotune_agent ()
226279
227- __all__ = ["autotune_agent" ]
280+ __all__ = ["autotune_agent" , "create_autotune_agent" ]
0 commit comments