File size: 10,656 Bytes
2c914eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SAL Architecture

## Technical Deep-Dive

---

## Overview

SAL consists of four interconnected components:

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Training Loop                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                             β”‚
β”‚   Input β†’ Model β†’ Loss β†’ Gradients                         β”‚
β”‚                              ↓                              β”‚
β”‚                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”‚
β”‚                 β”‚  Communication Layer   β”‚                  β”‚
β”‚                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                  β”‚
β”‚                 β”‚  β”‚ Stability        β”‚  β”‚                  β”‚
β”‚                 β”‚  β”‚ Analyzer         β”‚  β”‚                  β”‚
β”‚                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚                  β”‚
β”‚                 β”‚           ↓            β”‚                  β”‚
β”‚                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                  β”‚
β”‚                 β”‚  β”‚ Emergence        β”‚  β”‚                  β”‚
β”‚                 β”‚  β”‚ Field            β”‚  β”‚                  β”‚
β”‚                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚                  β”‚
β”‚                 β”‚           ↓            β”‚                  β”‚
β”‚                 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                  β”‚
β”‚                 β”‚  β”‚ Protection       β”‚  β”‚                  β”‚
β”‚                 β”‚  β”‚ Masks            β”‚  β”‚                  β”‚
β”‚                 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚                  β”‚
β”‚                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β”‚
β”‚                              ↓                              β”‚
β”‚                    Protected Gradients                      β”‚
β”‚                              ↓                              β”‚
β”‚                       Optimizer.step()                      β”‚
β”‚                                                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## Component 1: Communication Layer

The Communication Layer is the core of SAL. It sits between gradient computation and optimizer application.

### Class: `CommunicationLayer`

```python
from sal import CommunicationLayer

comm = CommunicationLayer(
    model=model,
    threshold=0.5,           # Base stability threshold
    threshold_adaptation=0.1, # How much threshold adapts
    soft_protection=True,     # Soft vs hard protection
    history_length=100,       # Steps to track
)
```

### Methods

#### `analyze() -> Dict[str, float]`

Analyzes all parameters and computes stability scores.

```python
stability_scores = comm.analyze()
# {'layer1.weight': 0.73, 'layer1.bias': 0.45, ...}
```

**Stability Score Formula:**

```
s(p) = 1 / (1 + Ξ”w Γ— g_norm)
```

Where:
- `Ξ”w` = weight change since last step
- `g_norm` = gradient magnitude

High stability = low change Γ— low gradient = parameter has settled.

#### `protect() -> Dict[str, float]`

Applies protection to gradients based on stability analysis.

```python
protection_rates = comm.protect()
# {'layer1.weight': 0.42, 'layer1.bias': 0.0, ...}
```

**Protection Formula (Soft):**

```
protected_gradient = gradient Γ— (1 - stability_score)
```

Stable parameters get reduced gradients. Volatile parameters get full gradients.

### Adaptive Threshold

The threshold adapts to training dynamics:

```
Ο„ = Ο„β‚€ + Ξ± Γ— (Οƒ_grad / ΞΌ_grad)
```

When gradients are noisy (high variance), protection increases.
When gradients are stable, protection decreases.

---

## Component 2: Stability Analyzer

Classifies parameters into the Stability Spectrum.

### Class: `StabilityAnalyzer`

```python
from sal import StabilityAnalyzer

analyzer = StabilityAnalyzer(
    model=model,
    protected_threshold=0.7,  # Score above this β†’ protected
    volatile_threshold=0.3,   # Score below this β†’ volatile
    history_length=50,        # Steps to track
)
```

### Methods

#### `analyze() -> Dict[str, float]`

Computes stability scores using multiple signals:

1. **Weight variance** β€” Low variance over time = stable
2. **Gradient consistency** β€” Consistent direction = stable
3. **Change magnitude** β€” Small changes = stable

```python
scores = analyzer.analyze()
```

#### `classify() -> StabilitySpectrum`

Returns the distribution across stability states:

```python
spectrum = analyzer.classify()
# StabilitySpectrum(protected=12.3, neutral=70.5, volatile=17.2)
```

### Stability States

| State | Score Range | Behavior |
|-------|-------------|----------|
| Protected | > 0.7 | Minimal updates |
| Neutral | 0.3 - 0.7 | Careful updates |
| Volatile | < 0.3 | Full updates |

---

## Component 3: Emergence Field

Measures coherence, novelty, and resonance in semantic space.

### Class: `EmergenceField`

```python
from sal import EmergenceField

field = EmergenceField(
    dimensions=768,           # Semantic space dimensions
    history_length=100,       # Patterns to remember
    coherence_threshold=0.6,  # Minimum for emergence
    novelty_threshold=0.4,    # Minimum for emergence
)
```

### Methods

#### `observe(pattern) -> EmergenceState`

Observes a pattern and measures its emergence characteristics:

```python
state = field.observe(embedding)
# EmergenceState(coherence=0.72, novelty=0.45, resonance=0.63, intensity=0.41)
```

#### `detect_emergence(coherence, novelty) -> bool`

Simple check for emergence:

```python
is_emergent = field.detect_emergence(0.72, 0.45)
# True
```

### Emergence Metrics

**Coherence:** How internally consistent is the pattern?
- Measures variance between chunks
- Measures local smoothness
- High coherence = structured, meaningful

**Novelty:** How different from known patterns?
- Compares to historical patterns via cosine similarity
- High novelty = genuinely new

**Resonance:** How well does it fit the field?
- Distance from field centroid
- High resonance = harmonious with existing patterns

**Emergence = Coherent Novelty that Resonates**

---

## Component 4: Pulse-Split-Cascade (PSC)

Semantic Game of Life for pattern evolution.

### Class: `PulseCascade`

```python
from sal import PulseCascade

cascade = PulseCascade(
    max_pulses=32,          # Maximum concurrent pulses
    max_generations=10,     # Maximum depth
    split_threshold=0.6,    # Coherence needed to split
    merge_threshold=0.8,    # Similarity needed to merge
    expire_threshold=0.3,   # Minimum coherence to survive
)
```

### Flow

```
1. INITIATE
   Prompt embedding creates root pulse
   
2. EVOLVE
   Each pulse evolves via evolve_fn
   Coherence, novelty, resonance are measured
   
3. SPLIT
   High-coherence pulses split into children
   Children have slight variations
   
4. MERGE
   Similar pulses merge (high cosine similarity)
   Merging combines embeddings and preserves best traits
   
5. EXPIRE
   Low-coherence pulses expire
   Their patterns are lost
   
6. EMERGE
   Best viable pulse is the emergent result
   No scoring β€” just natural selection
```

### Methods

#### `initiate(embedding) -> Pulse`

Start cascade from prompt:

```python
root = cascade.initiate(prompt_embedding)
```

#### `step(evolve_fn, measure_fn) -> List[Pulse]`

Advance cascade by one step:

```python
active = cascade.step(
    evolve_fn=lambda x: model(x),
    measure_fn=lambda x: (coherence(x), novelty(x), resonance(x)),
)
```

#### `emerge() -> Pulse`

Get the emergent result:

```python
result = cascade.emerge()
```

---

## Integration

### Minimal Integration (2 lines)

```python
# Standard training loop
output = model(input)
loss = criterion(output, target)
loss.backward()

# SAL integration
comm.analyze()   # ← Line 1
comm.protect()   # ← Line 2

optimizer.step()
optimizer.zero_grad()
```

### Full Integration

```python
from sal import CommunicationLayer, StabilityAnalyzer, EmergenceField

# Initialize
comm = CommunicationLayer(model)
stability = StabilityAnalyzer(model)
field = EmergenceField()

# Training loop
for epoch in range(epochs):
    for batch in dataloader:
        # Forward
        output = model(batch)
        loss = criterion(output, target)
        
        # Backward
        loss.backward()
        
        # SAL: Analyze
        comm.analyze()
        stability.update()
        
        # SAL: Observe emergence
        with torch.no_grad():
            state = field.observe(model.get_embedding())
        
        # SAL: Protect
        comm.protect()
        
        # Update
        optimizer.step()
        optimizer.zero_grad()
        
    # Log spectrum
    spectrum = stability.classify()
    print(f"Epoch {epoch}: {spectrum}")
```

---

## Configuration

### Recommended Defaults

| Parameter | Default | Description |
|-----------|---------|-------------|
| `threshold` | 0.5 | Base stability threshold |
| `threshold_adaptation` | 0.1 | Adaptation rate |
| `soft_protection` | True | Soft vs hard protection |
| `protected_threshold` | 0.7 | Score for protected state |
| `volatile_threshold` | 0.3 | Score for volatile state |
| `history_length` | 100 | Steps to track |

### Tuning Guidelines

**More Protection:** Increase `threshold`, decrease `threshold_adaptation`
**Less Protection:** Decrease `threshold`, increase `threshold_adaptation`
**Faster Adaptation:** Increase `history_length`
**More Stability:** Increase `protected_threshold`

---

## Performance

SAL adds approximately 10% computational overhead:
- Stability analysis: O(n) where n = number of parameters
- Protection application: O(n)
- Memory: O(n Γ— history_length) for tracking

This overhead is negligible compared to the benefits of reduced catastrophic forgetting and improved continual learning.

---

*For the philosophy behind these technical choices, see [Principles](principles.md).*