Files
LithosAnanke/v4/include/v4/inference.h
T
Claude 6d954ac279 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
2026-11-13 23:59:18 +00:00

42 lines
1.8 KiB
C

/* 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 <stdint.h>
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