File size: 3,339 Bytes
902d2bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: CC-BY-NC-4.0
"""CompoundingKernel: a self-improving search in which LLM agents write fast GPU kernels."""
from __future__ import annotations

import copy
import json
import os
import string

__version__ = "0.1.0"

# Our prompts are not included. Every model-facing text (the prompts, the first and follow-up
# turns, the tool descriptions) comes from load_prompt(). With no prompt_dir, or no file for a
# slot, the slot gets this placeholder, so the code shows the data flow without our text.
PROMPT_PLACEHOLDER = "<prompt not released>"

# The slots, and the fields each one is given (as $name in your own template files).
PROMPT_SLOTS = {
    "controller": (),
    "controller_start": ("problem_dir", "minutes", "budget", "slots"),
    "controller_continue": ("minutes_remaining", "used", "budget", "transient"),
    "child": (),
    "child_task": ("problem_dir", "parent_id", "parent_files", "parent_result", "direction"),
    "tool_query_archive": (),
    "tool_expand": (),
    "tool_compile_check": (),
    "tool_evaluate_kernel": (),
    "tool_profile_kernel": (),
    "tool_check_robustness": (),
    "tool_query_history": (),
    "tool_get_solution": (),
}


def load_prompt(name: str, prompt_dir: str | None = None, **fields) -> str:
    """The text for one model-facing slot: <prompt_dir>/<name>.txt with $fields filled in."""
    if prompt_dir:
        path = os.path.join(prompt_dir, name + ".txt")
        if os.path.isfile(path):
            with open(path, encoding="utf-8") as f:
                return string.Template(f.read()).safe_substitute(fields)
    return PROMPT_PLACEHOLDER


# The settings of one search (see examples/example_config.yaml for what each key means).
DEFAULTS = {
    "search": {"arm": "hyper", "problem_dir": None, "run_id": "run-1", "archive_dir": "runs",
               "time_limit_min": 600, "budget": 1000, "gpus": [0], "children_per_gpu": 4,
               "candidates_per_child": 1},
    "model": {"provider": "bedrock", "id": "global.anthropic.claude-opus-5-5", "aws_region": None,
              "controller_max_turns": 100000, "child_max_turns": 100000},
    "prompts": {"prompt_dir": None},
    "harness": {"compile_timeout_s": 300, "eval_timeout_s": 900, "lock_dir": "/tmp/ck-gpu-locks",
                "anchors": None, "bench_config": None},
    "checks": {"native_code": "refuse", "precision": "paper",
               "precision_exempt_boards": ["Quant"], "robustness_on_new_best": True,
               "robustness_shapes": 12, "sanitizer_on_new_best": True},
    "controller": {"reinvoke_max": 20, "min_left_min": 10, "idle_wait_min": 15, "grace_s": 60,
                   "backoff_s": 30, "backoff_cap_s": 600},
    "tree": {"c": 0.7, "width_start": 4, "width_decay": 0.6, "width_min": 1, "grace": 3,
             "margin": 0.15},
}


def load_config(path) -> dict:
    """A config file (YAML, or JSON without PyYAML) merged over DEFAULTS."""
    with open(path, encoding="utf-8") as f:
        text = f.read()
    try:
        import yaml
        data = yaml.safe_load(text)
    except ImportError:
        data = json.loads(text)
    cfg = copy.deepcopy(DEFAULTS)
    for section, values in (data or {}).items():
        cfg.setdefault(section, {}).update(values or {})
    return cfg