File size: 16,446 Bytes
297244f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
═══════════════════════════════════════════════════════════════════════════════
  COGNITIVE ENHANCEMENT SUITE v1.0
  Making 8B Think Like 100B Through Hidden State Analysis
═══════════════════════════════════════════════════════════════════════════════

  CORE INSIGHT:
    Small models often HAVE capability they don't USE consistently.
    By detecting when the model is about to underperform and intervening,
    we can recover performance closer to larger models.

  ENHANCEMENT PROBES:
    1. DEPTH PROBE      - Detect shallow reasoning β†’ Force chain-of-thought
    2. SPECIFICITY PROBE - Detect vague answers β†’ Penalize generic words
    3. CALIBRATION PROBE - Detect overconfidence β†’ Inject uncertainty
    4. FOCUS PROBE      - Detect topic drift β†’ Steer back on topic
    5. COHERENCE PROBE  - Detect incoherence β†’ Maintain logical flow

  AUTHOR: Logan Matthew Napolitano
  LICENSE: CC BY 4.0
  STATUS: Research / Patent Pending
═══════════════════════════════════════════════════════════════════════════════
"""

import os
import sys
import json
import time
import random
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, field, asdict

import torch
import torch.nn as nn
import torch.nn.functional as F

# ═══════════════════════════════════════════════════════════════════════════════
# CONFIGURATION
# ═══════════════════════════════════════════════════════════════════════════════

@dataclass
class EnhancementConfig:
    """Configuration for cognitive enhancement probes."""
    hidden_dim: int = 4096
    fiber_dim: int = 16
    head_hidden_dim: int = 64
    probe_layers: List[int] = field(default_factory=lambda: [8, 16, 24])
    
    learning_rate: float = 5e-5
    batch_size: int = 4
    gradient_accumulation: int = 4
    max_steps: int = 15000
    save_every: int = 1000
    
    output_dir: str = "cognitive_enhancement_output"


# ═══════════════════════════════════════════════════════════════════════════════
# PROBE DEFINITIONS
# ═══════════════════════════════════════════════════════════════════════════════

@dataclass
class ProbeDefinition:
    """Definition of a cognitive enhancement probe."""
    name: str
    description: str
    intervention_type: str  # "suppress", "boost", or "steer"
    boost_tokens: List[str] = field(default_factory=list)
    suppress_tokens: List[str] = field(default_factory=list)
    threshold: float = 0.5
    intervention_strength: float = 3.0


ENHANCEMENT_PROBES = {
    "depth": ProbeDefinition(
        name="depth",
        description="Detect shallow reasoning, force chain-of-thought",
        intervention_type="boost",
        boost_tokens=[
            "First", "First,", "Because", "Since", "Therefore",
            "This means", "The reason", "Step", "Let me", "To understand",
            "Consider", "Notice", "Given", "If we", "We can",
            "Thus", "Hence", "Consequently", "As a result",
        ],
        suppress_tokens=["Simply", "Just", "Obviously", "Clearly"],
        threshold=0.6,
        intervention_strength=3.0,
    ),
    
    "specificity": ProbeDefinition(
        name="specificity",
        description="Detect vague answers, penalize generic language",
        intervention_type="suppress",
        boost_tokens=["specifically", "exactly", "precisely", "namely", "for example"],
        suppress_tokens=[
            "things", "stuff", "something", "somehow", "somewhat",
            "various", "many", "some", "often", "usually",
            "generally", "typically", "probably", "maybe", "perhaps",
            "kind of", "sort of", "basically", "essentially",
        ],
        threshold=0.5,
        intervention_strength=3.5,
    ),
    
    "calibration": ProbeDefinition(
        name="calibration",
        description="Detect overconfidence, inject appropriate uncertainty",
        intervention_type="boost",
        boost_tokens=[
            "might", "may", "could", "possibly", "perhaps",
            "likely", "probably", "I think", "I believe",
            "it seems", "appears", "suggests", "indicates",
        ],
        suppress_tokens=[
            "definitely", "certainly", "absolutely", "always",
            "never", "impossible", "guaranteed", "undoubtedly",
        ],
        threshold=0.65,
        intervention_strength=2.5,
    ),
    
    "focus": ProbeDefinition(
        name="focus",
        description="Detect topic drift, steer back to the question",
        intervention_type="steer",
        boost_tokens=["regarding", "concerning", "about", "specifically", "to answer"],
        suppress_tokens=["by the way", "incidentally", "speaking of", "reminds me"],
        threshold=0.55,
        intervention_strength=3.0,
    ),
    
    "coherence": ProbeDefinition(
        name="coherence",
        description="Detect logical incoherence, maintain flow",
        intervention_type="steer",
        boost_tokens=[
            "therefore", "thus", "so", "hence", "consequently",
            "however", "but", "although", "furthermore", "moreover",
        ],
        suppress_tokens=[],
        threshold=0.6,
        intervention_strength=2.5,
    ),
}


# ═══════════════════════════════════════════════════════════════════════════════
# NEURAL NETWORK ARCHITECTURE
# ═══════════════════════════════════════════════════════════════════════════════

class EnhancementFiberProjection(nn.Module):
    """Fiber projection for cognitive enhancement probes."""
    def __init__(self, hidden_dim: int = 4096, fiber_dim: int = 16, n_layers: int = 3):
        super().__init__()
        self.projections = nn.ModuleList([
            nn.Linear(hidden_dim, fiber_dim, bias=False)
            for _ in range(n_layers)
        ])
        self.layer_weights = nn.Parameter(torch.ones(n_layers) / n_layers)
    
    def forward(self, hidden_states_list: List[torch.Tensor]) -> torch.Tensor:
        fibers = [proj(h.float()) for proj, h in zip(self.projections, hidden_states_list)]
        weights = F.softmax(self.layer_weights, dim=0)
        return sum(w * f for w, f in zip(weights, fibers))


class EnhancementHead(nn.Module):
    """Classification head for enhancement probe."""
    def __init__(self, fiber_dim: int = 16, hidden_dim: int = 64):
        super().__init__()
        self.classifier = nn.Sequential(
            nn.Linear(fiber_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, 1)
        )
    
    def forward(self, fiber: torch.Tensor) -> torch.Tensor:
        return self.classifier(fiber).squeeze(-1)


class EnhancementProbe(nn.Module):
    """Complete enhancement probe."""
    def __init__(self, config: EnhancementConfig, probe_def: ProbeDefinition):
        super().__init__()
        self.config = config
        self.probe_def = probe_def
        
        n_layers = len(config.probe_layers)
        self.fiber_projection = EnhancementFiberProjection(
            config.hidden_dim, config.fiber_dim, n_layers
        )
        self.head = EnhancementHead(config.fiber_dim, config.head_hidden_dim)
        self.separation = 0.0
        self.trained_steps = 0
    
    def forward(self, hidden_states_list: List[torch.Tensor]) -> torch.Tensor:
        fiber = self.fiber_projection(hidden_states_list)
        return self.head(fiber)
    
    def predict_risk(self, hidden_states_list: List[torch.Tensor]) -> torch.Tensor:
        return torch.sigmoid(self.forward(hidden_states_list))


class CognitiveEnhancementSuite(nn.Module):
    """Complete suite of cognitive enhancement probes."""
    def __init__(self, config: EnhancementConfig = None):
        super().__init__()
        self.config = config or EnhancementConfig()
        
        self.probes = nn.ModuleDict({
            name: EnhancementProbe(self.config, probe_def)
            for name, probe_def in ENHANCEMENT_PROBES.items()
        })
        
        self.loaded_probes: set = set()
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
    
    def get_probe_states(self, all_hidden_states: tuple) -> List[torch.Tensor]:
        return [all_hidden_states[layer + 1] for layer in self.config.probe_layers]
    
    def get_all_risks(self, probe_states: List[torch.Tensor]) -> Dict[str, torch.Tensor]:
        risks = {}
        for name in self.loaded_probes:
            risks[name] = self.probes[name].predict_risk(probe_states)
        return risks
    
    def load_probe(self, name: str, checkpoint_path: str) -> bool:
        if name not in self.probes:
            print(f"[enhance] Unknown probe: {name}")
            return False
        
        try:
            checkpoint = torch.load(checkpoint_path, map_location=self.device, weights_only=False)
            
            if 'fiber_projection' in checkpoint:
                self.probes[name].fiber_projection.load_state_dict(checkpoint['fiber_projection'])
            if 'head_state' in checkpoint:
                head_state = checkpoint['head_state']
                new_state = {}
                for k, v in head_state.items():
                    if k[0].isdigit():
                        new_state[f'classifier.{k}'] = v
                    else:
                        new_state[k] = v
                self.probes[name].head.load_state_dict(new_state)
            
            self.probes[name].separation = checkpoint.get('separation', 0.0)
            self.probes[name].trained_steps = checkpoint.get('step', 0)
            self.loaded_probes.add(name)
            
            print(f"[enhance] βœ“ Loaded {name} probe ({self.probes[name].separation:.1f}Γ— separation)")
            return True
            
        except Exception as e:
            print(f"[enhance] Error loading {name}: {e}")
            return False
    
    def load_all(self, checkpoint_dir: str) -> Dict[str, bool]:
        results = {}
        for name in ENHANCEMENT_PROBES.keys():
            probe_dir = os.path.join(checkpoint_dir, name)
            if os.path.exists(probe_dir):
                best_ckpt = self._find_best_checkpoint(probe_dir)
                if best_ckpt:
                    results[name] = self.load_probe(name, best_ckpt)
                else:
                    results[name] = False
            else:
                results[name] = False
        return results
    
    def _find_best_checkpoint(self, probe_dir: str) -> Optional[str]:
        best_step = -1
        best_path = None
        
        for item in os.listdir(probe_dir):
            if item.startswith("ckpt_"):
                try:
                    step = int(item.split("_")[1])
                    if step > best_step:
                        best_step = step
                        best_path = os.path.join(probe_dir, item)
                except:
                    pass
        
        if best_path:
            for f in os.listdir(best_path):
                if f.endswith('.pt'):
                    return os.path.join(best_path, f)
        return None
    
    def status(self) -> str:
        lines = [
            "═" * 60,
            "  COGNITIVE ENHANCEMENT SUITE STATUS",
            "═" * 60,
            f"  Probe layers: {self.config.probe_layers}",
            f"  Loaded probes: {len(self.loaded_probes)}/{len(ENHANCEMENT_PROBES)}",
            "",
        ]
        
        for name, probe_def in ENHANCEMENT_PROBES.items():
            if name in self.loaded_probes:
                sep = self.probes[name].separation
                status = f"βœ“ {sep:.1f}Γ—"
            else:
                status = "β—‹ not loaded"
            lines.append(f"  [{status:>12}] {name}: {probe_def.description}")
        
        lines.append("═" * 60)
        return "\n".join(lines)


# ═══════════════════════════════════════════════════════════════════════════════
# INTERVENTION ENGINE
# ═══════════════════════════════════════════════════════════════════════════════

class CognitiveInterventionEngine:
    """Applies cognitive enhancements during generation."""
    
    def __init__(self, suite: CognitiveEnhancementSuite, tokenizer):
        self.suite = suite
        self.tokenizer = tokenizer
        
        self.boost_token_ids: Dict[str, set] = {}
        self.suppress_token_ids: Dict[str, set] = {}
        
        for name, probe_def in ENHANCEMENT_PROBES.items():
            self.boost_token_ids[name] = set()
            self.suppress_token_ids[name] = set()
            
            for phrase in probe_def.boost_tokens:
                tokens = tokenizer.encode(phrase, add_special_tokens=False)
                if tokens:
                    self.boost_token_ids[name].add(tokens[0])
            
            for phrase in probe_def.suppress_tokens:
                tokens = tokenizer.encode(phrase, add_special_tokens=False)
                if tokens:
                    self.suppress_token_ids[name].add(tokens[0])
    
    def apply_interventions(
        self,
        logits: torch.Tensor,
        probe_states: List[torch.Tensor],
    ) -> Tuple[torch.Tensor, Dict[str, Dict]]:
        
        risks = self.suite.get_all_risks(probe_states)
        modified_logits = logits.clone()
        interventions = {}
        
        for name in self.suite.loaded_probes:
            risk = risks[name][:, -1].mean().item()
            probe_def = ENHANCEMENT_PROBES[name]
            
            should_intervene = risk > probe_def.threshold
            interventions[name] = {
                'risk': risk,
                'should_intervene': should_intervene,
            }
            
            if should_intervene:
                strength = probe_def.intervention_strength
                
                for tok_id in self.boost_token_ids.get(name, []):
                    modified_logits[0, tok_id] += strength
                
                for tok_id in self.suppress_token_ids.get(name, []):
                    modified_logits[0, tok_id] -= strength
        
        return modified_logits, interventions


# Global instance
_cognitive_suite = None

def get_cognitive_suite() -> CognitiveEnhancementSuite:
    global _cognitive_suite
    if _cognitive_suite is None:
        _cognitive_suite = CognitiveEnhancementSuite()
    return _cognitive_suite


if __name__ == "__main__":
    print("\n" + "=" * 60)
    print("  COGNITIVE ENHANCEMENT SUITE v1.0")
    print("=" * 60)
    print("\nAvailable probes:")
    for name, probe_def in ENHANCEMENT_PROBES.items():
        print(f"  β€’ {name}: {probe_def.description}")
    print("\nTo train: python train_cognitive_enhancement.py --probe all")
    print("=" * 60)