File size: 11,119 Bytes
82996ed
 
 
47d99a3
b3d5c94
 
 
82996ed
 
 
 
 
 
 
 
d8eeec6
82996ed
47d99a3
6be6d8e
 
 
b3d5c94
6be6d8e
b3d5c94
 
 
 
c5307a2
b3d5c94
6be6d8e
 
 
da20dfa
6be6d8e
 
 
b3d5c94
2518b48
 
6be6d8e
b3d5c94
6be6d8e
 
 
 
 
 
82996ed
2518b48
006d13b
 
2518b48
6be6d8e
2518b48
6be6d8e
 
82996ed
6be6d8e
9442887
006d13b
6be6d8e
9442887
6be6d8e
 
 
 
 
47d99a3
6be6d8e
82996ed
 
 
 
 
6be6d8e
82996ed
 
 
6be6d8e
82996ed
 
 
6be6d8e
82996ed
 
 
6be6d8e
 
82996ed
6be6d8e
82996ed
 
6be6d8e
82996ed
6be6d8e
82996ed
 
6be6d8e
 
 
 
82996ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6be6d8e
 
 
 
 
 
 
 
82996ed
 
 
 
6be6d8e
 
82996ed
 
 
6be6d8e
 
 
 
 
d8eeec6
6be6d8e
2518b48
6be6d8e
 
 
 
 
 
 
 
2518b48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6be6d8e
 
 
 
 
 
 
 
 
 
 
47d99a3
56b42b0
82996ed
2518b48
 
d8eeec6
 
6be6d8e
9442887
6be6d8e
 
2518b48
82996ed
6be6d8e
2518b48
d8eeec6
6be6d8e
82996ed
6be6d8e
 
 
82996ed
6be6d8e
2518b48
6be6d8e
 
a72d669
6be6d8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8eeec6
 
6be6d8e
 
 
82996ed
9442887
2518b48
9442887
 
82996ed
ee366d9
2518b48
9442887
2518b48
 
9442887
2518b48
 
 
 
 
 
 
b3d5c94
 
2518b48
 
b3d5c94
 
2518b48
 
b3d5c94
 
 
 
 
 
 
 
 
 
 
6415da0
2518b48
 
 
 
 
 
 
 
 
 
 
 
 
6415da0
2518b48
 
 
ee366d9
6be6d8e
b3d5c94
6415da0
 
 
9442887
ee366d9
 
 
2518b48
 
82996ed
 
 
6be6d8e
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
"""Inference script for the PyTorch Training Run Debugger.

Required environment variables (injected by evaluator):
    API_BASE_URL   — LLM API endpoint (must have default)
    MODEL_NAME     — Model identifier (must have default)
    HF_TOKEN       — API token (mandatory, no default)
"""

from __future__ import annotations

import asyncio
import json
import os
import sys
from typing import List, Optional

from openai import OpenAI
from openenv.core import GenericAction, GenericEnvClient

# ---------------------------------------------------------------------------
# Configuration — EXACTLY per hackathon spec
# ---------------------------------------------------------------------------
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o")
HF_TOKEN = os.getenv("HF_TOKEN")

IMAGE_NAME = os.getenv("IMAGE_NAME") or os.getenv("LOCAL_IMAGE_NAME")
ENV_URL = os.getenv("ENV_URL", "https://ujjwalpardeshi-pytorch-training-debugger.hf.space")
BENCHMARK = "pytorch-training-debugger"

MAX_STEPS = 25
SUCCESS_SCORE_THRESHOLD = 0.5
TEMPERATURE = 0.0
MAX_TOKENS = 300

# All tasks to run
ALL_TASK_IDS = ["task_001", "task_002", "task_003", "task_004", "task_005", "task_006", "task_007"]

# ---------------------------------------------------------------------------
# Structured logging — EXACTLY per hackathon spec
# ---------------------------------------------------------------------------


def log_start(task: str, env: str, model: str) -> None:
    print(f"[START] task={task} env={env} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_val = error if error else "null"
    done_val = str(done).lower()
    clean_action = action.replace("\n", " ").replace("\r", " ")
    print(
        f"[STEP] step={step} action={clean_action} reward={reward:.2f} done={done_val} error={error_val}",
        flush=True,
    )


def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(
        f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}",
        flush=True,
    )


# ---------------------------------------------------------------------------
# System prompt
# ---------------------------------------------------------------------------
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
- {"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>}
- {"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>"}
- {"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

IMPORTANT: Respond with ONLY a valid JSON action object."""


def _build_obs_summary(obs: dict) -> dict:
    """Build a compact observation summary for the LLM context."""
    summary: dict = {"available_actions": obs.get("available_actions", [])}
    if obs.get("error_log"):
        summary["error_log"] = obs["error_log"]
    if obs.get("training_loss_history"):
        summary["loss_trend"] = obs["training_loss_history"][:5]
    if obs.get("val_accuracy_history"):
        summary["val_acc_trend"] = obs["val_accuracy_history"][:5]
    if obs.get("gradient_stats"):
        summary["gradient_stats"] = [
            {
                "layer": g.get("layer_name", ""),
                "mean_norm": round(g.get("mean_norm", 0), 4),
                "exploding": g.get("is_exploding", False),
                "vanishing": g.get("is_vanishing", False),
            }
            for g in obs["gradient_stats"]
        ]
    if obs.get("data_batch_stats"):
        dbs = obs["data_batch_stats"]
        summary["data_overlap"] = dbs.get("class_overlap_score", 0)
        summary["duplicate_ratio"] = dbs.get("duplicate_ratio", 0)
    if obs.get("model_mode_info"):
        summary["model_modes"] = obs["model_mode_info"]
    if obs.get("model_weight_stats"):
        summary["weight_stats"] = [
            {
                "layer": w.get("layer_name", ""),
                "norm": round(w.get("weight_norm", 0), 4),
            }
            for w in obs["model_weight_stats"]
        ]
    if obs.get("code_snippet"):
        cs = obs["code_snippet"]
        summary["code"] = cs.get("code", "")[:600]
        summary["hint"] = cs.get("hint", "")
    if obs.get("notes"):
        summary["notes"] = obs["notes"]
    return summary


def get_model_message(
    client: OpenAI,
    step: int,
    last_obs_summary: dict,
    last_reward: float,
    history: List[str],
) -> str:
    """Get next action from the LLM with retry logic."""
    history_ctx = "\n".join(history[-5:]) if history else "No previous steps."
    user_content = (
        f"Step {step}. Last reward: {last_reward:+.2f}\n"
        f"Recent history:\n{history_ctx}\n\n"
        f"Current observation:\n"
        f"{json.dumps(last_obs_summary, indent=2, default=str)}\n\n"
        "What action should you take next? Respond with JSON only."
    )

    max_retries = 3
    for attempt in range(max_retries):
        try:
            completion = client.chat.completions.create(
                model=MODEL_NAME,
                messages=[
                    {"role": "system", "content": SYSTEM_PROMPT},
                    {"role": "user", "content": user_content},
                ],
                temperature=TEMPERATURE,
                max_tokens=MAX_TOKENS,
            )
            text = (completion.choices[0].message.content or "").strip()
            if text:
                return text
        except Exception as exc:
            print(f"[DEBUG] Model request failed (attempt {attempt+1}): {exc}", flush=True)
            if attempt < max_retries - 1:
                import time
                time.sleep((attempt + 1) * 2)
            else:
                raise
    return '{"action_type": "inspect_gradients"}'


def parse_action(raw: str) -> str:
    """Clean up LLM output to extract JSON action string."""
    text = raw.strip().strip("`").strip()
    if text.startswith("json"):
        text = text[4:].strip()
    try:
        json.loads(text)
        return text
    except json.JSONDecodeError:
        return '{"action_type": "inspect_gradients"}'


async def run_task(env: GenericEnvClient, client: OpenAI, task_id: str) -> None:
    """Run a single task episode with [START]/[END] logging."""
    history: List[str] = []
    rewards: List[float] = []
    steps_taken = 0
    score = 0.01
    success = False

    log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)

    try:
        result = await env.reset(task_id=task_id, seed=42)
        obs = result.observation
        last_reward = 0.0

        for step in range(1, MAX_STEPS + 1):
            if result.done:
                break

            obs_summary = _build_obs_summary(obs)
            raw = get_model_message(client, step, obs_summary, last_reward, history)
            action_str = parse_action(raw)

            action = GenericAction(**json.loads(action_str))
            result = await env.step(action)
            obs = result.observation

            reward = result.reward or 0.0
            done = result.done
            error = (
                obs.get("notes")
                if "invalid" in str(obs.get("notes", "")).lower()
                else None
            )

            rewards.append(reward)
            steps_taken = step
            last_reward = reward

            log_step(step=step, action=action_str, reward=reward, done=done, error=error)
            history.append(f"Step {step}: {action_str!r} -> reward {reward:+.2f}")

            if done:
                break

        # Score: clamp strictly between 0 and 1 (evaluator rejects 0.0 and 1.0)
        total_reward = sum(rewards)
        score = round(min(max(total_reward, 0.01), 0.99), 2)
        success = score >= SUCCESS_SCORE_THRESHOLD

    except Exception as exc:
        print(f"[DEBUG] Task {task_id} error: {exc}", flush=True)
        score = 0.01

    finally:
        log_end(success=success, steps=steps_taken, score=score, rewards=rewards)


async def main() -> None:
    # Optional: run specific task or all tasks
    target_task = os.getenv("TASK_NAME")
    tasks_to_run = [target_task] if target_task else ALL_TASK_IDS

    # Initialize client EXACTLY as spec: api_key=HF_TOKEN
    client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)

    print(f"[DEBUG] API_BASE_URL={API_BASE_URL}", flush=True)
    print(f"[DEBUG] HF_TOKEN={'set' if HF_TOKEN else 'NOT SET'}", flush=True)
    print(f"[DEBUG] MODEL_NAME={MODEL_NAME}", flush=True)
    print(f"[DEBUG] Tasks to run: {tasks_to_run}", flush=True)

    # Mandatory LLM proxy call — ensures at least one call goes through
    try:
        test_resp = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[{"role": "user", "content": "Say OK"}],
            max_tokens=5,
        )
        print(f"[DEBUG] LLM proxy test OK: {test_resp.choices[0].message.content}", flush=True)
    except Exception as exc:
        print(f"[DEBUG] LLM proxy test failed: {exc}", flush=True)

    completed_tasks: set = set()
    env = None
    try:
        if IMAGE_NAME:
            env = await GenericEnvClient.from_docker_image(IMAGE_NAME)
        else:
            env = GenericEnvClient(
                base_url=ENV_URL,
                message_timeout_s=120.0,
            )
            await env.connect()

        for task_id in tasks_to_run:
            await run_task(env, client, task_id)
            completed_tasks.add(task_id)

    except Exception as exc:
        print(f"[DEBUG] Fatal error: {exc}", flush=True)

    finally:
        # Emit [START]/[END] for any tasks that didn't run
        for task_id in tasks_to_run:
            if task_id not in completed_tasks:
                log_start(task=task_id, env=BENCHMARK, model=MODEL_NAME)
                log_end(success=False, steps=0, score=0.01, rewards=[])
        if env is not None:
            try:
                await env.close()
            except Exception:
                pass


if __name__ == "__main__":
    asyncio.run(main())