|
| 1 | +# ============================================================================= |
| 2 | +# Experiment 6 — Phi-Pi-Fib compression gate: model as library + chain. |
| 3 | +# |
| 4 | +# Architectural premise. Replace "trillions of dense weights" with: |
| 5 | +# |
| 6 | +# library : a finite set of permanent functions f_i: int -> int, |
| 7 | +# each indexed by a Fibonacci-attractor KEY. Sorted |
| 8 | +# ascending so phi-pi-fib search works. |
| 9 | +# |
| 10 | +# chain : a short sequence of TARGET KEYS. To execute the chain |
| 11 | +# on state x, walk the keys left to right; for each key |
| 12 | +# look up the library entry whose key is closest (via |
| 13 | +# phi-pi-fib nearest-neighbour search), apply that |
| 14 | +# entry's function to x, advance. |
| 15 | +# |
| 16 | +# gate : the lookup itself. The "compression gate" maps |
| 17 | +# (state, target_key) -> nearest_living_function. |
| 18 | +# When the target key matches an entry exactly, the |
| 19 | +# gate is identity. When it doesn't, the gate folds |
| 20 | +# to the nearest available attractor — this is what |
| 21 | +# gives the model "act as if it died" robustness. |
| 22 | +# |
| 23 | +# Four claims this experiment tests, with concrete numbers: |
| 24 | +# |
| 25 | +# (A) Composition. Short chains over a small library compute |
| 26 | +# non-trivial behavior. (Run a chain, show |
| 27 | +# the trace, demonstrate intermediate state.) |
| 28 | +# |
| 29 | +# (B) Compression. Total bytes (library + chain) vs equivalent |
| 30 | +# direct (state -> output) lookup table over |
| 31 | +# the input range. |
| 32 | +# |
| 33 | +# (C) Death tolerance. Delete one library function at a time, |
| 34 | +# re-run the SAME chain. The gate routes to |
| 35 | +# the nearest surviving function. Output |
| 36 | +# stays deterministic; the model degrades |
| 37 | +# gracefully instead of crashing. |
| 38 | +# |
| 39 | +# (D) Interchangeability. Same library, different chains — |
| 40 | +# different behaviors. The "parameters" |
| 41 | +# ARE the chain; swapping the chain swaps |
| 42 | +# the model. |
| 43 | +# |
| 44 | +# Run: |
| 45 | +# ./target/release/omnimcode-standalone experiments/hybrid_llm/experiment_6_compression_gate.omc |
| 46 | +# ============================================================================= |
| 47 | + |
| 48 | +# --------------------------------------------------------------------------- |
| 49 | +# Library: 12 primitive functions keyed by Fibonacci attractors. |
| 50 | +# |
| 51 | +# Each entry is (key, op_id). op_id dispatches in apply_op() below. |
| 52 | +# The library is implicitly sorted by key (ascending Fibonacci sequence) |
| 53 | +# so a Fibonacci-step search across it is well-defined. |
| 54 | +# --------------------------------------------------------------------------- |
| 55 | + |
| 56 | +h LIB_SIZE = 12; |
| 57 | + |
| 58 | +fn library_entry(i) -> array { |
| 59 | + h e = arr_new(2, 0); |
| 60 | + if i == 0 { arr_set(e, 0, 1); arr_set(e, 1, 0); return e; } |
| 61 | + if i == 1 { arr_set(e, 0, 2); arr_set(e, 1, 1); return e; } |
| 62 | + if i == 2 { arr_set(e, 0, 3); arr_set(e, 1, 2); return e; } |
| 63 | + if i == 3 { arr_set(e, 0, 5); arr_set(e, 1, 3); return e; } |
| 64 | + if i == 4 { arr_set(e, 0, 8); arr_set(e, 1, 4); return e; } |
| 65 | + if i == 5 { arr_set(e, 0, 13); arr_set(e, 1, 5); return e; } |
| 66 | + if i == 6 { arr_set(e, 0, 21); arr_set(e, 1, 6); return e; } |
| 67 | + if i == 7 { arr_set(e, 0, 34); arr_set(e, 1, 7); return e; } |
| 68 | + if i == 8 { arr_set(e, 0, 55); arr_set(e, 1, 8); return e; } |
| 69 | + if i == 9 { arr_set(e, 0, 89); arr_set(e, 1, 9); return e; } |
| 70 | + if i == 10 { arr_set(e, 0, 144); arr_set(e, 1, 10); return e; } |
| 71 | + arr_set(e, 0, 233); arr_set(e, 1, 11); return e; |
| 72 | +} |
| 73 | + |
| 74 | +# Apply primitive operation by op_id. These are the "permanent functions" — |
| 75 | +# small, composable, deterministic. The library is content-addressable by |
| 76 | +# its phi-keys; the op_id is just the dispatch trampoline. |
| 77 | +fn apply_op(op, x) -> int { |
| 78 | + if op == 0 { return x; } # identity |
| 79 | + if op == 1 { return x + 1; } # increment |
| 80 | + if op == 2 { return x * 2; } # double |
| 81 | + if op == 3 { return phi.fold(x); } # snap to nearest attractor |
| 82 | + if op == 4 { return x + phi.fold(x); } # add fold-shadow |
| 83 | + if op == 5 { return to_int(harmonic_interfere(x, 5)); } # interfere with 5 |
| 84 | + if op == 6 { return to_int(harmonic_interfere(x, 13)); } # interfere with 13 |
| 85 | + if op == 7 { return x * x; } # square |
| 86 | + if op == 8 { return phi.fold(x * 2); } # double then fold |
| 87 | + if op == 9 { return x - phi.fold(x); } # residual from attractor |
| 88 | + if op == 10 { return to_int(harmonic_interfere(x, 21)); } # interfere with 21 |
| 89 | + if op == 11 { return phi.fold(x + 7); } # phi-shifted fold |
| 90 | + return x; |
| 91 | +} |
| 92 | + |
| 93 | +# Pretty-print op so traces are readable. |
| 94 | +fn op_name(op) -> string { |
| 95 | + if op == 0 { return "identity"; } |
| 96 | + if op == 1 { return "increment"; } |
| 97 | + if op == 2 { return "double"; } |
| 98 | + if op == 3 { return "fold"; } |
| 99 | + if op == 4 { return "add_fold"; } |
| 100 | + if op == 5 { return "interfere_5"; } |
| 101 | + if op == 6 { return "interfere_13"; } |
| 102 | + if op == 7 { return "square"; } |
| 103 | + if op == 8 { return "double_fold"; } |
| 104 | + if op == 9 { return "residual"; } |
| 105 | + if op == 10 { return "interfere_21"; } |
| 106 | + if op == 11 { return "phi_shift_fold"; } |
| 107 | + return "?"; |
| 108 | +} |
| 109 | + |
| 110 | +# --------------------------------------------------------------------------- |
| 111 | +# THE GATE: phi-pi-fib nearest-key lookup over the (alive subset of the) |
| 112 | +# library. Returns the index of the entry whose key is closest to target, |
| 113 | +# among entries with alive_mask[i] == 1. |
| 114 | +# |
| 115 | +# A proper phi-fib search would walk Fibonacci-derived split points; for a |
| 116 | +# 12-entry library the linear scan is identical in effect (and exercises |
| 117 | +# the same nearest-when-missing semantics that give us death tolerance). |
| 118 | +# The bytecode VM-friendly version of phi-fib search lives in |
| 119 | +# omnimcode-core/src/phi_pi_fib.rs — wiring that in as an OMC builtin |
| 120 | +# is a follow-up. |
| 121 | +# |
| 122 | +# Returns -1 only if the entire library is dead, which we treat as a |
| 123 | +# total model failure. |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | +fn gate(target_key, alive_mask) -> int { |
| 126 | + h best_idx = -1; |
| 127 | + h best_dist = -1; |
| 128 | + h i = 0; |
| 129 | + while i < LIB_SIZE { |
| 130 | + if arr_get(alive_mask, i) == 1 { |
| 131 | + h entry = library_entry(i); |
| 132 | + h key = arr_get(entry, 0); |
| 133 | + h d = key - target_key; |
| 134 | + if d < 0 { d = 0 - d; } |
| 135 | + if best_dist < 0 { |
| 136 | + best_dist = d; |
| 137 | + best_idx = i; |
| 138 | + } else { |
| 139 | + if d < best_dist { |
| 140 | + best_dist = d; |
| 141 | + best_idx = i; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + i = i + 1; |
| 146 | + } |
| 147 | + return best_idx; |
| 148 | +} |
| 149 | + |
| 150 | +# --------------------------------------------------------------------------- |
| 151 | +# Run a chain. `keys` is a list of target keys (the "model parameters"); |
| 152 | +# `alive_mask` records which library entries are still available. |
| 153 | +# Returns the final state. |
| 154 | +# --------------------------------------------------------------------------- |
| 155 | +fn run_chain(initial_state, keys, alive_mask) -> int { |
| 156 | + h state = initial_state; |
| 157 | + h n = arr_len(keys); |
| 158 | + h i = 0; |
| 159 | + while i < n { |
| 160 | + h k = arr_get(keys, i); |
| 161 | + h idx = gate(k, alive_mask); |
| 162 | + if idx >= 0 { |
| 163 | + h entry = library_entry(idx); |
| 164 | + h op = arr_get(entry, 1); |
| 165 | + state = apply_op(op, state); |
| 166 | + } |
| 167 | + i = i + 1; |
| 168 | + } |
| 169 | + return state; |
| 170 | +} |
| 171 | + |
| 172 | +# Same, but trace each step. Returns final state, prints each transition. |
| 173 | +fn run_chain_traced(initial_state, keys, alive_mask) -> int { |
| 174 | + h state = initial_state; |
| 175 | + h n = arr_len(keys); |
| 176 | + h i = 0; |
| 177 | + while i < n { |
| 178 | + h target = arr_get(keys, i); |
| 179 | + h idx = gate(target, alive_mask); |
| 180 | + if idx >= 0 { |
| 181 | + h entry = library_entry(idx); |
| 182 | + h key_actual = arr_get(entry, 0); |
| 183 | + h op = arr_get(entry, 1); |
| 184 | + h prev = state; |
| 185 | + state = apply_op(op, state); |
| 186 | + print(concat_many( |
| 187 | + " step ", i, ": target_key=", target, |
| 188 | + " -> idx=", idx, |
| 189 | + " (key=", key_actual, ", op=", op_name(op), ")", |
| 190 | + " state ", prev, " -> ", state |
| 191 | + )); |
| 192 | + } else { |
| 193 | + print(concat_many(" step ", i, ": LIBRARY EXHAUSTED — chain breaks here.")); |
| 194 | + return state; |
| 195 | + } |
| 196 | + i = i + 1; |
| 197 | + } |
| 198 | + return state; |
| 199 | +} |
| 200 | + |
| 201 | +# Helper: build an all-alive mask. |
| 202 | +fn alive_all() -> array { |
| 203 | + h m = arr_new(LIB_SIZE, 0); |
| 204 | + h i = 0; |
| 205 | + while i < LIB_SIZE { |
| 206 | + arr_set(m, i, 1); |
| 207 | + i = i + 1; |
| 208 | + } |
| 209 | + return m; |
| 210 | +} |
| 211 | + |
| 212 | +# Helper: copy a mask and kill the entry at index `idx`. |
| 213 | +fn alive_with_kill(base, kill_idx) -> array { |
| 214 | + h m = arr_new(LIB_SIZE, 0); |
| 215 | + h i = 0; |
| 216 | + while i < LIB_SIZE { |
| 217 | + arr_set(m, i, arr_get(base, i)); |
| 218 | + i = i + 1; |
| 219 | + } |
| 220 | + arr_set(m, kill_idx, 0); |
| 221 | + return m; |
| 222 | +} |
| 223 | + |
| 224 | +# =========================================================================== |
| 225 | +# (A) COMPOSITION — run a chain, trace each step. |
| 226 | +# =========================================================================== |
| 227 | + |
| 228 | +print("== Experiment 6: Phi-Pi-Fib compression gate =="); |
| 229 | +print(""); |
| 230 | +print("Library: 12 primitive functions keyed by Fibonacci attractors"); |
| 231 | +print("Library keys = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]"); |
| 232 | +print(""); |
| 233 | + |
| 234 | +h alive = alive_all(); |
| 235 | +h chain_main = [3, 8, 13, 5, 21]; |
| 236 | +h initial = 7; |
| 237 | + |
| 238 | +print(concat_many("Chain: [3, 8, 13, 5, 21] initial state = ", initial)); |
| 239 | +print("Trace (target_key -> library lookup -> state transition):"); |
| 240 | +h final_main = run_chain_traced(initial, chain_main, alive); |
| 241 | +print(concat_many("Final state: ", final_main)); |
| 242 | +print(""); |
| 243 | + |
| 244 | +# =========================================================================== |
| 245 | +# (B) COMPRESSION — model storage vs equivalent direct lookup table. |
| 246 | +# =========================================================================== |
| 247 | + |
| 248 | +print("== (B) Compression analysis =="); |
| 249 | +print(""); |
| 250 | +print("Model storage (this prototype):"); |
| 251 | +h lib_entries = LIB_SIZE * 2; |
| 252 | +h chain_ints = arr_len(chain_main); |
| 253 | +h total_ints = lib_entries + chain_ints; |
| 254 | +print(concat_many(" library: ", LIB_SIZE, " entries x 2 ints = ", lib_entries, " ints")); |
| 255 | +print(concat_many(" chain: ", chain_ints, " ints")); |
| 256 | +print(concat_many(" total: ", total_ints, " ints (~", total_ints * 8, " bytes at i64)")); |
| 257 | +print(""); |
| 258 | +print("Equivalent dense (state -> output) lookup for state range [0, 1000]:"); |
| 259 | +print(" 1001 ints (~8008 bytes at i64)"); |
| 260 | +h ratio_times_100 = (1001 * 100) / total_ints; |
| 261 | +print(concat_many(" compression ratio: ", ratio_times_100, " / 100 (i.e. ~", ratio_times_100 / 100, "x smaller)")); |
| 262 | +print(""); |
| 263 | +print("Caveat: this prototype's input range is small; the compression ratio"); |
| 264 | +print("scales with the size of the implicit dense table. For LLM-scale inputs,"); |
| 265 | +print("the analogous comparison is library + chain (kilobytes) vs trillion-param"); |
| 266 | +print("weight matrix (terabytes) — 9 orders of magnitude apart in principle."); |
| 267 | +print(""); |
| 268 | + |
| 269 | +# =========================================================================== |
| 270 | +# (C) DEATH TOLERANCE — delete one library function at a time, re-run. |
| 271 | +# =========================================================================== |
| 272 | + |
| 273 | +print("== (C) Death tolerance: kill one library entry, re-run the same chain =="); |
| 274 | +print(""); |
| 275 | +print(concat_many("Baseline (all alive): final state = ", final_main)); |
| 276 | +print(""); |
| 277 | +print("Kill | final state | shift from baseline"); |
| 278 | +print("-----+-------------+--------------------"); |
| 279 | + |
| 280 | +h kill_idx = 0; |
| 281 | +while kill_idx < LIB_SIZE { |
| 282 | + h kill_mask = alive_with_kill(alive, kill_idx); |
| 283 | + h killed = run_chain(initial, chain_main, kill_mask); |
| 284 | + h shift = killed - final_main; |
| 285 | + if shift < 0 { shift = 0 - shift; } |
| 286 | + h kill_entry = library_entry(kill_idx); |
| 287 | + h kill_key = arr_get(kill_entry, 0); |
| 288 | + h kill_op_id = arr_get(kill_entry, 1); |
| 289 | + print(concat_many( |
| 290 | + " key=", kill_key, " (", op_name(kill_op_id), ")", |
| 291 | + " -> ", killed, |
| 292 | + " delta=", shift |
| 293 | + )); |
| 294 | + kill_idx = kill_idx + 1; |
| 295 | +} |
| 296 | +print(""); |
| 297 | +print("Every single library deletion completes the chain — no crashes, no"); |
| 298 | +print("undefined behaviour. The chain re-routes through the nearest surviving"); |
| 299 | +print("library entry. Output shifts continuously; the model 'acts as if it died'"); |
| 300 | +print("at that capability and falls back to the nearest available substitute."); |
| 301 | +print(""); |
| 302 | + |
| 303 | +# =========================================================================== |
| 304 | +# (D) INTERCHANGEABILITY — same library, different chains, different models. |
| 305 | +# =========================================================================== |
| 306 | + |
| 307 | +print("== (D) Interchangeable chains: same library, swap the 'parameters' =="); |
| 308 | +print(""); |
| 309 | + |
| 310 | +fn show_chain(label, keys, initial) { |
| 311 | + h all = alive_all(); |
| 312 | + h out = run_chain(initial, keys, all); |
| 313 | + print(concat_many(label, ": chain=", keys, " state ", initial, " -> ", out)); |
| 314 | +} |
| 315 | + |
| 316 | +print(concat_many("All chains start from state = ", initial)); |
| 317 | +show_chain("Model M1", [3, 8, 13, 5, 21], initial); |
| 318 | +show_chain("Model M2", [21, 5, 13, 8, 3], initial); # reverse order |
| 319 | +show_chain("Model M3", [1, 2, 3, 5, 8, 13, 21], initial); # ramp up |
| 320 | +show_chain("Model M4", [233, 144, 89, 55], initial); # high attractors |
| 321 | +show_chain("Model M5", [13, 13, 13, 13], initial); # single-op repeat |
| 322 | +show_chain("Model M6", [55, 21, 8, 3, 1], initial); # ramp down |
| 323 | +print(""); |
| 324 | +print("Six 'models' from one library, total storage 24 ints for the library"); |
| 325 | +print("plus 5-7 ints for each chain. Switching a model is a constant-time chain"); |
| 326 | +print("swap; the library never moves. The 'trillion-parameter' picture flips:"); |
| 327 | +print("permanent skills sit in the library, transient behaviour lives in chains."); |
| 328 | +print(""); |
| 329 | + |
| 330 | +print("== Caveats =="); |
| 331 | +print("- The chain is hand-authored here. In a real deployment, you need a"); |
| 332 | +print(" ROUTING POLICY that emits chains from inputs. That's the 'compression"); |
| 333 | +print(" gate' as a learnable component — future work."); |
| 334 | +print("- The library has 12 ops; a real one needs orders of magnitude more,"); |
| 335 | +print(" organised in a hierarchy. phi-pi-fib search becomes load-bearing"); |
| 336 | +print(" there: with 10^6 library entries, the gate must run in microseconds."); |
| 337 | +print("- Death tolerance gives graceful degradation, NOT preserved semantics."); |
| 338 | +print(" Removing a critical primitive can change output dramatically; the"); |
| 339 | +print(" guarantee is 'still deterministic and bounded', not 'still correct'."); |
| 340 | +print("== End =="); |
0 commit comments