File size: 14,301 Bytes
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d997548
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d997548
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d997548
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d997548
 
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d997548
 
 
 
 
 
 
 
 
 
 
 
 
 
49136b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Cell system integration for Berna R7 (R5-functional).

Bridges:
  - modeling_berna_v2.CellAwareMLP (gates driven by DNA)
  - cells/ package (CellManager, saturation, dna_kernel, plexus)
"""
import time
from pathlib import Path
from typing import Dict, List, Optional

import numpy as np
import torch

from cells.cell_manager import CellManager
from cells.cell_birth import birth_cells_for_layer
from cells.dna_kernel import DNA, random_dna, signal_route
from cells.saturation import Cell, N_CELLS_PER_LAYER
from cognitive_log import CognitiveLog, pattern_signature, hamming_similarity
from dynamic_birth import BirthRegistry, birth_from_pattern, find_birth_candidate
from cells.saturation import MAX_CELLS_TOTAL


# ====================== Build =========================

def _identity_dna(cell_id, seed=42):
    """
    DNA with a-gene = 1.0 across all 100 chromosomes.
    signal_route() returns mean of a-gene = 1.0 → gate = 1.0 → identity.
    Checkpoint 2600 stays compatible (no abrupt behavior change).
    """
    dna = random_dna(cell_id, seed=seed)
    for i in range(len(dna.genes)):
        dna.genes[i][0] = 1.0
    dna.signature = dna._compute_signature()
    return dna


def build_cells_and_dnas(config_dict, model_uuid="mother-001",
                          n_cells_per_layer=N_CELLS_PER_LAYER):
    """
    Build cells for all MLP layers + identity DNA for each.
    Returns (cells_by_layer, dnas).
    """
    num_layers = int(config_dict["num_hidden_layers"])
    interm = int(config_dict["intermediate_size"])

    cells_by_layer = {}
    all_cells = []
    for layer_idx in range(num_layers):
        layer_cells = birth_cells_for_layer(
            layer_idx=layer_idx,
            intermediate_size=interm,
            n_cells=n_cells_per_layer,
            model_uuid=model_uuid,
        )
        cells_by_layer[layer_idx] = layer_cells
        all_cells.extend(layer_cells)

    dnas = {}
    for i, c in enumerate(all_cells):
        dnas[c.cell_id] = _identity_dna(c.cell_id, seed=42 + i)
    return cells_by_layer, dnas


# ====================== Runtime System =========================

class CellSystem:
    """
    Owns cells_by_layer + dnas. Runs CellManager.step every N train steps.
    """

    def __init__(self, model_uuid="mother-001",
                 base_dir="/data/berna-r7/core/cells_runtime",
                 measure_every=50,
                 verbose=True):
        self.model_uuid = model_uuid
        self.base_dir = Path(base_dir)
        self.measure_every = measure_every
        self.verbose = verbose

        self.cells_by_layer: Dict[int, List[Cell]] = {}
        self.dnas: Dict[str, DNA] = {}
        self.manager: Optional[CellManager] = None
        self._last_step = 0
        self._n_split_events = 0
        self.cognitive_log = CognitiveLog(
            Path(base_dir) / model_uuid / "cognitive",
            model_uuid=model_uuid,
        )
        self.birth_registry = BirthRegistry()
        self._n_birth_events = 0

    # ---------- attach / load ----------

    def attach_to_model(self, model, config_dict):
        """Build fresh cells + DNA, push into model's CellAwareMLP layers."""
        cells_by_layer, dnas = build_cells_and_dnas(
            config_dict, model_uuid=self.model_uuid
        )
        self.cells_by_layer = cells_by_layer
        self.dnas = dnas

        for layer_idx, layer in enumerate(model.model.layers):
            layer.mlp.cells = cells_by_layer[layer_idx]
            layer.mlp.dnas = dnas
            layer.mlp._gate_cache = None
            layer.mlp._cache_version = -1

        self.manager = CellManager(
            model_uuid=self.model_uuid,
            base_dir=self.base_dir / self.model_uuid,
        )
        all_cells = []
        for cs in cells_by_layer.values():
            all_cells.extend(cs)
        self.manager.cells = all_cells
        self.manager.dnas = dnas

        self._assign_slots(model)

        if self.verbose:
            print(f"[CellSystem] attached: {len(all_cells)} cells, "
                  f"{len(dnas)} DNA records")

    def load_state(self):
        """Load previous state from disk. Overrides attached cells."""
        if self.manager is None:
            return False
        ok = self.manager.load()
        if ok:
            self.cells_by_layer = {}
            for c in self.manager.cells:
                self.cells_by_layer.setdefault(c.layer_idx, []).append(c)
            self.dnas = self.manager.dnas
            if self.verbose:
                print(f"[CellSystem] loaded: {len(self.manager.cells)} cells")
        return ok

    def _assign_slots(self, model):
        """Assign each cell a unique slot index per layer (0..255)."""
        for layer_idx, layer in enumerate(model.model.layers):
            mapping = {}
            for i, c in enumerate(layer.mlp.cells):
                mapping[c.cell_id] = i % 256
            layer.mlp.assign_slots(mapping)

    def sync_into_model(self, model):
        """Push loaded cells + dnas into model's layers."""
        for layer_idx, layer in enumerate(model.model.layers):
            layer.mlp.cells = self.cells_by_layer.get(layer_idx, [])
            layer.mlp.dnas = self.dnas
            layer.mlp._gate_cache = None
            layer.mlp._cache_version = -1
        self._assign_slots(model)


    # ---------- measurement ----------

    def _enable_measure(self, model, on=True):
        """Toggle _measure_now on all CellAwareMLP layers."""
        for layer in model.model.layers:
            layer.mlp._measure_now = on

    def _collect_activations(self, model):
        """Collect detached activations from last forward."""
        acts = {}
        for layer_idx, layer in enumerate(model.model.layers):
            inter = layer.mlp._last_inter_detached
            if inter is None:
                continue
            inter0 = inter[0].float()
            for cell in layer.mlp.cells:
                if cell.state != "active":
                    continue
                a = inter0[:, cell.neuron_start:cell.neuron_end]
                acts[cell.cell_id] = a.detach().cpu().numpy()
        return acts

    def _measure_forward(self, model, dataset, seq_len=512):
        """Run one small forward pass with _measure_now=True."""
        import torch as _torch
        self._enable_measure(model, True)
        try:
            idx = _torch.randint(0, len(dataset), (1,)).item()
            item = dataset[idx]
            x = item[0] if isinstance(item, (tuple, list)) else item
            x = x[:seq_len].unsqueeze(0)
            device = next(model.parameters()).device
            x = x.to(device)
            with _torch.no_grad():
                _ = model(input_ids=x)
        finally:
            self._enable_measure(model, False)

    def _compute_coactivations(self, acts, top_k=20, thresh=0.3):
        """
        A_uv = cosine similarity of mean activations.
        Only for pairs where both mean magnitudes > thresh.
        Keep top_k strongest pairs.
        """
        means = {cid: float(np.mean(np.abs(a))) for cid, a in acts.items()}
        active = [cid for cid, m in means.items() if m > thresh]
        pairs = []
        for i in range(len(active)):
            for j in range(i + 1, len(active)):
                u, v = active[i], active[j]
                au = acts[u].reshape(-1)
                av = acts[v].reshape(-1)
                n = min(len(au), len(av))
                if n < 2:
                    continue
                au = au[:n]; av = av[:n]
                nu = float(np.linalg.norm(au)) + 1e-9
                nv = float(np.linalg.norm(av)) + 1e-9
                cos = float(np.dot(au, av) / (nu * nv))
                pairs.append((cos, (u, v)))
        pairs.sort(reverse=True)
        return {p: c for c, p in pairs[:top_k]}

    def maybe_step(self, model, global_step, dataset=None):
        """
        If global_step is a multiple of measure_every → run one CellManager step.
        Returns summary dict or None.
        """
        if global_step % self.measure_every != 0:
            return None

        t0 = time.time()
        # Run a small measure-forward so _last_inter_detached is populated
        if dataset is not None:
            try:
                self._measure_forward(model, dataset)
            except Exception as e:
                if self.verbose:
                    print(f"[CellSystem] measure forward failed: {e}")
        acts = self._collect_activations(model)
        if not acts:
            if self.verbose:
                print(f"[CellSystem] step {global_step}: no activations collected")
            return None
        coacts = self._compute_coactivations(acts)

        # === Pattern fingerprint per cell ===
        for layer in model.model.layers:
            for cell in layer.mlp.cells:
                if cell.state != "active" or cell.cell_id not in acts:
                    continue
                fp = pattern_signature(acts[cell.cell_id], top_k=50)
                if not getattr(cell, "fp_history", None):
                    cell.fp_history = []
                cell.fp_history.append(fp)
                if len(cell.fp_history) > 100:
                    cell.fp_history = cell.fp_history[-100:]
                if not getattr(cell, "pattern_sig", ""):
                    cell.pattern_sig = fp
                # R8: do NOT update pattern_sig here — birth check comes next

        # === Refresh all K6 centralities ===
        try:
            self.manager.encompassment.refresh_all(hamming_fn=hamming_similarity)
        except Exception as e:
            if self.verbose:
                print(f"[CellSystem] refresh_all failed: {e}")

        # === Dynamic births ===
        for layer_idx, layer in enumerate(model.model.layers):
            for cell in list(layer.mlp.cells):
                if cell.state != "active" or cell.cell_id not in acts:
                    continue
                fp = pattern_signature(acts[cell.cell_id], top_k=50)
                # Register with EncompassmentEngine (K6 CEO)
                # Register with EncompassmentEngine V2 (K6 CEO)
                self.manager.encompassment.register(cell.cell_id, fp, layer_idx=cell.layer_idx)
                if (hamming_similarity(fp, cell.pattern_sig) < 0.70
                        and len(self.manager.cells) < MAX_CELLS_TOTAL):
                    parent_dna = self.dnas.get(cell.cell_id)
                    if parent_dna is None:
                        continue
                    new_cell, new_dna = birth_from_pattern(
                        parent_cell=cell,
                        parent_dna=parent_dna,
                        new_pattern_sig=fp,
                        step=global_step,
                        model_uuid=self.model_uuid,
                        registry=self.birth_registry,
                    )
                    if new_cell is not None:
                        self.manager.cells.append(new_cell)
                        self.manager.dnas[new_cell.cell_id] = new_dna
                        self.dnas[new_cell.cell_id] = new_dna
                        self._n_birth_events += 1
                        self.cognitive_log.record_birth(
                            cell_id=new_cell.cell_id,
                            parent_id=new_cell.parent_id,
                            pattern_sig=fp,
                            step=global_step,
                            layer_idx=layer_idx,
                        )
                        if self.verbose:
                            print(f"[CellSystem] BIRTH: {new_cell.cell_id} "
                                  f"(parent={new_cell.parent_id}, step={global_step})")

        # R8: SmartFreezer
        try:
            from forgetting.smart_freezer import SmartFreezer
            if not hasattr(self, "_smart_freezer"):
                self._smart_freezer = SmartFreezer(S_star=0.85, K6_star=0.60, T=3)
            n = self._smart_freezer.periodic_check(self.manager.cells, global_step)
            if n > 0 and self.verbose:
                print(f"[CellSystem] FROZE {n} cells at step {global_step}")
        except Exception as e:
            if self.verbose:
                print(f"[CellSystem] SmartFreezer failed: {e}")

        # R8: pass cell_domains to manager (for K2)
        self.manager.cell_domains = self.cell_domains
        summary = self.manager.step(
            activations_by_cell=acts,
            coactivations=coacts,
            facets_by_cell=None,
            training_step=global_step,
        )

        # === Log splits ===
        for sp in summary.get("splits", []):
            children = sp.get("children", ["?", "?"])
            self.cognitive_log.record_split(
                parent_id=sp.get("parent_id", "?"),
                child_a=children[0],
                child_b=children[1] if len(children) > 1 else "?",
                S=sp.get("S", 0.0),
                step=global_step,
                layer_idx=-1,
            )
        self.manager.save()

        # Re-sync model after potential splits
        self.cells_by_layer = {}
        for c in self.manager.cells:
            self.cells_by_layer.setdefault(c.layer_idx, []).append(c)
        self.dnas = self.manager.dnas
        self.sync_into_model(model)

        self._last_step = global_step
        n_splits = len(summary.get("splits", []))
        self._n_split_events += n_splits

        if self.verbose:
            dt = time.time() - t0
            print(f"[CellSystem] step {global_step}: "
                  f"active={summary['n_cells_active']}/{summary['n_cells_total']} "
                  f"splits_now={n_splits} splits_total={self._n_split_events} "
                  f"({dt:.1f}s)")

        return summary

    def self_narrative(self):
        return self.cognitive_log.sample_for_training(n=3)

    def save_narrative(self):
        return self.cognitive_log.write_narrative()

    def stats(self):
        n_total = len(self.manager.cells) if self.manager else 0
        n_active = sum(1 for c in self.manager.cells if c.state == "active") \
                   if self.manager else 0
        return {
            "n_cells_total": n_total,
            "n_cells_active": n_active,
            "n_split_events": self._n_split_events,
        }