|
5 | 5 | import os |
6 | 6 | from typing import AsyncGenerator, Optional |
7 | 7 |
|
8 | | -from google.adk.agents import BaseAgent, SequentialAgent |
| 8 | +from google.adk.agents import BaseAgent |
9 | 9 | from google.adk.agents.invocation_context import InvocationContext |
10 | 10 | from google.adk.events import Event, EventActions |
11 | 11 |
|
|
14 | 14 | from auto_agent.custom_types import CustomLlmAgent |
15 | 15 | from auto_agent.subagents.autotuning.autotune_tool import autotune_kernel |
16 | 16 | from auto_agent.subagents.autotuning.prompts import ( |
| 17 | + apply_best_config_prompt, |
17 | 18 | autotune_prompt, |
18 | 19 | summary_prompt, |
19 | 20 | ) |
@@ -159,22 +160,69 @@ async def _run_async_impl( |
159 | 160 | ) |
160 | 161 |
|
161 | 162 |
|
162 | | -# 3. Summarizer Agent |
| 163 | +# 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=thinking_planner, |
| 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 | +) |
| 173 | + |
| 174 | + |
| 175 | +# 4. Summarizer Agent |
163 | 176 | # This agent reads results from state and talks to the user. |
164 | 177 | autotune_summary_agent = CustomLlmAgent( |
165 | 178 | name="AutotuneSummaryAgent", |
166 | 179 | model=MODEL_NAME, |
167 | 180 | generate_content_config=model_config, |
168 | 181 | planner=thinking_planner, |
169 | 182 | instruction=summary_prompt.PROMPT, |
170 | | - description="Apply and Summarizes autotuning results.", |
171 | | - tools=[filesystem_tool_r, write_optimized_kernel_tool], |
| 183 | + description="Summarizes autotuning results.", |
| 184 | + tools=[filesystem_tool_r], |
172 | 185 | output_key="autotuning_summary", |
173 | 186 | ) |
174 | 187 |
|
175 | | -autotune_agent = SequentialAgent( |
176 | | - name="AutotuneAgent", |
177 | | - sub_agents=[autotune_planner_agent, autotune_runner, autotune_summary_agent], |
178 | | -) |
| 188 | + |
| 189 | +class CombinedAutotuneAgent(BaseAgent): |
| 190 | + """Chains autotuning steps and conditionally applies best config.""" |
| 191 | + |
| 192 | + def __init__(self, name: str): |
| 193 | + super().__init__(name=name) |
| 194 | + |
| 195 | + async def _run_async_impl( |
| 196 | + self, ctx: InvocationContext |
| 197 | + ) -> AsyncGenerator[Event, None]: |
| 198 | + logging.info(f"[{self.name}] Running AutotunePlannerAgent...") |
| 199 | + async for event in autotune_planner_agent.run_async(ctx): |
| 200 | + yield event |
| 201 | + |
| 202 | + logging.info(f"[{self.name}] Running AutotuneRunner...") |
| 203 | + async for event in autotune_runner.run_async(ctx): |
| 204 | + yield event |
| 205 | + |
| 206 | + autotune_results = ctx.session.state.get("autotune_results", {}) |
| 207 | + if ( |
| 208 | + autotune_results.get("status") == "success" |
| 209 | + and autotune_results.get("best_config") is not None |
| 210 | + and autotune_results.get("best_time_ms") is not None |
| 211 | + ): |
| 212 | + logging.info(f"[{self.name}] Running ApplyBestConfigAgent...") |
| 213 | + async for event in apply_best_config_agent.run_async(ctx): |
| 214 | + yield event |
| 215 | + else: |
| 216 | + logging.warning( |
| 217 | + f"[{self.name}] Autotune was not successful or no best configuration" |
| 218 | + " found. Skipping ApplyBestConfigAgent." |
| 219 | + ) |
| 220 | + |
| 221 | + logging.info(f"[{self.name}] Running AutotuneSummaryAgent...") |
| 222 | + async for event in autotune_summary_agent.run_async(ctx): |
| 223 | + yield event |
| 224 | + |
| 225 | + |
| 226 | +autotune_agent = CombinedAutotuneAgent(name="AutotuneAgent") |
179 | 227 |
|
180 | 228 | __all__ = ["autotune_agent"] |
0 commit comments