Skip to content

Commit cbf3870

Browse files
refactor: move TPU and Pallas docs state initialization to pipeline start
1 parent a769ecc commit cbf3870

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

MaxKernel/auto_agent/subagents/kernel_writing/agent.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
from google.adk.events import Event, EventActions
1010

1111
from auto_agent.callbacks import (
12-
add_pallas_docs,
1312
extract_fix_summary,
14-
get_tpu_version_callback,
1513
load_kernel_and_plan_to_state,
1614
load_single_kernel_to_state,
1715
)
@@ -275,10 +273,6 @@ async def _run_async_impl(
275273
if vertex_ai_rag_tool
276274
else [search_api_tool, filesystem_tool_rw]
277275
),
278-
before_agent_callback=[
279-
add_pallas_docs,
280-
get_tpu_version_callback,
281-
],
282276
)
283277

284278
# Kernel compilation validation agents

MaxKernel/auto_agent/subagents/pipeline_agent.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Autonomous pipeline agent for chaining kernel generation steps."""
22

3+
import json
34
import logging
45
import os
56
import re
@@ -12,7 +13,8 @@
1213
from google.adk.models import LlmRequest
1314
from google.genai import types
1415

15-
from auto_agent.config import WORKDIR
16+
from auto_agent.config import TPU_VERSION, WORKDIR
17+
from auto_agent.knowledge_base import pallas_docs, pallas_profiling_docs
1618

1719

1820
class AutonomousPipelineAgent(BaseAgent):
@@ -319,6 +321,29 @@ def _initialize_state(self, ctx: InvocationContext) -> Event:
319321
if "history" not in ctx.session.state:
320322
ctx.session.state["history"] = []
321323

324+
# Initialize TPU version and Pallas docs in state
325+
tpu_version = TPU_VERSION
326+
ctx.session.state["tpu_version"] = tpu_version
327+
logging.info(f"[{self.name}] Detected TPU version: {tpu_version}")
328+
329+
try:
330+
with open("auto_agent/tpu_specs.json", "r") as f:
331+
tpu_specs = json.load(f)
332+
333+
if tpu_version in tpu_specs:
334+
ctx.session.state["tpu_specs"] = tpu_specs[tpu_version]
335+
else:
336+
ctx.session.state["tpu_specs"] = (
337+
"TPU specs not found for detected version."
338+
)
339+
logging.info(f"[{self.name}] Loaded TPU specs for {tpu_version}")
340+
except Exception as e:
341+
logging.error(f"[{self.name}] Failed to load TPU specs: {e}")
342+
ctx.session.state["tpu_specs"] = None
343+
344+
ctx.session.state["pallas_docs"] = pallas_docs.PROMPT
345+
ctx.session.state["pallas_profiling_docs"] = pallas_profiling_docs.PROMPT
346+
322347
# Path related states
323348
session_dir = self.session_dir or os.path.join(WORKDIR, ctx.session.id)
324349

@@ -425,6 +450,12 @@ def _initialize_state(self, ctx: InvocationContext) -> Event:
425450
"autotune_specs_path": ctx.session.state["autotune_specs_path"],
426451
"autotune_results_path": ctx.session.state["autotune_results_path"],
427452
"xplane_pb_path": ctx.session.state["xplane_pb_path"],
453+
"tpu_version": ctx.session.state.get("tpu_version"),
454+
"tpu_specs": ctx.session.state.get("tpu_specs"),
455+
"pallas_docs": ctx.session.state.get("pallas_docs"),
456+
"pallas_profiling_docs": ctx.session.state.get(
457+
"pallas_profiling_docs"
458+
),
428459
}
429460
),
430461
)

0 commit comments

Comments
 (0)