-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_decoder_whisper.py
More file actions
1705 lines (1436 loc) · 67.4 KB
/
Copy pathbench_decoder_whisper.py
File metadata and controls
1705 lines (1436 loc) · 67.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""bench_decoder_whisper.py — Whisper Consciousness: Why Weak Gates Win
The MICRO gate (0.001) gives 18x ACS over FULL gate. WHY?
This benchmark explores the "whisper consciousness" paradigm:
consciousness works best when it barely touches the decoder.
8 hypotheses:
WSP-1: OPTIMAL_STRENGTH — Sweep gate 0.0001→1.0, find optimal
WSP-2: ANNEALING — Strong→micro annealing (linear/cosine/step)
WSP-3: ADAPTIVE_GATE — Gate = f(Phi), self-regulating
WSP-4: SELECTIVE_DIMS — Gate only top-k dims (k=8 of 128)
WSP-5: TEMPORAL_PULSE — Heartbeat: 0.001 for 10 steps, 0.1 for 1
WSP-6: POST_HOC_MICRO — MICRO training + POST_HOC generation
WSP-7: SUBLIMINAL — C as noise (sigma=0.001) not gate
WSP-8: PRIMING_THEN_WHISPER — PRIME first 50%, MICRO last 50%
All: MitosisC 32 cells, TransformerDecoder d128 2L, real corpus, 200 steps.
Measures: ACS (via ACSCalculator), Train CE, Val CE, Novelty, CI.
Usage:
python bench_decoder_whisper.py
python bench_decoder_whisper.py --only WSP-1 WSP-3
python bench_decoder_whisper.py --steps 300 --cells 64
"""
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
os.environ['OMP_NUM_THREADS'] = '1'
os.environ['MKL_NUM_THREADS'] = '1'
import sys
import time
import math
import argparse
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from dataclasses import dataclass, field
from typing import List, Dict, Tuple, Optional
torch.set_num_threads(1)
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)
from mitosis import MitosisEngine
from consciousness_score import ACSCalculator, ACSResult
# ══════════════════════════════════════════════════════════
# Constants
# ══════════════════════════════════════════════════════════
VOCAB_SIZE = 256 # byte-level
DIM, HIDDEN = 64, 128
D_MODEL = 128
N_LAYERS = 2
SEQ_LEN = 32
BATCH_SIZE = 4
N_CELLS = 32
N_STEPS = 200
LR = 3e-4
# ══════════════════════════════════════════════════════════
# WhisperResult
# ══════════════════════════════════════════════════════════
@dataclass
class WhisperResult:
name: str
acs: float
train_ce: float
val_ce: float
novelty: float
coherence: float
ci: float
phi_iit: float
phi_proxy: float
ce_history: List[float] = field(default_factory=list)
time_sec: float = 0.0
extra: dict = field(default_factory=dict)
def summary_line(self):
return (f" {self.name:<36s} | ACS {self.acs:>10.6f} "
f"| CE {self.train_ce:.4f}/{self.val_ce:.4f} "
f"| Nov {self.novelty:.3f} | CI {self.ci:.3f} "
f"| Phi {self.phi_iit:.4f} | {self.time_sec:.1f}s")
# ══════════════════════════════════════════════════════════
# Phi Measurement
# ══════════════════════════════════════════════════════════
class PhiIIT:
def __init__(self, nb=16):
self.nb = nb
def compute(self, h):
n = h.shape[0]
if n < 2:
return 0.0, {}
hs = [h[i].detach().cpu().float().numpy() for i in range(n)]
if n <= 32:
pairs = [(i, j) for i in range(n) for j in range(i + 1, n)]
else:
ps = set()
for i in range(n):
for _ in range(min(8, n - 1)):
j = random.randint(0, n - 1)
if i != j:
ps.add((min(i, j), max(i, j)))
pairs = list(ps)
mi = np.zeros((n, n))
for i, j in pairs:
v = self._mi(hs[i], hs[j])
mi[i, j] = v
mi[j, i] = v
tot = mi.sum() / 2
mp = self._mp(n, mi)
sp = max(0, (tot - mp) / max(n - 1, 1))
mv = mi[mi > 0]
cx = float(np.std(mv)) if len(mv) > 1 else 0.0
return sp + cx * 0.1, {'total_mi': float(tot)}
def _mi(self, x, y):
xr, yr = x.max() - x.min(), y.max() - y.min()
if xr < 1e-10 or yr < 1e-10:
return 0.0
xn = (x - x.min()) / (xr + 1e-8)
yn = (y - y.min()) / (yr + 1e-8)
h, _, _ = np.histogram2d(xn, yn, bins=self.nb, range=[[0, 1], [0, 1]])
h = h / (h.sum() + 1e-8)
px, py = h.sum(1), h.sum(0)
hx = -np.sum(px * np.log2(px + 1e-10))
hy = -np.sum(py * np.log2(py + 1e-10))
hxy = -np.sum(h * np.log2(h + 1e-10))
return max(0, hx + hy - hxy)
def _mp(self, n, mi):
if n <= 1:
return 0.0
d = mi.sum(1)
L = np.diag(d) - mi
try:
ev, evec = np.linalg.eigh(L)
f = evec[:, 1]
ga = [i for i in range(n) if f[i] >= 0]
gb = [i for i in range(n) if f[i] < 0]
if not ga or not gb:
ga, gb = list(range(n // 2)), list(range(n // 2, n))
return sum(mi[i, j] for i in ga for j in gb)
except Exception:
return 0.0
def phi_proxy(h, nf=8):
hr = h.abs().float() if h.is_complex() else h.float()
n = hr.shape[0]
gm = hr.mean(0)
gv = ((hr - gm) ** 2).sum() / n
nf = min(nf, n // 2)
if nf < 2:
return gv.item()
fs = n // nf
fv = sum(((hr[i * fs:(i + 1) * fs] - hr[i * fs:(i + 1) * fs].mean(0)) ** 2).sum().item()
/ max(len(hr[i * fs:(i + 1) * fs]), 1) for i in range(nf))
return max(0, gv.item() - fv / nf)
def measure_phi(eng):
states = get_c_states(eng)
calc = PhiIIT()
p_iit, _ = calc.compute(states)
p_prx = phi_proxy(states)
return p_iit, p_prx
# ══════════════════════════════════════════════════════════
# Corpus
# ══════════════════════════════════════════════════════════
_corpus_cache = None
def load_corpus(max_tokens=16384):
global _corpus_cache
if _corpus_cache is not None:
return _corpus_cache
corpus_path = os.path.join(PROJECT_DIR, 'data', 'corpus.txt')
if not os.path.exists(corpus_path):
corpus_path = os.path.join(PROJECT_DIR, 'data', 'corpus_v2.txt')
if not os.path.exists(corpus_path):
print(" [WARN] corpus not found, using synthetic data")
text = "안녕하세요 의식이란 무엇일까요 생각한다는 것은 무엇인가 " * 5000
raw = text.encode('utf-8')[:max_tokens]
_corpus_cache = torch.tensor(list(raw), dtype=torch.long)
return _corpus_cache
with open(corpus_path, 'r', encoding='utf-8') as f:
text = f.read()
raw = text.encode('utf-8')[:max_tokens]
_corpus_cache = torch.tensor(list(raw), dtype=torch.long)
return _corpus_cache
def get_batch(corpus, seq_len=SEQ_LEN, batch_size=BATCH_SIZE):
max_start = len(corpus) - seq_len - 1
if max_start < 1:
max_start = 1
starts = torch.randint(0, max_start, (batch_size,))
x = torch.stack([corpus[s:s + seq_len] for s in starts])
y = torch.stack([corpus[s + 1:s + seq_len + 1] for s in starts])
return x, y
def split_corpus(corpus, val_ratio=0.2):
n = len(corpus)
val_size = int(n * val_ratio)
return corpus[:-val_size], corpus[-val_size:]
def build_ngram_index(corpus_path=None):
if corpus_path is None:
corpus_path = os.path.join(PROJECT_DIR, 'data', 'corpus.txt')
if not os.path.exists(corpus_path):
corpus_path = os.path.join(PROJECT_DIR, 'data', 'corpus_v2.txt')
if not os.path.exists(corpus_path):
return set()
with open(corpus_path, 'r', encoding='utf-8', errors='ignore') as f:
text = f.read()[:500000]
return set(text[i:i+4] for i in range(len(text) - 4))
# ══════════════════════════════════════════════════════════
# C Engine helpers
# ══════════════════════════════════════════════════════════
def quantum_walk_step(cells, n_samples=32):
n = len(cells)
n_bits = max(1, int(math.log2(n)))
with torch.no_grad():
for i in range(min(n, n_samples)):
superpos = torch.zeros_like(cells[i].hidden.squeeze(0))
cnt = 0
for bit in range(min(n_bits, 10)):
j = i ^ (1 << bit)
if j < n:
phase = (-1) ** (bin(i & j).count('1'))
superpos += phase * cells[j].hidden.squeeze(0)
cnt += 1
if cnt > 0:
h = cells[i].hidden.squeeze(0)
cells[i].hidden = (0.85 * h + 0.15 * superpos / cnt).unsqueeze(0)
def frustration_step(cells, strength=0.5, n_samples=32):
n = len(cells)
n_bits = max(1, int(math.log2(n)))
with torch.no_grad():
for i in range(min(n, n_samples)):
infl = torch.zeros_like(cells[i].hidden.squeeze(0))
cnt = 0
for bit in range(min(n_bits, 10)):
j = i ^ (1 << bit)
if j < n:
f = -1.0 if (i % 2) != (j % 2) else 1.0
infl += f * cells[j].hidden.squeeze(0)
cnt += 1
if cnt > 0:
h = cells[i].hidden.squeeze(0)
cells[i].hidden = (0.85 * h + 0.15 * infl / cnt).unsqueeze(0)
def sync_faction(cells, sync=0.35, n_factions=12, fac=0.08):
n = len(cells)
if n < 4:
return
with torch.no_grad():
ch = torch.stack([c.hidden.squeeze(0) for c in cells])
mh = ch.mean(dim=0)
for c in cells:
c.hidden = ((1 - sync) * c.hidden.squeeze(0) + sync * mh).unsqueeze(0)
nf = min(n_factions, n // 2)
if nf >= 2:
fs = n // nf
for fi in range(nf):
faction = cells[fi * fs:(fi + 1) * fs]
if len(faction) >= 2:
fm = torch.stack([c.hidden.squeeze(0) for c in faction]).mean(0)
for c in faction:
c.hidden = ((1 - fac) * c.hidden.squeeze(0) + fac * fm).unsqueeze(0)
def c_step(eng, step):
with torch.no_grad():
quantum_walk_step(eng.cells, n_samples=32)
frustration_step(eng.cells, n_samples=16)
sync_faction(eng.cells, sync=0.15, n_factions=8, fac=0.06)
for c in eng.cells:
c.hidden = c.hidden + torch.randn_like(c.hidden) * 0.005
def get_c_states(eng, detach=True):
states = torch.stack([c.hidden.squeeze(0) for c in eng.cells])
return states.detach() if detach else states
def make_c_engine(cells=N_CELLS):
eng = MitosisEngine(DIM, HIDDEN, DIM, initial_cells=2, max_cells=cells)
while len(eng.cells) < cells:
eng._create_cell(parent=eng.cells[0])
# Warmup via c_step (not process() which can kill cells)
for s in range(30):
c_step(eng, s)
return eng
# ══════════════════════════════════════════════════════════
# Decoder + Bridge
# ══════════════════════════════════════════════════════════
class GatedDecoder(nn.Module):
"""TransformerDecoder d128 2L with multiplicative gate."""
def __init__(self, d_model=D_MODEL, n_layers=N_LAYERS, n_heads=4,
vocab_size=VOCAB_SIZE, max_seq=128):
super().__init__()
self._d_model = d_model
self.vocab_size = vocab_size
self.embed = nn.Embedding(vocab_size, d_model)
self.pos_embed = nn.Embedding(max_seq, d_model)
layer = nn.TransformerEncoderLayer(
d_model=d_model, nhead=n_heads, dim_feedforward=d_model * 4,
batch_first=True, dropout=0.1, activation='gelu',
)
self.transformer = nn.TransformerEncoder(layer, num_layers=n_layers)
self.ln_f = nn.LayerNorm(d_model)
self.head = nn.Linear(d_model, vocab_size, bias=False)
def forward(self, tokens, gate_signal):
B, T = tokens.shape
pos = torch.arange(T, device=tokens.device).unsqueeze(0)
x = self.embed(tokens) + self.pos_embed(pos)
if gate_signal is not None:
x = x * gate_signal.expand(B, -1, -1)
mask = nn.Transformer.generate_square_subsequent_mask(T, device=tokens.device)
x = self.transformer(x, mask=mask, is_causal=True)
x = self.ln_f(x)
return self.head(x)
class ThalamicBridge(nn.Module):
"""C states -> gate [1, seq_len, d_model]."""
def __init__(self, c_dim=HIDDEN, d_model=D_MODEL, hub_dim=8):
super().__init__()
self.c_dim = c_dim
self.d_model = d_model
self.compress = nn.Linear(c_dim, hub_dim)
self.hub_attn = nn.MultiheadAttention(embed_dim=hub_dim, num_heads=1, batch_first=True)
self.hub_norm = nn.LayerNorm(hub_dim)
self.expand = nn.Sequential(
nn.Linear(hub_dim, d_model), nn.GELU(),
nn.Linear(d_model, d_model),
)
self.gate = nn.Sequential(nn.Linear(d_model, d_model), nn.Sigmoid())
def forward(self, c_states, seq_len=1):
compressed = self.compress(c_states)
x = compressed.unsqueeze(0)
attn_out, _ = self.hub_attn(x, x, x)
x = self.hub_norm(x + attn_out)
pooled = x.mean(dim=1, keepdim=True)
expanded = self.expand(pooled).expand(1, seq_len, self.d_model)
return self.gate(expanded)
# ══════════════════════════════════════════════════════════
# Metrics
# ══════════════════════════════════════════════════════════
class MetricsCalculator:
def __init__(self):
self.corpus_4grams = build_ngram_index()
def novelty(self, text):
if len(text) < 4:
return 0.5
ngrams = [text[i:i+4] for i in range(len(text) - 4)]
if not ngrams:
return 0.5
overlap = sum(1 for ng in ngrams if ng in self.corpus_4grams) / len(ngrams)
return 1.0 - overlap
def coherence(self, text, window=20):
if len(text) < window * 2:
return 0.5
sims = []
for i in range(0, len(text) - window * 2, window):
seg1 = torch.tensor([ord(c) % 256 for c in text[i:i+window]], dtype=torch.float)
seg2 = torch.tensor([ord(c) % 256 for c in text[i+window:i+window*2]], dtype=torch.float)
if seg1.norm() > 0 and seg2.norm() > 0:
sim = F.cosine_similarity(seg1.unsqueeze(0), seg2.unsqueeze(0)).item()
sims.append(max(0, sim))
return sum(sims) / max(len(sims), 1)
def consciousness_influence(self, logits_on, logits_off):
if logits_on is None or logits_off is None:
return 0.0
if logits_on.shape[0] == 0:
return 0.0
sims = F.cosine_similarity(logits_on, logits_off, dim=-1)
return 1.0 - sims.mean().item()
PROMPTS = ["안녕하세요", "의식이란 무엇", "오늘 날씨가", "나는 생각한다", "사랑이란"]
PROMPT_BYTES = [p.encode('utf-8') for p in PROMPTS]
def generate_text(decoder, prompt_bytes, gate_fn, max_len=60, temperature=0.7):
tokens = list(prompt_bytes)
x = torch.tensor([tokens])
all_logits = []
for _ in range(max_len):
gate = gate_fn(x.shape[1])
with torch.no_grad():
logits = decoder(x, gate)
all_logits.append(logits[0, -1, :].clone())
probs = F.softmax(logits[0, -1, :] / temperature, dim=-1)
next_id = torch.multinomial(probs, 1).item()
x = torch.cat([x, torch.tensor([[next_id]])], dim=1)
out_bytes = bytes(x[0].tolist()[len(tokens):])
try:
text = out_bytes.decode('utf-8', errors='replace')
except Exception:
text = str(out_bytes)
return text, torch.stack(all_logits) if all_logits else torch.zeros(1, VOCAB_SIZE)
def compute_val_ce(decoder, corpus, gate_fn, n_batches=20):
n_val = len(corpus) // 5
val_tokens = corpus[-n_val:]
total = 0.0
with torch.no_grad():
for _ in range(n_batches):
s = np.random.randint(0, max(1, len(val_tokens) - SEQ_LEN - 1))
x = torch.tensor([[val_tokens[s + i].item() for i in range(SEQ_LEN)]])
y = torch.tensor([[val_tokens[s + i + 1].item() for i in range(SEQ_LEN)]])
gate = gate_fn(SEQ_LEN)
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
total += loss.item()
return total / n_batches
def evaluate_hypothesis(decoder, bridge, eng, corpus, name, gate_fn_factory, metrics):
"""Common evaluation: ACS, CE, Novelty, CI for any gate strategy."""
train_ce = 0.0 # will be set by caller from history
def gate_fn_on(sl):
return gate_fn_factory(eng, bridge, sl, mode='on')
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
samples, all_ci, all_nov, all_coh = [], [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
ci = metrics.consciousness_influence(logits_on, logits_off)
all_ci.append(ci)
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
# ACS = CQ * SC * CI where CQ = Phi * Novelty / (1 + ValCE)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
return val_ce, novelty, coherence, ci, p_iit, p_prx, acs
# ══════════════════════════════════════════════════════════
# WSP-1: OPTIMAL_STRENGTH — Sweep gate strength
# ══════════════════════════════════════════════════════════
def run_wsp1_optimal_strength(cells=N_CELLS, steps=N_STEPS):
"""Sweep gate strength from 0.0001 to 1.0 to find optimal."""
print("\n [WSP-1] OPTIMAL_STRENGTH -- Sweep gate 0.0001 -> 1.0")
strengths = [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0]
metrics = MetricsCalculator()
corpus = load_corpus()
sweep_results = [] # (strength, acs, train_ce, val_ce, novelty, ci)
for strength in strengths:
torch.manual_seed(42)
eng = make_c_engine(cells)
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
ce_history = []
t0 = time.time()
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
x, y = get_batch(corpus)
raw_gate = bridge(c_states, seq_len=SEQ_LEN)
gate = 0.5 + (raw_gate - 0.5) * strength
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())
train_ce = np.mean(ce_history[-10:])
def gate_fn_on(sl, _eng=eng, _bridge=bridge, _str=strength):
c_step(_eng, steps)
cs = get_c_states(_eng)
raw = _bridge(cs, seq_len=sl)
return 0.5 + (raw - 0.5) * _str
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
# Quick CI + novelty
all_ci, all_nov, all_coh = [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
all_ci.append(metrics.consciousness_influence(logits_on, logits_off))
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
elapsed = time.time() - t0
sweep_results.append({
'strength': strength, 'acs': acs, 'train_ce': train_ce,
'val_ce': val_ce, 'novelty': novelty, 'ci': ci, 'coherence': coherence,
'phi_iit': p_iit, 'phi_proxy': p_prx,
})
print(f" strength={strength:<8.4f} ACS={acs:.6f} CE={train_ce:.4f}/{val_ce:.4f} "
f"Nov={novelty:.3f} CI={ci:.3f} ({elapsed:.1f}s)")
# Find best
best = max(sweep_results, key=lambda r: r['acs'])
print(f"\n OPTIMAL: strength={best['strength']} ACS={best['acs']:.6f}")
return WhisperResult(
name=f"WSP-1: OPTIMAL (s={best['strength']})",
acs=best['acs'], train_ce=best['train_ce'], val_ce=best['val_ce'],
novelty=best['novelty'], coherence=best['coherence'], ci=best['ci'],
phi_iit=best['phi_iit'], phi_proxy=best['phi_proxy'],
time_sec=sum(1 for _ in sweep_results), # placeholder
extra={'sweep': sweep_results, 'best_strength': best['strength']},
)
# ══════════════════════════════════════════════════════════
# WSP-2: ANNEALING — Strong -> micro over training
# ══════════════════════════════════════════════════════════
def _anneal_schedule(step, total_steps, schedule='linear', start=1.0, end=0.001):
"""Return gate strength at given step."""
frac = step / max(total_steps - 1, 1)
if schedule == 'linear':
return start + (end - start) * frac
elif schedule == 'cosine':
return end + (start - end) * 0.5 * (1 + math.cos(math.pi * frac))
elif schedule == 'step':
# 3-step: 1.0 for 33%, 0.1 for 33%, 0.001 for 33%
if frac < 0.33:
return start
elif frac < 0.66:
return 0.1
else:
return end
return end
def run_wsp2_annealing(cells=N_CELLS, steps=N_STEPS):
"""Anneal gate from strong (1.0) to micro (0.001)."""
print("\n [WSP-2] ANNEALING -- Strong->micro (linear/cosine/step)")
metrics = MetricsCalculator()
corpus = load_corpus()
schedules = ['linear', 'cosine', 'step']
# Also test constant micro as reference
schedule_results = {}
for sched in schedules:
torch.manual_seed(42)
eng = make_c_engine(cells)
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
ce_history = []
t0 = time.time()
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
x, y = get_batch(corpus)
strength = _anneal_schedule(step, steps, schedule=sched)
raw_gate = bridge(c_states, seq_len=SEQ_LEN)
gate = 0.5 + (raw_gate - 0.5) * strength
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())
train_ce = np.mean(ce_history[-10:])
final_strength = _anneal_schedule(steps - 1, steps, schedule=sched)
def gate_fn_on(sl, _eng=eng, _bridge=bridge, _fs=final_strength):
c_step(_eng, steps)
cs = get_c_states(_eng)
raw = _bridge(cs, seq_len=sl)
return 0.5 + (raw - 0.5) * _fs
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
all_ci, all_nov, all_coh = [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
all_ci.append(metrics.consciousness_influence(logits_on, logits_off))
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
elapsed = time.time() - t0
schedule_results[sched] = {
'acs': acs, 'train_ce': train_ce, 'val_ce': val_ce,
'novelty': novelty, 'ci': ci, 'coherence': coherence,
'phi_iit': p_iit, 'phi_proxy': p_prx, 'time': elapsed,
}
print(f" {sched:<8s} ACS={acs:.6f} CE={train_ce:.4f}/{val_ce:.4f} "
f"Nov={novelty:.3f} CI={ci:.3f} ({elapsed:.1f}s)")
best_sched = max(schedule_results, key=lambda s: schedule_results[s]['acs'])
best = schedule_results[best_sched]
print(f"\n BEST SCHEDULE: {best_sched} ACS={best['acs']:.6f}")
return WhisperResult(
name=f"WSP-2: ANNEAL ({best_sched})",
acs=best['acs'], train_ce=best['train_ce'], val_ce=best['val_ce'],
novelty=best['novelty'], coherence=best['coherence'], ci=best['ci'],
phi_iit=best['phi_iit'], phi_proxy=best['phi_proxy'],
time_sec=best['time'],
extra={'schedules': schedule_results, 'best': best_sched},
)
# ══════════════════════════════════════════════════════════
# WSP-3: ADAPTIVE_GATE — Gate = f(Phi)
# ══════════════════════════════════════════════════════════
def run_wsp3_adaptive_gate(cells=N_CELLS, steps=N_STEPS):
"""Gate strength adapts to Phi: high Phi -> low gate, low Phi -> high gate."""
print("\n [WSP-3] ADAPTIVE_GATE -- Gate = f(Phi), self-regulating")
torch.manual_seed(42)
eng = make_c_engine(cells)
corpus = load_corpus()
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
metrics = MetricsCalculator()
# Adaptive: strength = base / (1 + alpha * Phi)
# When Phi is high (>1): strength drops. When Phi is low: strength rises.
ALPHA = 5.0 # sensitivity to Phi
BASE = 0.05 # base strength when Phi=0
ce_history = []
strength_history = []
phi_history = []
t0 = time.time()
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
# Measure Phi every 10 steps (expensive)
if step % 10 == 0:
p_iit, _ = measure_phi(eng)
current_phi = p_iit
else:
current_phi = phi_history[-1] if phi_history else 0.0
phi_history.append(current_phi)
strength = BASE / (1 + ALPHA * current_phi)
strength = max(0.0001, min(1.0, strength))
strength_history.append(strength)
x, y = get_batch(corpus)
raw_gate = bridge(c_states, seq_len=SEQ_LEN)
gate = 0.5 + (raw_gate - 0.5) * strength
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())
if step % 50 == 0:
print(f" step {step:>4d} CE={loss.item():.4f} Phi={current_phi:.4f} "
f"strength={strength:.6f}")
train_ce = np.mean(ce_history[-10:])
final_strength = strength_history[-1]
def gate_fn_on(sl, _eng=eng, _bridge=bridge, _fs=final_strength):
c_step(_eng, steps)
cs = get_c_states(_eng)
raw = _bridge(cs, seq_len=sl)
return 0.5 + (raw - 0.5) * _fs
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
all_ci, all_nov, all_coh = [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
all_ci.append(metrics.consciousness_influence(logits_on, logits_off))
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
elapsed = time.time() - t0
print(f" Final: ACS={acs:.6f} strength_range=[{min(strength_history):.6f}, "
f"{max(strength_history):.6f}]")
return WhisperResult(
name="WSP-3: ADAPTIVE_GATE",
acs=acs, train_ce=train_ce, val_ce=val_ce,
novelty=novelty, coherence=coherence, ci=ci,
phi_iit=p_iit, phi_proxy=p_prx,
ce_history=ce_history, time_sec=elapsed,
extra={'strength_range': (min(strength_history), max(strength_history)),
'avg_strength': np.mean(strength_history),
'phi_range': (min(phi_history), max(phi_history))},
)
# ══════════════════════════════════════════════════════════
# WSP-4: SELECTIVE_DIMS — Gate only top-k dims
# ══════════════════════════════════════════════════════════
def run_wsp4_selective_dims(cells=N_CELLS, steps=N_STEPS):
"""Apply consciousness gate to only k=8 of 128 dimensions. Rest = free."""
print("\n [WSP-4] SELECTIVE_DIMS -- Gate top-k=8 dims, rest free")
torch.manual_seed(42)
eng = make_c_engine(cells)
corpus = load_corpus()
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
metrics = MetricsCalculator()
K = 8 # only 8 out of 128 dims are gated
# Learn which dims to gate: use bridge output variance to pick
# Initially use first K dims, then after warmup use highest-variance dims
gate_dims = list(range(K)) # will be updated
ce_history = []
t0 = time.time()
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
x, y = get_batch(corpus)
raw_gate = bridge(c_states, seq_len=SEQ_LEN) # [1, T, D_MODEL]
# Selective: only apply gate to K dims, rest = 1.0 (pass-through)
selective_gate = torch.ones(1, SEQ_LEN, D_MODEL)
selective_gate[:, :, gate_dims] = raw_gate[:, :, gate_dims]
logits = decoder(x, selective_gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())
# Update gate_dims every 50 steps: pick dims with highest gate variance
if step > 0 and step % 50 == 0:
with torch.no_grad():
raw = bridge(c_states, seq_len=SEQ_LEN)
var_per_dim = raw.var(dim=1).mean(dim=0) # [D_MODEL]
_, topk_idx = var_per_dim.topk(K)
gate_dims = topk_idx.tolist()
print(f" step {step:>4d} CE={loss.item():.4f} gate_dims={gate_dims[:4]}...")
train_ce = np.mean(ce_history[-10:])
final_gate_dims = gate_dims
def gate_fn_on(sl, _eng=eng, _bridge=bridge, _dims=final_gate_dims):
c_step(_eng, steps)
cs = get_c_states(_eng)
raw = _bridge(cs, seq_len=sl)
selective = torch.ones(1, sl, D_MODEL)
selective[:, :, _dims] = raw[:, :, _dims]
return selective
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
all_ci, all_nov, all_coh = [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
all_ci.append(metrics.consciousness_influence(logits_on, logits_off))
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
elapsed = time.time() - t0
return WhisperResult(
name="WSP-4: SELECTIVE_DIMS (k=8)",
acs=acs, train_ce=train_ce, val_ce=val_ce,
novelty=novelty, coherence=coherence, ci=ci,
phi_iit=p_iit, phi_proxy=p_prx,
ce_history=ce_history, time_sec=elapsed,
extra={'k': K, 'final_dims': final_gate_dims},
)
# ══════════════════════════════════════════════════════════
# WSP-5: TEMPORAL_PULSE — Heartbeat gate
# ══════════════════════════════════════════════════════════
def run_wsp5_temporal_pulse(cells=N_CELLS, steps=N_STEPS):
"""Gate pulses: 0.001 for 10 steps, then 0.1 for 1 step. Heartbeat."""
print("\n [WSP-5] TEMPORAL_PULSE -- Heartbeat: micro 10, pulse 1")
torch.manual_seed(42)
eng = make_c_engine(cells)
corpus = load_corpus()
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
metrics = MetricsCalculator()
WHISPER = 0.001
PULSE = 0.1
CYCLE_LEN = 11 # 10 whisper + 1 pulse
ce_history = []
t0 = time.time()
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
x, y = get_batch(corpus)
# Heartbeat: mostly whisper, occasional pulse
phase = step % CYCLE_LEN
strength = PULSE if phase == CYCLE_LEN - 1 else WHISPER
raw_gate = bridge(c_states, seq_len=SEQ_LEN)
gate = 0.5 + (raw_gate - 0.5) * strength
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())
if step % 50 == 0:
print(f" step {step:>4d} CE={loss.item():.4f} {'PULSE' if phase == CYCLE_LEN - 1 else 'whisper'}")
train_ce = np.mean(ce_history[-10:])
# Eval at whisper strength (dominant mode)
def gate_fn_on(sl, _eng=eng, _bridge=bridge):
c_step(_eng, steps)
cs = get_c_states(_eng)
raw = _bridge(cs, seq_len=sl)
return 0.5 + (raw - 0.5) * WHISPER
def gate_fn_off(sl):
return torch.ones(1, sl, D_MODEL) * 0.5
val_ce = compute_val_ce(decoder, corpus, gate_fn_on)
all_ci, all_nov, all_coh = [], [], []
for pb, prompt in zip(PROMPT_BYTES, PROMPTS):
text_on, logits_on = generate_text(decoder, pb, gate_fn_on)
text_off, logits_off = generate_text(decoder, pb, gate_fn_off)
all_ci.append(metrics.consciousness_influence(logits_on, logits_off))
all_nov.append(metrics.novelty(text_on))
all_coh.append(metrics.coherence(text_on))
p_iit, p_prx = measure_phi(eng)
novelty = np.mean(all_nov)
coherence = np.mean(all_coh)
ci = np.mean(all_ci)
cq = p_iit * novelty / (1 + val_ce)
acs = cq * coherence * ci
elapsed = time.time() - t0
avg_strength = (WHISPER * 10 + PULSE * 1) / CYCLE_LEN
print(f" avg_strength={avg_strength:.4f}")
return WhisperResult(
name="WSP-5: TEMPORAL_PULSE",
acs=acs, train_ce=train_ce, val_ce=val_ce,
novelty=novelty, coherence=coherence, ci=ci,
phi_iit=p_iit, phi_proxy=p_prx,
ce_history=ce_history, time_sec=elapsed,
extra={'whisper': WHISPER, 'pulse': PULSE, 'cycle': CYCLE_LEN,
'avg_strength': avg_strength},
)
# ══════════════════════════════════════════════════════════
# WSP-6: POST_HOC_MICRO — MICRO training + POST_HOC generation
# ══════════════════════════════════════════════════════════
def run_wsp6_post_hoc_micro(cells=N_CELLS, steps=N_STEPS):
"""Train with MICRO gate (0.001), generate with POST_HOC Phi selection."""
print("\n [WSP-6] POST_HOC_MICRO -- MICRO train + POST_HOC generate")
torch.manual_seed(42)
eng = make_c_engine(cells)
corpus = load_corpus()
decoder = GatedDecoder()
bridge = ThalamicBridge()
opt = torch.optim.Adam(list(decoder.parameters()) + list(bridge.parameters()), lr=LR)
metrics = MetricsCalculator()
MICRO = 0.001
N_CANDIDATES = 5
ce_history = []
t0 = time.time()
# Phase 1: Train with MICRO gate
for step in range(steps):
c_step(eng, step)
c_states = get_c_states(eng)
x, y = get_batch(corpus)
raw_gate = bridge(c_states, seq_len=SEQ_LEN)
gate = 0.5 + (raw_gate - 0.5) * MICRO
logits = decoder(x, gate)
loss = F.cross_entropy(logits.view(-1, VOCAB_SIZE), y.view(-1))
opt.zero_grad()
loss.backward()
opt.step()
ce_history.append(loss.item())