-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
423 lines (365 loc) · 14.4 KB
/
visualizer.py
File metadata and controls
423 lines (365 loc) · 14.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
visualizer.py — Interactive Plotly chart generation for BrainGrow.
Four chart types:
plot_umap() — 2D projection of all slots (active by domain, dormant grey)
plot_histogram() — activation score distribution
plot_stage_diff() — highlights slots newly activated vs prior stages
plot_prune_comparison()— before/after activation density overlaid
"""
from __future__ import annotations
from typing import List
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from vector_space import VectorSpace
# UMAP is optional; fall back to PCA if not installed
try:
import umap as umap_module
_UMAP_AVAILABLE = True
except ImportError:
_UMAP_AVAILABLE = False
from sklearn.decomposition import PCA
# Max dormant slots shown in the UMAP scatter to keep rendering fast
_MAX_DORMANT_SHOWN = 300
# Max active slots shown — sample when network exceeds this for performance
_MAX_ACTIVE_SHOWN = 5_000
# Minimum points needed to attempt UMAP (needs n_neighbors + 1 at minimum)
_UMAP_MIN_POINTS = 20
def _add_query_star(fig: go.Figure, coords: np.ndarray, query_vector) -> None:
"""Overlay a yellow star marker at coords[-1] when query_vector is provided."""
if query_vector is None:
return
q_xy = coords[-1:]
fig.add_trace(go.Scatter(
x=q_xy[:, 0],
y=q_xy[:, 1],
mode="markers",
marker=dict(
color="rgba(255,255,0,0.95)",
size=18,
symbol="star",
line=dict(width=1.5, color="white"),
),
name="Query",
hoverinfo="name",
))
def _domain_colour_map(domains: list) -> dict:
"""Map each unique domain name to a Plotly qualitative colour."""
unique = sorted(set(domains))
palette = px.colors.qualitative.Plotly
return {d: palette[i % len(palette)] for i, d in enumerate(unique)}
# Shared layout kwargs for all 2-D scatter plots
_SCATTER_LAYOUT = dict(
template="plotly_dark",
xaxis_title="dim-1",
yaxis_title="dim-2",
legend=dict(x=0.01, y=0.99, bgcolor="rgba(0,0,0,0.3)"),
margin=dict(l=40, r=30, t=50, b=40),
)
def _reduce_2d(vectors: np.ndarray) -> np.ndarray:
"""Project *vectors* [N, D] to 2D using UMAP when possible, else PCA.
Callers are responsible for capping the number of points BEFORE calling
this function so that index correspondence is preserved in the returned
coords array.
"""
n = len(vectors)
if _UMAP_AVAILABLE and n >= _UMAP_MIN_POINTS:
try:
reducer = umap_module.UMAP(
n_components=2,
n_neighbors=min(15, n - 1),
min_dist=0.1,
metric='cosine',
low_memory=True,
n_jobs=-1,
)
return reducer.fit_transform(vectors)
except Exception:
pass
pca = PCA(n_components=2)
return pca.fit_transform(vectors)
class Visualizer:
"""Stateless renderer — all state lives in VectorSpace."""
# ------------------------------------------------------------------
def plot_umap(self, vs: VectorSpace, query_vector: np.ndarray | None = None) -> go.Figure:
"""
2-D projection of the vector space.
Active slots are coloured by domain; dormant slots are grey.
If *query_vector* is supplied it is rendered as a yellow star so the
caller can visually see where the query lands relative to active vs
dormant space.
"""
active_mask = vs.get_active_mask()
active_indices = active_mask.nonzero(as_tuple=True)[0].tolist()
dormant_indices = (~active_mask).nonzero(as_tuple=True)[0].tolist()
# Sample active indices when the network is very large to keep UMAP fast
_sampled = False
if len(active_indices) > _MAX_ACTIVE_SHOWN:
sample_idx = np.random.choice(
len(active_indices), _MAX_ACTIVE_SHOWN, replace=False
)
active_indices = [active_indices[i] for i in sample_idx]
_sampled = True
shown_dormant = dormant_indices[:_MAX_DORMANT_SHOWN]
all_indices = active_indices + shown_dormant
fig = go.Figure()
if not all_indices and query_vector is None:
fig.update_layout(
title="Vector Space — empty (no slots active yet)",
template="plotly_dark",
xaxis_title="dim-1",
yaxis_title="dim-2",
)
return fig
# Build the full set of vectors to project together so that the
# query star is embedded in the same coordinate space.
slot_vecs = (
vs.slots[all_indices].detach().numpy()
if all_indices
else np.empty((0, vs.D), dtype=np.float32)
)
if query_vector is not None:
q_arr = np.array(query_vector, dtype=np.float32).reshape(1, -1)
vectors = (
np.concatenate([slot_vecs, q_arr], axis=0)
if len(slot_vecs) > 0
else q_arr
)
else:
vectors = slot_vecs
if len(vectors) < 2:
fig.update_layout(
title="Not enough data to project",
template="plotly_dark",
)
return fig
coords = _reduce_2d(vectors)
n_active = len(active_indices)
# --- dormant scatter (grey, small) ---
if shown_dormant:
dom_xy = coords[n_active : n_active + len(shown_dormant)]
fig.add_trace(go.Scatter(
x=dom_xy[:, 0],
y=dom_xy[:, 1],
mode="markers",
marker=dict(color="rgba(140,140,140,0.18)", size=3),
name=f"Dormant ({len(dormant_indices):,})",
hoverinfo="name",
))
# --- active scatter coloured by domain ---
if active_indices:
act_xy = coords[:n_active]
domains = [vs.slot_domains.get(i, "unknown") for i in active_indices]
labels = [vs.slot_labels.get(i, f"slot_{i}") for i in active_indices]
activations = [float(vs.activation[i].item()) for i in active_indices]
domain_color = _domain_colour_map(domains)
for domain in domain_color:
idx_in_active = [j for j, d in enumerate(domains) if d == domain]
d_xy = act_xy[idx_in_active]
d_labels = [labels[j] for j in idx_in_active]
d_act = [activations[j] for j in idx_in_active]
marker_sizes = [6 + a * 10 for a in d_act]
fig.add_trace(go.Scatter(
x=d_xy[:, 0],
y=d_xy[:, 1],
mode="markers",
marker=dict(
color=domain_color[domain],
size=marker_sizes,
opacity=0.85,
line=dict(width=0.5, color="white"),
),
name=f"{domain} ({len(idx_in_active)})",
text=[
f"<b>{lbl}</b><br>activation: {a:.3f}"
for lbl, a in zip(d_labels, d_act)
],
hoverinfo="text",
))
# --- query vector star marker ---
_add_query_star(fig, coords, query_vector)
fig.update_layout(
title=(
f"Vector Space — {len(active_mask.nonzero(as_tuple=True)[0]):,} active"
+ (f" (showing {_MAX_ACTIVE_SHOWN:,} sampled)" if _sampled else "")
+ f" / {len(dormant_indices):,} dormant"
),
**_SCATTER_LAYOUT,
)
return fig
# ------------------------------------------------------------------
def plot_dense_umap(
self,
embeddings: np.ndarray,
labels: List[str],
domains: List[str],
query_vector: np.ndarray | None = None,
) -> go.Figure:
"""
UMAP / PCA plot for the DenseModel.
All slots are occupied (no dormant space). The query star will
visually land inside the cluster of coloured points, showing that
the dense model always forces an answer into occupied space.
"""
fig = go.Figure()
if len(embeddings) == 0:
fig.update_layout(
title="Dense Model — no data ingested yet",
template="plotly_dark",
)
return fig
# Subsample here (before building coords) so that index
# correspondence between embeddings/labels/domains and coords rows
# is maintained after _reduce_2d.
_sampled = False
if len(embeddings) > _MAX_ACTIVE_SHOWN:
idx = np.random.choice(len(embeddings), _MAX_ACTIVE_SHOWN, replace=False)
embeddings = embeddings[idx]
labels = [labels[i] for i in idx]
domains = [domains[i] for i in idx]
_sampled = True
n_emb = len(embeddings)
# Append query AFTER sampling so it is always the final row in
# coords and _add_query_star can reliably use coords[-1].
if query_vector is not None:
q_arr = np.array(query_vector, dtype=np.float32).reshape(1, -1)
vectors = np.concatenate([embeddings, q_arr], axis=0)
else:
vectors = embeddings
if len(vectors) < 2:
fig.update_layout(
title="Not enough data to project",
template="plotly_dark",
)
return fig
coords = _reduce_2d(vectors)
domain_color = _domain_colour_map(domains)
for domain in domain_color:
idx_in = [j for j, d in enumerate(domains) if d == domain]
d_xy = coords[idx_in]
d_labels = [labels[j] for j in idx_in]
fig.add_trace(go.Scatter(
x=d_xy[:, 0],
y=d_xy[:, 1],
mode="markers",
marker=dict(
color=domain_color[domain],
size=7,
opacity=0.85,
line=dict(width=0.5, color="white"),
),
name=f"{domain} ({len(idx_in)})",
text=d_labels,
hoverinfo="text",
))
_add_query_star(fig, coords, query_vector)
fig.update_layout(
title=(
f"Dense Model — {n_emb:,} slots (fully occupied, no dormant space)"
+ (" (sampled)" if _sampled else "")
),
**_SCATTER_LAYOUT,
)
return fig
# ------------------------------------------------------------------
def plot_histogram(self, vs: VectorSpace) -> go.Figure:
"""Activation distribution across all active slots."""
activations = vs.activation.detach().numpy()
active_vals = activations[activations > 0]
n_active = len(active_vals)
n_total = len(activations)
fig = go.Figure()
if n_active > 0:
fig.add_trace(go.Histogram(
x=active_vals,
nbinsx=40,
marker_color="rgba(80, 200, 140, 0.8)",
name="Active slots",
))
fig.update_layout(
title=f"Activation Distribution — {n_active:,} / {n_total:,} slots active",
xaxis_title="Activation Score",
yaxis_title="Slot Count",
template="plotly_dark",
margin=dict(l=40, r=30, t=50, b=40),
)
return fig
# ------------------------------------------------------------------
def plot_stage_diff(
self,
vs: VectorSpace,
new_slot_indices: List[int],
) -> go.Figure:
"""Highlight slots activated in the latest stage vs prior stages."""
active_mask = vs.get_active_mask()
active_indices = active_mask.nonzero(as_tuple=True)[0].tolist()
if not active_indices:
return go.Figure()
new_set = set(new_slot_indices)
vectors = vs.slots[active_indices].detach().numpy()
coords = _reduce_2d(vectors)
old_local = [j for j, idx in enumerate(active_indices) if idx not in new_set]
new_local = [j for j, idx in enumerate(active_indices) if idx in new_set]
fig = go.Figure()
if old_local:
old_xy = coords[old_local]
fig.add_trace(go.Scatter(
x=old_xy[:, 0],
y=old_xy[:, 1],
mode="markers",
marker=dict(color="rgba(90, 130, 210, 0.65)", size=7),
name=f"Previous stages ({len(old_local)})",
))
if new_local:
new_xy = coords[new_local]
fig.add_trace(go.Scatter(
x=new_xy[:, 0],
y=new_xy[:, 1],
mode="markers",
marker=dict(
color="rgba(255, 185, 0, 0.95)",
size=12,
symbol="star",
line=dict(width=1, color="white"),
),
name=f"New this stage ({len(new_local)})",
))
fig.update_layout(
title="Stage Diff — New Activations vs Prior Stages",
**_SCATTER_LAYOUT,
)
return fig
# ------------------------------------------------------------------
def plot_prune_comparison(
self,
before: np.ndarray,
after: np.ndarray,
) -> go.Figure:
"""Overlaid before/after activation histograms for the prune tab."""
before_active = before[before > 0]
after_active = after[after > 0]
fig = go.Figure()
fig.add_trace(go.Histogram(
x=before_active,
nbinsx=40,
marker_color="rgba(210, 80, 80, 0.65)",
name=f"Before pruning ({len(before_active):,} active)",
opacity=0.75,
))
fig.add_trace(go.Histogram(
x=after_active,
nbinsx=40,
marker_color="rgba(80, 200, 130, 0.75)",
name=f"After pruning ({len(after_active):,} active)",
opacity=0.75,
))
fig.update_layout(
barmode="overlay",
title="Prune Comparison — Before vs After",
xaxis_title="Activation Score",
yaxis_title="Slot Count",
template="plotly_dark",
legend=dict(x=0.55, y=0.99, bgcolor="rgba(0,0,0,0.3)"),
margin=dict(l=40, r=30, t=50, b=40),
)
return fig