Skip to content

Run frontier MoE models on hardware you already own β€” pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(inkling): grow the KV instead of restarting it, or reuse never fires

JustVuggb4c4f613e94b
diff --git a/c/inkling.c b/c/inkling.c
index 3625087a..f88b1113 100644
--- a/c/inkling.c
+++ b/c/inkling.c
@@ -1451,19 +1451,44 @@ static void state_reset(Model *m) {
static void kv_alloc(Model *m, int max_t) {
Cfg *c = &m->c;
if (m->K && max_t <= m->max_t) return; /* reuse across prompts when big enough */
- if (m->K) for (int i = 0; i < c->n_layers; i++) { free(m->K[i]); free(m->V[i]); }
- free(m->K); free(m->V);
- /* The record is sized with the KV it describes: growing these buffers
- * discards the positions it refers to, so it is dropped at the same
- * moment. A failed allocation just disables reuse. */
- kv_prefix_alloc(&m->kvp, max_t);
- m->kv_len = 0;
+
+ /* GROW, DO NOT RESTART.
+ *
+ * This used to free the buffers and allocate fresh ones, which discarded
+ * every position already computed. That was invisible while each turn
+ * re-prefilled anyway β€” but it defeats KV prefix reuse in exactly the case
+ * reuse exists for: a conversation whose prompt is longer every turn asks
+ * for a larger max_t every turn, so the state was thrown away just before
+ * the point of using it.
+ *
+ * K/V are laid out [kv_head][max_t][hd], so a larger max_t changes the
+ * stride: the old contents cannot be realloc'd, they have to be re-laid-out
+ * head by head. That copy costs a memcpy of what is already computed, which
+ * is nothing beside re-running the prefill that produced it. */
+ float **oldK = m->K, **oldV = m->V;
+ int old_max = m->max_t;
+ int keep = (m->K && m->kv_len > 0 && m->kv_len <= max_t) ? m->kv_len : 0;
+
m->max_t = max_t;
m->K = calloc(c->n_layers, sizeof(float*)); m->V = calloc(c->n_layers, sizeof(float*));
for (int i = 0; i < c->n_layers; i++) {
- m->K[i] = falloc((int64_t)L_KV(c,i) * max_t * L_HD(c,i));
- m->V[i] = falloc((int64_t)L_KV(c,i) * max_t * L_HD(c,i));
+ int kv = L_KV(c,i), hd = L_HD(c,i);
+ m->K[i] = falloc((int64_t)kv * max_t * hd);
+ m->V[i] = falloc((int64_t)kv * max_t * hd);
+ for (int h = 0; h < kv && keep; h++) {
+ memcpy(m->K[i] + (int64_t)h * max_t * hd,
+ oldK[i] + (int64_t)h * old_max * hd, (size_t)keep * hd * sizeof(float));
+ memcpy(m->V[i] + (int64_t)h * max_t * hd,
+ oldV[i] + (int64_t)h * old_max * hd, (size_t)keep * hd * sizeof(float));
+ }
}
+ if (oldK) for (int i = 0; i < c->n_layers; i++) { free(oldK[i]); free(oldV[i]); }
+ free(oldK); free(oldV);
+
+ /* the record describes those same positions, so it survives with them --
+ * unless its own allocation fails, in which case reuse simply stops. */
+ if (kv_prefix_grow(&m->kvp, max_t, keep)) m->kv_len = keep;
+ else m->kv_len = 0;
}
/* greedy generation, olmoe.c-style */
diff --git a/c/kv_prefix.h b/c/kv_prefix.h
index e4d3f7ed..52667411 100644
--- a/c/kv_prefix.h
+++ b/c/kv_prefix.h
@@ -75,6 +75,28 @@ static inline void kv_prefix_free(kv_prefix *p) {
p->tainted = 0;
}
+/* Grow the record to `cap`, keeping the first `keep` positions.
+ *
+ * For engines whose KV buffers are re-allocated when a longer prompt arrives.
+ * If those buffers are grown by COPYING their contents, the positions survive
+ * and so must the record β€” otherwise reuse can never fire in the one case it
+ * exists for: a conversation whose prompt gets longer every turn. Returns 0 if
+ * the record could not be preserved, and leaves it empty rather than stale:
+ * the caller must then treat the state as unreusable. */
+static inline int kv_prefix_grow(kv_prefix *p, int cap, int keep) {
+ if (!p || cap <= 0) return 0;
+ int *grown = (int *)calloc((size_t)cap, sizeof(int));
+ if (!grown) { kv_prefix_free(p); return 0; }
+ if (keep > p->len) keep = p->len;
+ if (keep > cap) keep = cap;
+ if (keep > 0 && p->fed) memcpy(grown, p->fed, (size_t)keep * sizeof(int));
+ free(p->fed);
+ p->fed = grown;
+ p->cap = cap;
+ p->len = keep > 0 ? keep : 0;
+ return 1;
+}
+
/* Record n tokens fed at absolute positions pos0..pos0+n-1. Out-of-range
* writes drop the record rather than truncating it: a partial record would
* claim coverage the state does not have. */
diff --git a/c/tests/test_kv_prefix.c b/c/tests/test_kv_prefix.c
index 23d5f827..d85129ad 100644
--- a/c/tests/test_kv_prefix.c
+++ b/c/tests/test_kv_prefix.c
@@ -72,10 +72,38 @@ int main(void) {
CHECK(p.len == 0, "overflowing write must drop the record, len=%d", p.len);
CHECK(kv_prefix_reuse(&p, turn2, 5) == 0, "dropped record must not be reused");
- /* Growing the buffers invalidates everything: the KV they describe is gone. */
- CHECK(kv_prefix_alloc(&p, 32), "grow failed");
+ /* kv_prefix_alloc RESTARTS: for callers that discard the KV outright. */
+ CHECK(kv_prefix_alloc(&p, 32), "alloc failed");
CHECK(p.len == 0 && p.tainted == 0, "alloc must reset the record");
+ /* kv_prefix_grow PRESERVES: for callers that grow the KV by copying it.
+ * This is the case CI caught β€” inkling's kv_alloc re-allocated on every
+ * longer prompt, so reuse could never fire in a growing conversation, the
+ * one situation it exists for. */
+ kv_prefix_record(&p, turn1, 0, 3);
+ kv_prefix_record(&p, gen, 3, 2); /* 5 positions held */
+ CHECK(kv_prefix_grow(&p, 64, 5), "grow failed");
+ CHECK(p.cap == 64, "cap=%d after grow, want 64", p.cap);
+ CHECK(p.len == 5, "len=%d after grow, want 5 preserved", p.len);
+ CHECK(kv_prefix_reuse(&p, turn3, 6) == 5,
+ "a preserved record must still be reusable after growing");
+
+ /* keep is clamped by what is actually held: a caller that copied FEWER
+ * positions than it claims must not end up describing uninitialised ones. */
+ CHECK(kv_prefix_grow(&p, 128, 999), "grow failed");
+ CHECK(p.len == 5, "keep must clamp to len, got %d", p.len);
+
+ /* keep=0 grows without preserving: the caller discarded the KV contents. */
+ CHECK(kv_prefix_grow(&p, 256, 0), "grow failed");
+ CHECK(p.len == 0, "keep=0 must leave the record empty, got %d", p.len);
+ CHECK(kv_prefix_reuse(&p, turn3, 6) == 0, "an emptied record reuses nothing");
+
+ /* Shrinking below what is held keeps only what fits, never past the end. */
+ kv_prefix_record(&p, turn1, 0, 3);
+ CHECK(kv_prefix_grow(&p, 2, 3), "grow failed");
+ CHECK(p.len <= 2 && p.cap == 2, "len=%d cap=%d must stay within cap",
+ p.len, p.cap);
+
/* Defensive: a NULL record is inert, not a crash. Engines disable reuse by
* leaving fed==NULL when the allocation fails. */
kv_prefix nul = {0};