File size: 8,766 Bytes
ecc81b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { useEffect, useMemo, useRef, useState } from "react";

import Weights from "./Weights.jsx";

import { fmtParams, latticeAxisOf } from "../spec.js";

// The right half: what the model *is*, next to what it does to the lattice.
// The 3-D view answers "which cells, in what order"; this answers "through
// what, with how many weights". Same spec, same active layer β€” the two halves
// are one picture cut down the middle, not two tools sharing a window.

const FAMILY = {
  scan: { color: "#ffc061", glyph: "β†’" },
  kernel: { color: "#c39bd8", glyph: "βŠ—" },
  flatten: { color: "#5eead4", glyph: "βŠ•" },
};

const S = {
  wrap: {
    width: "44%",
    minWidth: 300,
    display: "flex",
    flexDirection: "column",
    borderLeft: "1px solid #1e2635",
    background: "linear-gradient(180deg, #0e1320 0%, #0b0f18 100%)",
  },
  head: { padding: "12px 16px 10px", borderBottom: "1px solid #1a2233" },
  title: {
    color: "#5d6b84",
    fontSize: 10.5,
    fontWeight: 700,
    letterSpacing: 1.4,
    textTransform: "uppercase",
  },
  io: {
    display: "flex",
    alignItems: "center",
    gap: 8,
    marginTop: 7,
    fontSize: 12,
    color: "#9fb0cc",
    fontVariantNumeric: "tabular-nums",
    flexWrap: "wrap",
  },
  pill: {
    background: "#151d2e",
    border: "1px solid #253048",
    borderRadius: 6,
    padding: "3px 8px",
  },
  body: { flex: 1, overflowY: "auto", padding: "10px 16px 16px" },
  node: (active, color) => ({
    position: "relative",
    display: "flex",
    alignItems: "center",
    gap: 9,
    padding: "7px 10px",
    borderRadius: 8,
    fontSize: 12.5,
    cursor: "pointer",
    background: active ? "#1a2438" : "#121926",
    border: `1px solid ${active ? color : "#1f2a3d"}`,
    boxShadow: active ? `0 0 0 1px ${color}55, inset 2px 0 0 ${color}` : "none",
  }),
  bar: {
    position: "relative",
    height: 4,
    borderRadius: 99,
    background: "#0c1018",
    overflow: "hidden",
    marginTop: 5,
  },
  rail: {
    width: 1,
    height: 12,
    background: "#243049",
    margin: "0 auto",
  },
  chip: {
    fontSize: 10.5,
    padding: "1px 6px",
    borderRadius: 99,
    background: "#1c2740",
    color: "#9fb3d9",
    whiteSpace: "nowrap",
  },
  mech: {
    display: "flex",
    gap: 6,
    flexWrap: "wrap",
    marginTop: 8,
  },
  tabs: { display: "flex", gap: 2, marginTop: 10 },
  tab: (active) => ({
    background: "none",
    border: "none",
    borderBottom: active ? "2px solid #ffc061" : "2px solid transparent",
    color: active ? "#e8eef8" : "#7d8aa3",
    fontSize: 11.5,
    fontWeight: active ? 650 : 500,
    padding: "5px 10px",
    cursor: "pointer",
    letterSpacing: 0.3,
  }),
};

function Node({ layer, i, spec, active, maxParams, onSelect }) {
  const ref = useRef();
  const kind = layer.kind ?? "scan";
  const fam = FAMILY[kind] ?? FAMILY.scan;
  const isTime = kind === "scan" && latticeAxisOf(layer, spec) === null;

  // Keep the active layer in view as the animation advances, but never yank
  // the panel while the user is reading somewhere else β€” only scroll when the
  // node is actually outside the viewport.
  useEffect(() => {
    if (active && ref.current) {
      const el = ref.current;
      const box = el.getBoundingClientRect();
      const par = el.parentElement.parentElement.getBoundingClientRect();
      if (box.top < par.top || box.bottom > par.bottom) {
        el.scrollIntoView({ block: "nearest", behavior: "smooth" });
      }
    }
  }, [active]);

  const label =
    kind === "flatten"
      ? (layer.axes ?? []).join(" βŠ• ")
      : kind === "kernel"
        ? (layer.contracted ?? []).join(" βŠ— ")
        : layer.axis;
  const detail =
    kind === "flatten"
      ? layer.tokens
        ? `${layer.tokens} tokens`
        : ""
      : kind === "kernel"
        ? layer.axis
          ? `+ ${layer.axis} β†’`
          : ""
        : isTime
          ? "β†’ causal"
          : layer.reverse
            ? "←"
            : "β†’";

  const pct = maxParams ? (100 * (layer.n_params ?? 0)) / maxParams : 0;

  return (
    <div>
      <div
        ref={ref}
        className="td-row"
        style={S.node(active, fam.color)}
        onClick={() => onSelect(i)}
      >
        <span style={{ color: "#6b7a96", width: 26, flexShrink: 0 }}>L{i}</span>
        <span style={{ color: fam.color, width: 14, flexShrink: 0 }}>
          {fam.glyph}
        </span>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div
            style={{
              display: "flex",
              alignItems: "baseline",
              gap: 6,
              whiteSpace: "nowrap",
              overflow: "hidden",
            }}
          >
            <b style={{ color: active ? "#e8eef8" : "#c3cdde" }}>{label}</b>
            <span style={{ color: "#6b7a96", fontSize: 11.5 }}>{detail}</span>
          </div>
          <div style={S.bar}>
            <div
              style={{
                height: "100%",
                width: `${pct}%`,
                background: fam.color,
                opacity: active ? 0.95 : 0.45,
                transition: "opacity 160ms ease",
              }}
            />
          </div>
        </div>
        <div style={{ textAlign: "right", flexShrink: 0 }}>
          {layer.mixer && <div style={S.chip}>{layer.mixer}</div>}
          <div
            style={{
              color: "#6b7a96",
              fontSize: 11,
              marginTop: 3,
              fontVariantNumeric: "tabular-nums",
            }}
          >
            {fmtParams(layer.n_params ?? 0)}
          </div>
        </div>
      </div>
      <div style={S.rail} />
    </div>
  );
}

export default function ModelView({
  parsed,
  layerIndex,
  onSelectLayer,
  weights,
}) {
  const [view, setView] = useState("flow");
  const { spec } = parsed;
  const m = spec.model;
  const io = spec.io ?? {};

  const maxParams = useMemo(
    () => Math.max(1, ...spec.layers.map((l) => l.n_params ?? 0)),
    [spec],
  );
  // Which distinct mechanisms this model is actually built from β€” the mixers
  // in play plus the composition, rather than a class name that hides both.
  const mechanisms = useMemo(() => {
    const mixers = [
      ...new Set(spec.layers.map((l) => l.mixer).filter(Boolean)),
    ];
    const kinds = [...new Set(spec.layers.map((l) => l.kind ?? "scan"))];
    return { mixers, kinds };
  }, [spec]);

  const swept = spec.layers.reduce((a, l) => a + (l.n_params ?? 0), 0);

  return (
    <div style={S.wrap}>
      <div style={S.head}>
        <div style={S.title}>model</div>
        <div style={S.io}>
          <span style={S.pill}>{(io.input ?? []).join(" Γ— ") || "β€”"}</span>
          <span style={{ color: "#41568a" }}>β–Ά</span>
          <b style={{ color: "#e8eef8" }}>{m.kind}</b>
          <span style={{ color: "#41568a" }}>β–Ά</span>
          <span style={S.pill}>{(io.output ?? []).join(" Γ— ") || "β€”"}</span>
        </div>
        <div style={S.mech}>
          <span style={S.chip}>{spec.nd_method.name}</span>
          {mechanisms.mixers.map((x) => (
            <span key={x} style={{ ...S.chip, color: "#ffc9a0" }}>
              {x}
            </span>
          ))}
          {mechanisms.kinds.map((k) => (
            <span
              key={k}
              style={{ ...S.chip, color: FAMILY[k]?.color ?? "#9fb3d9" }}
            >
              {FAMILY[k]?.glyph} {k}
            </span>
          ))}
          <span style={S.chip}>{fmtParams(m.n_params)} total</span>
          {swept !== m.n_params && (
            // Layer weights rarely add up to the whole model: projections,
            // norms and the head live outside the swept stack. Saying so beats
            // letting the two numbers quietly disagree.
            <span style={{ ...S.chip, color: "#7d8aa3" }}>
              {fmtParams(m.n_params - swept)} outside layers
            </span>
          )}
        </div>
        <div style={S.tabs}>
          {["flow", "weights"].map((v) => (
            <button
              key={v}
              style={S.tab(view === v)}
              onClick={() => setView(v)}
            >
              {v === "flow" ? "layer flow" : `weights Β· L${layerIndex}`}
            </button>
          ))}
        </div>
      </div>

      <div style={S.body} className="td-panel">
        {view === "weights" && (
          <Weights weights={weights} layerIndex={layerIndex} />
        )}
        {view === "flow" &&
          spec.layers.map((l, i) => (
            <Node
              key={i}
              layer={l}
              i={i}
              spec={spec}
              active={i === layerIndex}
              maxParams={maxParams}
              onSelect={onSelectLayer}
            />
          ))}
      </div>
    </div>
  );
}