interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / runtime / rx_cog_engines.c

519 lines · 17687 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * rx_cog_engines.c -- stand-in cognition engines. See rx_cog_engines.h.
19 */
20#include "rx_cog_engines.h"
21
22#include <math.h>
23#include <stdlib.h>
24#include <string.h>
25
26uint64_t cog_rng(uint64_t *s) {
27 uint64_t z = (*s += 0x9e3779b97f4a7c15ull);
28 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
29 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
30 return z ^ (z >> 31);
31}
32
33static float frand(uint64_t *s) { return (float)((cog_rng(s) >> 40) * (1.0 / 16777216.0)); }
34
35/* ---- recognition ---- */
36
37static double rec_f(const float *x) {
38 return 1.5 * x[0] * x[1] + sin(2.5 * x[2]) + 0.8 * x[3] * x[3] - 0.45 + 0.6 * x[4] -
39 0.9 * x[5] * x[6] + 0.3 * cos(3.0 * x[7]) + 0.25 * x[8] * x[9] * x[10];
40}
41
42int rec_truth(const RecInput *in) { return rec_f(in->x) > 0.0; }
43
44void rec_sample(uint64_t *rng, RecInput *in) {
45 for (int i = 0; i < COG_DIM; i++) in->x[i] = 2.0f * frand(rng) - 1.0f;
46}
47
48typedef struct {
49 int nin, h1, h2;
50 float *w1, *b1, *w2, *b2, *w3, b3;
51} Mlp;
52
53static float gauss(uint64_t *s) {
54 float a = frand(s) + 1e-7f, b = frand(s);
55 return sqrtf(-2.0f * logf(a)) * cosf(6.2831853f * b);
56}
57
58static void mlp_init(Mlp *m, int nin, int h1, int h2, uint64_t seed) {
59 free(m->w1);
60 memset(m, 0, sizeof *m);
61 m->nin = nin;
62 m->h1 = h1;
63 m->h2 = h2;
64 int last = h2 ? h2 : h1;
65 size_t total = (size_t)nin * h1 + h1 + (size_t)h1 * h2 + h2 + last;
66 float *p = calloc(total, sizeof(float));
67 m->w1 = p;
68 p += (size_t)nin * h1;
69 m->b1 = p;
70 p += h1;
71 m->w2 = p;
72 p += (size_t)h1 * h2;
73 m->b2 = p;
74 p += h2;
75 m->w3 = p;
76 uint64_t s = seed;
77 for (int i = 0; i < nin * h1; i++) m->w1[i] = gauss(&s) / sqrtf((float)nin);
78 for (int i = 0; i < h1 * h2; i++) m->w2[i] = gauss(&s) / sqrtf((float)h1);
79 for (int i = 0; i < last; i++) m->w3[i] = gauss(&s) / sqrtf((float)last);
80}
81
82static size_t mlp_params(const Mlp *m) {
83 int last = m->h2 ? m->h2 : m->h1;
84 return (size_t)m->nin * m->h1 + m->h1 + (size_t)m->h1 * m->h2 + m->h2 + last + 1;
85}
86
87/* Returns the logit; fills hidden activations when given. */
88static float mlp_forward(const Mlp *m, const float *x, float *a1, float *a2) {
89 float t1[256], t2[256];
90 if (!a1) a1 = t1;
91 if (!a2) a2 = t2;
92 for (int j = 0; j < m->h1; j++) {
93 const float *w = m->w1 + (size_t)j * m->nin;
94 float s = m->b1[j];
95 for (int i = 0; i < m->nin; i++) s += w[i] * x[i];
96 a1[j] = tanhf(s);
97 }
98 const float *top = a1;
99 int last = m->h1;
100 if (m->h2) {
101 for (int k = 0; k < m->h2; k++) {
102 const float *w = m->w2 + (size_t)k * m->h1;
103 float s = m->b2[k];
104 for (int j = 0; j < m->h1; j++) s += w[j] * a1[j];
105 a2[k] = tanhf(s);
106 }
107 top = a2;
108 last = m->h2;
109 }
110 float s = m->b3;
111 for (int k = 0; k < last; k++) s += m->w3[k] * top[k];
112 return s;
113}
114
115static float sigmoid(float z) { return 1.0f / (1.0f + expf(-z)); }
116
117static void mlp_step(Mlp *m, const float *x, int y, float lr) {
118 float a1[256], a2[256], d1[256], d2[256];
119 float z = mlp_forward(m, x, a1, a2);
120 float g = sigmoid(z) - (float)y; /* d loss / d logit */
121 const float *top = m->h2 ? a2 : a1;
122 int last = m->h2 ? m->h2 : m->h1;
123 float *dtop = m->h2 ? d2 : d1;
124 for (int k = 0; k < last; k++) {
125 dtop[k] = g * m->w3[k] * (1.0f - top[k] * top[k]);
126 m->w3[k] -= lr * g * top[k];
127 }
128 m->b3 -= lr * g;
129 if (m->h2) {
130 for (int j = 0; j < m->h1; j++) d1[j] = 0;
131 for (int k = 0; k < m->h2; k++) {
132 float *w = m->w2 + (size_t)k * m->h1;
133 for (int j = 0; j < m->h1; j++) {
134 d1[j] += d2[k] * w[j];
135 w[j] -= lr * d2[k] * a1[j];
136 }
137 m->b2[k] -= lr * d2[k];
138 }
139 for (int j = 0; j < m->h1; j++) d1[j] *= 1.0f - a1[j] * a1[j];
140 }
141 for (int j = 0; j < m->h1; j++) {
142 float *w = m->w1 + (size_t)j * m->nin;
143 for (int i = 0; i < m->nin; i++) w[i] -= lr * d1[j] * x[i];
144 m->b1[j] -= lr * d1[j];
145 }
146}
147
148/* Small policy: logistic regression over x and x^2. */
149static float g_policy_w[2 * COG_DIM + 1];
150
151static void policy_features(const float *x, float *phi) {
152 for (int i = 0; i < COG_DIM; i++) {
153 phi[i] = x[i];
154 phi[COG_DIM + i] = x[i] * x[i];
155 }
156}
157
158static float policy_logit(const float *x) {
159 float phi[2 * COG_DIM];
160 policy_features(x, phi);
161 float s = g_policy_w[2 * COG_DIM];
162 for (int i = 0; i < 2 * COG_DIM; i++) s += g_policy_w[i] * phi[i];
163 return s;
164}
165
166static Mlp g_neural, g_neural_backup_shape, g_general, g_ens_a, g_ens_b;
167static float *g_neural_backup;
168
169void cog_engines_train(uint64_t seed, uint32_t examples, uint32_t epochs) {
170 RecInput *xs = malloc(examples * sizeof(RecInput));
171 uint8_t *ys = malloc(examples);
172 uint64_t rng = seed;
173 for (uint32_t i = 0; i < examples; i++) {
174 rec_sample(&rng, &xs[i]);
175 ys[i] = (uint8_t)rec_truth(&xs[i]);
176 }
177 mlp_init(&g_neural, COG_DIM, 12, 0, seed ^ 1);
178 mlp_init(&g_general, COG_DIM, 96, 96, seed ^ 2);
179 mlp_init(&g_ens_a, COG_DIM, 64, 64, seed ^ 3);
180 mlp_init(&g_ens_b, COG_DIM, 64, 64, seed ^ 4);
181 memset(g_policy_w, 0, sizeof g_policy_w);
182 uint32_t *order = malloc(examples * sizeof(uint32_t));
183 for (uint32_t i = 0; i < examples; i++) order[i] = i;
184 for (uint32_t e = 0; e < epochs; e++) {
185 for (uint32_t i = examples - 1; i > 0; i--) {
186 uint32_t j = (uint32_t)(cog_rng(&rng) % (i + 1));
187 uint32_t t = order[i];
188 order[i] = order[j];
189 order[j] = t;
190 }
191 float lr = 0.05f / (1.0f + 0.5f * (float)e);
192 for (uint32_t k = 0; k < examples; k++) {
193 const RecInput *x = &xs[order[k]];
194 int y = ys[order[k]];
195 float phi[2 * COG_DIM];
196 policy_features(x->x, phi);
197 float g = sigmoid(policy_logit(x->x)) - (float)y;
198 for (int i = 0; i < 2 * COG_DIM; i++) g_policy_w[i] -= lr * g * phi[i];
199 g_policy_w[2 * COG_DIM] -= lr * g;
200 mlp_step(&g_neural, x->x, y, lr);
201 mlp_step(&g_general, x->x, y, lr);
202 mlp_step(&g_ens_a, x->x, y, lr);
203 mlp_step(&g_ens_b, x->x, y, lr);
204 }
205 }
206 free(order);
207 free(xs);
208 free(ys);
209 size_t n = mlp_params(&g_neural);
210 free(g_neural_backup);
211 g_neural_backup = malloc(n * sizeof(float));
212 memcpy(g_neural_backup, g_neural.w1, (n - 1) * sizeof(float));
213 g_neural_backup[n - 1] = g_neural.b3;
214 g_neural_backup_shape = g_neural;
215}
216
217void cog_damage_neural(uint64_t seed, float scale) {
218 uint64_t s = seed;
219 size_t n = (size_t)g_neural.nin * g_neural.h1;
220 for (size_t i = 0; i < n; i++) g_neural.w1[i] += scale * gauss(&s);
221}
222
223void cog_restore_neural(void) {
224 size_t n = mlp_params(&g_neural);
225 memcpy(g_neural.w1, g_neural_backup, (n - 1) * sizeof(float));
226 g_neural.b3 = g_neural_backup[n - 1];
227}
228
229static uint32_t conf_of(float p) {
230 float c = p > 0.5f ? p : 1.0f - p;
231 uint32_t v = (uint32_t)(c * (float)RX_COG_CONF_ONE);
232 return v > RX_COG_CONF_ONE ? RX_COG_CONF_ONE : v;
233}
234
235static int rec_answer(float p, void *output, uint32_t *conf) {
236 ((RecOutput *)output)->label = p > 0.5f;
237 *conf = conf_of(p);
238 return 0;
239}
240
241static int eng_policy(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
242 (void)ctx;
243 if (op != RX_COG_OP_RECOGNIZE) return -1;
244 return rec_answer(sigmoid(policy_logit(((const RecInput *)input)->x)), output, conf);
245}
246
247static int eng_neural(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
248 (void)ctx;
249 if (op != RX_COG_OP_RECOGNIZE) return -1;
250 return rec_answer(sigmoid(mlp_forward(&g_neural, ((const RecInput *)input)->x, NULL, NULL)),
251 output, conf);
252}
253
254static int eng_general(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
255 (void)ctx;
256 if (op != RX_COG_OP_RECOGNIZE) return -1;
257 return rec_answer(sigmoid(mlp_forward(&g_general, ((const RecInput *)input)->x, NULL, NULL)),
258 output, conf);
259}
260
261static int eng_ensemble(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
262 (void)ctx;
263 if (op != RX_COG_OP_RECOGNIZE) return -1;
264 const float *x = ((const RecInput *)input)->x;
265 float p = (sigmoid(mlp_forward(&g_general, x, NULL, NULL)) +
266 sigmoid(mlp_forward(&g_ens_a, x, NULL, NULL)) +
267 sigmoid(mlp_forward(&g_ens_b, x, NULL, NULL)) +
268 sigmoid(mlp_forward(&g_neural, x, NULL, NULL))) / 4.0f;
269 return rec_answer(p, output, conf);
270}
271
272/* ---- planning ---- */
273
274void plan_map(uint64_t seed, uint8_t *blocked) {
275 uint64_t s = seed;
276 for (int i = 0; i < GRID_CELLS; i++) blocked[i] = frand(&s) < 0.28f;
277}
278
279static int manhattan(int a, int b) {
280 return abs(a % GRID_W - b % GRID_W) + abs(a / GRID_W - b / GRID_W);
281}
282
283void plan_sample(uint64_t *rng, const uint8_t *blocked, PlanInput *in) {
284 for (;;) {
285 int a = (int)(cog_rng(rng) % GRID_CELLS), b = (int)(cog_rng(rng) % GRID_CELLS);
286 if (blocked[a] || blocked[b]) continue;
287 int d = manhattan(a, b);
288 if (d < 20 || d > 90) continue;
289 in->blocked = blocked;
290 in->start = (uint16_t)a;
291 in->goal = (uint16_t)b;
292 in->bound = (uint16_t)(d + (int)(cog_rng(rng) % (uint64_t)(d / 2 + 1)));
293 return;
294 }
295}
296
297static int neighbours(int c, int *out) {
298 int x = c % GRID_W, y = c / GRID_W, n = 0;
299 if (x > 0) out[n++] = c - 1;
300 if (x < GRID_W - 1) out[n++] = c + 1;
301 if (y > 0) out[n++] = c - GRID_W;
302 if (y < GRID_H - 1) out[n++] = c + GRID_W;
303 return n;
304}
305
306/* Scratch shared by the planners; stamps avoid clearing per call. */
307static uint32_t g_stamp, g_seen[GRID_CELLS];
308static int32_t g_parent[GRID_CELLS], g_cost[GRID_CELLS];
309static int32_t g_heap[GRID_CELLS * 4], g_key[GRID_CELLS * 4];
310static int g_heap_n;
311
312static void heap_push(int cell, int key) {
313 int i = g_heap_n++;
314 while (i > 0) {
315 int p = (i - 1) / 2;
316 if (g_key[p] <= key) break;
317 g_heap[i] = g_heap[p];
318 g_key[i] = g_key[p];
319 i = p;
320 }
321 g_heap[i] = cell;
322 g_key[i] = key;
323}
324
325static int heap_pop(void) {
326 int top = g_heap[0];
327 int cell = g_heap[--g_heap_n], key = g_key[g_heap_n], i = 0;
328 for (;;) {
329 int l = 2 * i + 1, r = l + 1, m = i;
330 int mk = key;
331 if (l < g_heap_n && g_key[l] < mk) m = l, mk = g_key[l];
332 if (r < g_heap_n && g_key[r] < mk) m = r;
333 if (m == i) break;
334 g_heap[i] = g_heap[m];
335 g_key[i] = g_key[m];
336 i = m;
337 }
338 g_heap[i] = cell;
339 g_key[i] = key;
340 return top;
341}
342
343static int emit_path(const PlanInput *in, PlanOutput *out) {
344 int n = 0;
345 for (int c = in->goal; c != in->start; c = g_parent[c]) {
346 if (n >= PATH_MAX_STEPS) return -1;
347 out->path[n++] = (uint16_t)c;
348 }
349 for (int i = 0; i < n / 2; i++) {
350 uint16_t t = out->path[i];
351 out->path[i] = out->path[n - 1 - i];
352 out->path[n - 1 - i] = t;
353 }
354 out->len = (uint16_t)n;
355 return 0;
356}
357
358/* The check a verifiable planner runs before it claims a path. */
359static int path_valid(const PlanInput *in, const PlanOutput *out) {
360 if (out->len > in->bound || out->len == 0) return 0;
361 int at = in->start;
362 for (int i = 0; i < out->len; i++) {
363 int c = out->path[i];
364 if (c >= GRID_CELLS || in->blocked[c] || manhattan(at, c) != 1) return 0;
365 at = c;
366 }
367 return at == in->goal;
368}
369
370/* Best-first search on key = weight_g * g + h, stopping after `budget`
371 * expansions. weight_g 0 is greedy. */
372static int heuristic_plan(const PlanInput *in, PlanOutput *out, uint32_t *conf, int weight_g,
373 int weight_h, uint32_t budget) {
374 g_stamp++;
375 g_heap_n = 0;
376 g_seen[in->start] = g_stamp;
377 g_cost[in->start] = 0;
378 heap_push(in->start, weight_h * manhattan(in->start, in->goal));
379 uint32_t expanded = 0;
380 out->status = -1;
381 out->len = 0;
382 *conf = 0;
383 while (g_heap_n && expanded < budget) {
384 int c = heap_pop();
385 expanded++;
386 if (c == in->goal) {
387 if (emit_path(in, out) == 0 && path_valid(in, out)) {
388 out->status = 1;
389 *conf = RX_COG_CONF_ONE;
390 } else {
391 out->len = 0;
392 }
393 return 0;
394 }
395 int nb[4], k = neighbours(c, nb);
396 for (int i = 0; i < k; i++) {
397 int d = nb[i];
398 if (in->blocked[d]) continue;
399 int g = g_cost[c] + 1;
400 if (g_seen[d] == g_stamp && g_cost[d] <= g) continue;
401 if (g > in->bound) continue;
402 g_seen[d] = g_stamp;
403 g_cost[d] = g;
404 g_parent[d] = c;
405 if (g_heap_n < GRID_CELLS * 4) heap_push(d, weight_g * g + weight_h * manhattan(d, in->goal));
406 }
407 }
408 return 0;
409}
410
411static int eng_greedy(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
412 (void)ctx;
413 if (op != RX_COG_OP_PLAN) return -1;
414 const PlanInput *in = input;
415 return heuristic_plan(in, output, conf, 0, 1, 4u * in->bound);
416}
417
418static int eng_focused(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
419 (void)ctx;
420 if (op != RX_COG_OP_PLAN) return -1;
421 const PlanInput *in = input;
422 return heuristic_plan(in, output, conf, 4, 5, 24u * in->bound);
423}
424
425/* Exhaustive breadth-first exploration to depth `bound`: exact either way. */
426static int32_t g_queue[GRID_CELLS];
427
428static int eng_jspace(void *ctx, uint32_t op, const void *input, void *output, uint32_t *conf) {
429 (void)ctx;
430 if (op != RX_COG_OP_PLAN) return -1;
431 const PlanInput *in = input;
432 PlanOutput *out = output;
433 g_stamp++;
434 int head = 0, tail = 0;
435 g_queue[tail++] = in->start;
436 g_seen[in->start] = g_stamp;
437 g_cost[in->start] = 0;
438 out->len = 0;
439 while (head < tail) {
440 int c = g_queue[head++];
441 if (c == in->goal) {
442 if (emit_path(in, out) != 0 || !path_valid(in, out)) return -1;
443 out->status = 1;
444 *conf = RX_COG_CONF_ONE;
445 return 0;
446 }
447 if (g_cost[c] >= in->bound) continue;
448 int nb[4], k = neighbours(c, nb);
449 for (int i = 0; i < k; i++) {
450 int d = nb[i];
451 if (in->blocked[d] || g_seen[d] == g_stamp) continue;
452 g_seen[d] = g_stamp;
453 g_cost[d] = g_cost[c] + 1;
454 g_parent[d] = c;
455 g_queue[tail++] = d;
456 }
457 }
458 out->status = 0;
459 *conf = RX_COG_CONF_ONE;
460 return 0;
461}
462
463int plan_truth(const PlanInput *in) {
464 PlanInput wide = *in;
465 wide.bound = GRID_CELLS - 1;
466 static PlanOutput out;
467 uint32_t conf;
468 if (eng_jspace(NULL, RX_COG_OP_PLAN, &wide, &out, &conf) != 0) return -2;
469 return out.status == 1 ? out.len : -1;
470}
471
472int plan_correct(const PlanInput *in, const PlanOutput *out, int truth_len) {
473 if (out->status == 1) return path_valid(in, out);
474 if (out->status == 0) return truth_len == -1 || truth_len > in->bound; /* -2: truth unknown */
475 return 0;
476}
477
478/* ---- registration ---- */
479
480RxCogEngineFn cog_engine_fn(uint32_t id) {
481 switch (id) {
482 case ENG_POLICY: return eng_policy;
483 case ENG_NEURAL: return eng_neural;
484 case ENG_GENERAL: return eng_general;
485 case ENG_ENSEMBLE: return eng_ensemble;
486 case ENG_GREEDY: return eng_greedy;
487 case ENG_FOCUSED: return eng_focused;
488 case ENG_JSPACE: return eng_jspace;
489 }
490 return NULL;
491}
492
493int cog_register_all(RxCogRouter *r) {
494 const uint32_t REC = 1u << RX_COG_OP_RECOGNIZE, PLAN = 1u << RX_COG_OP_PLAN;
495 const uint64_t grid = GRID_CELLS * 16ull;
496 struct {
497 RxCogDeclaration d;
498 uint32_t reference;
499 } t[] = {
500 {{ENG_POLICY, RX_COG_CLASS_SMALL_POLICY, REC, 0, (2 * COG_DIM + 1) * 4ull, 0, 0,
501 RX_COG_PREC_APPROX, 0}, 0},
502 {{ENG_NEURAL, RX_COG_CLASS_NEURAL_MODULE, REC, 0, mlp_params(&g_neural) * 4ull, 0, 0,
503 RX_COG_PREC_APPROX, 0}, 0},
504 {{ENG_GENERAL, RX_COG_CLASS_GENERAL, REC, RX_COG_HW_CPU_P, mlp_params(&g_general) * 4ull, 0, 0,
505 RX_COG_PREC_APPROX, 0}, REC},
506 {{ENG_ENSEMBLE, RX_COG_CLASS_ENSEMBLE, REC, RX_COG_HW_CPU_P,
507 (mlp_params(&g_general) + 2 * mlp_params(&g_ens_a) + mlp_params(&g_neural)) * 4ull, 0, 0,
508 RX_COG_PREC_APPROX, 0}, 0},
509 {{ENG_GREEDY, RX_COG_CLASS_DETERMINISTIC, PLAN, 0, grid, 0, 256, RX_COG_PREC_EXACT, 1}, 0},
510 {{ENG_FOCUSED, RX_COG_CLASS_DETERMINISTIC, PLAN, 0, grid, 0, 1024, RX_COG_PREC_EXACT, 1}, 0},
511 {{ENG_JSPACE, RX_COG_CLASS_JSPACE, PLAN, 0, grid, 0, GRID_CELLS, RX_COG_PREC_EXACT, 1}, PLAN},
512 };
513 for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) {
514 int rc = rx_route_register(r, &t[i].d, cog_engine_fn(t[i].d.cognitive_engine_id), NULL,
515 t[i].reference);
516 if (rc != RX_COG_OK) return rc;
517 }
518 return RX_COG_OK;
519}