Run frontier MoE models on hardware you already own β pure C, zero deps, experts streamed from disk. Tiny engine, immense model. π¦
C
glm53: keep the KV prefix across image requests by keying the picture
LΓ©on Avic Simmons3213dd98a55e
diff --git a/c/Makefile b/c/Makefileindex 8a18cd70..97ae7ce2 100644--- a/c/Makefile+++ b/c/Makefile@@ -2729,6 +2729,9 @@ bench: iobench$(EXE)tests/test_kv_prefix$(EXE): tests/test_kv_prefix.c$(CC) $(CFLAGS) tests/test_kv_prefix.c -o tests/test_kv_prefix$(EXE) $(LDFLAGS)+tests/test_glm53_image_key$(EXE): tests/test_glm53_image_key.c+ $(CC) $(CFLAGS) tests/test_glm53_image_key.c -o tests/test_glm53_image_key$(EXE) $(LDFLAGS)+tests/test_kimi_request_state$(EXE): tests/test_kimi_request_state.c kimi_k3.c $(VK_OBJ)$(CC) $(NOCUDA_CFLAGS) tests/test_kimi_request_state.c $(VK_OBJ) -o tests/test_kimi_request_state$(EXE) $(NOCUDA_LDFLAGS)diff --git a/c/glm53.c b/c/glm53.cindex 06a482b0..77795a03 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -631,6 +631,7 @@ int main(int argc, char **argv) {#include "delta_attention.h"#include "sparse_index.h"#include "vision_tower.h"+#include "kv_image_key.h"/* Una matrice residente, nel formato in cui conviene tenerla.*@@ -3103,6 +3104,12 @@ typedef struct { float **state, **window; int n_layers; } Glm53PinState;typedef struct {GSession *session;int *tokens; /* la sequenza che lo slot tiene */+ /* La stessa sequenza, con ogni segnaposto immagine al posto del suo id+ * porta l'impronta dei byte che la torre ha visto (kv_image_key.h): due+ * foto diverse hanno gli stessi id, e il confronto che decide il riuso+ * deve distinguerle. Gli scatti (pins) confrontano gli id e passano da+ * qui per le posizioni immagine. */+ uint64_t *keys;int n, cap;/* Scatti dello stato (SUBMIT pin=1): la ricorrenza KDA, gli id e i logit* finali. Piu di uno perche i prefissi utili sono annidati: le istruzioni@@ -3209,7 +3216,8 @@ static int slot_pin_save(const GModel *m, KVSlot *slot, const int *tokens, int n/* Rimette lo scatto piu profondo che sia un prefisso stretto di questo prompt,* se la sessione che l'ha prodotto e ancora quella dello slot. Torna quante* posizioni sono gia' fatte, 0 se non si applica. */-static int slot_pin_restore(const GModel *m, KVSlot *slot, const int *tokens, int n) {+static int slot_pin_restore(const GModel *m, KVSlot *slot, const int *tokens, int n,+ int common) {const Cfg *c = &m->c;if (!slot->session || slot->pin_session != slot->session) return 0;const size_t ns = (size_t)c->kda_heads * c->kda_hd * c->kda_hd;@@ -3223,8 +3231,12 @@ static int slot_pin_restore(const GModel *m, KVSlot *slot, const int *tokens, in* riscritte con altri token. Che gli id combacino con la RICHIESTA* non basta; devono combaciare con la storia dello slot, che e' la* sola descrizione di cosa le righe tengono davvero (come- * kv_prefix_holds per gli altri motori, #1650). */+ * kv_prefix_holds per gli altri motori, #1650). E `common`, le+ * posizioni su cui la richiesta e la storia concordano per CHIAVE,+ * deve coprire lo scatto: sui segnaposto immagine gli id combaciano+ * sempre, l'impronta della foto no. */if (st && k->len <= slot->session->filled && k->len <= slot->n &&+ k->len <= common &&!memcmp(k->ids, slot->tokens, (size_t)k->len * sizeof(int))) {for (int i = 0; i < c->n_layers; i++) {GLayerState *ls = &slot->session->layer[i];@@ -3295,21 +3307,25 @@ static void slot_reset(const GModel *m, KVSlot *slot) {slot->blank_len = 0; /* il buffer resta: si riusa alla prossima corsa */}-/* Quanti token iniziali lo slot ha gia' in cache e puo' tenere. */-static int slot_shared(const KVSlot *slot, const int *tokens, int n) {- int shared = 0;- while (shared < slot->n && shared < n && slot->tokens[shared] == tokens[shared])- shared++;- return shared;+/* Quanti token iniziali lo slot ha gia' in cache e puo' tenere. Il confronto+ * e' per chiave, non per id: su un segnaposto immagine la chiave e' la foto. */+static int slot_shared(const KVSlot *slot, const uint64_t *keys, int n) {+ return kv_keys_shared(slot->keys, slot->n, keys, n);}-static void slot_remember(KVSlot *slot, const int *tokens, int n) {+static void slot_remember(KVSlot *slot, const int *tokens, const uint64_t *keys, int n) {if (n > slot->cap) {slot->tokens = realloc(slot->tokens, (size_t)n * sizeof(int));- if (!slot->tokens) { fprintf(stderr, "OOM allocating slot history\n"); exit(1); }+ slot->keys = realloc(slot->keys, (size_t)n * sizeof(uint64_t));+ if (!slot->tokens || !slot->keys) {+ fprintf(stderr, "OOM allocating slot history\n"); exit(1);+ }slot->cap = n;}- if (n > 0) memcpy(slot->tokens, tokens, (size_t)n * sizeof(int));+ if (n > 0) {+ memcpy(slot->tokens, tokens, (size_t)n * sizeof(int));+ memcpy(slot->keys, keys, (size_t)n * sizeof(uint64_t));+ }slot->n = n;}@@ -3323,10 +3339,11 @@ static void slot_remember(KVSlot *slot, const int *tokens, int n) {typedef struct {unsigned long long id;float *patches;+ size_t bytes; /* quanti ne porta: l'impronta li legge tutti */int grid_h, grid_w;} PendingImage;-static PendingImage g_pending = {0, NULL, 0, 0};+static PendingImage g_pending = {0, NULL, 0, 0, 0};static void pending_clear(void) {free(g_pending.patches);@@ -3425,6 +3442,7 @@ static int serve_read_req(ServeReq *q, char *verb, size_t verb_size) {(void)fgetc(stdin); /* il '\n' di chiusura */g_pending.id = id;g_pending.patches = patches;+ g_pending.bytes = (size_t)bytes;g_pending.grid_h = grid_h;g_pending.grid_w = grid_w;q->id = id;@@ -3601,6 +3619,24 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {q->id, total, q->max_tokens, room);return 0;}+ /* L'immagine annunciata per QUESTA richiesta, se c'e'. Entra nel+ * confronto del riuso, non lo annulla: sui segnaposto la chiave della+ * posizione e' l'impronta dei byte arrivati (kv_image_key.h), cosi' due+ * foto diverse con gli stessi id non concordano su nessuna posizione+ * immagine, e la stessa foto rimandata concorda su tutte. */+ const int image_here = g_pending.patches && g_pending.id == q->id;+ if (image_here && !m->has_vision) {+ pending_clear();+ free(sequence);+ serve_line("ERROR %llu BAD_REQUEST\n", q->id);+ return 0;+ }+ uint64_t *keys = malloc((size_t)room * sizeof(uint64_t));+ if (!keys) { free(sequence); serve_line("ERROR %llu BAD_REQUEST\n", q->id); return 0; }+ kv_keys_build(keys, sequence, total, m->c.image_token,+ image_here ? kv_image_key(kv_image_hash(g_pending.patches, g_pending.bytes,+ g_pending.grid_h, g_pending.grid_w))+ : KV_IMAGE_KEY_NONE);const double started = now_s();const double s_attn = m->t_attn, s_ffn = m->t_ffn, s_disk = m->t_disk, s_head = m->t_head;@@ -3625,7 +3661,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* e ne ha almeno una in piu': lo stato ricorrente dei layer KDA non si* riavvolge, quindi una divergenza a meta' cache obbliga a rifare. */const int cached = slot->session ? slot->session->filled : 0;- const int common = slot_shared(slot, sequence, total);+ const int common = slot_shared(slot, keys, total);int shared = 0;if (cached > 0 && cached < total && common >= cached)shared = cached;@@ -3646,7 +3682,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* macinare le posizioni che riporta. Si decide prima della fotografia,* che cambierebbe lo stato sotto. */float *resume = NULL;- const int plain = !(g_pending.patches && g_pending.id == q->id) && q->logprobs == 0;+ const int plain = !image_here && q->logprobs == 0;if (plain && cached > 0 && common == total) {if (total == cached && slot->tail_logit && slot->tail_len == cached) {resume = slot->tail_logit;@@ -3665,36 +3701,37 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* fallisce: se lo stato vivo e gia il prompt condiviso, il riuso normale* scatterebbe lo stesso ma il primo token fresco resterebbe senza* predittore, e quindi senza logprob, proprio quello che serve. */- int pinned = resume ? 0 : slot_pin_restore(m, slot, sequence, total);+ int pinned = resume ? 0 : slot_pin_restore(m, slot, sequence, total, common);if (pinned > 0) { shared = pinned; why = "pin"; }if (shared <= 0) {slot_reset(m, slot);slot->session = session_open(m, room);shared = 0;}- /* L'immagine annunciata per QUESTA richiesta, se c'e'. Il prefisso in- * cache non la riguarda: la torre ha gia' dato i suoi embedding quando- * quei token sono stati macinati, e i segnaposto stanno nel prompt. Se il- * riuso salta i token immagine, gli embedding da consumare sono quelli- * delle posizioni nuove, quindi il riuso si annulla quando c'e' un'immagine- * -- costa un prefill in piu' ed e' l'unica cosa che non puo' sbagliare. */- float *vision = NULL;- int n_vision = 0;- if (g_pending.patches && g_pending.id == q->id) {- if (!m->has_vision) {- pending_clear();- free(sequence);- serve_line("ERROR %llu BAD_REQUEST\n", q->id);- return 0;- }- if (shared) { /* niente riuso con un'immagine */- why = "image";- slot_reset(m, slot);- slot->session = session_open(m, room);- shared = 0;+ /* Gli embedding della torre. I segnaposto dentro il prefisso riusato sono+ * gia' stati macinati con QUESTA foto -- e' la chiave a dirlo -- quindi+ * alla torre si chiedono solo le posizioni nuove; se il prefisso le copre+ * tutte, la torre non gira. Un prefisso che tiene piu' posizioni immagine+ * di quante la torre ne produca per questa griglia non descrive questa+ * foto: si riparte da capo, come prima di questa chiave. */+ float *vision = NULL, *vision_new = NULL;+ int n_vision = 0, n_new = 0;+ if (image_here) {+ int held = kv_keys_images(keys, shared);+ const int wanted = kv_keys_images(keys, total);+ if (!(wanted > 0 && held == wanted)) {+ vision = vision_encode(m, g_pending.patches, g_pending.grid_h,+ g_pending.grid_w, &n_vision);+ if (held > n_vision) {+ why = "image";+ slot_reset(m, slot);+ slot->session = session_open(m, room);+ shared = 0;+ held = 0;+ }+ vision_new = vision + (size_t)held * m->vision.config.out_hidden;+ n_new = n_vision - held;}- vision = vision_encode(m, g_pending.patches, g_pending.grid_h,- g_pending.grid_w, &n_vision);pending_clear();}@@ -3710,7 +3747,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {PrefillWatch watch = { q->id, &ctl, &input_eof };float *logits = resume ? resume: forward_prefill(m, slot->session, sequence + shared,- total - shared, vision, n_vision, 0,+ total - shared, vision_new, n_new, 0,prefill_should_halt, &watch);g_echo_k = 0; g_echo_id = 0; /* la lettura riguarda il prefill, non la decodifica *//* Interrotto a meta' prefill: la sessione ha macinato solo `filled` token,@@ -3731,7 +3768,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* e una sua copia per ogni corsa, non per ogni token. Spento di* default: lo pagherebbe ogni risposta, anche di chi non fa mai Continue. */const char *rewind_setting = getenv("GLM53_REWIND");- const int keep_blank = q->logprobs == 0 && n_vision == 0 &&+ const int keep_blank = q->logprobs == 0 && !image_here &&rewind_setting && atoi(rewind_setting);int in_blank = 0;for (int step = 0; step < budget; step++) {@@ -3767,6 +3804,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* ciclo descrive la posizione `filled`, ed e' quello che il turno dopo* riprende se il suo prompt e' questa storia esatta. */if (is_stop(next)) break;+ keys[total] = (uint64_t)(uint32_t)next;sequence[total++] = next;emitted++;char piece[512];@@ -3804,15 +3842,17 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {* Non dopo un turno con un'immagine: il controllo del turno dopo e' sugli* id, e gli id dei segnaposto non dicono quale immagine ha fatto queste* righe. Una richiesta senza IMAGE e con gli stessi id riprenderebbe da- * embedding che non ha mandato. */+ * embedding che non ha mandato. `image_here`, non `n_vision == 0`: una+ * foto riusata per intero dal prefisso non fa girare la torre, ma il+ * turno resta un turno con immagine. */free(slot->tail_logit);- slot->tail_logit = n_vision == 0 ? logits : NULL;+ slot->tail_logit = image_here ? NULL : logits;slot->tail_len = slot->tail_logit ? session->filled : 0;if (!slot->tail_logit) free(logits);logits = NULL;/* La sessione resta allo slot per il turno dopo, con la sequenza che ha* davvero macinato: prompt piu' quello che ha generato. */- slot_remember(slot, sequence, total);+ slot_remember(slot, sequence, keys, total);/* Quanto prefisso lo slot ha risparmiato.** Su STDERR, non nel protocollo. La specifica dice che un server ignora le@@ -3864,6 +3904,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {if (n_vision > 0) slot_reset(m, slot);serve_line("ERROR %llu CANCELLED\n", q->id);free(sequence);+ free(keys);return input_eof ? -1 : 0;}/* ctl == SERVE_CTL_STOP non esce qui: cade nel DONE qui sotto, che e' il@@ -3888,6 +3929,7 @@ static int serve_one(GModel *m, Tok *tokenizer, ServeReq *q) {glm53_vk_report(m, "turn");#endiffree(sequence);+ free(keys);return input_eof ? -1 : 0;}diff --git a/c/kv_image_key.h b/c/kv_image_key.hnew file mode 100644index 00000000..901bccec--- /dev/null+++ b/c/kv_image_key.h@@ -0,0 +1,89 @@+/* kv_image_key.h β name an image span in a KV history by its content.+ *+ * A prefix-reuse record made of token ids cannot tell two images apart: the+ * gateway expands every image into the same run of `image_token_id`+ * placeholders, and the pixels travel beside the prompt in their own frame.+ * Two requests that differ only in the picture therefore have identical id+ * sequences, and an id-only comparison would reuse the attention state built+ * on the OTHER picture β an answer about the wrong photo, with no error.+ * glm53.c used to dodge this by throwing the cached prefix away whenever an+ * image was pending; kv_prefix.h's taint bit does the same for inkling's+ * audio. Both are safe and both pay a full prefill for the image that was+ * already there.+ *+ * THE IDEA. The position of an image token is keyed by what the tower was+ * given, not by the placeholder id: a 64-bit content hash of the patches and+ * their grid, with the top bit set so it can never equal a token id. Real+ * tokens keep their ids. Two histories then agree on an image position only+ * when the same bytes reached the tower, and the reuse decision stays the+ * same all-or-nothing comparison it always was. A placeholder that arrived+ * without a frame keys as "no content", which matches only another such+ * placeholder β the embedding table row is what both saw.+ *+ * The hash is FNV-1a over the grid and the patch bytes as the gateway sent+ * them (post-resize, pre-tower). Two different images meet on the same+ * 63-bit key with probability 2^-63 per pair.+ */+#ifndef KV_IMAGE_KEY_H+#define KV_IMAGE_KEY_H++#include <stddef.h>+#include <stdint.h>++#define KV_IMAGE_KEY_BIT (UINT64_C(1) << 63)++/* Content hash of one image as the tower receives it: the grid first, so the+ * same bytes at another geometry are another image, then the patches. */+static inline uint64_t kv_image_hash(const void *patches, size_t bytes,+ int grid_h, int grid_w) {+ uint64_t h = UINT64_C(0xcbf29ce484222325);+ const uint64_t prime = UINT64_C(0x100000001b3);+ const int dims[2] = { grid_h, grid_w };+ const unsigned char *p = (const unsigned char *)dims;+ for (size_t i = 0; i < sizeof(dims); i++) { h ^= p[i]; h *= prime; }+ p = (const unsigned char *)patches;+ for (size_t i = 0; i < bytes; i++) { h ^= p[i]; h *= prime; }+ return h;+}++/* The key an image position carries: above every token id by construction. */+static inline uint64_t kv_image_key(uint64_t hash) {+ return hash | KV_IMAGE_KEY_BIT;+}++/* Key for a placeholder the engine received no frame for. */+#define KV_IMAGE_KEY_NONE KV_IMAGE_KEY_BIT++static inline int kv_key_is_image(uint64_t key) {+ return (key & KV_IMAGE_KEY_BIT) != 0;+}++/* Build the keyed sequence: token ids as they are, image positions as+ * `image_key`. `image_token < 0` means the checkpoint has no image token and+ * nothing is substituted. Ids are widened unsigned so a stray negative id can+ * never carry the image bit. */+static inline void kv_keys_build(uint64_t *keys, const int *tokens, int n,+ int image_token, uint64_t image_key) {+ for (int t = 0; t < n; t++)+ keys[t] = (image_token >= 0 && tokens[t] == image_token)+ ? image_key : (uint64_t)(uint32_t)tokens[t];+}++/* Leading positions on which two keyed sequences agree. */+static inline int kv_keys_shared(const uint64_t *have, int have_n,+ const uint64_t *want, int want_n) {+ int shared = 0;+ while (shared < have_n && shared < want_n && have[shared] == want[shared])+ shared++;+ return shared;+}++/* How many of the first n positions are image positions: the tower outputs a+ * reused prefix already holds, so the caller can skip that many embeddings. */+static inline int kv_keys_images(const uint64_t *keys, int n) {+ int count = 0;+ for (int t = 0; t < n; t++) count += kv_key_is_image(keys[t]);+ return count;+}++#endif /* KV_IMAGE_KEY_H */diff --git a/c/tests/test_glm53_image_key.c b/c/tests/test_glm53_image_key.cnew file mode 100644index 00000000..2c700577--- /dev/null+++ b/c/tests/test_glm53_image_key.c@@ -0,0 +1,104 @@+/* test_glm53_image_key β the KV reuse decision when a request carries an image.+ *+ * Two requests can have identical token ids and different pictures: the+ * gateway expands every image into the same run of `image_token_id`, and the+ * pixels arrive in their own frame. If the slot compared ids alone, the second+ * request would answer from the attention state built on the first picture β+ * no crash, a plausible reply about the wrong photo. So the state-identity+ * rule gets its own cases here: same placeholders, different bytes β nothing+ * shared; same bytes β everything shared, and the tower outputs the prefix+ * already holds are counted so the caller can skip encoding them.+ */+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include "../kv_image_key.h"++static int failures;+#define CHECK(cond, ...) do { if (!(cond)) { \+ fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \+ fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); failures++; } } while (0)++/* The reuse rule glm53.c applies: the cached prefix is taken whole or not at+ * all, and there must be at least one new position to prefill. */+static int reuse(const uint64_t *have, int cached, const uint64_t *want, int n) {+ if (cached <= 0 || cached >= n) return 0;+ return kv_keys_shared(have, cached, want, n) >= cached ? cached : 0;+}++int main(void) {+ enum { IMG = 5, VOCAB = 200000 };+ const float a[8] = { 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f };+ float b[8]; memcpy(b, a, sizeof(a)); b[3] = 0.41f; /* one patch value off */++ /* ---- the hash names content, not the request ---- */+ const uint64_t ha = kv_image_hash(a, sizeof(a), 4, 4);+ CHECK(ha == kv_image_hash(a, sizeof(a), 4, 4), "same bytes, same grid: same hash");+ CHECK(ha != kv_image_hash(b, sizeof(b), 4, 4), "one value off: another image");+ CHECK(ha != kv_image_hash(a, sizeof(a), 2, 8), "same bytes at another grid: another image");+ CHECK(ha != kv_image_hash(a, sizeof(a) - 4, 4, 4), "a truncated payload is another image");++ /* ---- keys never collide with token ids ---- */+ const uint64_t ka = kv_image_key(ha), kb = kv_image_key(kv_image_hash(b, sizeof(b), 4, 4));+ CHECK(kv_key_is_image(ka) && kv_key_is_image(kb), "image keys carry the image bit");+ CHECK(kv_key_is_image(KV_IMAGE_KEY_NONE), "the frameless placeholder is still an image position");+ CHECK(ka != KV_IMAGE_KEY_NONE, "a real image is not the frameless placeholder");+ {+ const int ids[3] = { 0, VOCAB - 1, 0x7fffffff };+ uint64_t keys[3];+ kv_keys_build(keys, ids, 3, IMG, ka);+ for (int t = 0; t < 3; t++) {+ CHECK(!kv_key_is_image(keys[t]), "token id %d must not read as an image", ids[t]);+ CHECK(keys[t] == (uint64_t)ids[t], "token id %d keeps its id", ids[t]);+ }+ const int neg[1] = { -1 };+ kv_keys_build(keys, neg, 1, IMG, ka);+ CHECK(!kv_key_is_image(keys[0]), "a negative id must not read as an image");+ kv_keys_build(keys, ids, 3, -1, ka); /* checkpoint without an image token */+ CHECK(keys[0] == 0 && keys[2] == 0x7fffffff, "no image token: nothing is substituted");+ }++ /* ---- T10: identical placeholders, different content β no shared KV ---- */+ const int turn1[] = { 7, IMG, IMG, IMG, 9 }; /* what the slot holds */+ const int turn2[] = { 7, IMG, IMG, IMG, 9, 11 }; /* extends it by one token */+ const int cached = 5;+ uint64_t have[5], want[6];+ kv_keys_build(have, turn1, cached, IMG, ka); /* built on picture A */++ kv_keys_build(want, turn2, 6, IMG, kb); /* picture B, same ids */+ CHECK(kv_keys_shared(have, cached, want, 6) == 1,+ "different image: agreement stops at the first placeholder");+ CHECK(reuse(have, cached, want, 6) == 0, "different image: no reuse");++ kv_keys_build(want, turn2, 6, IMG, ka); /* picture A again */+ CHECK(kv_keys_shared(have, cached, want, 6) == cached, "same image: whole prefix agrees");+ CHECK(reuse(have, cached, want, 6) == cached, "same image: the prefix is reused");+ CHECK(kv_keys_images(want, cached) == 3,+ "the reused prefix already holds all 3 tower outputs");+ CHECK(kv_keys_images(want, 1) == 0, "no image position before the span");+ CHECK(kv_keys_images(want, 2) == 1, "a prefix ending mid-span holds one");++ kv_keys_build(want, turn2, 6, IMG, KV_IMAGE_KEY_NONE); /* placeholders, no frame */+ CHECK(reuse(have, cached, want, 6) == 0, "no frame: not the picture the slot holds");++ /* the slot built on frameless placeholders matches only the same */+ kv_keys_build(have, turn1, cached, IMG, KV_IMAGE_KEY_NONE);+ CHECK(reuse(have, cached, want, 6) == cached, "frameless then frameless: same input");+ kv_keys_build(want, turn2, 6, IMG, ka);+ CHECK(reuse(have, cached, want, 6) == 0, "frameless then picture A: not the same input");++ /* ---- the text rules did not move ---- */+ const int text1[] = { 7, 8, 9 }, text2[] = { 7, 8, 9, 10 }, other[] = { 7, 99, 9, 10 };+ uint64_t t1[3], t2[4], t3[4];+ kv_keys_build(t1, text1, 3, IMG, ka);+ kv_keys_build(t2, text2, 4, IMG, ka);+ kv_keys_build(t3, other, 4, IMG, ka);+ CHECK(reuse(t1, 3, t2, 4) == 3, "an extending text prompt reuses all 3");+ CHECK(reuse(t1, 3, t3, 4) == 0, "a diverging text prompt reuses nothing");+ CHECK(reuse(t1, 3, t1, 3) == 0, "equal length: nothing new to prefill, no reuse");+ CHECK(reuse(t1, 0, t2, 4) == 0, "an empty slot reuses nothing");++ if (failures) { fprintf(stderr, "%d failure(s)\n", failures); return 1; }+ puts("test_glm53_image_key: ok");+ return 0;+}