Spaces:
Running

File size: 9,485 Bytes
1d919a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// The network map. volume.safetensors is the cochain: balance.* is the resting
// current between cells, and each live frame (flow.js) replaces that with the
// token's activations. Columns are the axes, lines are the strongest cell-to-cell
// currents, and the rail is one tick per layer. One 2D view of the hot layer.
// A 3D volume or graph walks every layer's cells on the token path and costs
// a few tokens per second for each of those views.

const EDGES = [
  { role: 0, name: "q", a: "D", b: "Q", vol: "balance.q" },
  { role: 1, name: "k", a: "D", b: "KV", vol: "balance.k" },
  { role: 2, name: "v", a: "D", b: "KV", vol: "balance.v" },
  { role: 3, name: "o", a: "Q", b: "Dout", vol: "balance.o" },
  { role: 4, name: "gate", a: "D", b: "F", vol: "balance.gate" },
  { role: 5, name: "up", a: "D", b: "F", vol: "balance.up" },
  { role: 6, name: "down", a: "F", b: "Dout", vol: "balance.down" },
];

const NODE = { D: 0, Dout: 2, F: 4, Q: 5, KV: 6 };

export function mountMap(root) {
  const canvas = root.querySelector("#map-canvas");
  const meta = root.querySelector("#map-meta");
  const followBtn = root.querySelector("#map-follow");
  const ctx = canvas.getContext("2d");
  let vol = null;
  let counts = null;
  let layers = 0;
  let layer = 0;
  let follow = true;
  let frame = null;
  let token = "";
  let energy = null;
  let line = "";

  function say(text) {
    line = text;
    meta.textContent = text;
  }

  function structure(bytes) {
    vol = parseSafetensors(bytes);
    const q = vol.get("balance.q");
    const k = vol.get("balance.k");
    const g = vol.get("balance.gate");
    if (!q || !k || !g) {
      say("This volume has no cell map.");
      return;
    }
    layers = q.shape[0];
    counts = { D: q.shape[2], Q: q.shape[1], KV: k.shape[1], F: g.shape[1], Dout: q.shape[2] };
    energy = new Float32Array(layers);
    layer = hottest();
    say(counts.D + " D · " + counts.Q + " Q · " + counts.KV + " KV · " + counts.F + " F");
    draw();
    requestAnimationFrame(() => draw());
  }

  function note(text) {
    token = text || "";
  }

  function show(fr) {
    frame = fr;
    if (!counts && fr.layout) countsFromLayout(fr.layout);
    if (follow) layer = hottest();
    const cons = fr.mismatch < 1e-3 ? "conserved" : fr.mismatch.toExponential(2);
    const word = token ? " · " + JSON.stringify(token) : "";
    say("L" + layer + " · pos " + fr.pos + word + " · " + cons);
    draw();
  }

  function countsFromLayout(L) {
    layers = L.layers;
    counts = {
      D: L.axes.D.cells, Q: L.axes.Q.cells, KV: L.axes.KV.cells, F: L.axes.F.cells,
      Dout: L.axes.D.cells,
    };
    if (!energy || energy.length !== layers) energy = new Float32Array(layers);
  }

  function hottest() {
    let best = 0, at = 0;
    for (let i = 0; i < layers; i++) {
      const e = layerEnergy(i);
      energy[i] = e;
      if (e > best) { best = e; at = i; }
    }
    return at;
  }

  function layerEnergy(l) {
    let s = 0;
    for (const e of EDGES) {
      const m = matrix(l, e);
      if (!m) continue;
      for (let i = 0; i < m.data.length; i++) s += Math.abs(m.data[i]);
    }
    return s;
  }

  function matrix(l, edge) {
    if (frame) {
      const L = frame.layout;
      const rows = L.axes[L.roles[edge.role].rows].cells;
      const cols = L.axes[L.roles[edge.role].cols].cells;
      const off = L.edge[l * 7 + edge.role];
      return { rows, cols, data: new Float32Array(frame.flow.buffer, frame.flow.byteOffset + off, rows * cols) };
    }
    const t = vol && vol.get(edge.vol);
    if (!t) return null;
    const [, rows, cols] = t.shape;
    const n = rows * cols;
    return { rows, cols, data: t.data.subarray(l * n, (l + 1) * n) };
  }

  function nodeValue(name, i) {
    if (!frame) return 0;
    const v = nodeVec(NODE[name]);
    return v ? v[i] || 0 : 0;
  }

  function nodeVec(id) {
    return nodeVecAt(layer, id);
  }

  function nodeVecAt(l, id) {
    const L = frame.layout;
    const n = L.axes[L.nodes[id].axis].cells;
    const off = L.node[l * 10 + id];
    return new Float32Array(frame.flow.buffer, frame.flow.byteOffset + off, n);
  }

  function draw() {
    if (!counts) return;
    draw2d();
  }

  function draw2d() {
    const cssW = root.clientWidth;
    const cssH = canvas.clientHeight || root.clientHeight - 36;
    if (!cssW || !cssH || !counts) return;
    const dpr = Math.min(2, window.devicePixelRatio || 1);
    canvas.width = Math.round(cssW * dpr);
    canvas.height = Math.round(cssH * dpr);
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
    ctx.clearRect(0, 0, cssW, cssH);

    const rail = 18;
    const top = 22;
    const bot = cssH - 16;
    const span = Math.max(1, bot - top);
    const xOf = (name) => {
      const f = { D: 0.14, Q: 0.40, KV: 0.40, F: 0.68, Dout: 0.90 }[name];
      return rail + 8 + f * (cssW - rail - 16);
    };
    const band = (name) => name === "Q" ? [top, top + span * 0.58] : name === "KV" ? [top + span * 0.70, bot] : [top, bot];
    const cols = {};
    for (const name of ["D", "Q", "KV", "F", "Dout"]) {
      const [y0, y1] = band(name);
      const n = counts[name];
      cols[name] = [];
      for (let i = 0; i < n; i++) {
        const t = n === 1 ? 0.5 : i / (n - 1);
        cols[name].push({ x: xOf(name), y: y0 + t * (y1 - y0) });
      }
    }

    const maxE = energy ? Math.max(...energy, 1e-12) : 1;
    for (let i = 0; i < layers; i++) {
      const y0 = (i / layers) * cssH;
      const h = cssH / layers;
      const hot = energy ? energy[i] / maxE : 0;
      ctx.fillStyle = i === layer ? "rgba(232,168,124,0.95)" : "rgba(232,168,124," + (0.15 + 0.7 * hot) + ")";
      ctx.fillRect(4, y0 + 1, 6, Math.max(2, h - 2));
    }

    ctx.font = "11px Segoe UI, system-ui, sans-serif";
    ctx.fillStyle = "#8e96a3";
    ctx.textAlign = "center";
    ctx.fillText("D", xOf("D"), 14);
    ctx.fillText("Q", xOf("Q"), 14);
    ctx.fillText("F", xOf("F"), 14);
    ctx.fillText("D′", xOf("Dout"), 14);
    const kv = band("KV");
    ctx.fillText("KV", xOf("KV"), kv[0] - 4);

    const picked = [];
    let edgeMax = 1e-12;
    for (const e of EDGES) {
      const m = matrix(layer, e);
      if (!m) continue;
      const take = strongest(m, 70);
      for (const p of take) edgeMax = Math.max(edgeMax, Math.abs(p.v));
      picked.push([e, m, take]);
    }
    for (const [e, , take] of picked) {
      for (const p of take) {
        const from = cols[e.a][p.c];
        const to = cols[e.b][p.r];
        if (!from || !to) continue;
        const mag = Math.abs(p.v) / edgeMax;
        ctx.strokeStyle = p.v >= 0 ? "rgba(232,168,124," + (0.15 + 0.8 * mag) + ")" : "rgba(126,182,232," + (0.15 + 0.8 * mag) + ")";
        ctx.lineWidth = 0.6 + 1.6 * mag;
        ctx.beginPath();
        ctx.moveTo(from.x, from.y);
        ctx.lineTo(to.x, to.y);
        ctx.stroke();
      }
    }

    for (const name of ["D", "Q", "KV", "F", "Dout"]) {
      let max = 1e-12;
      const vals = cols[name].map((_, i) => {
        const v = nodeValue(name, i);
        max = Math.max(max, Math.abs(v));
        return v;
      });
      cols[name].forEach((p, i) => {
        const v = vals[i];
        const mag = Math.abs(v) / max;
        const r = frame ? 1.6 + 3.2 * mag : 1.8;
        ctx.fillStyle = !frame ? "rgba(236,231,223,0.85)" : v >= 0 ? "rgba(232,168,124," + (0.45 + 0.55 * mag) + ")" : "rgba(126,182,232," + (0.45 + 0.55 * mag) + ")";
        ctx.beginPath();
        ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
        ctx.fill();
      });
    }
  }

  canvas.addEventListener("click", (ev) => {
    const rect = canvas.getBoundingClientRect();
    const x = ev.clientX - rect.left;
    const y = ev.clientY - rect.top;
    if (x > 22 || !layers) return;
    layer = Math.min(layers - 1, Math.max(0, Math.floor(y / rect.height * layers)));
    follow = false;
    followBtn.setAttribute("aria-pressed", "false");
    if (frame) say("L" + layer + " · pinned");
    draw();
  });
  followBtn.addEventListener("click", () => {
    follow = !follow;
    followBtn.setAttribute("aria-pressed", follow ? "true" : "false");
    if (follow && frame) layer = hottest();
    draw();
  });
  const ro = new ResizeObserver(() => draw());
  ro.observe(root);
  draw();

  return { structure, frame: show, token: note };
}

// The n largest |currents|. Rows are destination cells, columns are sources.
function strongest(m, n) {
  const heap = [];
  for (let i = 0; i < m.data.length; i++) {
    const v = m.data[i];
    const a = Math.abs(v);
    if (a === 0) continue;
    const item = { v, a, r: (i / m.cols) | 0, c: i % m.cols };
    if (heap.length < n) {
      heap.push(item);
      continue;
    }
    let min = 0;
    for (let k = 1; k < heap.length; k++) if (heap[k].a < heap[min].a) min = k;
    if (a > heap[min].a) heap[min] = item;
  }
  return heap;
}

function parseSafetensors(buf) {
  const dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
  const n = Number(dv.getBigUint64(0, true));
  const header = JSON.parse(new TextDecoder().decode(buf.subarray(8, 8 + n)));
  const base = 8 + n;
  const map = new Map();
  for (const [name, meta] of Object.entries(header)) {
    if (!meta || !meta.shape || !meta.data_offsets) continue;
    const [a, b] = meta.data_offsets;
    const off = buf.byteOffset + base + a;
    const count = (b - a) / 4;
    map.set(name, {
      shape: meta.shape,
      data: meta.dtype === "F32" ? new Float32Array(buf.buffer, off, count) : null,
    });
  }
  return map;
}