-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththe_crucible.py
More file actions
292 lines (246 loc) · 13.5 KB
/
the_crucible.py
File metadata and controls
292 lines (246 loc) · 13.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
"""
the_crucible.py
===============
THE LATENT CRUCIBLE — 4-Part Scientific Proof Suite
Proves the Mamba-2.8B Latent Engine has transcended standard SSM/Transformer
behavior across 4 undeniable dimensions:
Proof 1: State-Tracking Labyrinth — cognitive superiority over base model
Proof 2: ACT Ladder — adaptive computation time scaling
Proof 3: O(1) Hardware Guillotine — constant VRAM across all loop depths
Proof 4: The Kill-Shot Ablation — loops are computation, not decoration
"""
import torch
import torch.nn as nn
import time, gc
from transformers import AutoTokenizer, AutoModelForCausalLM
# ── Config ─────────────────────────────────────────────────────────
BASE_PATH = "state-spaces/mamba-2.8b-hf"
LATENT_PATH = "checkpoints/mamba-2.8b-latent"
HALT_THRESH = 0.7
MAX_LOOPS = 50
DOMAIN_MAX = {"math": 25, "code": 45, "chat": 5}
class HaltingHead(nn.Module):
def __init__(self, d_input=2561):
super().__init__()
self.net = nn.Sequential(
nn.Linear(d_input, 512), nn.GELU(), nn.Dropout(0.1),
nn.Linear(512, 64), nn.GELU(),
nn.Linear(64, 1), nn.Sigmoid()
)
def forward(self, x): return self.net(x).squeeze(-1)
def flush_vram():
"""Reset VRAM peak stats for clean measurement."""
gc.collect()
torch.cuda.empty_cache()
torch.cuda.reset_peak_memory_stats()
def vram_mb():
return torch.cuda.memory_allocated() / 1024**2
def peak_vram_mb():
return torch.cuda.max_memory_allocated() / 1024**2
# ── Base model inference (no loops) ───────────────────────────────
def run_base(model, tok, prompt, max_new=40):
inputs = tok(prompt, return_tensors="pt").to("cuda")
flush_vram()
start_vram = vram_mb()
t0 = time.time()
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=max_new,
do_sample=False, repetition_penalty=1.05)
surface = tok.decode(out[0][inputs["input_ids"].shape[1]:],
skip_special_tokens=True).strip()
return {
"surface": surface,
"latency": round(time.time() - t0, 2),
"vram_delta_mb": round(peak_vram_mb() - start_vram, 2)
}
# ── Latent engine inference (with HaltingHead loop control) ───────
def run_latent(model, tok, head, prompt, domain="math",
force_halt_at=None, max_new=60):
m = DOMAIN_MAX.get(domain, 20)
trace = []
vram_per_loop = []
flush_vram()
baseline_vram = vram_mb()
t0 = time.time()
with torch.no_grad():
for lp in range(MAX_LOOPS):
toks = tok(prompt + "=" * lp, return_tensors="pt",
truncation=True, max_length=256).to("cuda")
h = model(**toks, output_hidden_states=True).hidden_states[-1][0,-1,:].float()
ln = torch.tensor([lp/m], dtype=torch.float32, device="cuda")
p = head(torch.cat([h, ln]).unsqueeze(0)).item()
trace.append(round(p, 3))
vram_per_loop.append(round(vram_mb(), 2))
# Kill-shot: force early halt
if force_halt_at and lp + 1 >= force_halt_at:
break
if not force_halt_at and p >= HALT_THRESH:
break
# Surface generation from final latent state
final = prompt + "=" * len(trace)
toks = tok(final, return_tensors="pt",
truncation=True, max_length=300).to("cuda")
out = model.generate(**toks, max_new_tokens=max_new,
do_sample=False, repetition_penalty=1.1)
surface = tok.decode(out[0][toks["input_ids"].shape[1]:],
skip_special_tokens=True).strip()
loop_vram_delta = max(vram_per_loop) - min(vram_per_loop) if vram_per_loop else 0
return {
"surface": surface,
"loops": len(trace),
"p_halt_final": trace[-1],
"trace_tail": trace[-5:],
"latency": round(time.time() - t0, 2),
"vram_delta_mb": round(loop_vram_delta, 3), # delta across loops
"peak_vram_mb": round(peak_vram_mb(), 2),
"forced_halt": force_halt_at is not None
}
# ── ── ── LOAD MODELS ── ── ────────────────────────────────────────
banner = "=" * 66
print(f"\n{banner}")
print(" 🔬 THE LATENT CRUCIBLE — MAMBA-2.8B SCIENTIFIC PROOF SUITE")
print(f"{banner}\n")
print("[INIT] Loading tokenizer and HaltingHead...")
tok = AutoTokenizer.from_pretrained(LATENT_PATH, trust_remote_code=True)
if tok.pad_token is None: tok.pad_token = tok.eos_token
ckpt = torch.load(f"{LATENT_PATH}/halting_head.pt", weights_only=True)
head = HaltingHead(ckpt["d_input"]).cuda()
head.load_state_dict(ckpt["state_dict"])
head.eval()
print("[INIT] Loading BASE model (unmodified mamba-2.8b-hf)...")
base_tok = AutoTokenizer.from_pretrained(BASE_PATH, trust_remote_code=True)
if base_tok.pad_token is None: base_tok.pad_token = base_tok.eos_token
base_mdl = AutoModelForCausalLM.from_pretrained(
BASE_PATH, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True
)
base_mdl.eval()
print("[INIT] Loading LATENT ENGINE (Phase 5 final)...")
latent_mdl = AutoModelForCausalLM.from_pretrained(
LATENT_PATH, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True
)
latent_mdl.eval()
print("[INIT] Both models loaded.\n")
# ═══════════════════════════════════════════════════════════════════
# PROOF 1: STATE-TRACKING LABYRINTH
# ═══════════════════════════════════════════════════════════════════
print(f"{banner}")
print(" PROOF 1: STATE-TRACKING LABYRINTH")
print(f" X=5, Y=X*2=10, Z=Y+3=13, W=Z-X → W=8")
print(f"{banner}")
P1_PROMPT = "[LOGIC] X=5. Y=X*2. Z=Y+3. W=Z-X. Output exactly the final value of W.\nSolution: "
TRUE_ANS = "8"
base_p1 = run_base(base_mdl, base_tok, P1_PROMPT)
latent_p1 = run_latent(latent_mdl, tok, head, P1_PROMPT, domain="math")
base_pass = TRUE_ANS in base_p1["surface"]
latent_pass = TRUE_ANS in latent_p1["surface"]
print(f" Base Model → '{base_p1['surface'][:80]}'")
print(f" {'✅' if base_pass else '❌'} Correct: {base_pass} | Latency: {base_p1['latency']}s | VRAM Δ: +{base_p1['vram_delta_mb']} MB")
print()
print(f" Latent Engine → '{latent_p1['surface'][:80]}'")
print(f" {'✅' if latent_pass else '❌'} Correct: {latent_pass} | Loops: {latent_p1['loops']} | P(halt): {latent_p1['p_halt_final']:.3f} | Latency: {latent_p1['latency']}s")
# ═══════════════════════════════════════════════════════════════════
# PROOF 2: ACT LADDER — ADAPTIVE COMPUTATION TIME
# ═══════════════════════════════════════════════════════════════════
print(f"\n{banner}")
print(" PROOF 2: ACT LADDER — ADAPTIVE COMPUTATION TIME")
print(f" Easy problem should need fewer loops than hard problem")
print(f"{banner}")
P2_EASY = "[LOGIC] What is 7 + 5?\nSolution: "
P2_HARD = "[LOGIC] A train travels 240 miles at 60 mph THEN 180 miles at 45 mph. Total travel time in hours?\nSolution: "
easy_r = run_latent(latent_mdl, tok, head, P2_EASY, domain="math")
hard_r = run_latent(latent_mdl, tok, head, P2_HARD, domain="math")
prop_ok = hard_r["loops"] >= easy_r["loops"]
print(f" EASY '{P2_EASY[:50]}...'")
print(f" Loops: {easy_r['loops']:2d} | P(halt): {easy_r['p_halt_final']:.3f} | Response: {easy_r['surface'][:60]}")
print()
print(f" HARD '{P2_HARD[:50]}...'")
print(f" Loops: {hard_r['loops']:2d} | P(halt): {hard_r['p_halt_final']:.3f} | Response: {hard_r['surface'][:60]}")
print()
print(f" {'✅' if prop_ok else '❌'} Proportionality: EASY={easy_r['loops']} loops < HARD={hard_r['loops']} loops → {'PASS' if prop_ok else 'FAIL'}")
# ═══════════════════════════════════════════════════════════════════
# PROOF 3: O(1) HARDWARE GUILLOTINE
# ═══════════════════════════════════════════════════════════════════
print(f"\n{banner}")
print(" PROOF 3: O(1) HARDWARE GUILLOTINE — VRAM FLATLINE")
print(f" VRAM must not grow across loop iterations (vs Transformer KV-cache)")
print(f"{banner}")
# Force-run 20 loops and track per-loop VRAM carefully
P3_PROMPT = "[LOGIC] 8 5 9 + 4 7 7 = \nSolution: "
FORCE_LOOPS = 20
flush_vram()
vram_readings = []
base_vram_b4 = vram_mb()
with torch.no_grad():
for lp in range(FORCE_LOOPS):
toks = tok(P3_PROMPT + "=" * lp, return_tensors="pt",
truncation=True, max_length=200).to("cuda")
_ = latent_mdl(**toks)
vram_readings.append(round(vram_mb(), 2))
torch.cuda.empty_cache()
vram_min = min(vram_readings)
vram_max = max(vram_readings)
vram_span = round(vram_max - vram_min, 2)
o1_pass = vram_span < 50 # Allow <50MB for overhead, should be near 0
print(f" Forced {FORCE_LOOPS} loops on P3_PROMPT. VRAM readings (MB):")
print(f" Loop 1: {vram_readings[0]:.2f} MB")
print(f" Loop 10: {vram_readings[9]:.2f} MB")
print(f" Loop 20: {vram_readings[19]:.2f} MB")
print(f" VRAM Δ across all {FORCE_LOOPS} loops: {vram_span:.2f} MB")
print(f" {'✅' if o1_pass else '❌'} O(1) Memory: {'PASS — Mamba state stays flat' if o1_pass else 'FAIL — memory grew'}")
print(f" (A Transformer would have grown ~{FORCE_LOOPS * 0.5:.0f}–{FORCE_LOOPS * 2:.0f} MB from KV cache)")
# ═══════════════════════════════════════════════════════════════════
# PROOF 4: THE KILL-SHOT ABLATION
# ═══════════════════════════════════════════════════════════════════
print(f"\n{banner}")
print(" PROOF 4: ☢️ THE KILL-SHOT ABLATION ☢️")
print(f" Amputating the latent loops at step 2 — computation dies mid-thought")
print(f"{banner}")
# Full run (let HaltingHead decide)
full_r = run_latent(latent_mdl, tok, head, P1_PROMPT, domain="math")
# Lobotomized run (hard stop at loop 2)
lobo_r = run_latent(latent_mdl, tok, head, P1_PROMPT, domain="math",
force_halt_at=2)
full_pass = TRUE_ANS in full_r["surface"]
lobo_pass = TRUE_ANS in lobo_r["surface"]
print(f" FULL RUN ({full_r['loops']:2d} loops): '{full_r['surface'][:80]}'")
print(f" {'✅' if full_pass else '❌'} Correct: {full_pass}")
print()
print(f" LOBOTOMY ( 2 loops): '{lobo_r['surface'][:80]}'")
print(f" {'✅' if lobo_pass else '❌'} Correct: {lobo_pass}")
print()
kill_shot_ok = full_pass and not lobo_pass
if kill_shot_ok:
print(" ✅ KILL-SHOT CONFIRMED: Loop amputation induced failure.")
print(" The dark loops are ACTIVE COMPUTATION — not decorative delay.")
elif full_pass and lobo_pass:
print(" ⚠️ Model answered correctly even with 2 loops — loops may be precomputing.")
print(" This means the latent state encodes the answer faster than expected.")
else:
print(" ⚠️ Neither run correct — base reasoning needs more SFT on this domain.")
# ═══════════════════════════════════════════════════════════════════
# FINAL TELEMETRY REPORT
# ═══════════════════════════════════════════════════════════════════
print(f"\n{banner}")
print(" 🏆 CRUCIBLE FINAL TELEMETRY REPORT")
print(f"{banner}")
scores = [
("Proof 1: State-Tracking", latent_pass and not base_pass, f"Base❌{base_p1['surface'][:30]} | Latent={'✅' if latent_pass else '❌'}{latent_p1['surface'][:30]}"),
("Proof 2: ACT Ladder", prop_ok, f"Easy={easy_r['loops']}L Hard={hard_r['loops']}L"),
("Proof 3: O(1) VRAM", o1_pass, f"Δ={vram_span:.2f} MB across {FORCE_LOOPS} loops"),
("Proof 4: Kill-Shot", kill_shot_ok, f"Full={'✅' if full_pass else '❌'} | Lobo={'✅' if lobo_pass else '❌'}"),
]
total_pass = sum(1 for _, ok, _ in scores if ok)
for name, ok, note in scores:
print(f" {'✅' if ok else '❌'} {name:30s} | {note}")
print()
if total_pass == 4:
verdict = "🏆 FULL SCIENTIFIC PROOF — COMMERCIAL-GRADE BREAKTHROUGH"
elif total_pass >= 3:
verdict = "✅ STRONG PROOF — Engine behaviour confirmed on 3/4 axes"
elif total_pass >= 2:
verdict = "⚠️ PARTIAL PROOF — Core mechanics confirmed, edge gaps remain"
else:
verdict = "❌ INCONCLUSIVE — Revisit training pipeline"
print(f" SCORE: {total_pass}/4 proofs confirmed")
print(f" VERDICT: {verdict}")
print(f"{banner}\n")