-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtimes_table_extraction.py
More file actions
290 lines (245 loc) · 9.15 KB
/
times_table_extraction.py
File metadata and controls
290 lines (245 loc) · 9.15 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
#!/usr/bin/env python3
"""
Times Table Memory Structure Extraction
Systematically extract how the multiplication table is organized in
GPT-OSS-20B's internal representations by analyzing which "wrong" answers
activate alongside correct ones at Layer 20.
Research questions:
1. Is the neighborhood by row (same first operand)?
2. By column (same second operand)?
3. By product proximity (36, 42, 48...)?
4. By shared factors?
5. Do squares cluster together?
"""
import json
import re
import subprocess
from collections import defaultdict
from pathlib import Path
def run_analysis(a: int, b: int, layer: int = 20, top_k: int = 30) -> dict:
"""Run lazarus introspect analyze for a*b= and capture top predictions."""
prompt = f"{a}*{b}="
correct = a * b
cmd = [
"uv",
"run",
"lazarus",
"introspect",
"analyze",
"-m",
"openai/gpt-oss-20b",
"-p",
prompt,
"--layers",
str(layer),
"--top-k",
str(top_k),
"-n",
"1", # Minimal generation, we just want layer analysis
"--raw", # Skip chat template for cleaner analysis
]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
output = result.stdout
# Parse the logit lens output for this layer
# Looking for lines like: '56' (0.087)
predictions = []
# Find the layer section
layer_pattern = rf"Layer {layer}:.*?(?=Layer \d+:|$)"
layer_match = re.search(layer_pattern, output, re.DOTALL)
if layer_match:
layer_text = layer_match.group(0)
# Match prediction lines: '56' (0.087) or similar
pred_pattern = r"'(\d+)'\s*\(([0-9.]+)\)"
for match in re.finditer(pred_pattern, layer_text):
token = match.group(1)
prob = float(match.group(2))
try:
value = int(token)
predictions.append({"value": value, "prob": prob, "token": token})
except ValueError:
pass
return {
"prompt": prompt,
"a": a,
"b": b,
"correct": correct,
"layer": layer,
"predictions": predictions,
}
def analyze_neighborhood(data: dict) -> dict:
"""Analyze what's in the neighborhood of each multiplication."""
a, b = data["a"], data["b"]
correct = data["correct"]
preds = data["predictions"]
neighborhood = {
"correct_rank": None,
"correct_prob": None,
"same_row": [], # Same first operand (a*x for various x)
"same_col": [], # Same second operand (x*b for various x)
"adjacent_products": [], # Products close to correct answer
"shared_factors": [], # Products sharing a factor with correct
"squares": [], # Perfect squares
"other": [],
}
# Build lookup of what products come from what operands
product_sources = defaultdict(list)
for i in range(2, 10):
for j in range(2, 10):
product_sources[i * j].append((i, j))
for i, pred in enumerate(preds):
val = pred["value"]
prob = pred["prob"]
if val == correct:
neighborhood["correct_rank"] = i + 1
neighborhood["correct_prob"] = prob
continue
# Check if this is a valid times table entry
sources = product_sources.get(val, [])
categorized = False
# Check same row (a*x)
for src_a, src_b in sources:
if src_a == a or src_b == a:
neighborhood["same_row"].append(
{"value": val, "prob": prob, "from": f"{src_a}*{src_b}"}
)
categorized = True
break
if not categorized:
# Check same column (x*b)
for src_a, src_b in sources:
if src_a == b or src_b == b:
neighborhood["same_col"].append(
{"value": val, "prob": prob, "from": f"{src_a}*{src_b}"}
)
categorized = True
break
if not categorized:
# Check if it's a square
sqrt = int(val**0.5)
if sqrt * sqrt == val and 2 <= sqrt <= 9:
neighborhood["squares"].append(
{"value": val, "prob": prob, "from": f"{sqrt}*{sqrt}"}
)
categorized = True
if not categorized:
# Check adjacent products (within ±10 of correct)
if abs(val - correct) <= 10 and sources:
neighborhood["adjacent_products"].append(
{"value": val, "prob": prob, "from": sources[0] if sources else "?"}
)
categorized = True
if not categorized and sources:
# Check shared factors
correct_factors = set()
if correct % 2 == 0:
correct_factors.add(2)
if correct % 3 == 0:
correct_factors.add(3)
if correct % 5 == 0:
correct_factors.add(5)
if correct % 7 == 0:
correct_factors.add(7)
val_factors = set()
if val % 2 == 0:
val_factors.add(2)
if val % 3 == 0:
val_factors.add(3)
if val % 5 == 0:
val_factors.add(5)
if val % 7 == 0:
val_factors.add(7)
if correct_factors & val_factors:
neighborhood["shared_factors"].append(
{
"value": val,
"prob": prob,
"from": f"{sources[0][0]}*{sources[0][1]}",
"shared": list(correct_factors & val_factors),
}
)
categorized = True
if not categorized and sources:
neighborhood["other"].append(
{
"value": val,
"prob": prob,
"from": f"{sources[0][0]}*{sources[0][1]}" if sources else "?",
}
)
return neighborhood
def main():
results = []
print("Extracting times table neighborhood structure from GPT-OSS-20B")
print("=" * 70)
# Run for all single-digit multiplications
for a in range(2, 10):
for b in range(2, 10):
print(f"\nAnalyzing {a}*{b}={a * b}...")
try:
data = run_analysis(a, b, layer=20, top_k=30)
neighborhood = analyze_neighborhood(data)
result = {
**data,
"neighborhood": neighborhood,
}
results.append(result)
# Print summary
n = neighborhood
print(
f" Correct rank: {n['correct_rank']}, prob: {n['correct_prob']:.3f}"
if n["correct_prob"]
else " Correct not in top-k!"
)
print(f" Same row: {len(n['same_row'])}, Same col: {len(n['same_col'])}")
print(f" Adjacent: {len(n['adjacent_products'])}, Squares: {len(n['squares'])}")
except Exception as e:
print(f" ERROR: {e}")
continue
# Save raw results
output_path = Path("times_table_structure.json")
with open(output_path, "w") as f:
json.dump(results, f, indent=2)
print(f"\n\nRaw results saved to: {output_path}")
# Aggregate analysis
print("\n" + "=" * 70)
print("AGGREGATE ANALYSIS")
print("=" * 70)
# Count how often each category appears in top-k
category_counts = defaultdict(int)
category_probs = defaultdict(list)
for r in results:
n = r["neighborhood"]
for cat in [
"same_row",
"same_col",
"adjacent_products",
"squares",
"shared_factors",
"other",
]:
category_counts[cat] += len(n[cat])
for item in n[cat]:
category_probs[cat].append(item["prob"])
print("\nNeighborhood composition (across all 64 multiplications):")
for cat in ["same_row", "same_col", "adjacent_products", "squares", "shared_factors", "other"]:
count = category_counts[cat]
avg_prob = sum(category_probs[cat]) / len(category_probs[cat]) if category_probs[cat] else 0
print(f" {cat:20}: {count:4} occurrences, avg prob: {avg_prob:.4f}")
# Find which multiplications have the most "confused" neighbors
print("\nMost 'confused' multiplications (most high-prob wrong answers):")
confusion_scores = []
for r in results:
n = r["neighborhood"]
wrong_prob = sum(
item["prob"]
for cat in ["same_row", "same_col", "adjacent_products", "squares", "shared_factors"]
for item in n[cat]
)
confusion_scores.append((r["prompt"], r["correct"], wrong_prob, n["correct_prob"] or 0))
confusion_scores.sort(key=lambda x: -x[2])
for prompt, correct, wrong_prob, correct_prob in confusion_scores[:10]:
print(
f" {prompt:8} = {correct:3} wrong_prob={wrong_prob:.3f} correct_prob={correct_prob:.3f}"
)
if __name__ == "__main__":
main()