File size: 10,465 Bytes
64cdbf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b4e832
 
64cdbf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b4e832
 
 
 
64cdbf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b4e832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64cdbf6
 
6b4e832
 
64cdbf6
 
 
 
 
 
 
 
 
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
"""Build the logbook figures from the reproduction results.

Palette: categorical slots 1 (blue) and 6 (orange) from the dataviz reference
palette -- validated with scripts/validate_palette.js in BOTH light and dark
(all checks pass; worst adjacent CVD dE 24.7 protan). Text stays in ink tokens,
never the series colour. Every chart also emits its raw numbers as CSV so the
figures are auditable and machine-readable.
"""
import json, glob, os, sys
import plotly.graph_objects as go

OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "outputs")
FIG = os.path.join(OUT, "figures")
os.makedirs(FIG, exist_ok=True)

# dataviz reference palette, categorical slots 1 and 6
BLUE_L, ORANGE_L = "#2a78d6", "#eb6834"
INK, INK2, GRID = "#0b0b0b", "#52514e", "rgba(0,0,0,0.10)"


def style(fig, title, ytitle, xtitle=""):
    fig.update_layout(
        title=dict(text=title, font=dict(size=17, color=INK)),
        paper_bgcolor="#fcfcfb", plot_bgcolor="#fcfcfb",
        font=dict(family="Inter, system-ui, sans-serif", size=13, color=INK2),
        yaxis=dict(title=ytitle, gridcolor=GRID, zerolinecolor=GRID,
                   linecolor=GRID, title_font=dict(color=INK2)),
        xaxis=dict(title=xtitle, gridcolor="rgba(0,0,0,0)", linecolor=GRID,
                   title_font=dict(color=INK2)),
        legend=dict(orientation="h", yanchor="bottom", y=1.02, x=0,
                    font=dict(color=INK2)),
        margin=dict(l=60, r=30, t=80, b=55), height=430,
        hovermode="x unified",
    )
    return fig


def save(fig, name, csv_rows, header):
    p = os.path.join(FIG, name + ".html")
    fig.write_html(p, include_plotlyjs="cdn", full_html=True)
    c = os.path.join(FIG, name + ".csv")
    with open(c, "w") as f:
        f.write(",".join(header) + "\n")
        for r in csv_rows:
            f.write(",".join(str(x) for x in r) + "\n")
    print("wrote", p, "and", c)
    return p, c


def load(name):
    p = os.path.join(OUT, name)
    return json.load(open(p)) if os.path.exists(p) else None


# ---------------------------------------------------------------- Fig 1: speedup
def fig_speedup():
    rows = []
    for label, fname, paper_sp in [
        ("Trip Plan\n(CCD-DS, V=4)", "c3_trip_ccd_ds.json", 3.48),
        ("HumanEval\n(CCD-DS, V=4)", "c4_he_ccd_ds.json", 3.04),
    ]:
        r = load(fname)
        if r:
            rows.append((label, paper_sp, r["speedup_vs_uniform"]))
    if not rows:
        return None
    fig = go.Figure()
    fig.add_bar(name="Paper (Table 1)", x=[r[0] for r in rows], y=[r[1] for r in rows],
                marker_color=BLUE_L, marker_line_width=0,
                text=[f"{r[1]:.2f}×" for r in rows], textposition="outside",
                textfont=dict(color=INK2))
    fig.add_bar(name="This reproduction", x=[r[0] for r in rows], y=[r[2] for r in rows],
                marker_color=ORANGE_L, marker_line_width=0,
                text=[f"{r[2]:.2f}×" for r in rows], textposition="outside",
                textfont=dict(color=INK2))
    fig.add_hline(y=1.0, line_dash="dot", line_color=INK2,
                  annotation_text="no speedup (structural ceiling at V=4, d=3)",
                  annotation_font=dict(color=INK2, size=11))
    style(fig, "CCD-DS decoding speedup: reported vs reproduced (Dream-7B)",
          "speedup over uniform b_t=1 (×)")
    fig.update_layout(barmode="group", bargap=0.3, bargroupgap=0.08)
    return save(fig, "fig_speedup", rows, ["config", "paper_speedup", "repro_speedup"])


# ---------------------------------------------------------------- Fig 2: the k law
def fig_k_law():
    rows = []
    for V in [1, 2, 4, 8, 16]:
        r = load(f"c5_abl_V{V}.json")
        if r:
            k = 256.0 / r["mean_steps"]
            rows.append((V, max(1.0, V / 4.0), k, r["mean_steps"], r["score"]))
    if not rows:
        return None
    if not rows:
        return None
    fig = go.Figure()
    fig.add_scatter(name="predicted  k = max(1, V/(d+1))", x=[r[0] for r in rows],
                    y=[r[1] for r in rows], mode="lines+markers",
                    line=dict(color=BLUE_L, width=2, dash="dash"),
                    marker=dict(size=9, color=BLUE_L))
    fig.add_scatter(name="measured (Dream-7B, Trip City=3)", x=[r[0] for r in rows],
                    y=[r[2] for r in rows], mode="lines+markers",
                    line=dict(color=ORANGE_L, width=2), marker=dict(size=9, color=ORANGE_L))
    style(fig, "CCD-DS throughput follows k = max(1, V/(d+1))  (d = 3)",
          "tokens decoded per step (k)", "buffer width V")
    fig.update_layout(hovermode="x unified")
    fig.update_xaxes(type="log", tickvals=[r[0] for r in rows],
                     ticktext=[str(r[0]) for r in rows])
    return save(fig, "fig_k_law", rows,
                ["V", "predicted_k", "measured_k", "mean_steps", "score"])


# ---------------------------------------------------------------- Fig 3: |I^c_t|
def fig_ic_hist():
    r = load("c3_trip_ccd_ds.json")
    if not r or "ic_sizes" not in r:
        return None
    from collections import Counter
    flat = [v for seq in r["ic_sizes"] for v in seq]
    c = Counter(flat)
    xs = sorted(c)
    rows = [(x, c[x]) for x in xs]
    fig = go.Figure()
    fig.add_bar(x=[str(x) for x in xs], y=[c[x] for x in xs], marker_color=BLUE_L,
                marker_line_width=0, text=[c[x] for x in xs], textposition="outside",
                textfont=dict(color=INK2), name="steps")
    style(fig, "Intersection size |I<sup>c</sup><sub>t</sub>| at the paper's V=4, d=3 "
               "(Dream-7B, Trip Plan)",
          "number of decoding steps", "|I^c_t|  (0 = fallback to baseline)")
    fig.update_layout(showlegend=False, bargap=0.35)
    return save(fig, "fig_ic_hist", rows, ["ic_size", "steps"])


# ---------------------------------------------------------------- Fig 4: main table
def fig_scores():
    specs = [
        ("Trip Plan", [("baseline", "c3_trip_baseline.json", 15.10),
                       ("CCD", "c3_trip_ccd.json", 16.93),
                       ("CCD-DS", "c3_trip_ccd_ds.json", 19.01)]),
        # prefer the merged n=64 arms (problems 0..63) where both halves exist,
        # so the figure matches the n reported in the analysis table
        ("HumanEval", [("baseline", "c4merged_he_baseline.json", 52.66),
                       ("CCD", "c4merged_he_ccd.json", 57.31),
                       ("CCD-DS", "c4_he_ccd_ds.json", 56.71)]),
    ]
    rows, labels, paper, repro = [], [], [], []
    for task, items in specs:
        for meth, fname, pv in items:
            r = load(fname)
            if r:
                labels.append(f"{task}<br>{meth}")
                paper.append(pv); repro.append(r["score"])
                rows.append((task, meth, pv, r["score"], r["n_examples"]))
    if not rows:
        return None
    fig = go.Figure()
    fig.add_bar(name="Paper (Table 1)", x=labels, y=paper, marker_color=BLUE_L,
                marker_line_width=0, text=[f"{v:.1f}" for v in paper],
                textposition="outside", textfont=dict(color=INK2))
    fig.add_bar(name="This reproduction", x=labels, y=repro, marker_color=ORANGE_L,
                marker_line_width=0, text=[f"{v:.1f}" for v in repro],
                textposition="outside", textfont=dict(color=INK2))
    style(fig, "Benchmark scores: reported vs reproduced (Dream-7B)", "score")
    fig.update_layout(barmode="group", bargap=0.3, bargroupgap=0.08)
    return save(fig, "fig_scores", rows, ["task", "method", "paper", "repro", "n"])


def fig_ablation_score():
    rows = []
    base = load("c5_abl_baseline.json")
    for V in [1, 2, 4, 8, 16]:
        r = load(f"c5_abl_V{V}.json")
        if r:
            rows.append((V, r["score"], r["mean_steps"]))
    if not rows or not base:
        return None
    fig = go.Figure()
    fig.add_scatter(name="CCD-DS (measured)", x=[r[0] for r in rows], y=[r[1] for r in rows],
                    mode="lines+markers", line=dict(color=ORANGE_L, width=2),
                    marker=dict(size=9, color=ORANGE_L))
    fig.add_hline(y=base["score"], line_dash="dot", line_color=INK2,
                  annotation_text=f"baseline {base['score']:.1f}",
                  annotation_font=dict(color=INK2, size=11))
    fig.add_scatter(name="paper's reported peak (buffer 4 → 70%)", x=[4], y=[70.0],
                    mode="markers", marker=dict(size=15, color=BLUE_L, symbol="star"))
    style(fig, "Claim 5: accuracy vs buffer width (Trip City=3, n=40)",
          "exact-match score", "buffer width V  (d = 3)")
    fig.update_xaxes(type="log", tickvals=[r[0] for r in rows],
                     ticktext=[str(r[0]) for r in rows])
    return save(fig, "fig_ablation_score", rows, ["V", "score", "steps"])


def fig_temperature():
    import csv as _csv
    p = os.path.join(OUT, "temperature.csv")
    if not os.path.exists(p):
        return None
    rows = []
    with open(p) as f:
        for d in _csv.DictReader(f):
            rows.append((float(d["temperature"]), float(d["baseline"]), float(d["ccd_ds"])))
    fig = go.Figure()
    fig.add_scatter(name="baseline", x=[r[0] for r in rows], y=[r[1] for r in rows],
                    mode="lines+markers", line=dict(color=BLUE_L, width=2),
                    marker=dict(size=9, color=BLUE_L))
    fig.add_scatter(name="CCD-DS", x=[r[0] for r in rows], y=[r[2] for r in rows],
                    mode="lines+markers", line=dict(color=ORANGE_L, width=2),
                    marker=dict(size=9, color=ORANGE_L))
    fig.add_vrect(x0=0.05, x1=0.75, fillcolor="rgba(0,0,0,0.05)", line_width=0,
                  annotation_text="Dream's confidence ranking collapses here (top_p=0.9)",
                  annotation_position="top left",
                  annotation_font=dict(color=INK2, size=10))
    style(fig, "Claim 6: score vs temperature (HumanEval, n=16, 256 steps, top_p=0.9)",
          "pass@1", "sampling temperature")
    return save(fig, "fig_temperature", rows, ["temperature", "baseline", "ccd_ds"])


if __name__ == "__main__":
    made = []
    for fn in (fig_speedup, fig_k_law, fig_ic_hist, fig_scores,
               fig_ablation_score, fig_temperature):
        try:
            r = fn()
            if r:
                made.append(r[0])
            else:
                print(f"skip {fn.__name__}: inputs missing")
        except Exception as e:
            print(f"skip {fn.__name__}: {e}")
    print(f"\n{len(made)} figures written to {FIG}")