v4/system/inference.c: v3's inference_engine.c and its Q48.16 arithmetic ported to the bit, no C library -- the variance, the window width (Levene), the decay slope (log-linear regression), the run with its 5% early exit. v4/tests/test_inference.c links v3's own and compares them: POST's fixed cases, 20000 random multiply/log pairs, 400 random trajectories (make -C v4 inference; in make -C v4 test). In each node's cycle every 1000 ticks (HEARTBEAT_INFERENCE_FREQUENCY), once the window is warm: the trajectory from the window's words' heat, the width and slope applied unless it exits early, then Loop #7 -- the heartrate slower when stable, faster when not, within 1/4..4x; Hera's re-arms the kernel's heartbeat (heartbeat_set_adaptive_period_ns), as v3's Hera. L8 reads its outcome. Test first: test_mesh_boot.c 72 checks, 80 on. amd64 PASS, 163 s. Not pushed (held to the end of Phase 7). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
82 lines
4.3 KiB
C
82 lines
4.3 KiB
C
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
|
|
* Copyright (c) 2023-2026 Robert A. James
|
|
*/
|
|
/* test_inference.c -- v4's inference engine (v4/inference.h) against v3's own, to the bit (#189, Phase 7,
|
|
* Task 3; Captain Bob, 2026-11-13: "Rewrite for v4", v3 the specification). v3's inference_engine.c and its
|
|
* Q48.16 code are linked in and run on the same trajectories: fixed ones, and many made at random. */
|
|
#include "v4/inference.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef uint64_t q48_16_t;
|
|
q48_16_t compute_variance_q48(const uint64_t *heat_data, uint64_t length);
|
|
uint32_t find_variance_inflection(const uint64_t *heat_data, uint64_t trajectory_length, q48_16_t full_variance);
|
|
uint64_t infer_decay_slope_q48(const uint64_t *heat_data, uint64_t length);
|
|
q48_16_t q48_log_approx(uint64_t x);
|
|
q48_16_t q48_mul(q48_16_t a, q48_16_t b);
|
|
|
|
/* what v3's engine reaches only through extract_heat_trajectory, which this test does not run */
|
|
void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id);
|
|
void *vm_dictionary_lookup_by_word_id(void *vm, uint32_t id) { (void)vm; (void)id; return 0; }
|
|
uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max);
|
|
uint64_t rolling_window_export_execution_history(const void *w, uint32_t *out, uint64_t max) { (void)w; (void)out; (void)max; return 0; }
|
|
void sf_mutex_lock(void *m);
|
|
void sf_mutex_lock(void *m) { (void)m; }
|
|
void sf_mutex_unlock(void *m);
|
|
void sf_mutex_unlock(void *m) { (void)m; }
|
|
|
|
static int failures = 0, checks = 0;
|
|
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
|
|
|
|
static uint32_t rng = 0x2545F491u;
|
|
static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5; return rng; }
|
|
|
|
static uint64_t h[4096];
|
|
|
|
static int same(uint64_t n)
|
|
{
|
|
return v4_inference_variance(h, n) == compute_variance_q48(h, n)
|
|
&& v4_inference_window_width(h, n) == find_variance_inflection(h, n, 0)
|
|
&& v4_inference_decay_slope(h, n) == infer_decay_slope_q48(h, n);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
unsigned k, bad = 0;
|
|
uint64_t x;
|
|
printf("v4 inference engine against v3's\n");
|
|
/* POST's own: 100 200 300, and 300 200 100 */
|
|
h[0] = 100; h[1] = 200; h[2] = 300;
|
|
CHECK(v4_inference_variance(h, 3) == compute_variance_q48(h, 3), "the variance of 100 200 300: %llu, v3 %llu",
|
|
(unsigned long long)v4_inference_variance(h, 3), (unsigned long long)compute_variance_q48(h, 3));
|
|
CHECK(v4_inference_window_width(h, 3) == find_variance_inflection(h, 3, 0), "its window width: %u, v3 %u",
|
|
v4_inference_window_width(h, 3), find_variance_inflection(h, 3, 0));
|
|
h[0] = 300; h[1] = 200; h[2] = 100;
|
|
CHECK(v4_inference_decay_slope(h, 3) == infer_decay_slope_q48(h, 3), "the decay slope of 300 200 100: %llu, v3 %llu",
|
|
(unsigned long long)v4_inference_decay_slope(h, 3), (unsigned long long)infer_decay_slope_q48(h, 3));
|
|
/* the arithmetic underneath */
|
|
for (k = 0; k < 20000; k++) {
|
|
uint64_t a = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40), b = ((uint64_t)rnd() << 32 | rnd()) >> (rnd() % 40);
|
|
if (v4_q48_mul(a, b) != q48_mul(a, b) || v4_q48_log(a) != q48_log_approx(a)) bad++;
|
|
}
|
|
CHECK(bad == 0, "Q48.16 multiply and log agree on 20000 random pairs (%u differ)", bad);
|
|
for (x = 0; x < 300000; x += 7) if (v4_q48_log(x) != q48_log_approx(x)) { bad++; break; }
|
|
CHECK(bad == 0, "and the log on every 7th value to 300000");
|
|
/* trajectories: short and long, flat, ramps, noise, bursts, zeros */
|
|
bad = 0;
|
|
for (k = 0; k < 400; k++) {
|
|
uint64_t n = 2 + rnd() % 4095, i, shape = k % 5, top = 1 + rnd() % (k % 3 == 0 ? 60u : 100000u);
|
|
for (i = 0; i < n; i++)
|
|
h[i] = shape == 0 ? top
|
|
: shape == 1 ? (n - i) * top / n
|
|
: shape == 2 ? rnd() % (top + 1)
|
|
: shape == 3 ? ((i / 64) % 2 ? top : rnd() % 3)
|
|
: (rnd() % 4 ? 0 : rnd() % (top + 1));
|
|
if (!same(n)) { bad++; if (bad < 4) printf(" differ: shape %u, n %lu\n", (unsigned)shape, (unsigned long)n); }
|
|
}
|
|
CHECK(bad == 0, "variance, window width and decay slope agree on 400 trajectories (%u differ)", bad);
|
|
printf(" %d checks, %d failures\n", checks, failures);
|
|
return failures ? 1 : 0;
|
|
}
|