From 56c7cd2f805c22807ede7c3575d9873c9460e20d Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 30 Sep 2026 16:43:28 +0200 Subject: [PATCH] STATE_HASH: pooled= keeps its 0.1.29 meaning (the completed indexer rows); the extent with the spare row is pooled_full= pooled= hashes rows [0, L/4) of every QSA layer's idx_pooled again, as in 0.1.29 and earlier, so a state hash from this engine compares with an older one field by field. The PR's extent (those rows plus the spare row at L/4, which a conversation restore writes back) is pooled_full=; dead= stays. The cache gates compare pooled and pooled_full; the cache-off gate against an older engine compares pooled directly and needs pooled_full only as proof that the candidate is this build. A failed checkpoint save/restore prints its reason to stderr (the ERR line has none). The cache harness reads its files as UTF-8 (Windows' default code page stopped it at the tokenizer), and the isolation gate passes absolute fixture paths (the engine runs in the config's cwd). --- src/program/generate.cpp | 23 +++++++++++++++-------- tools/conversation_cache_disabled.py | 14 +++++++------- tools/conversation_cache_growth.py | 4 ++-- tools/conversation_cache_isolation.py | 6 ++++-- tools/conversation_cache_parity.py | 15 ++++++++------- tools/conversation_cache_soak.py | 6 +++--- tools/test_conversation_cache_disabled.py | 6 ++++-- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/program/generate.cpp b/src/program/generate.cpp index fc11cb04..2afd0591 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -739,13 +739,17 @@ ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g) { /// Copies the running state out. The caller has synchronized the device. bool checkpoint_save(ConvCheckpoint& c, const strata::core::SessionState& ss, const strata::core::ModelGeometry& g) { std::string error; - return strata::core::conversation_checkpoint_save(c, ss, g, error); + if (strata::core::conversation_checkpoint_save(c, ss, g, error)) return true; + std::fprintf(stderr, "strata serve: checkpoint save: %s\n", error.c_str()); // the caller's ERR has no reason + return false; } /// Puts a checkpoint's running state back; the positional cells below it are the caller's to guarantee. bool checkpoint_restore(const ConvCheckpoint& c, strata::core::SessionState& ss, const strata::core::ModelGeometry& g) { std::string error; - return strata::core::conversation_checkpoint_restore(c, ss, g, error); + if (strata::core::conversation_checkpoint_restore(c, ss, g, error)) return true; + std::fprintf(stderr, "strata serve: checkpoint restore: %s\n", error.c_str()); + return false; } // --control-vector-scaled: llama.cpp's `common_control_vector_load` (every file's `direction.` times its scale, @@ -4395,8 +4399,10 @@ int main(int argc, char** argv) { } uint64_t h_ple = hash_dev(ss.ple_hist, ss.ple_hist ? z.ple : 0, 1469598103934665603ull); uint64_t h_tail = 1469598103934665603ull, h_pool = h_tail, h_kv = h_tail, h_stale = h_tail; - uint64_t h_dead = h_tail; - uint64_t h_pool_complete = h_tail; // upstream's extent, without the moving spare row + // pooled= keeps its 0.1.29 meaning: the completed rows [0, L / idx_block) only. pooled_full= adds the + // spare row at L / idx_block (the `dead` key the next block completion overwrites), which a + // conversation restore writes back; dead= is the spare key itself + uint64_t h_dead = h_tail, h_pool_full = h_tail; const int64_t kvb = qs.head_dim, scb = (qs.head_dim / 64) * 2; // a state's K/V arrays and their bytes per (cell, head) row: the host copy when it has one auto kv_arrays = [&](const strata::core::QsaState& st) { @@ -4424,8 +4430,9 @@ int main(int argc, char** argv) { const strata::core::QsaState& st = ss.qsa_states[i]; h_tail = hash_dev(st.idx_tail, z.tail, h_tail); h_dead = hash_dev(st.idx_dead, z.dead, h_dead); - h_pool_complete = hash_dev(st.idx_pooled, (size_t) (L / qs.idx_block) * qs.idx_dim * 4, h_pool_complete); - h_pool = hash_dev(st.idx_pooled, (size_t) (L > 0 ? L / qs.idx_block + 1 : 0) * qs.idx_dim * 4, h_pool); + h_pool = hash_dev(st.idx_pooled, (size_t) (L / qs.idx_block) * qs.idx_dim * 4, h_pool); + h_pool_full = hash_dev(st.idx_pooled, (size_t) (L > 0 ? L / qs.idx_block + 1 : 0) * qs.idx_dim * 4, + h_pool_full); // KV streaming: the host copy is the identity layout and holds every cell for (const auto& [pool, w] : kv_arrays(st)) { h_kv = hash_cells(pool, w, 0, L, h_kv); @@ -4442,11 +4449,11 @@ int main(int argc, char** argv) { return 1; } std::fprintf(stderr, "strata serve: STATE_HASH L=%lld gdn=%016llx ple=%016llx tail=%016llx pooled=%016llx " - "kv=%016llx mtp=%016llx stale=%016llx dead=%016llx pooled_complete=%016llx ple_prev=%d,%d\n", (long long) L, + "kv=%016llx mtp=%016llx stale=%016llx dead=%016llx pooled_full=%016llx ple_prev=%d,%d\n", (long long) L, (unsigned long long) h_gdn, (unsigned long long) h_ple, (unsigned long long) h_tail, (unsigned long long) h_pool, (unsigned long long) h_kv, (unsigned long long) h_mtp, (unsigned long long) h_stale, (unsigned long long) h_dead, - (unsigned long long) h_pool_complete, ss.ple_prev[0], ss.ple_prev[1]); + (unsigned long long) h_pool_full, ss.ple_prev[0], ss.ple_prev[1]); } const int64_t req_hits = drive.d.cache_hits - decode_hits0; const int64_t req_look = (drive.d.cache_hits + drive.d.cache_admitted + drive.d.cache_refused) - decode_look0; diff --git a/tools/conversation_cache_disabled.py b/tools/conversation_cache_disabled.py index db3c05f2..15c8d791 100644 --- a/tools/conversation_cache_disabled.py +++ b/tools/conversation_cache_disabled.py @@ -22,7 +22,8 @@ from serve.server import StrataEngine, child_env NAMES = ('short', 'A', 'A-live', 'A-checkpoint', 'B', 'A-return', 'B-return', 'A-return-again') SETTINGS = ('version', 'context', 'kv', 'kv_resident', 'expert_slots', 'spec', 'mtp_max', 'lookup', 'cvec', 'pcie_frac', 'spec_min_p', 'pool_workers') -# Untouched 0.1.27 does not emit the indexer `dead` field added by this PR. +# Untouched upstream does not emit the indexer `dead` and `pooled_full` fields +# added by this PR; `pooled` is the completed-row extent in both engines. # Draft scratch and unused page padding are not comparable authoritative state. STATE_KEYS = ('L', 'gdn', 'ple', 'tail', 'pooled', 'kv', 'ple_prev') @@ -34,10 +35,9 @@ def state_hashes(text, *, candidate=False): fields = dict(re.findall(r'(\w+)=([0-9a-f,-]+)', line)) require(set(STATE_KEYS) <= fields.keys(), 'missing upstream-compatible state fields') if candidate: - # The PR's pooled hash includes the spare row. Require the - # separately emitted upstream extent instead of comparing unlike bytes. - require('pooled_complete' in fields, 'candidate lacks completed-row fingerprint') - fields['pooled'] = fields['pooled_complete'] + # Evidence that the candidate is a build with this change: it + # also fingerprints the spare row (compared by the cache-on gates). + require('pooled_full' in fields, 'candidate lacks the pooled_full fingerprint') hashes.append({key: fields[key] for key in STATE_KEYS}) return hashes @@ -109,7 +109,7 @@ def main(): if not a.run: print('Dry run: upstream and disabled candidates; identical inputs/settings, known answers and state parity.') return - cfg = json.loads(a.config.read_text()) + cfg = json.loads(a.config.read_text(encoding='utf-8')) tok_path = Path(cfg['tokenizer']) tok = load_tokenizer(tok_path) tpl_path = tok_path / 'chat_template.jinja' @@ -176,7 +176,7 @@ def main(): engine.proc.wait(timeout=30) engine.log.close() arm['request_digest'] = hashlib.sha256(json.dumps(results['requests'], sort_keys=True).encode()).hexdigest() - log_text = log.read_text() + log_text = log.read_text(encoding='utf-8') hashes = state_hashes(log_text, candidate=index != 0) require(len(hashes) == len(arm['records']), f'{name}: missing state hashes') for record, fingerprint in zip(arm['records'], hashes): diff --git a/tools/conversation_cache_growth.py b/tools/conversation_cache_growth.py index d28683a1..f52cef83 100644 --- a/tools/conversation_cache_growth.py +++ b/tools/conversation_cache_growth.py @@ -76,7 +76,7 @@ def main(): if not a.run: print('Dry run: paired full/incremental captures; growth, interruption, rewind and branch; no model loaded.') return - cfg = json.loads(a.config.read_text()) + cfg = json.loads(a.config.read_text(encoding='utf-8')) tok_path = Path(cfg['tokenizer']) tok = load_tokenizer(tok_path) template = ChatTemplate(tok_path / 'chat_template.jinja') @@ -147,7 +147,7 @@ def main(): engine.close() engine.proc.wait(timeout=30) engine.log.close() - log_text = log.read_text() + log_text = log.read_text(encoding='utf-8') hashes = state_hashes(log_text) require(len(hashes) == len(arm['records']), 'missing state fingerprints') for record, state in zip(arm['records'], hashes): diff --git a/tools/conversation_cache_isolation.py b/tools/conversation_cache_isolation.py index 5c201300..c0f0d52b 100644 --- a/tools/conversation_cache_isolation.py +++ b/tools/conversation_cache_isolation.py @@ -75,10 +75,12 @@ def main(): ap.add_argument('--scenario', choices=('image', 'add', 'project'), required=True) ap.add_argument('--run', action='store_true') args = ap.parse_args() + # the fixtures are passed to an engine that runs in the config's cwd, not this shell's + args.output = args.output.resolve() if not args.run: print(f'Dry run: {args.scenario} isolation; no model loaded or files created.') return - cfg = json.loads(args.config.read_text()) + cfg = json.loads(args.config.read_text(encoding='utf-8')) native = cfg['args'][cfg['args'].index('--native') + 1] geometry = GGUFFile(native).metadata width = geometry['qwen4exp.embedding_length'] @@ -130,7 +132,7 @@ def main(): generate('off-again', A, steering=False) finally: engine.close() - hashes = state_hashes(log.read_text()) + hashes = state_hashes(log.read_text(encoding='utf-8')) require(len(hashes) == len(records), 'missing isolation state hashes') for record, state in zip(records, hashes): record['state'] = state diff --git a/tools/conversation_cache_parity.py b/tools/conversation_cache_parity.py index cea5fc59..6e7b4fca 100644 --- a/tools/conversation_cache_parity.py +++ b/tools/conversation_cache_parity.py @@ -15,7 +15,8 @@ from serve.server import StrataEngine, child_env from serve.frontend import ChatTemplate import strata_tokenizer as ST -STATE_KEYS = ('L', 'gdn', 'ple', 'tail', 'dead', 'pooled', 'kv', 'ple_prev') +# pooled: the completed indexer rows (the 0.1.29 extent); pooled_full: those plus the spare row +STATE_KEYS = ('L', 'gdn', 'ple', 'tail', 'dead', 'pooled', 'pooled_full', 'kv', 'ple_prev') def require(condition, message): if not condition: @@ -23,12 +24,12 @@ def require(condition, message): def load_tokenizer(path): - vocab = json.loads((path / 'vocab.json').read_text()) + vocab = json.loads((path / 'vocab.json').read_text(encoding='utf-8')) tokens = [None] * len(vocab) for token, index in vocab.items(): tokens[index] = token - return ST.Tokenizer(tokens, (path / 'merges.txt').read_text().split('\n'), - json.loads((path / 'token_type.json').read_text())) + return ST.Tokenizer(tokens, (path / 'merges.txt').read_text(encoding='utf-8').split('\n'), + json.loads((path / 'token_type.json').read_text(encoding='utf-8'))) def state_hashes(text): @@ -170,7 +171,7 @@ def main(): print(f'Dry run: {a.scenario}; paired baseline/candidate; fixed residency, greedy output.') print('No model loaded. Use --run only with a separately available GPU/test window.') return - cfg = json.loads(a.config.read_text()) + cfg = json.loads(a.config.read_text(encoding='utf-8')) p = Path(cfg['tokenizer']) tok = load_tokenizer(p) tpl = ChatTemplate(p / 'chat_template.jinja') @@ -217,13 +218,13 @@ def main(): generate(continuation, 8, 'A+-checkpoint') finally: engine.close() - hashes = state_hashes(log.read_text()) + hashes = state_hashes(log.read_text(encoding='utf-8')) require(len(hashes) == len(records), 'missing state hashes') for record, fingerprint in zip(records, hashes): record['state'] = fingerprint results[label] = records if label == 'candidate' and a.scenario != 'reuse': - log_text = log.read_text() + log_text = log.read_text(encoding='utf-8') parks = re.findall(r'conversation cache: parked \d+ tokens .*?parked=(\d+) bytes=(\d+) evictions=(\d+)(?: snapshot_bytes=(\d+))?', log_text) results['pressure'] = { 'parks': [dict(zip(('parked', 'bytes', 'evictions', 'snapshot_bytes'), diff --git a/tools/conversation_cache_soak.py b/tools/conversation_cache_soak.py index a7c95fdd..fc9bb0e5 100644 --- a/tools/conversation_cache_soak.py +++ b/tools/conversation_cache_soak.py @@ -78,7 +78,7 @@ def main(): print(f'Dry run: {args.cycles} known-answer A/B/A cycles; approximate lengths {lengths}.') print('No model loaded or files created. Use --run only in an exclusive GPU test window.') return - cfg = json.loads(args.config.read_text()) + cfg = json.loads(args.config.read_text(encoding='utf-8')) path = Path(cfg['tokenizer']) tok = load_tokenizer(path) template = ChatTemplate(path / 'chat_template.jinja') @@ -117,7 +117,7 @@ def main(): def generate(ids, count): nonlocal hash_count output = [t for t in engine.generate(ids, count, {'temperature': 0}, threading.Event()) if t is not None] - text = log.read_text() + text = log.read_text(encoding='utf-8') hashes = state_hashes(text) require(len(hashes) == hash_count + 1 and int(hashes[-1]['L']) >= len(ids), 'missing current state fingerprint') hash_count += 1 @@ -125,7 +125,7 @@ def main(): occupancy = re.findall(r'parked=\d+ bytes=(\d+)', text) rss = None try: - match = re.search(r'^VmRSS:\s+(\d+) kB', Path(f'/proc/{engine.proc.pid}/status').read_text(), re.M) + match = re.search(r'^VmRSS:\s+(\d+) kB', Path(f'/proc/{engine.proc.pid}/status').read_text(encoding='utf-8'), re.M) if match: rss = int(match[1]) * 1024 except OSError: diff --git a/tools/test_conversation_cache_disabled.py b/tools/test_conversation_cache_disabled.py index 3eec19b2..d389ac9f 100644 --- a/tools/test_conversation_cache_disabled.py +++ b/tools/test_conversation_cache_disabled.py @@ -35,8 +35,10 @@ class DisabledCacheGateTest(unittest.TestCase): line = 'strata serve: STATE_HASH L=2 gdn=ab ple=cd tail=ef pooled=12 kv=34 mtp=56 stale=78 ple_prev=1,2' self.assertEqual(set(state_hashes(line)[0]), set(STATE_KEYS)) self.assertEqual(state_hashes(line), state_hashes(line + ' dead=99')) - self.assertEqual(state_hashes(line), state_hashes(line.replace('pooled=12', 'pooled=ff') + - ' pooled_complete=12', candidate=True)) + # the candidate's pooled= is the upstream extent; pooled_full= (with the spare row) is not compared + self.assertEqual(state_hashes(line), state_hashes(line + ' dead=99 pooled_full=ff', candidate=True)) + self.assertNotEqual(state_hashes(line), state_hashes(line.replace('pooled=12', 'pooled=ff') + + ' pooled_full=12', candidate=True)) with self.assertRaises(AssertionError): state_hashes(line, candidate=True) with self.assertRaises(AssertionError):