feat(v4): Phase 7 Task 3 -- L8, v3's Jacquard selector, in each node's heartbeat cycle (#189)
v3's vm_heartbeat_update_l8 with its table (ssm_jacquard.c, compudynamics.c), rewritten in integers: the regime from the window's entropy, the prediction CV and the locality of the last 32 calls; the current card scored (ANOVA from inference, joint convergence under L5/L6), decayed 0.99, the next chosen by UCB1 over 64 cards; its gates L1-L7 applied to the loops, L1 and L4 within the build's ceilings. Every gate off and card 8 at the start, as v3's kernel boot. Applied through card_bits: v3 applies the card's number, one bit off -- filed #196, ruled "v4 correct, v3 as is". v4_mesh_l8_force as v3's ssm_l8_force_config; the exact decay and window tests hold a card. Test first: test_mesh_boot.c 67 checks, 75 on. amd64 PASS, 156 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:
+129
-4
@@ -400,6 +400,130 @@ static void window_service(v4_mesh_physics *p)
|
||||
p->last_pattern_diversity = diversity;
|
||||
}
|
||||
|
||||
/* ---- L8 (v3's vm_heartbeat_update_l8 with its table: ssm_jacquard.c, compudynamics.c) ---- */
|
||||
|
||||
static uint8_t card_bits(unsigned card) { return (uint8_t)(((card & 0x3Fu) << 1) | V4_L8_L7); }
|
||||
|
||||
static uint64_t isqrt64(uint64_t n)
|
||||
{
|
||||
uint64_t x, y;
|
||||
if (n == 0) return 0;
|
||||
x = n; y = (x + 1) / 2;
|
||||
while (y < x) { x = y; y = (x + n / x) / 2; }
|
||||
return x;
|
||||
}
|
||||
static uint32_t ln_q16(uint32_t n) /* v3's cd_ln_approx_q16 */
|
||||
{
|
||||
uint32_t bits = 0, t;
|
||||
if (n <= 1) return 0;
|
||||
for (t = n >> 1; t; t >>= 1) bits++;
|
||||
return bits * 45426u;
|
||||
}
|
||||
|
||||
/* the card's gates, onto the loops -- within the build's ceilings (v3: ENABLE_PIPELINING, the cache's) */
|
||||
static void l8_apply(v4_mesh_physics *p)
|
||||
{
|
||||
uint8_t b = card_bits(p->l8.current_card);
|
||||
p->l1_cache = (uint8_t)((b & V4_L8_L1) && ENABLE_HOTWORDS_CACHE);
|
||||
p->l2_window = (uint8_t)((b & V4_L8_L2) != 0);
|
||||
p->l3_decay = (uint8_t)((b & V4_L8_L3) != 0);
|
||||
p->l4_pipelining = (uint8_t)((b & V4_L8_L4) && ENABLE_PIPELINING);
|
||||
p->l5_window_inference = (uint8_t)((b & V4_L8_L5) != 0);
|
||||
p->l6_decay_inference = (uint8_t)((b & V4_L8_L6) != 0);
|
||||
p->l7_adaptive_heartrate = (uint8_t)((b & V4_L8_L7) != 0);
|
||||
}
|
||||
|
||||
/* v3's ssm_l8_seed_from_doe: the DoE's top 5% of cards high, L1 or L4 low, the rest middling; card 8 first */
|
||||
static void l8_seed(v4_mesh_l8 *t)
|
||||
{
|
||||
static const uint8_t top5[] = { 17u, 23u, 35u, 39u, 49u };
|
||||
unsigned c, r, i;
|
||||
for (c = 0; c < V4_L8_CARDS; c++) {
|
||||
uint8_t b = card_bits(c);
|
||||
int top = 0, l1 = (b & V4_L8_L1) != 0, l4 = (b & V4_L8_L4) != 0;
|
||||
uint32_t score;
|
||||
for (i = 0; i < 5; i++) if (b == top5[i]) top = 1;
|
||||
score = top ? (65535u * 80u) / 100u : (l1 && l4) ? (65535u * 5u) / 100u
|
||||
: (l1 || l4) ? (65535u * 10u) / 100u : (65535u * 40u) / 100u;
|
||||
for (r = 0; r < V4_L8_REGIMES; r++) { t->regime_scores[r * V4_L8_CARDS + c] = score; t->regime_ticks[r * V4_L8_CARDS + c] = 0; }
|
||||
t->has_prev[c] = 0; t->prev_window[c] = 0; t->prev_locality_q16[c] = 0;
|
||||
}
|
||||
for (r = 0; r < V4_L8_REGIMES; r++) t->total_regime_ticks[r] = 0;
|
||||
t->current_card = V4_L8_START;
|
||||
t->current_regime = 0;
|
||||
}
|
||||
|
||||
/* locality over the last 32 calls (v3's cd_classify_ids, its locality part): how many repeat one of the 8 before */
|
||||
static uint32_t l8_locality(const v4_mesh_physics *p)
|
||||
{
|
||||
uint32_t ids[32], n = 32, i, j, matches = 0;
|
||||
uint64_t have = p->total_executions < V4_MESH_WINDOW ? p->total_executions : V4_MESH_WINDOW;
|
||||
if (have < n) n = (uint32_t)have;
|
||||
if (n <= 8) return 0;
|
||||
for (i = 0; i < n; i++) ids[i] = p->history[(p->window_pos + V4_MESH_WINDOW - n + i) % V4_MESH_WINDOW];
|
||||
for (i = 8; i < n; i++)
|
||||
for (j = i - 8; j < i; j++) if (ids[j] == ids[i]) { matches++; break; }
|
||||
return (uint32_t)(((uint64_t)matches * 65536u) / (n - 8));
|
||||
}
|
||||
|
||||
/* a tick of L8: the regime; the card scored on how this tick went; the next card by UCB1; its gates */
|
||||
static void l8_update(v4_mesh_physics *p)
|
||||
{
|
||||
v4_mesh_l8 *t = &p->l8;
|
||||
uint32_t locality = l8_locality(p), window = p->effective_window_size, anova, combined, r, c, ci, total, lnt, best = 0, best_ucb = 0;
|
||||
int entropy_high = (uint64_t)window * 100u >= 75u * (uint64_t)V4_MESH_WINDOW; /* >= 0.75 */
|
||||
int cv_high = p->prefetch_attempts == 0 ? 1 /* v3: 0.5 when no data */
|
||||
: 100u * (p->prefetch_attempts - p->prefetch_hits) >= 15u * p->prefetch_attempts; /* 1 - accuracy >= 0.15 */
|
||||
uint8_t active = card_bits(t->current_card);
|
||||
int64_t delta, ns;
|
||||
uint32_t old, decayed;
|
||||
if (t->hold) return;
|
||||
r = (uint32_t)((entropy_high ? 4u : 0u) | (cv_high ? 2u : 0u) | (locality >= 32768u ? 1u : 0u));
|
||||
t->current_regime = (uint8_t)r;
|
||||
anova = p->last_inference_tick == p->tick ? (p->inference_early_exited ? 65536u : 0u) : 32768u;
|
||||
c = t->current_card;
|
||||
if (((active & V4_L8_L5) || (active & V4_L8_L6)) && t->has_prev[c]) { /* joint convergence */
|
||||
uint32_t dw = window > t->prev_window[c] ? window - t->prev_window[c] : t->prev_window[c] - window;
|
||||
uint32_t dl = locality > t->prev_locality_q16[c] ? locality - t->prev_locality_q16[c] : t->prev_locality_q16[c] - locality;
|
||||
uint64_t dwq = t->prev_window[c] ? ((uint64_t)dw * 65536u) / t->prev_window[c] : 0, dlq = dl, err;
|
||||
if (dwq > 65536u) dwq = 65536u;
|
||||
if (dlq > 65536u) dlq = 65536u;
|
||||
err = (isqrt64(dwq * dwq + dlq * dlq) * 65536u) / 92682u;
|
||||
if (err > 65536u) err = 65536u;
|
||||
combined = (uint32_t)(((uint64_t)49152u * (65536u - (uint32_t)err) + (uint64_t)16384u * anova) / 65536u);
|
||||
} else combined = anova;
|
||||
delta = ((int64_t)combined - 32768) * 16384 / 32768;
|
||||
old = t->regime_scores[r * V4_L8_CARDS + c];
|
||||
decayed = (uint32_t)(((uint64_t)old * 64881u) / 65536u);
|
||||
ns = (int64_t)decayed + delta;
|
||||
t->regime_scores[r * V4_L8_CARDS + c] = ns < 0 ? 0u : ns > 65535 ? 65535u : (uint32_t)ns;
|
||||
t->regime_ticks[r * V4_L8_CARDS + c]++;
|
||||
if (t->total_regime_ticks[r] < 0xFFFFFFFFu) t->total_regime_ticks[r]++;
|
||||
t->prev_window[c] = window; t->prev_locality_q16[c] = locality; t->has_prev[c] = 1;
|
||||
total = t->total_regime_ticks[r];
|
||||
lnt = ln_q16(total ? total : 1);
|
||||
for (ci = 0; ci < V4_L8_CARDS; ci++) {
|
||||
uint32_t nc = t->regime_ticks[r * V4_L8_CARDS + ci], ucb;
|
||||
if (nc == 0) ucb = 65535u;
|
||||
else {
|
||||
ucb = t->regime_scores[r * V4_L8_CARDS + ci] + (uint32_t)(((uint64_t)6554u * (uint32_t)isqrt64(lnt / nc)) / 256u);
|
||||
if (ucb > 65535u) ucb = 65535u;
|
||||
}
|
||||
if (ucb > best_ucb) { best_ucb = ucb; best = ci; }
|
||||
}
|
||||
if (best != t->current_card) t->changes++;
|
||||
t->current_card = (uint8_t)best;
|
||||
l8_apply(p);
|
||||
}
|
||||
|
||||
void v4_mesh_l8_force(v4_mesh *ms, unsigned place, unsigned card, int hold)
|
||||
{
|
||||
if (!ms->physics || place >= ms->capacity || card >= V4_L8_CARDS) return;
|
||||
ms->physics[place].l8.current_card = (uint8_t)card;
|
||||
ms->physics[place].l8.hold = (uint8_t)(hold != 0);
|
||||
l8_apply(&ms->physics[place]);
|
||||
}
|
||||
|
||||
/* 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)
|
||||
@@ -407,6 +531,7 @@ static void heartbeat_cycle(unsigned place, v4_mesh_physics *p)
|
||||
p->tick++;
|
||||
background_decay(place, p);
|
||||
window_service(p);
|
||||
l8_update(p);
|
||||
}
|
||||
|
||||
/* at `call` (exec.h), on a word -- any other target, nothing -- in v3's order (physics_pre_execute,
|
||||
@@ -500,12 +625,12 @@ static void words_attach(unsigned place, v4_fabric_node *x)
|
||||
p->tick = tick;
|
||||
p->cycle_every = V4_MESH_CHECK_FREQUENCY;
|
||||
p->decay_slope_q48 = V4_MESH_INITIAL_DECAY_SLOPE_Q48;
|
||||
p->l3_decay = 1;
|
||||
p->l2_window = 1; /* v3: "No config yet: recorded" */
|
||||
p->effective_window_size = V4_MESH_WINDOW;
|
||||
p->prev_id = V4_MESH_NO_WORD;
|
||||
p->l4_pipelining = ENABLE_PIPELINING ? 1 : 0; /* the build's ceiling; L8 turns it within it */
|
||||
p->l1_cache = ENABLE_HOTWORDS_CACHE ? 1 : 0;
|
||||
/* 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;
|
||||
}
|
||||
x->es.on_call = on_call;
|
||||
x->es.on_call_ctx = &the->kernel[place];
|
||||
|
||||
Reference in New Issue
Block a user