From 6d954ac279dc52a58315a8f84127f55c57dcdbb2 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Nov 2026 23:59:18 +0000 Subject: [PATCH] feat(v4): Phase 7 Task 3 -- the inference engine and Loop #7 in each node's heartbeat cycle (#189) 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 Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm --- ...ngine-step4-phase7-the-loops-and-parity.md | 9 +- .../runs/doe-amd64-20261113-235618.csv | 0 kernel/Makefile | 4 +- kernel/src/v4/sk_v4.c | 3 + logs/20261113-235618/amd64/README.md | 1 + .../amd64/qemu-amd64-20261113-235618.log | 3 + v4/Makefile | 22 +- v4/include/v4/inference.h | 41 ++++ v4/include/v4/mesh.h | 16 +- v4/system/inference.c | 193 ++++++++++++++++++ v4/system/mesh.c | 42 +++- v4/tests/test_inference.c | 81 ++++++++ v4/tests/test_mesh_boot.c | 33 ++- 13 files changed, 434 insertions(+), 14 deletions(-) create mode 100644 experiments/bare_metal/runs/doe-amd64-20261113-235618.csv create mode 100644 logs/20261113-235618/amd64/README.md create mode 100644 logs/20261113-235618/amd64/qemu-amd64-20261113-235618.log create mode 100644 v4/include/v4/inference.h create mode 100644 v4/system/inference.c create mode 100644 v4/tests/test_inference.c diff --git a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md index a912995e..bb0f09cf 100644 --- a/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md +++ b/docs/v4.0.0/plans/2026-11-13-engine-step4-phase7-the-loops-and-parity.md @@ -128,7 +128,14 @@ Phase 4). dictionary, so a word not called cools. The window's adaptive pass (v3's `rolling_window_run_adaptive_pass`): diversity against the last; halve to 256, double to 4096. Test first (`test_mesh_boot.c`: 62 checks; 70 on). -- [ ] Inference every 1000 ticks (v3's `vm_tick_inference_engine`, `inference_engine.c`). +- [x] Inference every 1000 ticks (v3's `vm_tick_inference_engine`): `v4/system/inference.c`, v3's + `inference_engine.c` and Q48.16 arithmetic ported to the bit -- `v4/tests/test_inference.c` links + v3's own and compares them on POST's cases, 20000 random multiply and log pairs, and 400 + trajectories (`make -C v4 inference`). The trajectory from the window's newest words' heat; the + window width and slope applied unless it exits early (as v3, the last inferred slope re-applied + on a full run even with L6 off). Loop #7: the heartrate 25% slower when stable, 20% faster when + not, within 1/4..4x; Hera's re-arms the kernel's heartbeat. Test first (72 checks; 80 on). + amd64 PASS, 163 s. - [x] L8's update (v3's `vm_heartbeat_update_l8` with its table, `ssm_jacquard.c`, `compudynamics.c`): the regime (window entropy >= 0.75, prediction CV >= 0.15, locality >= 1/2, as exact integer comparisons), the card scored (ANOVA from inference; joint convergence when L5 or L6), the next by diff --git a/experiments/bare_metal/runs/doe-amd64-20261113-235618.csv b/experiments/bare_metal/runs/doe-amd64-20261113-235618.csv new file mode 100644 index 00000000..e69de29b diff --git a/kernel/Makefile b/kernel/Makefile index cca892f6..d40cb7e8 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -737,8 +737,8 @@ KERNEL_EXTRA_SRCS += $(KERNEL_SRC)/v4/sk_v4.c $(KERNEL_SRC)/v4/sk_v4_identity.c # v4/system/boot.c is the boot the hosted v4 system runs too: the ROM # assembles the system, then the capsules from this kernel's own capsule # directory, then the prompt. -LOADER_V4_OBJS := $(patsubst v4/src/%.c,$(LOADER_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(LOADER_OBJ_DIR)/v4engine/v4_rom.o $(LOADER_OBJ_DIR)/v4engine/v4_map.o $(LOADER_OBJ_DIR)/v4engine/v4_mesh_rom.o $(LOADER_OBJ_DIR)/v4system/mesh.o $(LOADER_OBJ_DIR)/v4system/boot.o $(LOADER_OBJ_DIR)/v4system/blocks.o $(LOADER_OBJ_DIR)/v4system/post.o $(LOADER_OBJ_DIR)/v4system/post_cases.o -KERNEL_V4_OBJS := $(patsubst v4/src/%.c,$(KERNEL_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(KERNEL_OBJ_DIR)/v4engine/v4_rom.o $(KERNEL_OBJ_DIR)/v4engine/v4_map.o $(KERNEL_OBJ_DIR)/v4engine/v4_mesh_rom.o $(KERNEL_OBJ_DIR)/v4system/mesh.o $(KERNEL_OBJ_DIR)/v4system/boot.o $(KERNEL_OBJ_DIR)/v4system/blocks.o $(KERNEL_OBJ_DIR)/v4system/post.o $(KERNEL_OBJ_DIR)/v4system/post_cases.o +LOADER_V4_OBJS := $(patsubst v4/src/%.c,$(LOADER_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(LOADER_OBJ_DIR)/v4engine/v4_rom.o $(LOADER_OBJ_DIR)/v4engine/v4_map.o $(LOADER_OBJ_DIR)/v4engine/v4_mesh_rom.o $(LOADER_OBJ_DIR)/v4system/mesh.o $(LOADER_OBJ_DIR)/v4system/inference.o $(LOADER_OBJ_DIR)/v4system/boot.o $(LOADER_OBJ_DIR)/v4system/blocks.o $(LOADER_OBJ_DIR)/v4system/post.o $(LOADER_OBJ_DIR)/v4system/post_cases.o +KERNEL_V4_OBJS := $(patsubst v4/src/%.c,$(KERNEL_OBJ_DIR)/v4engine/%.o,$(V4_ENGINE_SRCS)) $(KERNEL_OBJ_DIR)/v4engine/v4_rom.o $(KERNEL_OBJ_DIR)/v4engine/v4_map.o $(KERNEL_OBJ_DIR)/v4engine/v4_mesh_rom.o $(KERNEL_OBJ_DIR)/v4system/mesh.o $(KERNEL_OBJ_DIR)/v4system/inference.o $(KERNEL_OBJ_DIR)/v4system/boot.o $(KERNEL_OBJ_DIR)/v4system/blocks.o $(KERNEL_OBJ_DIR)/v4system/post.o $(KERNEL_OBJ_DIR)/v4system/post_cases.o endif LOADER_SRCS := $(LOADER_SRCS_BASE) $(LOADER_EXTRA_SRCS) diff --git a/kernel/src/v4/sk_v4.c b/kernel/src/v4/sk_v4.c index 0b25c6b3..3187ae0f 100644 --- a/kernel/src/v4/sk_v4.c +++ b/kernel/src/v4/sk_v4.c @@ -88,6 +88,8 @@ static void *sk_v4_alloc(void *self, unsigned long bytes) return p; } static void sk_v4_release(void *self, void *p) { (void)self; kfree(p); } +/* Loop #7 on Hera: the kernel's heartbeat re-armed at the period her inference sets, as v3's Hera does */ +static void sk_v4_heartrate(void *self, uint64_t ns) { (void)self; heartbeat_set_adaptive_period_ns(ns); } static void sk_v4_charge_one(void *ctx, v4_cell id, v4_uheat_t calls) { (void)ctx; @@ -322,6 +324,7 @@ void sk_v4_boot(void (*chain)(void), const BootDisk *boot_disk) sk_v4_mesh.forgot = sk_v4_forgot; sk_v4_mesh.physics = sk_v4_physics; sk_v4_mesh.alloc = sk_v4_alloc; sk_v4_mesh.release = sk_v4_release; + sk_v4_mesh.heartrate = sk_v4_heartrate; sk_v4_mesh.out = sk_v4_out; sk_v4_mesh.key = sk_v4_key; sk_v4_mesh.words = sk_v4_words; sk_v4_mesh.word_count = sizeof sk_v4_words / sizeof sk_v4_words[0]; sk_v4_mesh.serve = sk_v4_serve; diff --git a/logs/20261113-235618/amd64/README.md b/logs/20261113-235618/amd64/README.md new file mode 100644 index 00000000..b9281f64 --- /dev/null +++ b/logs/20261113-235618/amd64/README.md @@ -0,0 +1 @@ +Development boot, not acceptance evidence (2026-11-13): amd64 with #189 Phase 7 Task 3 -- inference every 1000 ticks and Loop #7 (Hera re-arming the kernel heartbeat). RESULT: PASS; POST 539/539; Stadium words promotions=141, CONSERVED; 163 s. The protocol run is the evidence. diff --git a/logs/20261113-235618/amd64/qemu-amd64-20261113-235618.log b/logs/20261113-235618/amd64/qemu-amd64-20261113-235618.log new file mode 100644 index 00000000..ea291e4d --- /dev/null +++ b/logs/20261113-235618/amd64/qemu-amd64-20261113-235618.log @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:056d44c480ff8d54a2b5a854539fa3963550a966ff2f404750effe2971bc7912 +size 9471 diff --git a/v4/Makefile b/v4/Makefile index 8984d1f4..ef577337 100644 --- a/v4/Makefile +++ b/v4/Makefile @@ -354,13 +354,14 @@ sanitize: $(foreach w,$(WIDTHS),$(foreach t,$(TESTS),san-$(w)-$(notdir $(t)))) s # THE MESH BOOT (system/mesh.c; #189, ENGINE step 4 Phase 1, Task 5b): the kernel's boot of the # fabric, tested on the host with tests/test_mesh_boot.c as its owner. Linked as the hosted product # is, with the registry (src/manage.c) and a mesh node's ROM; 64-bit cells, as the capsules are. -MESH_BOOT_SRCS := $(HERE)/system/mesh.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c +MESH_BOOT_SRCS := $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(BINDIR)/test_mesh_boot: $(HERE)/tests/test_mesh_boot.c $(MESH_BOOT_SRCS) $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BLOCKS_SRC) $(V3_BLOCK_SRCS) $(wildcard $(HERE)/include/v4/*.h) $(HERE)/Makefile $(CC) $(CFLAGS) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot.o $(CC) $(SYSTEM_CFLAGS) -c $(HERE)/system/mesh.c -o $(BINDIR)/mesh-64.o + $(CC) $(SYSTEM_CFLAGS) -c $(HERE)/system/inference.c -o $(BINDIR)/inference-64.o @rm -rf $(BINDIR)/v3o-mesh && mkdir -p $(BINDIR)/v3o-mesh cd $(BINDIR)/v3o-mesh && $(CC) $(V3_CFLAGS) -I$(HERE)/include -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(V3_BLOCK_SRCS) $(BLOCKS_SRC) - $(CC) $(SYSTEM_CFLAGS) $(BINDIR)/test_mesh_boot.o $(BINDIR)/mesh-64.o $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@ + $(CC) $(SYSTEM_CFLAGS) $(BINDIR)/test_mesh_boot.o $(BINDIR)/mesh-64.o $(BINDIR)/inference-64.o $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@ .PHONY: mesh-boot mesh-boot: $(BINDIR)/test_mesh_boot @@ -371,7 +372,7 @@ mesh-boot: $(BINDIR)/test_mesh_boot MESH_ON := -DENABLE_PIPELINING=1 -DENABLE_HOTWORDS_CACHE=1 $(BINDIR)/test_mesh_boot_on: $(BINDIR)/test_mesh_boot $(CC) $(CFLAGS) $(MESH_ON) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/test_mesh_boot_on.o - $(CC) $(SYSTEM_CFLAGS) $(MESH_ON) $(BINDIR)/test_mesh_boot_on.o $(HERE)/system/mesh.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@ + $(CC) $(SYSTEM_CFLAGS) $(MESH_ON) $(BINDIR)/test_mesh_boot_on.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/v3o-mesh/*.o -o $@ .PHONY: mesh-boot-on mesh-boot-on: $(BINDIR)/test_mesh_boot_on @@ -384,14 +385,25 @@ $(BINDIR)/san-test_mesh_boot: $(BINDIR)/test_mesh_boot $(CC) $(CFLAGS) $(MESH_SAN) -D_POSIX_C_SOURCE=200809L -I$(HERE)/include $(V3_INC) -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(HERE)/tests/test_mesh_boot.c -o $(BINDIR)/san-test_mesh_boot.o @rm -rf $(BINDIR)/san-v3o-mesh && mkdir -p $(BINDIR)/san-v3o-mesh cd $(BINDIR)/san-v3o-mesh && $(CC) $(V3_CFLAGS) $(MESH_SAN) -I$(HERE)/include -DV4_CELL_BITS=64 $(HOST_DEFS) -c $(V3_BLOCK_SRCS) $(BLOCKS_SRC) - $(CC) $(SYSTEM_CFLAGS) $(MESH_SAN) $(BINDIR)/san-test_mesh_boot.o $(HERE)/system/mesh.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/san-v3o-mesh/*.o -o $@ + $(CC) $(SYSTEM_CFLAGS) $(MESH_SAN) $(BINDIR)/san-test_mesh_boot.o $(HERE)/system/mesh.c $(HERE)/system/inference.c $(HERE)/src/manage.c $(BINDIR)/mesh-rom-64.c $(MACHINE_64) $(CAPSULE_DIR_C) $(ENGINE_SRCS) $(SYSTEM_SRCS) $(BINDIR)/san-v3o-mesh/*.o -o $@ .PHONY: san-mesh-boot san-mesh-boot: $(BINDIR)/san-test_mesh_boot @echo " [ASan+UBSan mesh boot, V4_CELL_BITS=64]" @$(BINDIR)/san-test_mesh_boot -test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on +# THE INFERENCE ENGINE against v3's own (v4/inference.h; #189 Phase 7): v3's inference_engine.c and its Q48.16 +# code linked in, the two compared to the bit. +$(BINDIR)/test_inference: $(HERE)/tests/test_inference.c $(HERE)/system/inference.c $(HERE)/include/v4/inference.h $(ROOT_DIR)/v3/src/inference_engine.c $(ROOT_DIR)/v3/src/word_source/q48_16_words.c $(HERE)/Makefile + @mkdir -p $(BINDIR)/v3o-inf + cd $(BINDIR)/v3o-inf && $(CC) $(V3_CFLAGS) -c $(ROOT_DIR)/v3/src/inference_engine.c $(ROOT_DIR)/v3/src/word_source/q48_16_words.c + $(CC) $(CFLAGS) -I$(HERE)/include $(HERE)/tests/test_inference.c $(HERE)/system/inference.c $(BINDIR)/v3o-inf/*.o -o $@ + +.PHONY: inference +inference: $(BINDIR)/test_inference + @$(BINDIR)/test_inference + +test: $(addprefix test-,$(WIDTHS)) mesh-boot mesh-boot-on inference @echo "all v4 tests passed" clean: diff --git a/v4/include/v4/inference.h b/v4/include/v4/inference.h new file mode 100644 index 00000000..ae04e6f6 --- /dev/null +++ b/v4/include/v4/inference.h @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: LicenseRef-Starship-1.0 + * Copyright (c) 2023-2026 Robert A. James + */ +/* inference.h -- the inference engine (Loops #5 and #6), for a node's heartbeat (v4/mesh.h): #189 Phase 7, + * Task 3 (Captain Bob, 2026-11-13: "Rewrite for v4"). v3's inference_engine.c, in its own Q48.16 arithmetic, + * to the bit: what it computes from a heat trajectory -- the variance, the window width where the variance + * stops changing (Levene), the decay slope (a log-linear regression) -- and the run that applies them, with its + * early exit when the variance has moved less than 5%. v3's results are the specification; + * v4/tests/test_inference.c compares the two. + * + * Nothing here uses the C library; the caller supplies the scratch. + */ +#ifndef V4_INFERENCE_H +#define V4_INFERENCE_H + +#include + +typedef uint64_t v4_q48; /* v3's q48_16_t */ + +v4_q48 v4_q48_mul(v4_q48 a, v4_q48 b); +v4_q48 v4_q48_div(v4_q48 a, v4_q48 b); +v4_q48 v4_q48_exp(v4_q48 q); +v4_q48 v4_q48_log(uint64_t x); + +/* v3's compute_variance_q48, find_variance_inflection (window width, 256 .. 4096), infer_decay_slope_q48 */ +v4_q48 v4_inference_variance(const uint64_t *heat, uint64_t n); +uint32_t v4_inference_window_width(const uint64_t *heat, uint64_t n); +uint64_t v4_inference_decay_slope(const uint64_t *heat, uint64_t n); + +/* v3's InferenceOutputs, and its inference_engine_run on a trajectory already taken: l5 and l6 are L8's gates + * on the window and slope inference */ +typedef struct { + uint32_t adaptive_window_width; + uint64_t adaptive_decay_slope; + v4_q48 window_variance_q48; + v4_q48 slope_fit_quality_q48; + uint8_t early_exited; +} v4_inference_out; +void v4_inference_run(const uint64_t *trajectory, uint64_t n, int l5, int l6, v4_inference_out *out); + +#endif diff --git a/v4/include/v4/mesh.h b/v4/include/v4/mesh.h index ce383b2d..a0c0e72e 100644 --- a/v4/include/v4/mesh.h +++ b/v4/include/v4/mesh.h @@ -38,6 +38,7 @@ #include "v4/blocks.h" #include "v4/fabric.h" #include "v4/manage.h" +#include "v4/inference.h" #define V4_MESH_HERA 10 /* Hera's number, as the host mesh numbers her */ #define V4_MESH_CONSOLE 1 /* the console's number as a sender, and her port to it */ @@ -111,6 +112,13 @@ typedef struct { #define HEARTBEAT_CHECK_FREQUENCY 256 #endif #define V4_MESH_CHECK_FREQUENCY ((uint32_t)HEARTBEAT_CHECK_FREQUENCY) +#ifndef HEARTBEAT_INFERENCE_FREQUENCY +#define HEARTBEAT_INFERENCE_FREQUENCY 1000 /* Kconfig.heartbeat, as v3's */ +#endif +#ifndef HEARTBEAT_TICK_NS +#define HEARTBEAT_TICK_NS 10000ULL +#endif +#define V4_MESH_INFERENCE_EVERY ((uint64_t)HEARTBEAT_INFERENCE_FREQUENCY) /* L8, the Jacquard selector (v3's ssm_jacquard.c and compudynamics.c, #188: 64 cards, L7 always on): a card's * gates are ((card & 0x3F) << 1) | L7, in v3's SSM_CFG_* bits. Applied as card_bits() -- not as v3's @@ -164,9 +172,13 @@ typedef struct { /* L8 and the gates it sets that the other loops read: l1_cache .. l4_pipelining above, and these */ uint8_t l5_window_inference, l6_decay_inference, l7_adaptive_heartrate; v4_mesh_l8 l8; - /* inference (v3's last_inference_outputs): did it run at the last tick, and exit early */ + /* inference (Loops #5 and #6, v4/inference.h), every V4_MESH_INFERENCE_EVERY ticks: its outputs (v3's + * last_inference_outputs), the tick of its last full run, its counts; and Loop #7, the heartrate it sets */ + v4_inference_out inference; uint64_t last_inference_tick; uint8_t inference_early_exited; + uint64_t inference_runs, inference_early_exits; + uint64_t tick_target_ns; /* v3's tick_target_ns: HEARTBEAT_TICK_NS, a quarter to four times it */ } v4_mesh_physics; typedef struct v4_mesh v4_mesh; @@ -203,6 +215,8 @@ struct v4_mesh { v4_mesh_account *accounts; unsigned char *is_word; v4_mesh_physics *physics; /* `capacity` of them: each node's (#189, Phase 7) */ + /* Loop #7 on Hera: the kernel's heartbeat period, re-armed (v3: heartbeat_set_adaptive_period_ns; may be 0) */ + void (*heartrate)(void *self, uint64_t kernel_period_ns); /* memory for a word's transition counters, zeroed, and given back (pipelining; may be 0: none recorded) */ void *(*alloc)(void *self, unsigned long bytes); void (*release)(void *self, void *p); diff --git a/v4/system/inference.c b/v4/system/inference.c new file mode 100644 index 00000000..1e8e4e1b --- /dev/null +++ b/v4/system/inference.c @@ -0,0 +1,193 @@ +/* SPDX-License-Identifier: LicenseRef-Starship-1.0 + * Copyright (c) 2023-2026 Robert A. James + */ +/* inference.c -- the inference engine for a node's heartbeat. See v4/inference.h (#189, Phase 7). */ +#include "v4/inference.h" + +#define WINDOW 4096u /* v3's ROLLING_WINDOW_SIZE */ +#define WINDOW_MIN 256u /* v3's ADAPTIVE_MIN_WINDOW_SIZE */ +#define MAX_CHUNKS (WINDOW / WINDOW_MIN) + +/* ---- Q48.16, as v3's (v3/include/q48_16.h, v3/src/word_source/q48_16_words.c) ---- */ + +static v4_q48 from_u64(uint64_t u) { return u << 16; } +static uint64_t to_u64(v4_q48 q) /* v3: (int64_t)q >> 16, arithmetic */ +{ + return (q & 0x8000000000000000ULL) ? ~((~q) >> 16) : q >> 16; +} + +/* the high 128-bit product, >> 16: v3's __uint128_t path, written out (64 x 64 in four parts) */ +v4_q48 v4_q48_mul(v4_q48 a, v4_q48 b) +{ + uint64_t a_hi = a >> 32, a_lo = a & 0xFFFFFFFFULL, b_hi = b >> 32, b_lo = b & 0xFFFFFFFFULL; + uint64_t p_ll = a_lo * b_lo, p_lh = a_lo * b_hi, p_hl = a_hi * b_lo, p_hh = a_hi * b_hi; + uint64_t carry = 0, mid = p_lh + p_hl, hi, lo; + if (mid < p_lh) carry++; + hi = p_hh + (mid >> 32) + (carry << 32); + lo = p_ll + ((mid & 0xFFFFFFFFULL) << 32); + if (lo < p_ll) hi++; + return (hi << 48) | (lo >> 16); +} + +v4_q48 v4_q48_div(v4_q48 a, v4_q48 b) +{ + if (b == 0) return 0; + if (a > 0x0000FFFFFFFFFFFFULL) return 0xFFFFFFFFFFFFFFFFULL; + return (a << 16) / b; +} + +v4_q48 v4_q48_exp(v4_q48 q) +{ + int64_t qs = (int64_t)q; + int negative, n; + v4_q48 x, result, term; + if (q == 0) return 65536; + if (qs >= 1048576) return 0xFFFFFFFFFFFFFFFFULL; + if (qs <= -1048576) return 0; + negative = qs < 0; + x = negative ? (v4_q48)(-qs) : q; + result = 65536; + term = x; + result += term; + for (n = 2; n <= 10; n++) { + term = v4_q48_mul(term, x); + term = v4_q48_div(term, from_u64((uint64_t)n)); + result += term; + if (term < 50) break; + } + if (negative) result = v4_q48_div(65536, result); + return result; +} + +v4_q48 v4_q48_log(uint64_t x) +{ + const v4_q48 ln2 = 45426; + int k = 0, iter; + uint64_t m = x; + v4_q48 y, result; + if (x == 0 || x == 65536) return 0; + if (m >= 131072) { while (m >= 131072) { m >>= 1; k++; } } + else if (m < 65536) { while (m < 65536) { m <<= 1; k--; } } + y = m > 65536 ? m - 65536 : 0; + for (iter = 0; iter < 6; iter++) { + v4_q48 e = v4_q48_exp(y), corr; + int64_t d; + if (e == 0) break; + d = (int64_t)m - (int64_t)e; + if (d > 0) { corr = v4_q48_div((v4_q48)d, e); y = y + corr; } + else if (d < 0) { corr = v4_q48_div((v4_q48)(-d), e); y = y > corr ? y - corr : 0; } + if (d < 100 && d > -100) break; + } + result = y; + if (k > 0) result = result + v4_q48_mul(from_u64((uint64_t)k), ln2); + else if (k < 0) result = result - v4_q48_mul(from_u64((uint64_t)-k), ln2); + return result; +} + +/* ---- the engine (v3/src/inference_engine.c) ---- */ + +v4_q48 v4_inference_variance(const uint64_t *heat, uint64_t n) +{ + uint64_t sum = 0, sq = 0, i; + v4_q48 mean; + if (n == 0) return 0; + for (i = 0; i < n; i++) sum += heat[i]; + mean = v4_q48_div(from_u64(sum), from_u64(n)); + for (i = 0; i < n; i++) { + v4_q48 h = from_u64(heat[i]), d = h > mean ? h - mean : mean - h; + sq += to_u64(v4_q48_mul(d, d)); + } + return v4_q48_div(from_u64(sq), from_u64(n)); +} + +static v4_q48 median(const v4_q48 *data, uint32_t n) /* v3's: a sorted copy's middle */ +{ + v4_q48 s[MAX_CHUNKS]; + uint32_t i, j; + if (n == 0) return 0; + if (n == 1) return data[0]; + for (i = 0; i < n; i++) s[i] = data[i]; + for (i = 0; i < n - 1; i++) + for (j = 0; j < n - i - 1; j++) + if (s[j] > s[j + 1]) { v4_q48 t = s[j]; s[j] = s[j + 1]; s[j + 1] = t; } + return s[n / 2]; +} + +static v4_q48 mean_q48(const v4_q48 *data, uint32_t n) /* v3's: of the integer parts */ +{ + uint64_t sum = 0; + uint32_t i; + if (n == 0) return 0; + for (i = 0; i < n; i++) sum += data[i] >> 16; + return from_u64(sum / n); +} + +static v4_q48 levene(const v4_q48 *vars, uint32_t k, uint32_t size) +{ + v4_q48 med, z[MAX_CHUNKS], zbar, ssd = 0, num, den; + uint32_t i; + if (k < 2) return 0; + med = median(vars, k); + for (i = 0; i < k; i++) z[i] = vars[i] > med ? vars[i] - med : med - vars[i]; + zbar = mean_q48(z, k); + for (i = 0; i < k; i++) { v4_q48 d = z[i] > zbar ? z[i] - zbar : zbar - z[i]; ssd = ssd + v4_q48_mul(d, d); } + num = v4_q48_mul(from_u64(k - 1u), v4_q48_mul(from_u64(size), ssd)); + den = v4_q48_mul(from_u64(k), v4_inference_variance(z, k)); /* v3 takes the z's variance as raw values */ + return den > 0 ? v4_q48_div(num, den) : 0; +} + +uint32_t v4_inference_window_width(const uint64_t *heat, uint64_t n) +{ + uint32_t size, max = n < WINDOW ? (uint32_t)n : WINDOW; + const v4_q48 critical = 425984; /* v3's q48_from_double(6.5) */ + if (n == 0) return WINDOW / 2; + for (size = WINDOW_MIN; size <= max; size += 64) { + uint32_t k = (uint32_t)(n / size), i; + v4_q48 vars[MAX_CHUNKS]; + if (k < 3) continue; + if (k > MAX_CHUNKS) k = MAX_CHUNKS; /* n <= 4096, so never more */ + for (i = 0; i < k; i++) vars[i] = v4_inference_variance(&heat[(uint64_t)i * size], size); + if (levene(vars, k, size) <= critical) return size; + } + return max; +} + +uint64_t v4_inference_decay_slope(const uint64_t *heat, uint64_t n) +{ + uint64_t sum_t, sum_t_sq, t, num, den; + v4_q48 sum_log = 0, sum_t_log = 0; + int64_t a, b, ns; + if (n < 2) return 0; + sum_t = (n * (n - 1)) / 2; + sum_t_sq = (n * (n - 1) * (2 * n - 1)) / 6; + for (t = 0; t < n; t++) { + v4_q48 l; + if (heat[t] == 0) continue; + l = v4_q48_log(heat[t]); + sum_log = sum_log + l; + sum_t_log = sum_t_log + v4_q48_mul(from_u64(t), l); + } + a = (int64_t)v4_q48_mul(from_u64(n), sum_t_log); + b = (int64_t)v4_q48_mul(from_u64(sum_t), sum_log); + ns = a - b; + num = ns < 0 ? (uint64_t)(-ns) : (uint64_t)ns; + den = n * sum_t_sq - sum_t * sum_t; + if (den == 0) den = 1; + return num / den; +} + +void v4_inference_run(const uint64_t *traj, uint64_t n, int l5, int l6, v4_inference_out *out) +{ + v4_q48 var; + if (!traj || n < 2) { out->early_exited = 1; return; } + var = v4_inference_variance(traj, n); + if (out->window_variance_q48 != 0) { /* v3's has_variance_stabilized: within 5% */ + v4_q48 last = out->window_variance_q48, d = var > last ? var - last : last - var; + if (v4_q48_div(d, last) <= 3276) { out->early_exited = 1; return; } + } + if (l5) out->adaptive_window_width = v4_inference_window_width(traj, n); + if (l6) out->adaptive_decay_slope = v4_inference_decay_slope(traj, n); + out->window_variance_q48 = var; + out->slope_fit_quality_q48 = 52429; /* v3's: 0.8, a placeholder it says it is */ + out->early_exited = 0; +} diff --git a/v4/system/mesh.c b/v4/system/mesh.c index 59b120c8..c9a9bc76 100644 --- a/v4/system/mesh.c +++ b/v4/system/mesh.c @@ -524,11 +524,50 @@ void v4_mesh_l8_force(v4_mesh *ms, unsigned place, unsigned card, int hold) l8_apply(&ms->physics[place]); } +/* ---- inference (v3's vm_tick_inference_engine) and Loop #7 ---- */ + +static uint64_t trajectory[V4_MESH_WINDOW]; + +static void inference_tick(unsigned place, v4_mesh_physics *p) +{ + uint64_t have, span, i; + v4_fabric_node *x = v4_fabric_node_at(&the->f, place); + if (!p->is_warm || !x) return; + p->inference_runs++; + window_service(p); + /* the heat of each word in the window, the newest `span` of them, oldest first (v3's extract_heat_trajectory) */ + have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW; + span = p->effective_window_size < have ? p->effective_window_size : have; + for (i = 0; i < span; i++) { + uint32_t id = p->history[(p->window_pos + V4_MESH_WINDOW - span + i) % V4_MESH_WINDOW]; + trajectory[i] = (id < V4_MESH_WORD_IDS && accounts_of(place)[id].xt) ? (uint64_t)accounts_of(place)[id].heat : 0; + } + v4_inference_run(trajectory, span, p->l5_window_inference, p->l6_decay_inference, &p->inference); + if (!p->inference.early_exited) { + if (p->inference.adaptive_window_width > 0 && p->inference.adaptive_window_width != p->effective_window_size) + p->effective_window_size = p->inference.adaptive_window_width; + if (p->inference.adaptive_decay_slope > 0 && p->inference.adaptive_decay_slope != p->decay_slope_q48) + p->decay_slope_q48 = p->inference.adaptive_decay_slope; + p->last_inference_tick = p->tick; + } else p->inference_early_exits++; + p->inference_early_exited = p->inference.early_exited; + /* Loop #7, the adaptive heartrate: stable, 25% slower; else 20% faster -- within 1/4 to 4 times the nominal */ + { + uint64_t cur = p->tick_target_ns, n; + if (p->inference.early_exited) { n = (cur * 125u) / 100u; if (n > HEARTBEAT_TICK_NS * 4u) n = HEARTBEAT_TICK_NS * 4u; } + else { n = (cur * 80u) / 100u; if (n < HEARTBEAT_TICK_NS / 4u) n = HEARTBEAT_TICK_NS / 4u; } + p->tick_target_ns = n; + if (place == the->hera_place && the->heartrate) /* Hera's re-arms the kernel's (10 ms base) */ + the->heartrate(the->self, (p->tick_target_ns * 10000000ULL) / HEARTBEAT_TICK_NS); + } +} + /* a node's heartbeat cycle, in v3's order (vm_heartbeat_run_cycle): its tick (and inference, every 1000); * background decay; the window's service; L8's update; the snapshot */ static void heartbeat_cycle(unsigned place, v4_mesh_physics *p) { p->tick++; + if (p->tick - p->last_inference_tick >= V4_MESH_INFERENCE_EVERY) inference_tick(place, p); background_decay(place, p); window_service(p); l8_update(p); @@ -630,7 +669,8 @@ static void words_attach(unsigned place, v4_fabric_node *x) /* every gate off, and L8's table on card 8, as v3's kernel boot (vm_bootstrap.c): the first cycle's L8 * applies its card */ l8_seed(&p->l8); - p->last_inference_tick = ~(uint64_t)0; + p->last_inference_tick = 0; /* as v3's */ + p->tick_target_ns = HEARTBEAT_TICK_NS; } x->es.on_call = on_call; x->es.on_call_ctx = &the->kernel[place]; diff --git a/v4/tests/test_inference.c b/v4/tests/test_inference.c new file mode 100644 index 00000000..9a26844d --- /dev/null +++ b/v4/tests/test_inference.c @@ -0,0 +1,81 @@ +/* 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 +#include +#include + +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; +} diff --git a/v4/tests/test_mesh_boot.c b/v4/tests/test_mesh_boot.c index f4610202..517cba73 100644 --- a/v4/tests/test_mesh_boot.c +++ b/v4/tests/test_mesh_boot.c @@ -36,7 +36,9 @@ static v4_mesh ms; static v4_mesh_account accounts[PLACES * V4_MESH_WORD_IDS]; /* the words' records (#189, Phase 2) */ static unsigned char is_word[PLACES * (V4_NODE_WORDS / 8u)]; static v4_mesh_physics physics[PLACES]; -static unsigned forgotten; +static unsigned forgotten, heartrates; +static uint64_t heartrate_ns; +static void heartrate(void *self, uint64_t ns) { (void)self; heartrates++; heartrate_ns = ns; } static v4_cell forgotten_id = -1; static void forgot(void *self, unsigned place, v4_cell id) { (void)self; (void)place; forgotten++; forgotten_id = id; } static v4_uheat_t charged[V4_MESH_WORD_IDS]; @@ -110,7 +112,7 @@ int main(void) ms.out = out; ms.key = key; ms.words = words; ms.word_count = 1; ms.serve = serve; ms.capsules = capsules; ms.capsule_count = 2; ms.im = &im; ms.self = 0; ms.accounts = accounts; ms.is_word = is_word; ms.forgot = forgot; - ms.physics = physics; ms.alloc = alloc; ms.release = release; + ms.physics = physics; ms.alloc = alloc; ms.release = release; ms.heartrate = heartrate; /* ---- the boot ---- */ CHECK(v4_mesh_boot(&ms) == 1, "the boot: %s", said); @@ -216,6 +218,23 @@ int main(void) r = line(V4_MESH_HERA, "FORGET L8T", &how); } + /* ---- inference every 1000 ticks, and Loop #7 (#189 Phase 7, Task 3) ---- */ + { + v4_mesh_physics *p = &ms.physics[0]; + uint64_t t0 = p->tick, runs0 = p->inference_runs; + r = line(V4_MESH_HERA, ": IW ; : LIW 30 0 DO 10000 0 DO IW LOOP LOOP ; LIW", &how); /* 300000 calls: over 1000 ticks */ + CHECK(how == V4_TEXT_COMPLETED && p->tick - t0 > 1000u && p->is_warm, "a long run: over 1000 ticks (%lu), the window warm", + (unsigned long)(p->tick - t0)); + CHECK(p->inference_runs > runs0, "inference ran (%lu times)", (unsigned long)p->inference_runs); + CHECK(p->tick_target_ns != HEARTBEAT_TICK_NS && p->tick_target_ns >= HEARTBEAT_TICK_NS / 4u + && p->tick_target_ns <= HEARTBEAT_TICK_NS * 4u, "Loop #7 moved the heartrate, within its bounds: %lu ns", + (unsigned long)p->tick_target_ns); + CHECK(heartrates > 0 && heartrate_ns == (p->tick_target_ns * 10000000ULL) / HEARTBEAT_TICK_NS, + "and on Hera re-armed the kernel's heartbeat: %lu ns", (unsigned long)heartrate_ns); + CHECK(!p->inference.early_exited ? p->last_inference_tick > 0 : 1, "a full run marks its tick"); + r = line(V4_MESH_HERA, "FORGET IW", &how); + } + /* ---- the words' accounts (#189, Phase 2; Captain Bob, 2026-11-13: the word ID in the header, charged * per heartbeat tick) ---- */ { @@ -229,8 +248,9 @@ int main(void) /* Phase 7, Task 1: v3's physics on the record (DictPhysics; v3/include/vm.h) */ CHECK(a && a->heat == 0 && a->temperature_q8 == 0 && a->state_flags == 0, "a new word's physics: no heat, no temperature, no state flags (background decay may have touched it, as v3's)"); - CHECK(ms.physics[0].decay_slope_q48 == 21845u && ms.physics[0].l3_decay == 1, - "each node has v3's initial decay slope (Q48.16, 1/3) and Loop #3 on"); + CHECK(ms.physics[0].decay_slope_q48 == 21845u || ms.physics[0].decay_slope_q48 == ms.physics[0].inference.adaptive_decay_slope, + "each node's decay slope: v3's initial (Q48.16, 1/3), or the one inference has set since (%lu)", + (unsigned long)ms.physics[0].decay_slope_q48); r = line(V4_MESH_HERA, ": IMM ; IMMEDIATE ' IMM .", &how); { v4_cell ix = (v4_cell)strtol(r, 0, 10); @@ -246,6 +266,8 @@ int main(void) const v4_mesh_account *a; uint64_t total0; uint32_t every = p->cycle_every; + uint64_t slope = p->decay_slope_q48; + p->decay_slope_q48 = 21845u; /* v3's initial, for decay's arithmetic seen exactly */ r = line(V4_MESH_HERA, ": P7 DUP * ; ' P7 .", &how); a = v4_mesh_account_at(&ms, 0, (v4_cell)strtol(r, 0, 10)); /* the tick, by execution (Captain Bob, 2026-11-13: "By execution, as v3"): every 256 word calls a @@ -299,6 +321,8 @@ int main(void) v4_cell q7; const v4_mesh_account *qa; uint64_t c0 = p->decay_cycles; + p->last_inference_tick = p->tick; /* no inference in this check's 1000 ticks: v3's slope stays */ + p->decay_slope_q48 = 21845u; r = line(V4_MESH_HERA, ": Q7 ; : LQ7 100 0 DO Q7 LOOP ; LQ7 ' Q7 .", &how); q7 = (v4_cell)strtol(r, 0, 10); qa = v4_mesh_account_at(&ms, 0, q7); @@ -314,6 +338,7 @@ int main(void) r = line(V4_MESH_HERA, "FORGET Q7", &how); } v4_mesh_l8_force(&ms, 0, ms.physics[0].l8.current_card, 0); /* L8 chooses again */ + p->decay_slope_q48 = slope; r = line(V4_MESH_HERA, "FORGET P7", &how); } r = line(V4_MESH_HERA, "3 SQ .", &how);