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(qwen38): reject pins whose attention rows were overwritten

bokiko024bef576e38
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index a9af52fa..32c7c79e 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -843,7 +843,9 @@ jobs:
# the oracle fixture has no tokenizer; serve mode needs one
python3 tools/make_edge_tiny_tokenizer.py --vocab-size "$(python3 -c 'import json;print(json.load(open("qwen38_tiny/config.json"))["vocab_size"])')" qwen38_tiny
make qwen38 >/dev/null
- QWEN38_TINY=qwen38_tiny python3 -m unittest -v tests.test_qwen38_dashboard
+ QWEN38_TINY=qwen38_tiny python3 -m unittest -v tests.test_qwen38_dashboard tests.test_qwen38_brio
+ python3 tools/make_edge_tiny_tokenizer.py --vocab-size 64 qwen38_tiny_fp8
+ QWEN38_TINY=qwen38_tiny_fp8 python3 -m unittest -v tests.test_qwen38_brio
inkling-oracle:
name: Inkling oracle (token-exact vs transformers)
diff --git a/c/Makefile b/c/Makefile
index edc703f6..7a8a12ac 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1120,7 +1120,7 @@ deepseek_v41$(EXE): deepseek_v41.c cli_args.h st.h json.h tok.h tok_unicode.h co
# loader and SERVE=1 protocol. With CUDA=1 it links the same expert tier as
# qwen36 (fp8 streaming mode: hot experts get VRAM copies, the RAM LRU stays);
# without it the tier header's inline stubs keep the build toolkit-free.
-qwen38$(EXE): qwen38.c pin_pool.h cli_args.h qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h omp_tune.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h edge_adapter_internal.h edge_adapters.h edge_runtime.h qwen38_vision.h segment_adapter_internal.h segment_adapters.h segment_runtime.h qwen36_tier.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
+qwen38$(EXE): qwen38.c pin_pool.h cli_args.h qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h omp_tune.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h edge_adapter_internal.h edge_adapters.h edge_runtime.h qwen38_vision.h segment_adapter_internal.h segment_adapters.h segment_runtime.h qwen36_tier.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) qwen38.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o qwen38$(EXE) $(QWEN36_LDFLAGS)
.PHONY: qwen38-tiny-generate qwen38-tiny-check
@@ -1275,13 +1275,13 @@ tests/test_st_pread$(EXE): tests/test_st_pread.c st.h json.h compat.h
tests/test_st_slice$(EXE): tests/test_st_slice.c st.h json.h compat.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
-tests/test_qwen38_tokenizer$(EXE): tests/test_qwen38_tokenizer.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h edge_runtime.c edge_runtime.h edge_adapters.h edge_adapter_internal.h st.h json.h compat.h quant.h route_trace.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_tokenizer$(EXE): tests/test_qwen38_tokenizer.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h edge_runtime.c edge_runtime.h edge_adapters.h edge_adapter_internal.h st.h json.h compat.h quant.h route_trace.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< edge_runtime.c -o $@ $(NOCUDA_LDFLAGS)
-tests/test_qwen38_config$(EXE): tests/test_qwen38_config.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_config$(EXE): tests/test_qwen38_config.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)
-tests/test_qwen38_serve_framing$(EXE): tests/test_qwen38_serve_framing.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_serve_framing$(EXE): tests/test_qwen38_serve_framing.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)
tests/test_qwen38_vision$(EXE): tests/test_qwen38_vision.c qwen38_vision.h st.h json.h compat.h
@@ -1303,13 +1303,13 @@ qwen38-vision-serve-check: qwen38$(EXE)
$(PYTHON) tools/make_edge_tiny_tokenizer.py --vocab-size 64 ./qwen38_mm_tiny
$(PYTHON) tests/test_qwen38_vision_serve.py --binary ./qwen38$(EXE) --fixture ./qwen38_mm_tiny
-tests/test_qwen38_prefix$(EXE): tests/test_qwen38_prefix.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_prefix$(EXE): tests/test_qwen38_prefix.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)
-tests/test_qwen38_metrics$(EXE): tests/test_qwen38_metrics.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_metrics$(EXE): tests/test_qwen38_metrics.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)
-tests/test_qwen38_native_weights$(EXE): tests/test_qwen38_native_weights.c qwen38.c qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h segment_runtime.c segment_runtime.h segment_adapters.h segment_adapter_internal.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
+tests/test_qwen38_native_weights$(EXE): tests/test_qwen38_native_weights.c qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h segment_runtime.c segment_runtime.h segment_adapters.h segment_adapter_internal.h st.h json.h compat.h quant.h route_trace.h serve_codec.h tok_unicode.h tok_unicode_o200k.h
$(CC) $(NOCUDA_CFLAGS) $< segment_runtime.c -o $@ $(NOCUDA_LDFLAGS)
tests/test_st_map$(EXE): tests/test_st_map.c st.h json.h compat.h
@@ -1607,7 +1607,7 @@ $(SEGMENT_BUILD_DIR)/qwen36.o: qwen36.c segment_runtime.h edge_runtime.h \
edge_adapter_internal.h st.h | $(SEGMENT_BUILD_DIR)
$(CC) $(SEGMENT_CPU_CFLAGS) -DQWEN36_NO_MAIN -c qwen36.c -o $@
-$(SEGMENT_BUILD_DIR)/qwen38.o: qwen38.c qwen38_core.h segment_runtime.h \
+$(SEGMENT_BUILD_DIR)/qwen38.o: qwen38.c qwen38_core.h kv_prefix.h segment_runtime.h \
segment_adapters.h segment_adapter_internal.h edge_runtime.h \
edge_adapters.h edge_adapter_internal.h st.h json.h compat.h quant.h \
qwen38_nfc.h qwen38_nfc_tables.h route_trace.h tok_unicode.h \
@@ -1869,7 +1869,7 @@ tests/test_qwen36_tier_int8_decode$(EXE): tests/test_qwen36_tier_int8_decode.c t
# The qwen38 engine through its own main() on the fake backend: tier start
# from the FP8 fixture, reduced CPU list, qt_note on recycled slots, oracle
# tokens unchanged. Needs the fixture (qwen38-tiny-fp8-generate).
-tests/test_qwen38_tier_engine$(EXE): tests/test_qwen38_tier_engine.c tests/qwen36_fake_cuda.h qwen38.c qwen38_core.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h quant.h route_trace.h
+tests/test_qwen38_tier_engine$(EXE): tests/test_qwen38_tier_engine.c tests/qwen36_fake_cuda.h qwen38.c qwen38_core.h kv_prefix.h qwen36_tier.c qwen36_tier.h backend_cuda.h cli_args.h st.h json.h compat.h quant.h route_trace.h
$(CC) $(CFLAGS) -DCOLI_CUDA $< -o $@ $(LDFLAGS)
tests/test_serve_poll$(EXE): tests/test_serve_poll.c serve_poll.h
diff --git a/c/qwen38.c b/c/qwen38.c
index 9eea6cfb..45c57d12 100644
--- a/c/qwen38.c
+++ b/c/qwen38.c
@@ -1514,8 +1514,10 @@ static int q38_prefix_cache_save(Model *m,const int *ids,int len,const float *lo
static int q38_prefix_restore(Model *m,const int *ids,int len){
if(!m||!ids||len<1||!g_q38_prefix.valid||g_q38_prefix.owner!=m||
g_q38_prefix.len<1||g_q38_prefix.len>len||
- memcmp(g_q38_prefix.ids,ids,(size_t)g_q38_prefix.len*sizeof(int)))return 0;
- q38_prefix_copy_state(m,0);m->kv_len=g_q38_prefix.len;return g_q38_prefix.len;
+ memcmp(g_q38_prefix.ids,ids,(size_t)g_q38_prefix.len*sizeof(int))||
+ !kv_prefix_holds(&m->kvp,g_q38_prefix.ids,g_q38_prefix.len))return 0;
+ q38_prefix_copy_state(m,0);m->kv_len=g_q38_prefix.len;
+ m->kvp.len=g_q38_prefix.len;return g_q38_prefix.len;
}
static const float *q38_prefix_cached_logits(Model *m){
@@ -1632,13 +1634,20 @@ static int serve_one(Model *m, ServeReq *q){
* il client ha dichiarato, e battono la cache automatica, che insegue solo
* l'ultimo prompt. Se nessuno serve, si ricade su quella. */
int reuse=0; const float *pin_lo=NULL;
+ /* Image embeddings are not described by token identity. */
+ if(m->vis_map && m->vis_rows_n>0){
+ kv_prefix_taint(&m->kvp);q38_prefix_cache_invalidate();
+ }
{
int ps=coli_pin_best(&g_q38_pins,ids,np);
while(ps>=0){
ColiPin *k=&g_q38_pins.slot[ps];
Q38PinState *st=(Q38PinState*)k->state;
- if(st && q38_pin_state_copy(m,&st,0)){
- m->kv_len=k->len; reuse=k->len; pin_lo=k->logit;
+ /* Pins omit K/V/indexer rows. Reject one whose rows were overwritten. */
+ if(st && kv_prefix_holds(&m->kvp,k->ids,k->len) &&
+ q38_pin_state_copy(m,&st,0)){
+ m->kv_len=k->len; m->kvp.len=k->len;
+ reuse=k->len; pin_lo=k->logit;
coli_pin_touch(&g_q38_pins,ps);
break;
}
diff --git a/c/qwen38_core.h b/c/qwen38_core.h
index 30dcfdb2..ddcbc4ee 100644
--- a/c/qwen38_core.h
+++ b/c/qwen38_core.h
@@ -9,6 +9,7 @@
*/
#ifndef COLI_QWEN38_CORE_H
#define COLI_QWEN38_CORE_H
+#include "kv_prefix.h"
#include <pthread.h> /* q38_ehit_mark publishes the lazy HITS table under a lock */
#define Q38_MAX_LAYERS 512
@@ -127,6 +128,7 @@ typedef struct {
float **DN_rec, **DN_conv;
float **K, **V, **IK;
int kv_len, kv_cap, max_t;
+ kv_prefix kvp; /* token identity of the live attention rows, not a snapshot */
st_tensor *ple_parts[Q38_MAX_PLE_PARTS];
char ple_part_names[Q38_MAX_PLE_PARTS][320];
int64_t ple_part_start[Q38_MAX_PLE_PARTS + 1];
@@ -2130,6 +2132,7 @@ static void q38_moe(Model *m,Layer *l,int layer,const float *x,int S,float *out)
}
static void reset_recurrent(Model *m) {
+ kv_prefix_clear(&m->kvp);
Cfg *c=&m->c;
for(int i=0;i<c->layers;i++)if(!c->is_attn[i]){
memset(m->DN_rec[i],0,(size_t)c->dn_vheads*c->dn_kdim*c->dn_vdim*sizeof(float));
@@ -2146,6 +2149,7 @@ static void ensure_kv(Model *m) {
m->K[i]=falloc((int64_t)c->kv_heads*m->max_t*c->head_dim);m->V[i]=falloc((int64_t)c->kv_heads*m->max_t*c->head_dim);m->IK[i]=falloc((int64_t)m->max_t*c->idx_dim);
}
m->kv_cap=m->max_t;
+ kv_prefix_alloc(&m->kvp,m->kv_cap); /* ensure_kv discards the old rows */
}
/* Run only the requested native layer interval over hyper-residual activations.
@@ -2226,6 +2230,10 @@ static float *step(Model *m,const int *ids,int S,int pos_base) {
q38_gr_read(m,&l->mlp_gr,hyper,S,mixed,inject);q38_moe(m,l,i,mixed,S,block);q38_gr_apply(c,hyper,block,inject,S);
}
q38_gr_read(m,&m->final_gr,hyper,S,mixed,NULL);m->kv_len=pos_base+S;
+ /* Rewinding and writing a shorter branch invalidates its old tail. */
+ if(m->kvp.len>pos_base)m->kvp.len=pos_base;
+ kv_prefix_record(&m->kvp,ids,pos_base,S);
+ if(m->vis_map && m->vis_rows_n>0)kv_prefix_taint(&m->kvp);
float *logit=falloc(c->vocab);double phase_started=now_s();
/* Lettura del prefill: la posizione p predice il token p+1. Il primo token
* fresco lo predice la fotografia del prefisso, quando c'e. Pagata solo da
@@ -2310,6 +2318,7 @@ static void q38_layer_free(Layer *l) {
static void q38_model_free(Model *m) {
if(!m) return;
+ kv_prefix_free(&m->kvp);
for(int i=0;i<m->c.layers;i++) {
if(m->L)q38_layer_free(&m->L[i]);
if(m->cache) {
diff --git a/c/tests/test_qwen38_brio.py b/c/tests/test_qwen38_brio.py
new file mode 100644
index 00000000..d7f1468e
--- /dev/null
+++ b/c/tests/test_qwen38_brio.py
@@ -0,0 +1,149 @@
+"""Pinned Qwen3.8 scores must survive unrelated requests and sibling branches.
+
+Uses the generated tiny fixture and its 64-token byte tokenizer. All prompt
+bytes are below 64; no unknown-token aliases can hide a divergent prefix.
+QWEN38_TINY selects BF16 or FP8 fixtures. No full checkpoint is needed.
+"""
+import math
+import os
+from pathlib import Path
+import queue
+import subprocess
+import sys
+import threading
+import time
+import unittest
+
+HERE = Path(__file__).resolve().parent.parent
+ENGINE = Path(os.environ.get('QWEN38_BINARY', HERE / ('qwen38.exe' if sys.platform == 'win32' else 'qwen38')))
+FIXTURE = Path(os.environ.get('QWEN38_TINY', HERE / 'qwen38_tiny'))
+
+
+class Engine:
+ def __init__(self):
+ env = dict(os.environ, SNAP=str(FIXTURE), SERVE='1',
+ TOK=str(FIXTURE / 'tokenizer.json'), OMP_NUM_THREADS='2',
+ COLI_NO_OMP_TUNE='1', COLI_CUDA='0', Q38_TRUNK_GPU='0',
+ Q38_MAXT='128')
+ self.p = subprocess.Popen([str(ENGINE), '1', '8'], env=env,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL)
+ self.frames = queue.Queue()
+ def read():
+ try:
+ while True:
+ raw = self.p.stdout.readline()
+ if not raw:
+ raise EOFError('engine closed')
+ fields = raw.decode().replace('\x01', '').split()
+ if fields and fields[0] in ('ECHO', 'DATA'):
+ n = int(fields[2])
+ if len(self.p.stdout.read(n)) != n or self.p.stdout.read(1) != b'\n':
+ raise EOFError('short protocol payload')
+ self.frames.put(fields)
+ except Exception as exc:
+ self.frames.put(exc)
+ self.reader = threading.Thread(target=read, daemon=True)
+ self.reader.start()
+ try:
+ deadline = time.monotonic() + 30
+ while self.next(deadline)[0] != 'READY':
+ pass
+ except BaseException:
+ self.close()
+ raise
+
+ def next(self, deadline):
+ frame = self.frames.get(timeout=max(.01, deadline - time.monotonic()))
+ if isinstance(frame, Exception):
+ raise frame
+ return frame
+
+ def submit(self, rid, text, count=0, extension=' logprobs=1'):
+ data = text.encode('ascii')
+ assert all(b < 64 for b in data)
+ self.p.stdin.write(f'SUBMIT {rid} 0 {len(data)} {count} 0 1{extension}\n'.encode()
+ + data + b'\n')
+ self.p.stdin.flush()
+ scores = {}
+ accepted = False
+ deadline = time.monotonic() + 30
+ while True:
+ f = self.next(deadline)
+ if f[0] in ('ACCEPT', 'ECHO', 'DATA', 'DONE', 'ERROR'):
+ assert f[1] == str(rid), f
+ if f[0] == 'ACCEPT':
+ assert int(f[2]) == len(data), f
+ accepted = True
+ elif f[0] == 'ECHO':
+ score = float(f[4])
+ assert math.isfinite(score), f
+ scores[int(f[3])] = score
+ elif f[0] == 'ERROR':
+ raise AssertionError(f)
+ elif f[0] == 'DONE':
+ assert accepted
+ return scores
+
+ def close(self):
+ self.p.terminate()
+ try:
+ self.p.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ self.p.kill()
+ self.p.wait(timeout=5)
+ self.reader.join(timeout=5)
+ self.p.stdin.close()
+ self.p.stdout.close()
+
+
[email protected](ENGINE.is_file() and (FIXTURE / 'tokenizer.json').is_file(),
+ 'build qwen38 and generate QWEN38_TINY with a byte tokenizer')
+class Qwen38Brio(unittest.TestCase):
+ def engine(self):
+ e = Engine()
+ self.addCleanup(e.close)
+ return e
+
+ def test_stale_pin_after_unrelated_generation(self):
+ prefix, option = '123 456:', ' 0'
+ for repetition in range(2):
+ with self.subTest(repetition=repetition):
+ e = self.engine()
+ e.submit(1, prefix, extension=' logprobs=1 pin=1')
+ warm = e.submit(2, prefix + option)
+ e.submit(3, '789 012:', count=3, extension='')
+ after = e.submit(4, prefix + option)
+ fresh = self.engine().submit(1, prefix + option)
+ tail = {8, 9}
+ self.assertEqual(set(warm), tail, 'valid pins must still skip the prefix')
+ self.assertTrue(tail <= set(after))
+ self.assertEqual({p: warm[p] for p in tail}, {p: fresh[p] for p in tail})
+ self.assertEqual({p: after[p] for p in tail}, {p: fresh[p] for p in tail})
+
+ def test_shorter_branch_drops_old_tail(self):
+ e = self.engine()
+ e.submit(1, '123 456:', extension=' logprobs=1 pin=1')
+ e.submit(2, '123 456: 01', extension=' logprobs=1 pin=1')
+ e.submit(3, '123 456: ')
+ after = e.submit(4, '123 456: 012')
+ fresh = self.engine().submit(1, '123 456: 012')
+ self.assertEqual(set(after), set(range(8, 12)))
+ self.assertEqual(after, {p: fresh[p] for p in after})
+
+ def test_nested_pins_and_sibling_branches(self):
+ e = self.engine()
+ e.submit(1, '123', extension=' logprobs=1 pin=1')
+ e.submit(2, '123 456:', extension=' logprobs=1 pin=1')
+ e.submit(3, '123 456: 0')
+ # Same shallow pin, a different question overwrites the deeper pin's KV.
+ sibling = e.submit(4, '123 789: 1')
+ self.assertEqual(set(sibling), set(range(3, 10)))
+ returned = e.submit(5, '123 456: 0')
+ fresh = self.engine().submit(1, '123 456: 0')
+ self.assertEqual(set(returned), set(range(3, 10)))
+ self.assertEqual(returned, {p: fresh[p] for p in returned})
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/c/tests/test_qwen38_prefix.c b/c/tests/test_qwen38_prefix.c
index 6baf9927..7d13068e 100644
--- a/c/tests/test_qwen38_prefix.c
+++ b/c/tests/test_qwen38_prefix.c
@@ -30,6 +30,7 @@ static int check_state(const Model *m,float value){
static void free_fake(Model *m){
q38_prefix_cache_release(m);
+ kv_prefix_free(&m->kvp);
for(int i=0;i<m->c.layers;i++){
free(m->DN_rec[i]);free(m->DN_conv[i]);
free(m->K[i]);free(m->V[i]);free(m->IK[i]);
@@ -61,6 +62,7 @@ int main(void){
const int first[]={1,2,3};float first_logits[8];
for(int i=0;i<8;i++)first_logits[i]=(float)(100+i);
+ kv_prefix_record(&m.kvp,first,0,3);
fill_state(&m,7.f);CHECK(q38_prefix_cache_save(&m,first,3,first_logits,0));
fill_state(&m,99.f);m.kv_len=3;
const int extension[]={1,2,3,4};
@@ -73,6 +75,7 @@ int main(void){
/* An exact prompt restores the recurrent state and its saved logits. */
fill_state(&m,55.f);CHECK(q38_prefix_restore(&m,first,3)==3);CHECK(check_state(&m,7.f));
fill_state(&m,56.f);CHECK(q38_prefix_restore(&m,extension,4)==3);CHECK(check_state(&m,7.f));
+ kv_prefix_record(&m.kvp,extension,0,4);
CHECK(q38_prefix_cache_save(&m,extension,4,first_logits,0));
fill_state(&m,88.f);CHECK(q38_prefix_restore(&m,extension,4)==4);CHECK(check_state(&m,7.f));
const float *cached=q38_prefix_cached_logits(&m);CHECK(cached&&cached[0]==100.f&&cached[7]==107.f);
@@ -84,11 +87,13 @@ int main(void){
CHECK(q38_prefix_cache_save(&m,first,3,first_logits,1));
CHECK(g_q38_prefix.pinned&&g_q38_prefix.len==3);
CHECK(q38_prefix_restore(&m,extension,4)==3);
+ kv_prefix_record(&m.kvp,extension,0,4);
CHECK(q38_prefix_cache_save(&m,extension,4,first_logits,0));
CHECK(!g_q38_prefix.pinned&&g_q38_prefix.len==4);
CHECK(q38_prefix_cache_save(&m,extension,4,first_logits,1));
q38_prefix_cache_invalidate();
CHECK(!g_q38_prefix.pinned&&!g_q38_prefix.valid);
+ kv_prefix_record(&m.kvp,extension,0,4);
CHECK(q38_prefix_cache_save(&m,extension,4,first_logits,0));
/* A mismatch is never restored; the serve miss path invalidates then
@@ -101,6 +106,14 @@ int main(void){
for(size_t i=0;i<g_q38_prefix.rec_cells;i++)CHECK(m.DN_rec[0][i]==0.f);
for(size_t i=0;i<g_q38_prefix.conv_cells;i++)CHECK(m.DN_conv[0][i]==0.f);
for(size_t i=0;i<g_q38_prefix.ple_cells;i++)CHECK(m.PLE_conv_state[i]==0.f);
+ /* A saved recurrent state cannot resurrect discarded attention rows. */
+ kv_prefix_record(&m.kvp,extension,0,4);
+ CHECK(q38_prefix_cache_save(&m,extension,4,first_logits,0));
+ kv_prefix_clear(&m.kvp);
+ CHECK(q38_prefix_restore(&m,extension,4)==0);
+ kv_prefix_record(&m.kvp,extension,0,4);
+ kv_prefix_taint(&m.kvp);
+ CHECK(q38_prefix_restore(&m,extension,4)==0);
free_fake(&m);
puts("qwen38 prefix: single QSA allocation, exact/extension reuse, reset: ok");
return 0;