round 41d: clm frequent mid-prompt edit hardening (2-pass non-overlapping scr_relocate, lcp>=0, max_runs=64, active_path replacement)
72 unmodified lines7374757677777879808134 unmodified lines72 unmodified lines- 24 kib `tail_bak_` qsa raw-key snapshot/restore in `Window::run` / `Window::commit` for unrestricted `T <= 4` speculative rollback across 4-cell qsa block boundaries- opt-in online cost-model `SpecGate` (`STRATA_SPEC_GATE=suffix|1`), step-aware `STRATA_METAL_SPEC_MIN_P_SCHEDULE` / `STRATA_METAL_SPEC_MIN_CUM_P`, and exact/typical `temp > 0` speculative verification (`STRATA_SPEC_SAMPLE=exact|typical`)- 4-thread `F_NOCACHE` nvme session save/load (`--save-session` / `--load-session`, `Window::session_save` / `session_load`) serializing all 36 gdn + 12 qsa + ple + mtp sliding-window states (`mtp_cell0_`, `mtp_cell1_`, `mtp_sink_`) directly from/into `MTLBuffer`s at `5.2 gb/s`- bonsai-style automatic nvme prompt & conversation `.kvc` cache (`--cache-disk` / `--cache-disk-size 16384` in `--serve`): startup `.kvc` header indexing (`< 1 ms`), post-`DONE` zero-latency auto-save with obsolete-prefix pruning, 2-prompt shared-prefix learning & `.pin` checkpointing (`prompts-seen.bin`), zero-copy `prefix-share` delta `.kvc` storage against pinned prefixes (`.parent` + `SessionHeader` `base_cells`, storing only `[c0, n_cells)` positional cells while reading `[0, c0)` directly from the parent `.kvc`), and automatic cross-conversation / post-restart nvme restore + `scr` splice over disk- suffix cache reuse (`scr`, `Window::scr_relocate` + `strata::core::scr_plan` + `scr_carry` `0..3` newline pad insertion for `Δ ≡ 0 (mod 4)` + `<path>.pads` + 2-slot mru `ck_ring` in `--serve`) relocating qsa group-0 neox rope keys/pooled keys and mtp cell bounds in place while retaining post-edit 36-layer gdn state, including automatic `[A B C] -> [A C D]` stripped-`<think>` multi-turn reuse- suffix cache reuse (`scr`, `Window::scr_relocate` + `strata::core::scr_plan` with `max_runs = 64` + 2-pass non-overlapping left/right-shift relocation before gap prefill + `scr_carry` `0..3` newline pad insertion for `Δ ≡ 0 (mod 4)` + `<path>.pads` + in-place `active_path` `.kvc` replacement across clm edits + 2-slot mru `ck_ring` in `--serve`) relocating qsa group-0 neox rope keys/pooled keys and mtp cell bounds in place while retaining post-edit 36-layer gdn state (`lcp >= 0` multi-span clm memory edits & stripped-`<think>` turns)
### apple silicon metal decode kernels (`ntok = 1..8`)- `gemv.metal`: m5 per-format/per-shape/per-`ntok` tile selector in `Window::gemv_tile` & `mtp_encode::tile` (`{32,2,4,1}` for `2560->512`, `{8,2,4,1}` for `q2_0 640->2560` at `ntok >= 4`), `#pragma clang loop unroll(full)` across `NTOK`/`NR`/`KS` loops, `fmt<F>::TAB > 1` barrier guards, `gemv_cat` (`ssm_alpha + ssm_beta`, mtp `k + v` / `gate + up`) & `q8_0` `gemv_catk`, `gemv_direct_x` (`q8_0`), `gemv_q2_raw_combine` with ieee-754 fp16 `0x64006400u` mantissa-or decode (`q2_0r`), and 5-register `packed_uint4` `gemv_iq4_combine` (`iq4_xs`)- `moe.metal` & `quant.metal`: single-buffer `q[UNIT]` gate/up register reuse (`NE > 1`), `NE == 1` pointer/activation hoisting in `gu_body`/`dn_body`, `UNIT == 32` fused `qg`/`qu` decode (`q6_k`), `q2_combine<F>` (`q2_0`/`q2_0r`/`iq2_xs`), 6-vgpr `deq_iq_packed` + `iq_combine`, ieee-754 fp16 `0x64006400u` mantissa-or decode in `q2_combine<T_Q2_0R>`, 64-bit `uint2` grid loads in `deq<T_IQ2_XXS/IQ2_XS/IQ2_S>`, `ushort` `q2`/`s2` + pre-shifted `qh` in `deq<T_IQ2_S>`, `sign4_iq2xs` + dead sign-table (`ksg`/`load_signs`) removal + shrunk `TAB` (`1024`/`512`/`2048`), `QUANT_CTAB` constant-memory tables (`iq3_s` pinned to threadgroup `tab`), m5 `gl=16` / `dl=8,dr=2` tiles, `plan_body` `topk10` `((i * 205) >> 11)` fast division + `unroll_count(10)` + `sg == 0` `simd_prefix_exclusive_sum`, and `moe_route` `e512` + preloaded 16-bit `res_mask`34 unmodified lines735 unmodified linesExpand all7367377387397407417427437447457407417427433 unmodified lines74774874975075175275375475575675775875976076176276376476576619 unmodified lines7867877887897847907917927937947952 unmodified lines79879980080180280380480580629 unmodified lines83683783883984084183384284384484584684784884985085185285385485585616 unmodified lines87387487587686686786886987087187287787887988088187388288388488547 unmodified lines9339349359369379389399409414 unmodified lines94694794894994195095195295395491 unmodified lines1046104710481049104110501051105210531054105510561057105810501059106010611062106311 unmodified lines10751076107710781070107910801081108210831084108510861087108810891090109110921093109410951096109710 unmodified lines110811091110111111011112111311141115111641 unmodified lines115811591160116111511162116311641165116611671168116911701160117111721173117411751 unmodified line1177117811791180118111821183118411851186118711881189119011911171119211931194119511961 unmodified line11981199120012011202120312041205120612071208120912101188118911901191121112121213121456 unmodified lines127112721273127412751276127712781279123 unmodified linesExpand all1403140414051406138714071408140914101411404 unmodified linesExpand all735 unmodified linesExpand all if (!enabled() || n_pre < (int64_t) kLearnMinTokens) return -1; const size_t cap = std::min<size_t>((size_t) n_pre, kLearnMaxTokens); std::vector<int64_t> cur(ids.begin(), ids.begin() + cap); size_t best_lcp = 0; for (const auto& s : seen_prompts) { const size_t lcp = common_prefix(cur, s, kLearnMaxTokens); if (lcp >= kLearnMinTokens && lcp < cur.size() && lcp < s.size()) { best_lcp = std::max(best_lcp, lcp); } } bool added = false; for (auto& s : seen_prompts) { if (is_prefix(s, cur)) { s = cur;3 unmodified lines if (is_prefix(cur, s)) { added = true; break; } const size_t lcp = common_prefix(cur, s, kLearnMaxTokens); const auto spans = strata::core::scr_detail::find_spans( s, cur, (int64_t) lcp, strata::core::ScrParams{32, 64, 16, 4}); if (!spans.empty()) { // Same CLM conversation after a mid-prompt edit/compaction: update in place, do not pin the fold point s = cur; added = true; break; } if (lcp >= kLearnMinTokens && lcp < cur.size() && lcp < s.size()) { best_lcp = std::max(best_lcp, lcp); } } if (!added) { if (seen_prompts.size() >= kSeenMax) seen_prompts.pop_front(); seen_prompts.push_back(std::move(cur));19 unmodified lines entries.push_back(std::move(e)); } }
void save_current(const std::vector<int64_t>& ids, const std::vector<int64_t>& pads, bool pinned, std::string& err) { void save_current(const std::vector<int64_t>& ids, const std::vector<int64_t>& pads, bool pinned, std::string& err, std::string* active_path = nullptr) { if (!enabled() || ids.size() < kLearnMinTokens) return; namespace fs = std::filesystem; std::error_code ec; for (size_t i = 0; i < entries.size(); ++i) {2 unmodified lines if (pinned && !entries[i].pinned) { entries[i].pinned = true; if (FILE* pf = std::fopen((entries[i].path + ".pin").c_str(), "wb")) std::fclose(pf); } if (!pinned && active_path != nullptr) *active_path = entries[i].path; touch(i); return; } }29 unmodified lines } else { fs::remove(path + ".pin", ec); } const size_t sz = (size_t) fs::file_size(path, ec); const std::string prev_active = (!pinned && active_path != nullptr) ? *active_path : ""; for (auto it = entries.begin(); it != entries.end();) { if (it->path == path || (!it->pinned && is_prefix(it->ids, ids))) { if (it->path == path || (!it->pinned && (is_prefix(it->ids, ids) || (!prev_active.empty() && it->path == prev_active)))) { if (it->path != path) { remove_files(it->path); } it = entries.erase(it); } else { ++it; } } if (!pinned && active_path != nullptr) *active_path = path; entries.push_back({path, parent_path, ids, pads, base_cells, sz, pinned}); prune_orphans(); enforce_limits(); }16 unmodified lines const int64_t q_n_pre = std::max<int64_t>(0, (int64_t) q_ids.size() - 1); int64_t cand = 0; if ((int64_t) e.ids.size() <= q_n_pre && is_prefix(e.ids, q_ids)) { cand = (int64_t) e.ids.size(); } else { const size_t lcp = common_prefix(e.ids, q_ids, (size_t) q_n_pre); if (lcp >= 32) { const strata::core::ScrPlan plan = strata::core::scr_plan( e.ids, q_ids, {(int64_t) e.ids.size()}, strata::core::ScrParams{32, 8, 16, 4}); if (!plan.empty() && (int64_t) q_ids.size() + plan.pads() + max_new <= max_cells) { cand = plan.n_prefix + plan.reused(); } else if (e.ids.size() >= 32 && q_n_pre >= 32) { const strata::core::ScrPlan plan = strata::core::scr_plan( e.ids, q_ids, {(int64_t) e.ids.size()}, strata::core::ScrParams{32, 64, 16, 4}); if (!plan.empty() && (int64_t) q_ids.size() + plan.pads() + max_new <= max_cells) { cand = plan.n_prefix + plan.reused(); } } } if (cand >= 32 && cand >= best_score) { best_score = cand;47 unmodified lines } }; std::vector<int64_t> cached_ids; std::vector<int64_t> cached_pads; std::string active_path; bool ram_dirty = false; std::vector<float> lg_buf((size_t) n_vocab); std::mt19937 rng(42); // temp > 0 with drafts: speculative sampling (spec_sample), else STRATA_SPEC_SAMPLE=greedy: the drafts kept while4 unmodified lines
while (std::getline(std::cin, line)) { if (line.empty()) continue; if (line == "QUIT" || line.rfind("QUIT ", 0) == 0) { if (ram_dirty && cached_ids.size() >= 32) disk_cache.save_current(cached_ids, cached_pads, false, err); if (ram_dirty && cached_ids.size() >= 32) disk_cache.save_current(cached_ids, cached_pads, false, err, &active_path); break; } if (line == "STOP") continue; if (line.rfind("SAVE ", 0) == 0) {91 unmodified lines // Estimate how many tokens the live RAM state + ck_ring can reuse before checking the NVMe tier int64_t ram_reused = 0; if (lcp == cached_ids.size() && lcp > 0) { ram_reused = (int64_t) lcp; } else if (!cached_ids.empty() && lcp >= 32) { } else if (cached_ids.size() >= 32 && n_pre >= 32) { std::vector<int64_t> ck_lens = {(int64_t) cached_ids.size()}; for (const auto& s : ck_ring) { if (!s.ids.empty() && s.ids.size() <= cached_ids.size() && std::equal(s.ids.begin(), s.ids.end(), cached_ids.begin())) { ck_lens.push_back((int64_t) s.ids.size()); } } const strata::core::ScrPlan plan = strata::core::scr_plan( cached_ids, ids, ck_lens, strata::core::ScrParams{32, 8, 16, 4}); cached_ids, ids, ck_lens, strata::core::ScrParams{32, 64, 16, 4}); if (!plan.empty() && (int64_t) ids.size() + plan.pads() + max_new <= max_cells) { ram_reused = plan.n_prefix + plan.reused(); } }11 unmodified lines int64_t disk_reused = 0; const int disk_idx = disk_cache.find_best(raw_ids, max_new, max_cells, kScrPadToken, disk_reused); if (disk_idx >= 0 && disk_reused >= ram_reused + 32) { if (ram_dirty && cached_ids.size() >= 32) { disk_cache.save_current(cached_ids, cached_pads, false, err); disk_cache.save_current(cached_ids, cached_pads, false, err, &active_path); ram_dirty = false; } const int disk_idx2 = disk_cache.find_best(raw_ids, max_new, max_cells, kScrPadToken, disk_reused); if (disk_idx2 >= 0) { const std::string load_path = disk_cache.entries[(size_t) disk_idx2].path; const std::string load_parent = disk_cache.entries[(size_t) disk_idx2].parent_path; const bool load_pinned = disk_cache.entries[(size_t) disk_idx2].pinned; const std::vector<int64_t> load_pads = disk_cache.entries[(size_t) disk_idx2].pads; std::vector<int32_t> i32; int64_t cells = 0; if (me::window_session_load(load_path.c_str(), i32, &cells, err, load_parent.c_str())) { cached_ids.assign(i32.begin(), i32.end()); cached_pads = load_pads; active_path = load_pinned ? "" : load_path; ram_dirty = false; ck_ring.clear(); save_ck_at(cached_ids); disk_cache.touch((size_t) disk_idx2);10 unmodified lines while (lcp < lim1 && cached_ids[lcp] == ids[lcp]) ++lcp; } } } else if (ram_reused == 0 && ram_dirty && cached_ids.size() >= 32) { disk_cache.save_current(cached_ids, cached_pads, false, err); disk_cache.save_current(cached_ids, cached_pads, false, err, &active_path); ram_dirty = false; } }
41 unmodified lines cached_ids.assign(ids.begin(), ids.begin() + n_pre); ram_dirty = true; } else { // 2. Check SCR mid-context fold [A B C] -> [A B' C D] (including stripped <think> blocks) via scr_plan if (!cached_ids.empty() && lcp >= 32) { if (cached_ids.size() >= 32 && n_pre >= 32) { std::vector<int64_t> ck_lens = {(int64_t) cached_ids.size()}; for (const auto& s : ck_ring) { if (!s.ids.empty() && s.ids.size() <= cached_ids.size() && std::equal(s.ids.begin(), s.ids.end(), cached_ids.begin())) { ck_lens.push_back((int64_t) s.ids.size()); } } const strata::core::ScrPlan plan = strata::core::scr_plan( cached_ids, ids, ck_lens, strata::core::ScrParams{32, 8, 16, 4}); cached_ids, ids, ck_lens, strata::core::ScrParams{32, 64, 16, 4}); if (!plan.empty() && (int64_t) ids.size() + plan.pads() + max_new <= max_cells) { ids = strata::core::scr_padded(ids, plan, kScrPadToken); n_pre = std::max<int64_t>(0, (int64_t) ids.size() - 1); const auto new_pads = strata::core::scr_pad_positions(plan);1 unmodified line std::sort(cached_pads.begin(), cached_pads.end()); cached_pads.erase(std::unique(cached_pads.begin(), cached_pads.end()), cached_pads.end()); save_ck_at(cached_ids); bool scr_ok = true; // Pass 1: relocate left-shifting runs (shift < 0) in forward order 0 .. m-1 for (size_t ri = 0; scr_ok && ri < plan.runs.size(); ++ri) { const auto& r = plan.runs[ri]; if (r.shift() < 0 && !me::window_scr_relocate(r.old_start, r.at, r.n, err)) scr_ok = false; } // Pass 2: relocate right-shifting runs (shift > 0) in reverse order m-1 .. 0 for (size_t ri = plan.runs.size(); scr_ok && ri-- > 0;) { const auto& r = plan.runs[ri]; if (r.shift() > 0 && !me::window_scr_relocate(r.old_start, r.at, r.n, err)) scr_ok = false; } int64_t cur_p = plan.restore > 0 ? plan.restore : (int64_t) lcp; if (plan.restore > 0) { if (scr_ok && plan.restore > 0) { for (const auto& s : ck_ring) { if ((int64_t) s.ids.size() == plan.restore) { me::window_checkpoint_restore(s.ck, err); ss.ple_prev[0] = s.prev0;1 unmodified line break; } } } // Pass 3: prefill edited gaps [cur_p, r.at) now that all runs sit at [r.at, r.at + r.n) for (const auto& r : plan.runs) { if (!scr_ok) break; if (r.at > cur_p) { if (!me::prompt_run_rows(ids.data() + cur_p, r.at - cur_p, cur_p, on_chunk, err)) { scr_ok = false; break; } } if (r.shift() != 0 && !me::window_scr_relocate(r.old_start, r.at, r.n, err)) { scr_ok = false; break; } cur_p = r.at + r.n; } if (scr_ok && plan.adopt > 0) { for (const auto& s : ck_ring) {56 unmodified lines me::window_reset(); ss.ple_prev[0] = ss.ple_prev[1] = -1; cached_ids.clear(); cached_pads.clear(); active_path.clear(); if (n_pre > 0) { if (!run_prefill_span(0, n_pre)) { std::printf("ERR %s\n", err.c_str()); std::fflush(stdout);123 unmodified linesExpand all std::printf("DONE %d %zu %.1f %.1f %s 0 0 %lld\n", gen, ids.size(), prefill_ms, decode_ms, finish, (long long) reused); std::fflush(stdout); if (disk_cache.enabled() && cached_ids.size() >= 32) { disk_cache.save_current(cached_ids, cached_pads, false, err); disk_cache.save_current(cached_ids, cached_pads, false, err, &active_path); ram_dirty = false; } } }404 unmodified linesExpand all