"""Small interactive figures with explicit evidence scopes.""" import pandas as pd import plotly.graph_objects as go from .research_catalog import COMPONENTS, EDGES from .validation import parse_copy_arm COLORS = ["#63dfff", "#8f9bff", "#ffd184", "#ea92c8"] def style(fig, title, height=420): fig.update_layout(template="plotly_dark", title=dict(text=title, font_size=16), paper_bgcolor="#101b2a", plot_bgcolor="#101b2a", font=dict(family="Inter, system-ui, sans-serif", color="#d9e7f4", size=12), margin=dict(l=55, r=20, t=65, b=65), height=height, legend=dict(orientation="h", y=-0.22), hovermode="closest") fig.update_xaxes(gridcolor="#253448", zeroline=False) fig.update_yaxes(gridcolor="#253448", zeroline=False) return fig def empty_figure(message="Awaiting reviewed evidence"): fig = go.Figure() fig.add_annotation(text=message, x=0.5, y=0.5, xref="paper", yref="paper", showarrow=False) fig.update_xaxes(visible=False) fig.update_yaxes(visible=False) return style(fig, "No comparable measurements", 350) def architecture(selected="Expert cache"): positions = {"API entry": (0, 3), "Inference execution": (1, 3), "Resident trunk": (2, 3), "Attention / KV": (3, 4), "Expert routing": (3, 2), "Expert cache": (4, 2), "Loader workers": (5, 2), "SSD expert bank": (6, 2), "Prefetch": (4, 0)} fig = go.Figure() for left, right in EDGES: x0, y0 = positions[left]; x1, y1 = positions[right] fig.add_trace(go.Scatter(x=[x0, x1], y=[y0, y1], mode="lines", line=dict(color="#49647f", width=2), hoverinfo="skip", showlegend=False)) fig.add_annotation(x=x0 + (x1-x0)*0.8, y=y0+(y1-y0)*0.8, ax=x0+(x1-x0)*0.6, ay=y0+(y1-y0)*0.6, xref="x", yref="y", axref="x", ayref="y", showarrow=True, arrowcolor="#7d98b2", arrowhead=2) labels = list(COMPONENTS) fig.add_trace(go.Scatter(x=[positions[n][0] for n in labels], y=[positions[n][1] for n in labels], text=[n.replace(" ", "
", 1) for n in labels], customdata=labels, mode="markers+text", textposition="top center", marker=dict(size=[26 if n == selected else 16 for n in labels], color=["#63dfff" if n == selected else "#7186ad" for n in labels]), hovertemplate="%{customdata}Architectural relationship", showlegend=False)) style(fig, "DeepSeek path · architectural illustration", 400) fig.update_xaxes(visible=False, range=[-0.5, 6.7], fixedrange=True) fig.update_yaxes(visible=False, range=[-0.8, 5], fixedrange=True) return fig def copy_population(dataset, metric, timer, direction): records = [] for row in dataset["rows"]: arm = parse_copy_arm(row["arm"]) if arm and row["field"] == metric and arm["timer"] == timer and arm["direction"] == direction: records.append({**row, **arm}) return records def copy_chart(dataset, metric="time_ms", timer="events", direction="h2d"): records = copy_population(dataset, metric, timer, direction) if not records: return empty_figure() fig = go.Figure() unit = records[0]["unit"] for index, backing in enumerate(("pinned", "pageable")): rows = sorted([r for r in records if r["backing"] == backing], key=lambda r: r["bytes"]) fig.add_trace(go.Scatter(x=[r["bytes"] for r in rows], y=[r.get("value") for r in rows], name=backing, mode="markers+lines", connectgaps=False, line=dict(color=COLORS[index]), marker_size=8, customdata=[[r["tag"], r.get("n", "unavailable"), r.get("note", ""), r["source"]] for r in rows], hovertemplate="%{x:,} bytes
%{y:.5g} " + unit + "
%{customdata[0]} · n=%{customdata[1]}
%{customdata[3]}
%{customdata[2]}%{fullData.name}")) title = "Median copy timing · measured" if metric == "time_ms" else "Copy bandwidth · derived from bytes / median time" style(fig, title) fig.update_xaxes(title="Transfer size (bytes, logarithmic)", type="log") fig.update_yaxes(title=("p50 time (ms)" if metric == "time_ms" else "Bandwidth (GB/s, decimal)"), rangemode="tozero") return fig def evidence_table(rows): columns = ["arm", "field", "value", "unit", "tag", "n", "p50", "p95", "ci_low", "ci_high", "ci_method", "source", "note"] return pd.DataFrame([{key: ("Unavailable" if row.get(key) is None else row[key]) for key in columns} for row in rows], columns=columns) def capacity_chart(slots, total): fig = go.Figure(go.Bar(x=["Fits in chosen budget", "Outside chosen budget"], y=[slots, total-slots], marker_color=COLORS[:2], hovertemplate="%{x}
%{y:,} expertsHypothetical capacity")) style(fig, "Hypothetical capacity · uniform records, no overhead", 340) fig.update_yaxes(title="Expert count", rangemode="tozero") return fig