File size: 7,138 Bytes
a59b710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecfeab6
a59b710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecfeab6
a59b710
 
 
 
 
 
 
 
 
 
 
ecfeab6
 
a59b710
 
 
 
ecfeab6
 
 
 
 
a59b710
 
 
 
 
 
 
 
 
 
 
 
ecfeab6
 
 
 
 
 
 
 
 
 
a59b710
 
ecfeab6
a59b710
ecfeab6
a59b710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
from pathlib import Path


def _load_json(path: Path) -> dict:
    with path.open("r", encoding="utf-8") as f:
        return json.load(f)


def _safe_import_matplotlib():
    try:
        import matplotlib.pyplot as plt

        return plt
    except ImportError as exc:
        raise SystemExit(
            "matplotlib is required. Install it with: py -3 -m pip install matplotlib"
        ) from exc


def _extract_log_history(run_dir: Path) -> list[dict]:
    """Prefer `metrics.jsonl` (written by `train_sinchan.py`), else `trainer_state.json`."""
    metrics_path = run_dir / "metrics.jsonl"
    if metrics_path.is_file():
        rows: list[dict] = []
        for line in metrics_path.read_text(encoding="utf-8").splitlines():
            line = line.strip()
            if not line:
                continue
            try:
                row = json.loads(line)
                if isinstance(row, dict):
                    rows.append(row)
            except json.JSONDecodeError:
                continue
        if rows:
            return rows

    trainer_state = run_dir / "trainer_state.json"
    if not trainer_state.exists():
        return []
    data = _load_json(trainer_state)
    history = data.get("log_history", [])
    return history if isinstance(history, list) else []


def _plot_training_curves(plt, history: list[dict], assets_dir: Path) -> list[Path]:
    outputs: list[Path] = []
    if not history:
        return outputs

    steps = []
    rewards = []
    losses = []
    entropies: list[tuple[int, float]] = []
    for row in history:
        step = row.get("step") if isinstance(row, dict) else None
        if step is None:
            continue
        try:
            step_val = int(step)
        except Exception:
            continue

        reward_keys = ["reward", "rewards/mean", "train/reward", "episode_reward", "objective"]
        reward_val = None
        for key in reward_keys:
            if key in row and isinstance(row[key], (int, float)):
                reward_val = float(row[key])
                break

        loss_keys = ["loss", "train/loss", "policy_loss", "objective/kl", "train_loss"]
        loss_val = None
        for key in loss_keys:
            if key in row and isinstance(row[key], (int, float)):
                loss_val = float(row[key])
                break

        if reward_val is not None:
            steps.append(step_val)
            rewards.append(reward_val)
        if loss_val is not None:
            losses.append((step_val, loss_val))
        if "entropy" in row and isinstance(row["entropy"], (int, float)):
            entropies.append((step_val, float(row["entropy"])))

    if rewards:
        plt.figure(figsize=(9, 5))
        plt.plot(steps, rewards, marker="o", linewidth=1.5)
        flat_r = len(rewards) > 0 and all(r == rewards[0] for r in rewards)
        plt.title(
            "Training reward vs step"
            + (" (flat — short run; use eval/baselines for env-scale signal)" if flat_r else "")
        )
        plt.xlabel("Step")
        plt.ylabel("Reward")
        plt.grid(alpha=0.3)
        out = assets_dir / "reward_curve_total.png"
        plt.tight_layout()
        plt.savefig(out, dpi=150)
        plt.close()
        outputs.append(out)

    if losses:
        x = [xv for xv, _ in losses]
        y = [yv for _, yv in losses]
        # TRL sometimes logs 0 loss on very short GRPO runs; entropy still moves — plot it
        # so the chart is a faithful readout, not a blank line.
        if all(v == 0.0 for v in y) and entropies:
            x = [a for a, _ in entropies]
            y = [b for _, b in entropies]
            y_label = "Entropy (proxy — reported loss was 0)"
            plot_title = "Policy entropy vs step (short run; loss column was 0 in logs)"
        else:
            y_label = "Loss"
            plot_title = "Training loss vs step"
        plt.figure(figsize=(9, 5))
        plt.plot(x, y, marker="o", linewidth=1.5, color="tab:red")
        plt.title(plot_title)
        plt.xlabel("Step")
        plt.ylabel(y_label)
        plt.grid(alpha=0.3)
        out = assets_dir / "loss_curve.png"
        plt.tight_layout()
        plt.savefig(out, dpi=150)
        plt.close()
        outputs.append(out)

    return outputs


def _plot_eval_summary(plt, eval_summary_path: Path, assets_dir: Path) -> list[Path]:
    if not eval_summary_path.exists():
        return []

    data = _load_json(eval_summary_path)
    policies = []
    avg_rewards = []
    for key in ["avg_reward_random", "avg_reward_rule_based"]:
        if key in data and isinstance(data[key], (int, float)):
            label = key.replace("avg_reward_", "")
            policies.append(label)
            avg_rewards.append(float(data[key]))

    if not policies:
        return []

    plt.figure(figsize=(7, 5))
    bars = plt.bar(policies, avg_rewards, color=["tab:gray", "tab:green"][: len(policies)])
    plt.title("Baseline Policy Comparison")
    plt.xlabel("Policy")
    plt.ylabel("Average Episode Reward")
    plt.grid(axis="y", alpha=0.25)
    for bar, val in zip(bars, avg_rewards):
        plt.text(
            bar.get_x() + bar.get_width() / 2,
            bar.get_height(),
            f"{val:.3f}",
            ha="center",
            va="bottom",
        )
    out = assets_dir / "baseline_comparison.png"
    plt.tight_layout()
    plt.savefig(out, dpi=150)
    plt.close()
    return [out]


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Generate reward/loss/eval PNG charts for hackathon evidence."
    )
    parser.add_argument(
        "--run-dir",
        default="training/artifacts/run1",
        help="Training output directory containing trainer_state.json",
    )
    parser.add_argument(
        "--eval-summary",
        default="training/artifacts/eval_summary.json",
        help="JSON output produced by training/evaluate_scenarios.py",
    )
    parser.add_argument(
        "--assets-dir",
        default="assets",
        help="Directory where PNG charts will be written",
    )
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    run_dir = Path(args.run_dir)
    eval_summary = Path(args.eval_summary)
    assets_dir = Path(args.assets_dir)
    assets_dir.mkdir(parents=True, exist_ok=True)

    plt = _safe_import_matplotlib()

    produced: list[Path] = []
    produced.extend(_plot_training_curves(plt, _extract_log_history(run_dir), assets_dir))
    produced.extend(_plot_eval_summary(plt, eval_summary, assets_dir))

    if produced:
        print("Generated charts:")
        for path in produced:
            print(f"- {path}")
    else:
        print(
            "No charts generated. Ensure trainer_state.json and/or eval_summary.json exist "
            "at the provided paths."
        )


if __name__ == "__main__":
    main()