File size: 8,445 Bytes
7336adb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
"""LLM baseline agent using Google Gemini (via OpenAI-compatible SDK).

Requires GEMINI_API_KEY environment variable (or pass via --api-key).
Uses temperature=0.0 for near-deterministic behavior.
Usage:
    GEMINI_API_KEY=... python baseline_inference.py
    python baseline_inference.py --api-key YOUR_KEY
"""

from __future__ import annotations

import argparse
import json
import os
import sys
from pathlib import Path

# Load .env file if present
_env_path = Path(__file__).parent / ".env"
if _env_path.exists():
    for line in _env_path.read_text().splitlines():
        line = line.strip()
        if line and not line.startswith("#") and "=" in line:
            key, _, value = line.partition("=")
            os.environ.setdefault(key.strip(), value.strip())

try:
    from openai import OpenAI
except ImportError:
    print("Error: openai package not installed. Run: pip install openai")
    sys.exit(1)

from ml_training_debugger.models import MLTrainingAction
from server.environment import MLTrainingEnvironment

ALL_TASKS = [
    "task_001",
    "task_002",
    "task_003",
    "task_004",
    "task_005",
    "task_006",
    "task_007",
]

SYSTEM_PROMPT = """You are an expert ML engineer debugging a PyTorch training run.
You are interacting with an environment that simulates a broken training job.

Available actions (respond with JSON only, no explanation):
- {"action_type": "inspect_gradients"} - View gradient statistics per layer
- {"action_type": "inspect_data_batch"} - View data batch statistics and confusion matrix
- {"action_type": "inspect_model_modes"} - View model layer modes (train/eval)
- {"action_type": "inspect_model_weights"} - View model weight statistics
- {"action_type": "inspect_code"} - View PyTorch training code
- {"action_type": "modify_config", "target": "<field>", "value": <val>} - Change a hyperparameter
- {"action_type": "add_callback"} - Add gradient clipping/scheduler
- {"action_type": "patch_data_loader"} - Fix data pipeline issues
- {"action_type": "fix_model_mode"} - Call model.train()
- {"action_type": "fix_code", "line": <int>, "replacement": "<code>"} - Fix a code line
- {"action_type": "restart_run"} - Restart training (requires a fix first)
- {"action_type": "mark_diagnosed", "diagnosis": "<cause>"} - Submit diagnosis

Valid diagnoses: lr_too_high, vanishing_gradients, data_leakage, overfitting, batchnorm_eval_mode, code_bug, scheduler_misconfigured

Strategy:
1. First investigate by inspecting gradients, data, model modes, and code
2. Form a hypothesis based on the evidence gathered
3. Apply the correct fix for the identified root cause
4. Restart training to verify the fix works
5. Submit your diagnosis

IMPORTANT: Respond with ONLY a valid JSON action object. No explanation, no markdown, no code blocks."""


def run_llm_episode(task_id: str, client: OpenAI, model_name: str) -> float:
    """Run one LLM agent episode."""
    env = MLTrainingEnvironment()
    obs = env.reset(seed=42, episode_id=f"llm_{task_id}", task_id=task_id)

    initial_obs = {
        "training_loss_history": obs.training_loss_history[:5],
        "val_accuracy_history": obs.val_accuracy_history[:5],
        "current_config": obs.current_config.model_dump(),
        "error_log": obs.error_log,
        "available_actions": obs.available_actions,
        "notes": obs.notes,
        "gpu_memory_used_gb": obs.gpu_memory_used_gb,
    }

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {
            "role": "user",
            "content": f"New episode started for a broken PyTorch training run.\n\nInitial observation:\n{json.dumps(initial_obs, indent=2, default=str)}",
        },
    ]

    for step in range(25):
        if obs.done:
            break

        try:
            response = client.chat.completions.create(
                model=model_name,
                messages=messages,
                temperature=0.0,
                max_tokens=300,
            )
            action_text = response.choices[0].message.content.strip()
        except Exception as e:
            print(f"    Step {step}: API error — {e}", file=sys.stderr)
            break

        # Clean up common LLM formatting issues
        action_text = action_text.strip("`").strip()
        if action_text.startswith("json"):
            action_text = action_text[4:].strip()

        messages.append({"role": "assistant", "content": action_text})

        try:
            action_data = json.loads(action_text)
            action = MLTrainingAction(**action_data)
        except (json.JSONDecodeError, Exception) as e:
            messages.append(
                {
                    "role": "user",
                    "content": f"Invalid action format: {e}. Respond with ONLY valid JSON.",
                }
            )
            continue

        obs = env.step(action)

        obs_summary: dict = {
            "reward": obs.reward,
            "done": obs.done,
            "step": obs.episode_state.step_count,
            "available_actions": obs.available_actions,
        }
        if obs.error_log:
            obs_summary["error_log"] = obs.error_log
        if obs.gradient_stats:
            obs_summary["gradient_stats"] = [
                {
                    "layer": g.layer_name,
                    "mean_norm": round(g.mean_norm, 4),
                    "exploding": g.is_exploding,
                    "vanishing": g.is_vanishing,
                }
                for g in obs.gradient_stats
            ]
        if obs.data_batch_stats:
            obs_summary["data_overlap"] = obs.data_batch_stats.class_overlap_score
            obs_summary["duplicate_ratio"] = obs.data_batch_stats.duplicate_ratio
        if obs.model_mode_info:
            obs_summary["model_modes"] = obs.model_mode_info
        if obs.code_snippet:
            obs_summary["code"] = obs.code_snippet.code[:600]
            obs_summary["hint"] = obs.code_snippet.hint

        messages.append(
            {
                "role": "user",
                "content": f"Observation after your action:\n{json.dumps(obs_summary, indent=2, default=str)}",
            }
        )

    session = env._get_session()
    return session.last_score if session and session.last_score is not None else 0.0


PROVIDERS = {
    "groq": {
        "env_key": "GROQ_API_KEY",
        "base_url": "https://api.groq.com/openai/v1",
        "default_model": "llama-3.3-70b-versatile",
    },
    "cerebras": {
        "env_key": "CEREBRAS_API_KEY",
        "base_url": "https://api.cerebras.ai/v1",
        "default_model": "llama3.1-8b",
    },
    "gemini": {
        "env_key": "GEMINI_API_KEY",
        "base_url": "https://generativelanguage.googleapis.com/v1beta/openai/",
        "default_model": "gemini-2.0-flash",
    },
    "openai": {
        "env_key": "OPENAI_API_KEY",
        "base_url": None,
        "default_model": "gpt-4o",
    },
}


def main() -> None:
    parser = argparse.ArgumentParser(description="LLM baseline agent")
    parser.add_argument("--url", default="http://localhost:7860")
    parser.add_argument("--api-key", default=None, help="API key")
    parser.add_argument(
        "--provider",
        default="groq",
        choices=list(PROVIDERS.keys()),
        help="LLM provider (default: groq)",
    )
    parser.add_argument("--model", default=None, help="Model name (auto-detected from provider)")
    args = parser.parse_args()

    prov = PROVIDERS[args.provider]
    api_key = args.api_key or os.environ.get(prov["env_key"])
    if not api_key:
        print(f"Error: Set {prov['env_key']} env var or pass --api-key")
        sys.exit(1)

    model_name = args.model or prov["default_model"]
    client_kwargs: dict = {"api_key": api_key}
    if prov["base_url"]:
        client_kwargs["base_url"] = prov["base_url"]
    client = OpenAI(**client_kwargs)

    scores: dict[str, float] = {}
    print(f"Running LLM baseline with {args.provider}/{model_name}...", file=sys.stderr)

    for task_id in ALL_TASKS:
        try:
            score = run_llm_episode(task_id, client, model_name)
            scores[task_id] = round(score, 4)
            print(f"  {task_id}: {score:.4f}", file=sys.stderr)
        except Exception as e:
            print(f"  {task_id}: ERROR — {e}", file=sys.stderr)
            scores[task_id] = 0.0

    print(json.dumps(scores, indent=2))


if __name__ == "__main__":
    main()