Skip to content

Commit a5a10d5

Browse files
Merge pull request #64 from AI-Hypercomputer/shangkun-context-management
Implement context management technique to prevent context overflow
2 parents 8830839 + 05be589 commit a5a10d5

13 files changed

Lines changed: 92 additions & 168 deletions

File tree

MaxKernel/auto_agent/agent.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
for the human-in-the-loop kernel generation process.
55
"""
66

7+
from google.adk.apps.app import App
8+
9+
from auto_agent.config import get_compaction_config
10+
from auto_agent.constants import EVENTS_COMPACTION
711
from auto_agent.subagents.autotuning.agent import autotune_agent
812
from auto_agent.subagents.kernel_writing import (
913
implement_kernel_agent,
@@ -29,4 +33,16 @@
2933
max_iterations=5,
3034
)
3135

32-
__all__ = ["root_agent"]
36+
if EVENTS_COMPACTION:
37+
compaction_config = get_compaction_config()
38+
else:
39+
compaction_config = None
40+
41+
42+
app = App(
43+
name="auto_agent",
44+
root_agent=root_agent,
45+
events_compaction_config=compaction_config,
46+
)
47+
48+
__all__ = ["root_agent", "app"]

MaxKernel/auto_agent/config.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import os
44

5+
from google.adk.apps.app import EventsCompactionConfig
56
from google.adk.planners import BuiltInPlanner
67
from google.genai import types
78

8-
from auto_agent.constants import TOP_K, TOP_P
9+
from auto_agent.constants import TEMPERATURE, TOP_K, TOP_P
910

1011
# Environment variables
1112
WORKDIR = os.environ.get("WORKDIR", os.path.dirname(os.path.abspath(__file__)))
@@ -14,17 +15,32 @@
1415
INCLUDE_THOUGHTS = os.environ.get("INCLUDE_THOUGHTS", "true").lower() == "true"
1516
MAX_COMPILATION_RETRIES = int(os.environ.get("MAX_COMPILATION_RETRIES", "6"))
1617

18+
# Set events compaction policy to avoid memory overflow
19+
def get_compaction_config():
20+
return EventsCompactionConfig(
21+
token_threshold=200000,
22+
event_retention_size=100,
23+
compaction_interval=1,
24+
overlap_size=0,
25+
)
26+
1727
# Model configuration
1828
model_config = types.GenerateContentConfig(
19-
temperature=0.5,
29+
temperature=TEMPERATURE,
2030
top_p=TOP_P,
2131
top_k=TOP_K,
2232
)
2333

24-
# Planner configuration with thinking/reasoning traces
25-
thinking_planner = BuiltInPlanner(
26-
thinking_config=types.ThinkingConfig(
27-
include_thoughts=INCLUDE_THOUGHTS,
28-
thinking_level="high",
34+
35+
def get_thinking_planner(level: str = "high") -> BuiltInPlanner:
36+
"""Returns a BuiltInPlanner configured with the specified thinking level.
37+
38+
Args:
39+
level: The thinking level to use. Can be 'high', 'medium', or 'low'.
40+
"""
41+
return BuiltInPlanner(
42+
thinking_config=types.ThinkingConfig(
43+
include_thoughts=INCLUDE_THOUGHTS,
44+
thinking_level=level,
45+
)
2946
)
30-
)

MaxKernel/auto_agent/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
MODEL_NAME = "gemini-3.1-pro-preview"
22
MAX_ITERATIONS = 5
33
LLM_GEN_RETRY_COUNT = 3
4-
TEMPERATURE = 0.1
4+
TEMPERATURE = 0.5
55
TOP_P = 0.9
66
TOP_K = 5
77
REQUEST_TIMEOUT = 3600 * 3
88
TPU_SERVER_PORT = 5463
99
CPU_SERVER_PORT = 5464
1010
EVAL_SERVER_PORT = 1245
11+
EVENTS_COMPACTION = True

MaxKernel/auto_agent/subagents/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
- kernel_writing: Planning, implementation, and compilation validation
55
- testing: Test generation, validation, and execution
66
- profiling: Performance profiling and analysis
7-
- explanation: Explanations for the kernel generation process
87
"""
98

10-
from . import explanation, kernel_writing, profiling, testing
9+
from . import kernel_writing, profiling, testing
1110

1211
__all__ = [
1312
"kernel_writing",
1413
"testing",
1514
"profiling",
16-
"explanation",
1715
]

MaxKernel/auto_agent/subagents/autotuning/agent.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from google.adk.agents.invocation_context import InvocationContext
1010
from google.adk.events import Event, EventActions
1111

12-
from auto_agent.config import model_config, thinking_planner
12+
from auto_agent.config import get_thinking_planner, model_config
1313
from auto_agent.constants import MODEL_NAME
1414
from auto_agent.custom_types import CustomLlmAgent
1515
from auto_agent.subagents.autotuning.autotune_tool import autotune_kernel
@@ -32,7 +32,7 @@
3232
name="AutotunePlannerAgent",
3333
model=MODEL_NAME,
3434
generate_content_config=model_config,
35-
planner=thinking_planner,
35+
planner=get_thinking_planner("high"),
3636
instruction=autotune_prompt.PROMPT,
3737
description="Prepares code template and search space for auto-tuning Pallas kernels.",
3838
tools=[filesystem_tool_r, write_autotune_specs_tool, search_api_tool],
@@ -165,7 +165,7 @@ async def _run_async_impl(
165165
name="ApplyBestConfigAgent",
166166
model=MODEL_NAME,
167167
generate_content_config=model_config,
168-
planner=thinking_planner,
168+
planner=get_thinking_planner("high"),
169169
instruction=apply_best_config_prompt.PROMPT,
170170
description="Applies autotuning results to the optimized kernel file.",
171171
tools=[filesystem_tool_r, write_optimized_kernel_tool],
@@ -178,7 +178,6 @@ async def _run_async_impl(
178178
name="AutotuneSummaryAgent",
179179
model=MODEL_NAME,
180180
generate_content_config=model_config,
181-
planner=thinking_planner,
182181
instruction=summary_prompt.PROMPT,
183182
description="Summarizes autotuning results.",
184183
tools=[filesystem_tool_r],

MaxKernel/auto_agent/subagents/explanation/__init__.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

MaxKernel/auto_agent/subagents/explanation/agent.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

MaxKernel/auto_agent/subagents/explanation/prompts/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

MaxKernel/auto_agent/subagents/explanation/prompts/explanation_prompt.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

MaxKernel/auto_agent/subagents/kernel_writing/agent.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
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
)
18-
from auto_agent.config import model_config, thinking_planner, MAX_COMPILATION_RETRIES
16+
from auto_agent.config import get_thinking_planner, model_config, MAX_COMPILATION_RETRIES
1917
from auto_agent.constants import MODEL_NAME
2018
from auto_agent.custom_types import CustomLlmAgent
2119
from auto_agent.subagents.kernel_writing.kernel_compilation import (
@@ -267,18 +265,14 @@ async def _run_async_impl(
267265
name="PlanKernelAgent",
268266
model=MODEL_NAME,
269267
generate_content_config=model_config,
270-
planner=thinking_planner,
268+
planner=get_thinking_planner("high"),
271269
instruction=kernel_planning_prompt.PROMPT,
272270
description="Creates or revises a detailed optimization plan for a Pallas kernel.",
273271
tools=(
274272
[search_api_tool, filesystem_tool_rw, vertex_ai_rag_tool]
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
@@ -287,7 +281,6 @@ async def _run_async_impl(
287281
name="ReadFileForValidationAgent",
288282
model=MODEL_NAME,
289283
generate_content_config=model_config,
290-
planner=thinking_planner,
291284
instruction=read_file_prompt.PROMPT,
292285
description="Reads the kernel file mentioned by the user or from state for validation.",
293286
tools=[filesystem_tool_r],
@@ -297,7 +290,7 @@ async def _run_async_impl(
297290
name="FixKernelCompilationAgent",
298291
model=MODEL_NAME,
299292
generate_content_config=model_config,
300-
planner=thinking_planner,
293+
planner=get_thinking_planner("high"),
301294
instruction=fix_kernel_compilation.PROMPT,
302295
description="Fixes compilation errors in the generated kernel while preserving optimization strategy.",
303296
tools=(
@@ -319,7 +312,7 @@ async def _run_async_impl(
319312
name="AddDebugStatementsAgent",
320313
model=MODEL_NAME,
321314
generate_content_config=model_config,
322-
planner=thinking_planner,
315+
planner=get_thinking_planner("high"),
323316
instruction=add_debug_statements.PROMPT,
324317
description="Adds strategic debugging statements to diagnose persistent compilation issues.",
325318
tools=[filesystem_tool_r, write_optimized_kernel_tool],
@@ -331,7 +324,7 @@ async def _run_async_impl(
331324
name="CleanupDebugStatementsAgent",
332325
model=MODEL_NAME,
333326
generate_content_config=model_config,
334-
planner=thinking_planner,
327+
planner=get_thinking_planner("high"),
335328
instruction=cleanup_debug_statements.PROMPT,
336329
description="Removes debugging statements from successfully compiled kernel.",
337330
tools=[filesystem_tool_r, write_optimized_kernel_tool],
@@ -358,7 +351,6 @@ async def _run_async_impl(
358351
name="KernelCompilationSummaryAgent",
359352
model=MODEL_NAME,
360353
generate_content_config=model_config,
361-
planner=thinking_planner,
362354
instruction=kernel_compilation_summary.PROMPT,
363355
description="Summarizes kernel compilation validation results with full trace on failure.",
364356
include_contents="none",
@@ -379,7 +371,7 @@ async def _run_async_impl(
379371
name="ImplementKernelAgent",
380372
model=MODEL_NAME,
381373
generate_content_config=model_config,
382-
planner=thinking_planner,
374+
planner=get_thinking_planner("high"),
383375
instruction=kernel_implementation_prompt.PROMPT,
384376
description="Implements the optimized Pallas kernel following the plan.",
385377
tools=(

0 commit comments

Comments
 (0)