Spaces:
Sleeping

File size: 5,017 Bytes
c995899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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(" ", "<br>", 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}<extra>Architectural relationship</extra>", 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<br>%{y:.5g} " + unit + "<br>%{customdata[0]} 路 n=%{customdata[1]}<br>%{customdata[3]}<br>%{customdata[2]}<extra>%{fullData.name}</extra>"))
    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}<br>%{y:,} experts<extra>Hypothetical capacity</extra>"))
    style(fig, "Hypothetical capacity 路 uniform records, no overhead", 340)
    fig.update_yaxes(title="Expert count", rangemode="tozero")
    return fig