ccd-repro-code / scripts /make_figures.py
ashishk1331's picture
full reproduction bundle: scripts, configs, outputs, figures
6b4e832 verified
Raw
History Blame Contribute Delete
10.5 kB
"""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}")