forked from JamePeng/llama-cpp-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivation_analysis_visual.py
More file actions
324 lines (283 loc) · 16.5 KB
/
Copy pathactivation_analysis_visual.py
File metadata and controls
324 lines (283 loc) · 16.5 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
#!/usr/bin/env python3
"""
Visual activation delta analysis — generates an HTML report with
annotated plots explaining what the data means in plain language.
"""
from __future__ import annotations
import json
import argparse
import base64
import io
from pathlib import Path
import numpy as np
def load_dataset(npz_path: str):
data = np.load(npz_path)
meta_path = npz_path.replace(".npz", "_metadata.json")
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)
return data, meta
def fig_to_base64(fig) -> str:
buf = io.BytesIO()
fig.savefig(buf, format="png", dpi=130, bbox_inches="tight")
buf.seek(0)
return base64.b64encode(buf.read()).decode("utf-8")
def make_report(npz_path: str, output_html: str):
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import cosine_similarity
import umap
data, meta = load_dataset(npz_path)
deltas_3d = data["deltas"] # (n_examples, n_layer, n_embd)
n_ex, n_layer, n_embd = deltas_3d.shape
names = [m["name"] for m in meta["memories"]]
form_ids = [m.get("surface_form_id", 0) for m in meta["memories"]]
detail_scores = [m["n_injected_details"] for m in meta["memories"]]
detail_totals = [len(m["detail_keys"]) for m in meta["memories"]]
labels = [f"{n} f{f}" for n, f in zip(names, form_ids)]
# Unique memories for coloring
unique_names = list(dict.fromkeys(names))
cmap = plt.cm.Set1(np.linspace(0, 1, max(len(unique_names), 2)))
name_to_color = {n: cmap[i] for i, n in enumerate(unique_names)}
colors = [name_to_color[n] for n in names]
deltas_flat = deltas_3d.reshape(n_ex, -1)
plots = {}
# ──────────────────────────────────────────────────────────────
# PLOT 1: Simple scatter — "Where does each memory land?"
# ──────────────────────────────────────────────────────────────
reducer = umap.UMAP(n_neighbors=min(8, n_ex - 1), min_dist=0.3, random_state=42)
emb = reducer.fit_transform(deltas_flat)
fig, ax = plt.subplots(figsize=(10, 8))
for i in range(n_ex):
marker = "o" if detail_scores[i] / max(detail_totals[i], 1) >= 0.3 else "x"
size = 60 + detail_scores[i] * 15
ax.scatter(emb[i, 0], emb[i, 1], c=[colors[i]], s=size, marker=marker,
edgecolors="black", linewidths=0.5, zorder=3)
# Annotate
short = names[i].split("_")[0] + f" f{form_ids[i]}"
score_pct = int(100 * detail_scores[i] / max(detail_totals[i], 1))
ax.annotate(f"{short}\n{score_pct}%", (emb[i, 0], emb[i, 1]),
fontsize=7, ha="center", va="bottom",
xytext=(0, 8), textcoords="offset points")
patches = [mpatches.Patch(color=name_to_color[n], label=n.replace("_", " ").title())
for n in unique_names]
ax.legend(handles=patches, loc="best", fontsize=8)
ax.set_title("Each dot = one prefix injection attempt\n"
"(bigger dot = more details transferred, X = weak transfer <30%)",
fontsize=11)
ax.set_xlabel("← UMAP dimension 1 → (not meaningful on its own)")
ax.set_ylabel("← UMAP dimension 2 → (not meaningful on its own)")
ax.grid(True, alpha=0.2)
plots["scatter"] = fig_to_base64(fig)
plt.close(fig)
# ──────────────────────────────────────────────────────────────
# PLOT 2: Surface form consistency — the key question
# ──────────────────────────────────────────────────────────────
from collections import defaultdict
mem_groups = defaultdict(list)
for i, n in enumerate(names):
mem_groups[n].append(i)
fig, ax = plt.subplots(figsize=(10, 6))
y_offset = 0
bar_data = []
for name in unique_names:
indices = mem_groups[name]
if len(indices) < 2:
continue
group_deltas = deltas_flat[indices]
norms = np.linalg.norm(group_deltas, axis=1, keepdims=True)
norms = np.maximum(norms, 1e-8)
normalized = group_deltas / norms
sim = normalized @ normalized.T
# All pairwise similarities
for ii in range(len(indices)):
for jj in range(ii + 1, len(indices)):
pair_label = f"{names[indices[ii]].split('_')[0]} f{form_ids[indices[ii]]} ↔ f{form_ids[indices[jj]]}"
score_a = int(100 * detail_scores[indices[ii]] / max(detail_totals[indices[ii]], 1))
score_b = int(100 * detail_scores[indices[jj]] / max(detail_totals[indices[jj]], 1))
bar_data.append((pair_label, sim[ii, jj], f"{score_a}% & {score_b}%"))
bar_data.sort(key=lambda x: x[1], reverse=True)
y_pos = range(len(bar_data))
bar_colors = ["#2ecc71" if v > 0.7 else "#f39c12" if v > 0.3 else "#e74c3c" for _, v, _ in bar_data]
ax.barh(y_pos, [v for _, v, _ in bar_data], color=bar_colors, edgecolor="black", linewidth=0.5)
ax.set_yticks(y_pos)
ax.set_yticklabels([f"{lbl} ({scores})" for lbl, _, scores in bar_data], fontsize=8)
ax.set_xlabel("Cosine Similarity (1.0 = identical direction, 0 = unrelated, -1 = opposite)")
ax.set_title("Do different phrasings of the same facts\nproduce the same activation change?", fontsize=12)
ax.axvline(x=0.7, color="green", linestyle="--", alpha=0.5, label="Strong match (>0.7)")
ax.axvline(x=0.0, color="gray", linestyle="-", alpha=0.3)
ax.legend(fontsize=8)
ax.set_xlim(-0.5, 1.1)
ax.invert_yaxis()
plt.tight_layout()
plots["consistency"] = fig_to_base64(fig)
plt.close(fig)
# ──────────────────────────────────────────────────────────────
# PLOT 3: Layer delta magnitude — where's the action?
# ──────────────────────────────────────────────────────────────
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Left: raw delta magnitudes per layer
for i in range(n_ex):
layer_norms = np.linalg.norm(deltas_3d[i], axis=1)
alpha = 0.8 if detail_scores[i] / max(detail_totals[i], 1) >= 0.3 else 0.2
ax1.plot(range(n_layer), layer_norms, color=colors[i], alpha=alpha, linewidth=1.2)
ax1.set_xlabel("Layer Index")
ax1.set_ylabel("Activation Delta Magnitude (L2 norm)")
ax1.set_title("How much does each layer change\nwhen we inject knowledge?", fontsize=11)
ax1.axvspan(0, n_layer * 0.20, alpha=0.05, color="blue", label="Early (syntax)")
ax1.axvspan(n_layer * 0.35, n_layer * 0.55, alpha=0.08, color="green", label="Core (semantic)")
ax1.axvspan(n_layer * 0.90, n_layer, alpha=0.05, color="red", label="Final (output)")
ax1.legend(fontsize=7, loc="upper left")
ax1.grid(True, alpha=0.2)
# Right: PCA of all-layer deltas
pca = PCA()
pca.fit(deltas_flat - deltas_flat.mean(axis=0))
cumvar = np.cumsum(pca.explained_variance_ratio_)
ax2.plot(range(1, len(cumvar) + 1), cumvar, "b-o", markersize=4)
ax2.axhline(y=0.90, color="red", linestyle="--", alpha=0.5, label="90% variance")
ax2.axhline(y=0.99, color="orange", linestyle="--", alpha=0.5, label="99% variance")
n90 = int(np.argmax(cumvar >= 0.90)) + 1
n99 = int(np.argmax(cumvar >= 0.99)) + 1
ax2.annotate(f"{n90} components\nfor 90%", (n90, 0.90), fontsize=9,
xytext=(n90 + 1, 0.80), arrowprops=dict(arrowstyle="->"))
ax2.annotate(f"{n99} components\nfor 99%", (n99, 0.99), fontsize=9,
xytext=(n99 + 1, 0.88), arrowprops=dict(arrowstyle="->"))
ax2.set_xlabel("Number of Principal Components")
ax2.set_ylabel("Cumulative Variance Explained")
ax2.set_title("How 'structured' is the delta signal?\n(fewer components = more structured)", fontsize=11)
ax2.legend(fontsize=8)
ax2.grid(True, alpha=0.2)
plt.tight_layout()
plots["layers_pca"] = fig_to_base64(fig)
plt.close(fig)
# ──────────────────────────────────────────────────────────────
# PLOT 4: The actual question — do different memories separate?
# ──────────────────────────────────────────────────────────────
# Subtract dominant PC1 to see if memory-specific structure remains
pc1 = pca.components_[0]
projections = deltas_flat @ pc1
residuals = deltas_flat - np.outer(projections, pc1)
reducer2 = umap.UMAP(n_neighbors=min(8, n_ex - 1), min_dist=0.3, random_state=42)
emb2 = reducer2.fit_transform(residuals)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Left: raw UMAP
for i in range(n_ex):
marker = "o" if detail_scores[i] / max(detail_totals[i], 1) >= 0.3 else "x"
ax1.scatter(emb[i, 0], emb[i, 1], c=[colors[i]], s=80, marker=marker,
edgecolors="black", linewidths=0.5)
short = names[i].split("_")[0][:5] + f"f{form_ids[i]}"
ax1.annotate(short, (emb[i, 0], emb[i, 1]), fontsize=6, ha="center", va="bottom",
xytext=(0, 5), textcoords="offset points")
ax1.set_title("Raw deltas\n(dominated by universal injection signal)", fontsize=10)
ax1.grid(True, alpha=0.2)
# Right: PC1-subtracted UMAP
for i in range(n_ex):
marker = "o" if detail_scores[i] / max(detail_totals[i], 1) >= 0.3 else "x"
ax2.scatter(emb2[i, 0], emb2[i, 1], c=[colors[i]], s=80, marker=marker,
edgecolors="black", linewidths=0.5)
short = names[i].split("_")[0][:5] + f"f{form_ids[i]}"
ax2.annotate(short, (emb2[i, 0], emb2[i, 1]), fontsize=6, ha="center", va="bottom",
xytext=(0, 5), textcoords="offset points")
ax2.set_title("After removing universal component (PC1)\n(memory-specific signal only)", fontsize=10)
ax2.grid(True, alpha=0.2)
patches = [mpatches.Patch(color=name_to_color[n], label=n.replace("_", " ").title())
for n in unique_names]
fig.legend(handles=patches, loc="lower center", ncol=4, fontsize=7)
plt.tight_layout(rect=[0, 0.08, 1, 1])
plots["separation"] = fig_to_base64(fig)
plt.close(fig)
# ──────────────────────────────────────────────────────────────
# BUILD HTML
# ──────────────────────────────────────────────────────────────
html = f"""<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>Activation Delta Analysis</title>
<style>
body {{ font-family: 'Segoe UI', system-ui, sans-serif; max-width: 1100px; margin: 0 auto;
padding: 20px; background: #1a1a2e; color: #e0e0e0; }}
h1 {{ color: #7fdbca; border-bottom: 2px solid #7fdbca40; padding-bottom: 10px; }}
h2 {{ color: #c792ea; margin-top: 40px; }}
.plot {{ text-align: center; margin: 20px 0; }}
.plot img {{ max-width: 100%; border: 1px solid #333; border-radius: 8px; }}
.explain {{ background: #16213e; padding: 15px 20px; border-radius: 8px;
border-left: 4px solid #7fdbca; margin: 15px 0; line-height: 1.6; }}
.warn {{ border-left-color: #f39c12; }}
.good {{ border-left-color: #2ecc71; }}
.key {{ color: #f7dc6f; font-weight: bold; }}
table {{ border-collapse: collapse; margin: 10px 0; }}
td, th {{ padding: 6px 12px; border: 1px solid #333; text-align: left; }}
th {{ background: #16213e; color: #7fdbca; }}
</style>
</head><body>
<h1>Activation Delta Analysis Report</h1>
<p>{n_ex} examples across {len(unique_names)} memories, {n_layer} transformer layers, {n_embd}-dim embeddings</p>
<h2>1. The Map — Where Each Injection Attempt Lands</h2>
<div class="explain">
<strong>What this shows:</strong> Each dot is one experiment — we injected a set of character details
via KV cache prefix, then captured how much the model's internal state changed compared to
no injection. UMAP squashes the 200,000-dimensional activation change into 2D so we can see
if similar injections land near each other.<br><br>
<span class="key">Bigger dots</span> = more details successfully transferred into the output.
<span class="key">X markers</span> = weak transfer (<30% of details appeared).
<span class="key">Same color</span> = same character/memory.
</div>
<div class="plot"><img src="data:image/png;base64,{plots['scatter']}"></div>
<h2>2. The Key Question — Does Rewording the Same Facts Give the Same Signal?</h2>
<div class="explain">
<strong>What this shows:</strong> For memories where we tried multiple phrasings of the same facts
(e.g. bullet points vs prose vs narrative), this measures whether those different wordings produced
the <em>same direction</em> of activation change.<br><br>
<span class="key">Green bar (>0.7)</span> = "Yes, these phrasings reliably produce the same internal signal"
— the model is encoding the <em>meaning</em>, not the exact words.<br>
<span class="key">Red bar (<0.3)</span> = "No, different wordings produce different/opposite signals"
— either the phrasing failed to inject, or the model is tracking surface tokens.<br><br>
The percentages show how many details each form actually transferred.
</div>
<div class="plot"><img src="data:image/png;base64,{plots['consistency']}"></div>
<h2>3. Where in the Model Does the Change Happen?</h2>
<div class="explain">
<strong>Left plot:</strong> Each line is one injection attempt. The x-axis is the transformer layer
(0 = input, {n_layer-1} = output). The y-axis is how much that layer's output changed.
<span class="key">Faded lines</span> = weak injections (<30% detail transfer).<br><br>
Notice the massive jump at layer ~19 (48% depth) — that's where the model transitions from
"processing syntax" to "reasoning about meaning."<br><br>
<strong>Right plot:</strong> PCA tells us how many independent "directions" the activation changes
span. If one component explains 90%, all injections are pushing in roughly the same direction —
there's a universal "I've been primed" signal.
</div>
<div class="plot"><img src="data:image/png;base64,{plots['layers_pca']}"></div>
<h2>4. After Removing the Universal Signal — Can We Tell Memories Apart?</h2>
<div class="explain">
<strong>Left:</strong> Raw UMAP (dominated by the universal "you were primed" component — 91% of variance).<br>
<strong>Right:</strong> After subtracting that universal component. If same-colored dots cluster together
here, it means each memory has a <em>unique fingerprint</em> in the remaining 9% of signal.
That fingerprint is what a translator MLP would learn to convert into weight changes.<br><br>
<span class="key">What we want to see:</span> Same-color dots clustering together on the right plot,
especially for memories with multiple surface forms (same color, different numbers).
</div>
<div class="plot"><img src="data:image/png;base64,{plots['separation']}"></div>
<h2>Summary</h2>
<div class="explain good">
<strong>What the data says so far:</strong><br>
• 91% of all activation change is a <span class="key">single universal direction</span> — the model
has one dominant "I was given context" axis<br>
• Elara's 3 surface forms align at <span class="key">0.90 cosine similarity</span> — proof that the model
encodes meaning, not tokens<br>
• Surface forms that <em>fail</em> to transfer details also fail to align — the metric is
accurately measuring real semantic injection<br>
• We need more data (100+ examples) with more surface form variations to build training data
for the MLP translator
</div>
</body></html>"""
with open(output_html, "w", encoding="utf-8") as f:
f.write(html)
print(f"Report saved to: {output_html}")
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--dataset", required=True)
ap.add_argument("--output", default="scripts/activation_report.html")
args = ap.parse_args()
make_report(args.dataset, args.output)