File size: 9,586 Bytes
cbc33fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Final cross-model report: results table, quality-vs-diversity frontier chart,
and qualitative examples. Reads outputs/eval/results.csv.
"""
from __future__ import annotations

import argparse
import csv
import json
import sys
from pathlib import Path

import numpy as np

import logbook

ROOT = Path(__file__).resolve().parent.parent

ORDER = ["base", "E0-baseline", "E1-div-individual", "E2-div-group",
         "E3-multipos", "E4-divpo-emb", "E4-divpo-prob"]

PRETTY = {
    "base": "Base (Qwen3-4B-Instruct)",
    "E0-baseline": "E0 · GRPO, quality only",
    "E1-div-individual": "E1 · div-grpo-individual (d_i)",
    "E2-div-group": "E2 · div-grpo-group (d_i + m_i)",
    "E3-multipos": "E3 · multi-positive weighted DPO",
    "E4-divpo-emb": "E4a · DivPO (embedding)",
    "E4-divpo-prob": "E4b · DivPO (probability)",
}


def load(path: Path) -> list[dict]:
    rows = list(csv.DictReader(open(path)))
    for r in rows:
        for k, v in r.items():
            if k != "model":
                try:
                    r[k] = float(v)
                except (TypeError, ValueError):
                    pass
    key = {m: i for i, m in enumerate(ORDER)}
    rows.sort(key=lambda r: key.get(r["model"], 99))
    return rows


def frontier_chart(rows, out_path):
    import matplotlib
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(1, 3, figsize=(17, 5.2))
    cols = plt.cm.tab10(np.linspace(0, 1, 10))

    panels = [
        ("pairwise", "Mean pairwise embedding distance", 0),
        ("logdet", "Log-det volume (semantic coverage)", 1),
        ("eff_rank", "Effective rank (mode count)", 2),
    ]
    base = next((r for r in rows if r["model"] == "base"), None)

    for (xk, xlabel, i) in panels:
        a = ax[i]
        for j, r in enumerate(rows):
            a.scatter(r[xk], r["quality"], s=170, color=cols[j % 10],
                      edgecolor="black", zorder=3,
                      label=PRETTY.get(r["model"], r["model"]))
            a.annotate(r["model"].replace("-div", "").replace("divpo-", ""),
                       (r[xk], r["quality"]), fontsize=7,
                       xytext=(5, 5), textcoords="offset points")
        if base:
            a.axhline(base["quality"], ls=":", c="gray", lw=1)
            a.axvline(base[xk], ls=":", c="gray", lw=1)
        a.set_xlabel(xlabel); a.set_ylabel("Judge quality (0-10)")
        a.grid(alpha=.3)
        a.set_title(f"Quality vs {xlabel.split('(')[0].strip()}", fontsize=10)

    handles, labels = ax[0].get_legend_handles_labels()
    fig.legend(handles, labels, loc="lower center", ncol=4, fontsize=8,
               frameon=False, bbox_to_anchor=(0.5, -0.06))
    fig.suptitle("Quality–diversity frontier (dotted lines = base model)", fontsize=13)
    plt.tight_layout()
    plt.savefig(out_path, dpi=140, bbox_inches="tight")
    plt.close()


def delta_table(rows):
    base = next((r for r in rows if r["model"] == "base"), None)
    if not base:
        return []
    keys = ["quality", "pairwise", "logdet", "distinct4", "self_bleu",
            "eff_rank", "n_clusters", "ends_cleanly"]
    out = []
    for r in rows:
        d = {"model": PRETTY.get(r["model"], r["model"])}
        for k in keys:
            base_v = base.get(k, 0.0)
            d[f"Δ{k}"] = round(r.get(k, 0.0) - base_v, 4)
        out.append(d)
    return out


QUALITY_TOLERANCE = 0.3   # judge points we are willing to lose for diversity


def recommend(rows) -> str:
    """Pick the arm to scale, from the data rather than from vibes.

    Rule, stated before looking at results: among arms whose quality is within
    QUALITY_TOLERANCE of the BASE model (not of the best arm -- we care about not
    degrading the model, not about winning on quality), take the largest gain in
    effective rank. Effective rank is the primary diversity measure because it is
    the one metric shown to separate collapse from spread (see 02_setup). Ties
    and near-ties are reported rather than hidden.
    """
    base = next((r for r in rows if r["model"] == "base"), None)
    if not base:
        return "_No base row; cannot compute deltas._"

    cand = []
    for r in rows:
        if r["model"] == "base":
            continue
        dq = r["quality"] - base["quality"]
        der = r.get("eff_rank", 0.0) - base.get("eff_rank", 0.0)
        dld = r.get("logdet", 0.0) - base.get("logdet", 0.0)
        cand.append({"model": PRETTY.get(r["model"], r["model"]),
                     "raw": r["model"], "Δquality": round(dq, 3),
                     "Δeff_rank": round(der, 3), "Δlogdet": round(dld, 3),
                     "ends_cleanly": round(r.get("ends_cleanly", 0.0), 3),
                     "eligible": dq >= -QUALITY_TOLERANCE and r.get("ends_cleanly", 0) > 0.9})

    ok = [c for c in cand if c["eligible"]]
    ranked = sorted(ok, key=lambda c: -c["Δeff_rank"])

    if not ranked:
        return (f"**No arm qualifies.** Every arm either lost more than "
                f"{QUALITY_TOLERANCE} judge points against base or fell below a "
                f"90% clean-completion rate. The honest recommendation is to fix "
                f"the objective before scaling anything to 8B.\n\n"
                + logbook.table(cand, ["model", "Δquality", "Δeff_rank",
                                       "Δlogdet", "ends_cleanly", "eligible"]))

    win = ranked[0]
    runner = ranked[1] if len(ranked) > 1 else None
    margin = (win["Δeff_rank"] - runner["Δeff_rank"]) if runner else None
    close = margin is not None and margin < 0.25

    txt = (f"**Scale `{win['model']}` to Qwen3-8B.** It gains "
           f"{win['Δeff_rank']:+.2f} effective rank and {win['Δlogdet']:+.2f} "
           f"log-det against base while holding quality at "
           f"{win['Δquality']:+.2f} and completion at "
           f"{100*win['ends_cleanly']:.0f}%.\n\n")
    if runner:
        txt += (f"Runner-up is `{runner['model']}` "
                f"({runner['Δeff_rank']:+.2f} effective rank). ")
        txt += ("The margin is **{:.2f}, which is small enough that this "
                "ordering should not be treated as settled** on 50 eval prompts "
                "— both are worth carrying forward.\n\n".format(margin)
                if close else
                "The margin ({:.2f}) is clear.\n\n".format(margin))
    excluded = [c for c in cand if not c["eligible"]]
    if excluded:
        txt += ("Excluded for quality or completeness regression: "
                + ", ".join(f"`{c['model']}` (Δq {c['Δquality']:+.2f}, "
                            f"complete {100*c['ends_cleanly']:.0f}%)"
                            for c in excluded) + ".\n\n")
    txt += logbook.table(cand, ["model", "Δquality", "Δeff_rank", "Δlogdet",
                                "ends_cleanly", "eligible"])
    return txt


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--results", default="outputs/eval/results.csv")
    ap.add_argument("--samples", default="outputs/eval/samples")
    args = ap.parse_args()

    rows = load(ROOT / args.results)
    fig = logbook.FIGS / "frontier.png"
    fig.parent.mkdir(parents=True, exist_ok=True)
    frontier_chart(rows, fig)

    main_cols = ["model", "quality", "ends_cleanly", "pairwise", "logdet",
                 "distinct4", "self_bleu", "eff_rank", "n_clusters", "tok_entropy", "words"]
    tbl = [{k: (PRETTY.get(r["model"], r["model"]) if k == "model"
                else round(r.get(k, 0.0), 4)) for k in main_cols} for r in rows]

    # qualitative examples
    qual = ""
    sdir = ROOT / args.samples
    for r in rows:
        p = sdir / f"{r['model']}_examples.json"
        if not p.exists():
            continue
        ex = json.load(open(p))
        if not ex:
            continue
        e = ex[0]
        qual += f"\n### {PRETTY.get(r['model'], r['model'])}\n\n"
        qual += f"*Prompt:* {e['prompt'][:200]}\n\n"
        qual += (f"*Set stats:* pairwise {e['pairwise']:.3f} · logdet {e['logdet']:.2f} "
                 f"· eff_rank {e['eff_rank']:.2f} · clusters {e['n_clusters']} "
                 f"· self-BLEU {e['self_bleu']:.3f}\n\n")
        for i, t in enumerate(e["texts"][:3]):
            first = t.strip().split("\n")[0][:180]
            qual += f"{i+1}. {first}…\n"
    body = f"""# Results — diversity-aware post-training for creative story generation

## Main table

{logbook.table(tbl, main_cols)}

`self_bleu` is inverted in meaning: **lower is more diverse**. `tok_entropy` is
monitor-only and was never optimized.

## Change vs. base model

{logbook.table(delta_table(rows))}

## Quality–diversity frontier

![frontier](figures/frontier.png)

## Recommendation — which method to scale to Qwen3-8B

Selection rule, fixed before results were seen: among arms whose judge quality
is within {QUALITY_TOLERANCE} points of the **base** model and whose clean-completion
rate stays above 90%, take the largest gain in **effective rank**. Quality is
measured against base rather than against the best arm because the goal is to
add diversity without degrading the model, not to win on quality. Effective rank
is the primary diversity axis because it is the one mode metric demonstrated to
separate collapse from spread (`02_setup_and_deviations.md`, §11).

{recommend(rows)}

## Qualitative examples (first 3 of 16 samples, same prompt)
{qual}
"""
    (logbook.LOGS.parent / "report.md").write_text(body)
    print(body[:2500])
    print("\nwrote report.md and", fig)
    return 0


if __name__ == "__main__":
    sys.exit(main())