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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-13 23:59:18 +00:00
parent d09eee9a08
commit 6d954ac279
13 changed files with 434 additions and 14 deletions
+41 -1
View File
@@ -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];