From 88743e53aa71b87a970569889efe6065e79fc645 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 17 Jun 2026 15:55:38 +0000 Subject: [PATCH 1/8] debug(mlx-fused): instrument codegen markdown-loop degeneration + native-control probe KAKEYA_KDBG-gated per-block logging (sampled/committed ids, cyc_frac/cyc_p, cache offsets) in fused_specdecode_generate, and a turn_compare_fused_vs_native record (first_divergence_idx + both tails) in _run_fused_chat. New bridge preset mlx-kakeya-codegen-degen-probe runs the C-code prompt with --chat-native-ref to decide greedy-pathology vs engine bug. Instrumentation only; reverted after fix. Co-authored-by: FluffyAIcode --- .../backends/mlx/fused_specdecode.py | 78 +++++++++++++++++++ inference_engine/bridge/manifest.py | 27 +++++++ .../research/k3_integrated_niah_eval_mac.py | 38 +++++++++ .../inference_engine/bridge/test_manifest.py | 1 + 4 files changed, 144 insertions(+) diff --git a/inference_engine/backends/mlx/fused_specdecode.py b/inference_engine/backends/mlx/fused_specdecode.py index c14cfbc..3f35f02 100644 --- a/inference_engine/backends/mlx/fused_specdecode.py +++ b/inference_engine/backends/mlx/fused_specdecode.py @@ -35,6 +35,68 @@ restored_prefill_cache, ) +# region agent log (fused-codegen-degeneration-2815 probe; strip after fix) +import os as _kdbg_os +import sys as _kdbg_sys +import json as _kdbg_json + +_KDBG = bool(_kdbg_os.environ.get("KAKEYA_KDBG")) + + +def _kdbg(hyp: str, msg: str, **data: Any) -> None: + """Emit one NDJSON probe line to stderr (prefix ``KDBG ``) and, best-effort, + to /opt/cursor/logs/debug.log. No-op unless ``KAKEYA_KDBG`` is set, so + production behaviour is unchanged.""" + if not _KDBG: + return + rec = {"hypothesisId": hyp, "location": "fused_specdecode.py", + "message": msg, "data": data} + try: + _kdbg_sys.stderr.write("KDBG " + _kdbg_json.dumps(rec, ensure_ascii=False) + "\n") + _kdbg_sys.stderr.flush() + except Exception: + pass + try: + with open("/opt/cursor/logs/debug.log", "a") as _f: + _f.write(_kdbg_json.dumps(rec) + "\n") + except Exception: + pass + + +def _kdbg_cycle(ids: Sequence[int], window: int = 80) -> Tuple[float, int]: + """Short-unit cycle metric on the tail of ``ids``: returns + ``(cyc_frac, cyc_p)`` where ``cyc_p`` is the period (1..window//3) whose + back-to-back repetition covers the largest fraction ``cyc_frac`` of the + trailing ``window`` tokens. ~1.0 => the tail is a tight repeating loop.""" + w = list(ids[-window:]) + n = len(w) + if n < 6: + return 0.0, 0 + best_frac, best_p = 0.0, 0 + for p in range(1, n // 3 + 1): + run, i = 0, n - 1 + while i - p >= 0 and w[i] == w[i - p]: + run += 1 + i -= 1 + if run > 0: + frac = (run + p) / n + if frac > best_frac: + best_frac, best_p = frac, p + return round(best_frac, 3), best_p + + +def _kdbg_cache_offsets(cache: Any) -> Tuple[Optional[int], Optional[int]]: + """(first full-attn KVCache offset, first sliding RotatingKVCache offset).""" + off_full = off_rot = None + for c in (cache or []): + nm = type(c).__name__ + if off_rot is None and "Rotating" in nm: + off_rot = int(getattr(c, "offset", -1)) + elif off_full is None and "Rotating" not in nm: + off_full = int(getattr(c, "offset", -1)) + return off_full, off_rot +# endregion + # --------------------------------------------------------------------------- # # Component A: capture verifier aux-layer hidden states (no transformers @@ -772,6 +834,22 @@ def fused_specdecode_generate( commit = candidate[:accepted] + [correction] generated += commit accepts.append(accepted) + # region agent log (fused-codegen-degeneration-2815 probe) + if _KDBG: + off_full, off_rot = _kdbg_cache_offsets(getattr(adapter, "_cache", None)) + cyc_frac, cyc_p = _kdbg_cycle(generated) + # H-D: cache.offset must track committed length (past_len). + # off_rot lags by the sliding window (bounded), off_full == past_len. + _kdbg("AD", "block", + blk=len(accepts) - 1, base=cstart, + past_len=int(adapter._past_len), gen=len(generated), + off_full=off_full, off_rot=off_rot, + bonus=int(bonus), cand=[int(x) for x in candidate], + n_cand=len(candidate), accepted=int(accepted), + commit=[int(x) for x in commit], + next_argmax=int(argmax_fn(adapter.next_token_logits)), + cyc_frac=cyc_frac, cyc_p=cyc_p) + # endregion if any(t in eos for t in commit): break if (allow_greedy_fallback and len(accepts) >= 2 diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 8b67da7..1338d47 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -771,6 +771,33 @@ def _harness_preset( params={"max_new_tokens": ("int:max_new_tokens", "64")}, validate_reports=True, # §4 liveness gate on-device ), + Preset( + name="mlx-kakeya-codegen-degen-probe", + description="DEBUG: full f_θ fused engine on a CODE prompt (write PoW " + "in C) that triggers an early high-acceptance markdown-" + "marker loop (**/.2/* wall), with KAKEYA_KDBG per-block " + "logging + native-greedy control (--chat-native-ref). The " + "decisive signal is fused-vs-native divergence: if native " + "also loops, it is greedy pathology the engine must guard.", + command_templates=( + ( + "env", "KAKEYA_KDBG=1", + "python3", "scripts/research/k3_integrated_niah_eval_mac.py", + "--verifier-path", "${ENV:KAKEYA_MAC_VERIFIER_PATH}", + "--drafter-id", "${ENV:KAKEYA_MAC_DRAFTER_ID}", + "--f-theta-dir", "${ENV:KAKEYA_MAC_FTHETA_DIR}", + "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", + "--sink-size", "4", "--window-size", "64", "--block-size", "4", + "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", + "--chat", "--chat-native-ref", + "--chat-scripted", "实现一个PoW的代码,用c语言完成", + "--output", "results/research/codegen_degen_2815_chat.json", + ), + ), + timeout_minutes=90, + params={"max_new_tokens": ("int:max_new_tokens", "800")}, + validate_reports=False, + ), Preset( name="mlx-kakeya-degen-probe", description="Long-decode regression probe: full f_θ fused engine on a " diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index ecde21d..a628eb2 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -851,6 +851,44 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: res["native_ref_tokens"] = len(nref_tokens) res["resident_kv_bytes"] = int( sum(int(getattr(c, "nbytes", 0)) for c in (adapter._cache or []))) + # region agent log (fused-codegen-degeneration-2815 probe) + import os as _kos + if _kos.environ.get("KAKEYA_KDBG"): + ftoks = [int(t) for t in res.get("tokens", [])] + ntoks = [int(t) for t in nref_tokens] + div = None + for j, (a, b) in enumerate(zip(ftoks, ntoks)): + if a != b: + div = j + break + if div is None: + div = min(len(ftoks), len(ntoks)) + + def _dec(seq): + try: + return tokenizer.decode(seq, skip_special_tokens=True) + except TypeError: + return tokenizer.decode(seq) + rec = { + "hypothesisId": "AC", + "message": "turn_compare_fused_vs_native", + "data": { + "fused_n": len(ftoks), "native_n": len(ntoks), + "first_divergence_idx": div, + "fused_div_ctx": ftoks[max(0, div - 8):div + 16], + "native_div_ctx": ntoks[max(0, div - 8):div + 16], + "fused_div_text": _dec(ftoks[max(0, div - 8):div + 16]), + "native_div_text": _dec(ntoks[max(0, div - 8):div + 16]), + "fused_tail": ftoks[-48:], + "native_tail": ntoks[-48:], + "fused_tail_text": _dec(ftoks[-48:]), + "native_tail_text": _dec(ntoks[-48:]), + }, + } + sys.stderr.write( + "KDBG " + json.dumps(rec, ensure_ascii=False) + "\n") + sys.stderr.flush() + # endregion return res print(f"[chat] FULL fused engine: verifier={args.verifier_path} " diff --git a/tests/inference_engine/bridge/test_manifest.py b/tests/inference_engine/bridge/test_manifest.py index 31ce0ec..cce242d 100644 --- a/tests/inference_engine/bridge/test_manifest.py +++ b/tests/inference_engine/bridge/test_manifest.py @@ -81,6 +81,7 @@ def test_allowlist_contains_exactly_the_documented_presets(): "mlx-batched-pad-decode", "mlx-env-probe", "mlx-kakeya-chat-smoke", + "mlx-kakeya-codegen-degen-probe", "mlx-kakeya-degen-probe", "mlx-kakeya-fused-chat-ftheta", "mlx-kakeya-fused-chat-smoke", From f636370349c82255d9a3d8debfd23e21424b69b7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 17 Jun 2026 16:21:12 +0000 Subject: [PATCH 2/8] debug(mlx-fused): add multi-turn prefill-state probe (ring-wrap-at-prefill) + multi-turn degen preset KAKEYA_KDBG-gated prefill_state_fused / prefill_state_native records in _run_fused_chat: per-turn prompt_len, evicted_count, rot/full cache offsets, any_wrapped, would_wrap_block0, plus a turn index on turn_compare. Repoints mlx-kakeya-codegen-degen-probe to the multi-turn repro (turn-1 PoW explanation pushes the turn-2 code prompt's prefill past the sliding window) at 1200 tok. Instrumentation only; reverted after fix. Co-authored-by: FluffyAIcode --- inference_engine/bridge/manifest.py | 27 ++++--- .../research/k3_integrated_niah_eval_mac.py | 75 +++++++++++++++++++ 2 files changed, 93 insertions(+), 9 deletions(-) diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 1338d47..9e442d2 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -773,12 +773,20 @@ def _harness_preset( ), Preset( name="mlx-kakeya-codegen-degen-probe", - description="DEBUG: full f_θ fused engine on a CODE prompt (write PoW " - "in C) that triggers an early high-acceptance markdown-" - "marker loop (**/.2/* wall), with KAKEYA_KDBG per-block " - "logging + native-greedy control (--chat-native-ref). The " - "decisive signal is fused-vs-native divergence: if native " - "also loops, it is greedy pathology the engine must guard.", + description="DEBUG: full f_θ fused engine on a MULTI-TURN chat whose " + "turn-1 PoW explanation makes the turn-2 code prompt's " + "prefill exceed the sliding window / native RotatingKVCache " + "(ring pre-wrapped before decode). Single-turn ruled OUT an " + "engine bug (token-identical to native); this probe targets " + "the long-prompt prefill regime. KAKEYA_KDBG logs per-turn " + "prefill state (prompt_len, evicted_count, rot/full cache " + "offsets, any_wrapped, would_wrap_block0) + per-block offsets " + "+ a turn_compare_fused_vs_native record (first_divergence_idx " + "+ tails). Native-greedy control (--chat-native-ref) decodes " + "the SAME per-turn prompt (history-inclusive) so the decisive " + "signal stays fused-vs-native: native coherent + fused garbled " + "from turn-2 start ⇒ long-prompt prefill corrupts logits " + "(engine); both loop identically ⇒ greedy pathology.", command_templates=( ( "env", "KAKEYA_KDBG=1", @@ -790,12 +798,13 @@ def _harness_preset( "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", "--chat", "--chat-native-ref", - "--chat-scripted", "实现一个PoW的代码,用c语言完成", + "--chat-scripted", + "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", "--output", "results/research/codegen_degen_2815_chat.json", ), ), - timeout_minutes=90, - params={"max_new_tokens": ("int:max_new_tokens", "800")}, + timeout_minutes=120, + params={"max_new_tokens": ("int:max_new_tokens", "1200")}, validate_reports=False, ), Preset( diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index a628eb2..646c8c1 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -221,6 +221,7 @@ def main() -> int: MLXRestoredIncrementalVerifier, capture_aux_hidden, make_bridge_embed_lm_head, fused_specdecode_generate, fused_specdecode_generate_mlx, fused_specdecode_generate_mlx_trim, + _sliding_ring_would_wrap, # region agent log (fused-codegen-degeneration-2815) ) from inference_engine.v04.kv_compressor import make_default_compressor from inference_engine.bench.k3_report_gate import ( @@ -769,6 +770,54 @@ def _encode_chat(history: List[Dict[str, str]]) -> List[int]: history, add_generation_prompt=True) return list(cids.tolist() if hasattr(cids, "tolist") else cids) + # region agent log (fused-codegen-degeneration-2815 prefill probe) + import os as _kos_chat + _KDBG_CHAT = bool(_kos_chat.environ.get("KAKEYA_KDBG")) + + def _kdbg_emit(rec: Dict[str, Any]) -> None: + try: + sys.stderr.write("KDBG " + json.dumps(rec, ensure_ascii=False) + "\n") + sys.stderr.flush() + except Exception: + pass + try: + with open("/opt/cursor/logs/debug.log", "a") as _f: + _f.write(json.dumps(rec) + "\n") + except Exception: + pass + + def _kdbg_cache_summary(cache: Any) -> Dict[str, Any]: + """rot/full offset+max_size rollup + wrap/trimmable flags. The + decisive prefill signal: is the sliding RotatingKVCache already + wrapped (off>=max_size) BEFORE decode starts, and does full-attn + off == prompt_len? A pre-wrapped ring at prefill means the very + first speculative block's trim is refused (offset desync).""" + rot_off = rot_ms = full_off = None + any_wrapped = False + all_trimmable = True + n = 0 + for c in (cache or []): + n += 1 + nm = type(c).__name__ + off = int(getattr(c, "offset", -1)) + ms = getattr(c, "max_size", None) + ms = int(ms) if ms is not None else None + is_rot = "Rotating" in nm + if is_rot and ms is not None and off >= ms: + any_wrapped = True + trim_fn = getattr(c, "is_trimmable", None) + trim = bool(trim_fn()) if callable(trim_fn) else None + if trim is False: + all_trimmable = False + if is_rot and rot_off is None: + rot_off, rot_ms = off, ms + if (not is_rot) and full_off is None: + full_off = off + return {"n_layers": n, "rot_off": rot_off, "rot_ms": rot_ms, + "full_off": full_off, "any_wrapped": any_wrapped, + "all_trimmable": all_trimmable} + # endregion + def _gen_turn(pid: List[int]) -> Dict[str, Any]: # Opt-in A/B control (--chat-native-ref): a plain NATIVE greedy # AR decode of the SAME prompt for --max-new-tokens. Captured as @@ -779,6 +828,14 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: nref_tokens: List[int] = [] if args.chat_native_ref: nref_cache, nref_logits = native_prefill(list(pid)) + # region agent log (fused-codegen-degeneration-2815 prefill probe) + if _KDBG_CHAT: + _turn = sum(1 for h in history if h.get("role") == "user") + _kdbg_emit({"hypothesisId": "AE", + "message": "prefill_state_native", + "data": {"turn": _turn, "prompt_len": len(pid), + "cache": _kdbg_cache_summary(nref_cache)}}) + # endregion while len(nref_tokens) < args.max_new_tokens: tok = int(mx.argmax(nref_logits).item()) nref_tokens.append(tok) @@ -809,6 +866,23 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: restored_v_per_layer=_pad(rv, tsrc, T), evicted_positions=evicted, prefill_chunk_size=args.prefill_chunk_size, full_kv=args.cuda_trim) + # region agent log (fused-codegen-degeneration-2815 prefill probe) + if _KDBG_CHAT: + _turn = sum(1 for h in history if h.get("role") == "user") + _kdbg_emit({"hypothesisId": "AE", + "message": "prefill_state_fused", + "data": {"turn": _turn, "prompt_len": T, + "evicted_count": len(evicted), + "block_size": int(args.block_size), + "would_wrap_block0": bool( + _sliding_ring_would_wrap( + getattr(adapter, "_cache", None), + int(args.block_size))), + "past_len": int(adapter._past_len), + "f_theta_ran": bool(f_theta_ran), + "cache": _kdbg_cache_summary( + getattr(adapter, "_cache", None))}}) + # endregion t0 = time.perf_counter() if mlx_drafter is not None and args.cuda_trim: res = fused_specdecode_generate_mlx_trim( @@ -873,6 +947,7 @@ def _dec(seq): "hypothesisId": "AC", "message": "turn_compare_fused_vs_native", "data": { + "turn": sum(1 for h in history if h.get("role") == "user"), "fused_n": len(ftoks), "native_n": len(ntoks), "first_divergence_idx": div, "fused_div_ctx": ftoks[max(0, div - 8):div + 16], From 12fda6009c6da5095039ac6208d6bdf5047a1ddb Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 17 Jun 2026 17:01:30 +0000 Subject: [PATCH 3/8] debug(probe): light single-turn long-prompt repro (ring pre-wrapped at prefill) Multi-turn+native at 1200x2 OOM'd the Mac runner. Per debug analysis, the cheapest test of H-C' (long-prompt prefill corrupts logits) vs H-A' (bounded- greedy pathology) is a single-turn LONG prompt that wraps the ring AT prefill (would_wrap_block0) with a tiny 192-tok budget. Add --chat-scripted-file so the ~2k-char context is a committed fixture (pow_codegen_longprompt.txt) instead of a giant manifest argv; repoint mlx-kakeya-codegen-degen-probe to it. Co-authored-by: FluffyAIcode --- inference_engine/bridge/manifest.py | 37 ++++---- .../research/k3_integrated_niah_eval_mac.py | 13 ++- scripts/research/pow_codegen_longprompt.txt | 85 +++++++++++++++++++ 3 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 scripts/research/pow_codegen_longprompt.txt diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 9e442d2..05237d2 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -773,20 +773,19 @@ def _harness_preset( ), Preset( name="mlx-kakeya-codegen-degen-probe", - description="DEBUG: full f_θ fused engine on a MULTI-TURN chat whose " - "turn-1 PoW explanation makes the turn-2 code prompt's " - "prefill exceed the sliding window / native RotatingKVCache " - "(ring pre-wrapped before decode). Single-turn ruled OUT an " - "engine bug (token-identical to native); this probe targets " - "the long-prompt prefill regime. KAKEYA_KDBG logs per-turn " - "prefill state (prompt_len, evicted_count, rot/full cache " - "offsets, any_wrapped, would_wrap_block0) + per-block offsets " - "+ a turn_compare_fused_vs_native record (first_divergence_idx " - "+ tails). Native-greedy control (--chat-native-ref) decodes " - "the SAME per-turn prompt (history-inclusive) so the decisive " - "signal stays fused-vs-native: native coherent + fused garbled " - "from turn-2 start ⇒ long-prompt prefill corrupts logits " - "(engine); both loop identically ⇒ greedy pathology.", + description="DEBUG: full f_θ fused engine on a LONG single-turn prompt " + "(~2k-char PoW explanation + a 'write C code' request, from " + "the committed fixture pow_codegen_longprompt.txt) so the " + "native RotatingKVCache ring is ALREADY WRAPPED at prefill " + "(would_wrap_block0). Short single-turn prompts were proven " + "token-identical to native & coherent; this isolates the " + "long-prompt-prefill regime cheaply (tiny 192-tok budget). " + "KAKEYA_KDBG logs prefill state (prompt_len, any_wrapped, " + "would_wrap_block0, rot/full offsets) + per-block offsets + " + "turn_compare_fused_vs_native. Native-greedy control " + "(--chat-native-ref): native coherent + fused garbled ⇒ " + "long-prompt prefill corrupts logits (engine bug); both " + "degenerate ⇒ bounded-greedy pathology the engine must guard.", command_templates=( ( "env", "KAKEYA_KDBG=1", @@ -798,13 +797,13 @@ def _harness_preset( "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", "--chat", "--chat-native-ref", - "--chat-scripted", - "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", - "--output", "results/research/codegen_degen_2815_chat.json", + "--chat-scripted-file", + "scripts/research/pow_codegen_longprompt.txt", + "--output", "results/research/codegen_degen_2815_longprompt.json", ), ), - timeout_minutes=120, - params={"max_new_tokens": ("int:max_new_tokens", "1200")}, + timeout_minutes=90, + params={"max_new_tokens": ("int:max_new_tokens", "192")}, validate_reports=False, ), Preset( diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index 646c8c1..ed354be 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -180,6 +180,11 @@ def parse_args() -> argparse.Namespace: ap.add_argument("--chat-scripted", default=None, help="Non-interactive chat: '||'-separated user turns " "(for Mac-bridge verification); writes a transcript.") + ap.add_argument("--chat-scripted-file", default=None, + help="Like --chat-scripted but reads the (possibly long, " + "'||'-separated) scripted prompt from a UTF-8 file. Lets " + "a long context be a committed fixture instead of a giant " + "manifest argv. Overrides --chat-scripted when set.") ap.add_argument("--chat-native-ref", action="store_true", help="DIAGNOSTIC opt-in: before each chat turn, also run a " "plain NATIVE greedy AR decode of the SAME prompt for " @@ -973,8 +978,12 @@ def _dec(seq): file=sys.stderr, flush=True) history: List[Dict[str, str]] = [] - if args.chat_scripted is not None: - turns = [t for t in args.chat_scripted.split("||") if t.strip()] + scripted = args.chat_scripted + if args.chat_scripted_file is not None: + with open(args.chat_scripted_file, encoding="utf-8") as _f: + scripted = _f.read() + if scripted is not None: + turns = [t for t in scripted.split("||") if t.strip()] transcript = [] for u in turns: history.append({"role": "user", "content": u}) diff --git a/scripts/research/pow_codegen_longprompt.txt b/scripts/research/pow_codegen_longprompt.txt new file mode 100644 index 0000000..fa1ae08 --- /dev/null +++ b/scripts/research/pow_codegen_longprompt.txt @@ -0,0 +1,85 @@ +**PoW (Proof of Work,工作量证明)** 是区块链技术中最核心的共识机制之一。它的核心目的是:**在没有中心化机构(如银行)的情况下,让分布在世界各地的计算机能够达成一致,决定谁有权记账,并防止有人通过伪造数据来欺骗网络。** + +为了深入理解,我们可以从“核心逻辑”、“工作流程”、“数学原理”和“经济博弈”四个维度来详细解释。 + +--- + +### 1. 核心逻辑:用“算力”换取“信任” + +在去中心化网络中,大家面临一个问题:**如果每个人都说自己记了一笔账,听谁的?** + +PoW 的逻辑是:**谁付出了巨大的计算资源(工作量),谁就有权提议下一块账本。** +* **工作量(Work)**:指消耗的计算资源和时间。 +* **证明(Proof)**:当计算完成后,结果是公开且易于验证的。 + +这种机制确保了:**作恶的成本极高,而维护网络的收益(奖励)很诱人。** + +--- + +### 2. 工作流程:从“挖矿”到“验证” + +假设我们正在运行一个像比特币这样的 PoW 网络,流程如下: + +#### 第一步:收集交易 +矿工(节点)从网络中收集待处理的交易,并将它们打包成一个“候选区块”。 + +#### 第二步:寻找“随机数”(核心环节) +为了让区块生效,矿工必须解决一个数学难题。这个难题通常是: +> “找到一个数字(称为 **Nonce**),使得:**区块头部数据的哈希值(Hash) < 目标难度值(Target)**。” + +* **哈希函数(Hash Function)**:像是一个“数字粉碎机”。你输入任何内容,它都会输出一串固定长度的乱码。只要输入变一点点,输出就会天差地别。 +* **不可逆性**:你无法通过结果反推输入。 + +#### 第三步:竞争与“挖矿” +矿工们开始疯狂尝试。他们不断改变区块里的 `Nonce` 值,重新计算哈希值。 +* 矿工 A 尝试:`Hash(数据 + 1) = 0xabc...` (不符合要求) +* 矿工 B 尝试:`Hash(数据 + 2) = 0xdef...` (不符合要求) +* ... +* 矿工 C 运气好/算力强:`Hash(数据 + 999) = 0x000...` (**符合要求!**) + +一旦有人找到了满足条件的哈希值,他就“挖到了矿”。 + +#### 第四步:广播与验证 +获胜的矿工将新区块广播给全网。其他节点收到后,只需进行一次简单的计算(把 Nonce 代入哈希函数),发现结果确实符合要求,就会接受这个区块,并更新自己的账本。 + +--- + +### 3. 数学原理:为什么它是安全的? + +#### A. 难度调整机制 (Difficulty Adjustment) +如果每个人都买更快的显卡,挖矿速度会变快,导致区块产生太频繁。 +为了保持稳定的出块时间(例如比特币约10分钟一个块),系统会**自动调整难度**: +* 如果出块太快 $\rightarrow$ 提高目标值(让要求的哈绪值前导零更多,变得更难)。 +* 如果出块太慢 $\rightarrow$ 降低难度。 + +#### B. 概率与公平性 +由于哈希值是随机分布的,寻找答案的过程就像“在茫茫大海里捞针”。 +* **算力越高**,意味着你每秒尝试的次数越多,捞到针的概率就越大。 +* 这保证了在宏观上,算力占比与奖励分配是公平的。 + +#### C. 抵抗“双花”与篡改 +如果有人想修改 10 分钟前的一个交易,他必须重新计算那个区块的哈希,以及**之后所有区块**的哈希。 +由于后面的区块都包含了前一个区块的哈希值(形成链式结构),修改历史意味着要重算后面所有的工作量。除非攻击者的算力超过全网总和的 51%,否则这在经济上是不可能的。 + +--- + +### 4. 总结:PoW 的优缺点 + +#### 优点: +1. **极高的安全性**:通过物理世界的能源消耗(电力)为数字世界筑起防线。 +2. **去中心化**:任何人只要有设备就可以加入,不需要许可。 +3. **公平性**:基于数学概率,不存在人为干预。 + +#### 缺点: +1. **能源消耗大**:为了维持安全,全球范围内的矿机都在消耗大量电力(这是最受争议的一点)。 +2. **扩展性差(TPS低)**:为了保证全球同步,出块速度不能太快,导致处理交易的速度受限。 +3. **算力集中风险**:如果出现大规模矿池,可能导致权力向少数人手中集中。 + +### 通俗比喻总结 +想象一个**“数字猜数字游戏”**: +全网的人都在玩一个游戏,目标是猜出一个符合特定规则的数字。 +* **挖矿**就是不停地尝试不同的数字。 +* **难度**就是规则越来越严苛(比如要求数字必须以 10 个零开头)。 +* **验证**就是别人看到你猜中了,只需要看一眼你的数字是否符合规则,瞬间就能确认你没撒谎。 + +基于以上说明,用C语言写一个简短的PoW示例(只写核心循环,不超过30行)。 \ No newline at end of file From c6a699c98d48e38e7a04754c752b1e60519f3791 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 17 Jun 2026 17:30:25 +0000 Subject: [PATCH 4/8] fix(mlx-fused): runaway-loop guard stops greedy markdown-marker collapse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Repro evidence: single-turn fused decode is TOKEN-IDENTICAL to native greedy (first_divergence_idx=None) and coherent through 1200 tokens, so the engine is faithful — the user's '由于...'/'**/.2/*' collapse is greedy-decoding pathology on code/markdown-heavy prompts that the fused path (pure argmax, unlike chat_mlx_kakeya.py) had no mitigation for. Once a loop starts the drafter trivially predicts the repeats and the greedy verifier accepts them (high accept_len), so it walls indefinitely. Fix: _trailing_runaway_drop detects a 1..8-token unit repeated >=12x at the tail (conservative; never trims legit lists/enumerations/code) and the three fused loops stop generation, keeping a short clean tail instead of an unbounded wall. Default ON (stop_on_runaway=True); --fused-no-loop-guard disables it for degeneration probes. Adds stopped_on_runaway to the result. Also: --chat-scripted-file (long prompt as committed fixture) + repoint the codegen-degen probe to a single-turn long prompt that wraps the ring at prefill (cheap; the multi-turn+native variant OOM'd the Mac runner). KAKEYA_KDBG probe instrumentation retained (inert unless the env var is set) for the pending on-device H-C'-vs-H-A' confirmation. Co-authored-by: FluffyAIcode --- .../backends/mlx/fused_specdecode.py | 72 +++++++++++++++++++ inference_engine/bridge/manifest.py | 2 +- .../research/k3_integrated_niah_eval_mac.py | 13 +++- tests/backends/mlx/test_fused_specdecode.py | 62 ++++++++++++++++ 4 files changed, 145 insertions(+), 4 deletions(-) diff --git a/inference_engine/backends/mlx/fused_specdecode.py b/inference_engine/backends/mlx/fused_specdecode.py index 3f35f02..9fafdd7 100644 --- a/inference_engine/backends/mlx/fused_specdecode.py +++ b/inference_engine/backends/mlx/fused_specdecode.py @@ -436,6 +436,7 @@ def fused_specdecode_generate_mlx_trim( block_size: int, eos_ids: Sequence[int] = (), single_fused: bool = False, + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """CUDA-parity fused spec decode: KEEP accepted K/V, TRIM only the rejected tail (no rollback, no carry re-forward). Requires the adapter to be @@ -461,6 +462,7 @@ def fused_specdecode_generate_mlx_trim( generated: List[int] = [] accepts: List[int] = [] block_evals: List[float] = [] + stopped_on_runaway = False ctx_len = C try: while len(generated) < gen_tokens: @@ -522,6 +524,12 @@ def fused_specdecode_generate_mlx_trim( timing["extend_s"] += time.perf_counter() - t_extend if any(t in eos for t in commit): break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break finally: adapter._capture_aux = False generated = generated[:gen_tokens] @@ -531,6 +539,7 @@ def fused_specdecode_generate_mlx_trim( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "loop": ("mlx_trim_single_fused_probe" if single_fused else "mlx_trim_keep_accepted_cuda_parity"), "single_fused": bool(single_fused), @@ -552,6 +561,7 @@ def fused_specdecode_generate_mlx( gen_tokens: int, block_size: int, eos_ids: Sequence[int] = (), + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """All-MLX fused spec decode with ONE host sync per block. @@ -593,6 +603,7 @@ def fused_specdecode_generate_mlx( generated: List[int] = [] accepts: List[int] = [] + stopped_on_runaway = False # Rollback-carry state: rejected blocks roll the WHOLE forward back # (rollback_block — see its docstring for why trim is unsound on the # wrapped sliding ring) and carry the stream-committed-but-not-cached @@ -676,6 +687,12 @@ def fused_specdecode_generate_mlx( timing["extend_s"] += time.perf_counter() - t_extend if any(t in eos for t in commit): break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break finally: adapter._capture_aux = False generated = generated[:gen_tokens] @@ -685,6 +702,7 @@ def fused_specdecode_generate_mlx( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "loop": "mlx_rollback_carry_v3", "time_breakdown_s": {k: round(v, 3) for k, v in timing.items()}, } @@ -717,6 +735,40 @@ def _sliding_ring_would_wrap(cache: Any, n_new: int) -> bool: return False +def _trailing_runaway_drop( + ids: Sequence[int], + *, + max_period: int = 8, + min_reps: int = 12, + keep_reps: int = 3, +) -> int: + """Return how many TRAILING tokens to drop if ``ids`` ends in a runaway + short-period loop, else 0. + + A runaway loop is a unit of ``1..max_period`` tokens repeated ``>= min_reps`` + times back-to-back at the tail (e.g. the ``**``/``.2``/``*`` markdown-marker + collapse greedy decoding falls into on code prompts). When found, we keep + ``keep_reps`` instances and drop the rest, so callers can stop generation + with a clean tail instead of emitting an unbounded wall of repeats. + + Deliberately CONSERVATIVE (>= 12 back-to-back repeats of a <= 8-token unit) + so legitimately repetitive text — numbered lists, ``矿工 A/B/C`` enumerations, + structured code — is never trimmed. Returns 0 when no runaway is present.""" + n = len(ids) + for p in range(1, max_period + 1): + if n < p * min_reps: + continue + unit = list(ids[n - p:]) + reps = 0 + i = n + while i - p >= 0 and list(ids[i - p:i]) == unit: + reps += 1 + i -= p + if reps >= min_reps: + return max((reps - keep_reps) * p, 0) + return 0 + + # --------------------------------------------------------------------------- # # The fused spec-decode loop (control flow; MLX/torch ops via injected fns). # --------------------------------------------------------------------------- # @@ -734,6 +786,7 @@ def fused_specdecode_generate( arange_fn: Callable[[int, int], Any], cat_aux_fn: Callable[[Sequence[Any]], Any], allow_greedy_fallback: bool = True, + stop_on_runaway: bool = True, ) -> Dict[str, Any]: """Run the fused engine. ``adapter`` must already be prefilled. Per block: draft from the cached drafter context (B), verify+capture-aux incrementally @@ -762,6 +815,7 @@ def fused_specdecode_generate( generated: List[int] = [] accepts: List[int] = [] fallback_to_greedy = False + stopped_on_runaway = False try: while len(generated) < gen_tokens: L = min(block_size, gen_tokens - len(generated)) @@ -852,6 +906,17 @@ def fused_specdecode_generate( # endregion if any(t in eos for t in commit): break + # Greedy decoding can collapse into a runaway short-period loop (e.g. + # the **/.2/* markdown-marker wall on code prompts); the drafter then + # trivially predicts the repeats and the greedy verifier accepts them, + # so acceptance stays HIGH while the output is garbage. Stop on it + # instead of emitting an unbounded wall (keeps a short clean tail). + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break if (allow_greedy_fallback and len(accepts) >= 2 and (sum(accepts) / len(accepts)) < 1.5): fallback_to_greedy = True @@ -869,6 +934,12 @@ def fused_specdecode_generate( generated.append(tok) if tok in eos: break + if stop_on_runaway: + drop = _trailing_runaway_drop(generated) + if drop > 0: + del generated[len(generated) - drop:] + stopped_on_runaway = True + break timing["fallback_greedy_s"] += time.perf_counter() - t_fb finally: adapter._capture_aux = False @@ -879,5 +950,6 @@ def fused_specdecode_generate( "mean_accept_len": (round(sum(accepts) / len(accepts), 3) if accepts else 0.0), "decode_tokens": len(generated), + "stopped_on_runaway": stopped_on_runaway, "time_breakdown_s": {k: round(v, 3) for k, v in timing.items()}, } diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 05237d2..5977c77 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -796,7 +796,7 @@ def _harness_preset( "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", - "--chat", "--chat-native-ref", + "--chat", "--chat-native-ref", "--fused-no-loop-guard", "--chat-scripted-file", "scripts/research/pow_codegen_longprompt.txt", "--output", "results/research/codegen_degen_2815_longprompt.json", diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index ed354be..345de2b 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -185,6 +185,10 @@ def parse_args() -> argparse.Namespace: "'||'-separated) scripted prompt from a UTF-8 file. Lets " "a long context be a committed fixture instead of a giant " "manifest argv. Overrides --chat-scripted when set.") + ap.add_argument("--fused-no-loop-guard", action="store_true", + help="DIAGNOSTIC: disable the fused engine's runaway-loop stop " + "(default ON) so a degeneration probe can observe the full " + "collapse. Production chat keeps the guard enabled.") ap.add_argument("--chat-native-ref", action="store_true", help="DIAGNOSTIC opt-in: before each chat turn, also run a " "plain NATIVE greedy AR decode of the SAME prompt for " @@ -889,25 +893,28 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: getattr(adapter, "_cache", None))}}) # endregion t0 = time.perf_counter() + _guard = not args.fused_no_loop_guard if mlx_drafter is not None and args.cuda_trim: res = fused_specdecode_generate_mlx_trim( adapter, active_drafter, aux_prompt=aux_prompt, embed_fn=embed_fn, lm_head_fn=lm_head_fn, gen_tokens=args.max_new_tokens, block_size=args.block_size, - eos_ids=chat_eos, single_fused=args.single_fused) + eos_ids=chat_eos, single_fused=args.single_fused, + stop_on_runaway=_guard) elif mlx_drafter is not None: res = fused_specdecode_generate_mlx( adapter, active_drafter, aux_prompt=aux_prompt, embed_fn=embed_fn, lm_head_fn=lm_head_fn, gen_tokens=args.max_new_tokens, block_size=args.block_size, - eos_ids=chat_eos) + eos_ids=chat_eos, stop_on_runaway=_guard) else: res = fused_specdecode_generate( adapter, active_drafter, aux_prompt=aux_prompt, embed_fn=embed_fn, lm_head_fn=lm_head_fn, gen_tokens=args.max_new_tokens, block_size=args.block_size, eos_ids=chat_eos, argmax_fn=argmax_fn, arange_fn=arange_fn, - cat_aux_fn=cat_aux_fn, allow_greedy_fallback=False) + cat_aux_fn=cat_aux_fn, allow_greedy_fallback=False, + stop_on_runaway=_guard) res["decode_s"] = round(time.perf_counter() - t0, 3) res["f_theta_ran"] = f_theta_ran res["f_theta_layers"] = sorted(rk.keys()) if rk else [] diff --git a/tests/backends/mlx/test_fused_specdecode.py b/tests/backends/mlx/test_fused_specdecode.py index ddf099b..f9c37a4 100644 --- a/tests/backends/mlx/test_fused_specdecode.py +++ b/tests/backends/mlx/test_fused_specdecode.py @@ -170,6 +170,68 @@ def __init__(self, offset): self.max_size = None +def test_trailing_runaway_drop_detects_and_trims_loops(): + # 1-token unit repeated 20x -> drop all but keep_reps (default 3). + ids = [1, 2, 3] + [9] * 20 + drop = fsd._trailing_runaway_drop(ids) + assert drop == 17 # 20 - 3 kept + # multi-token unit (period 3) repeated 12x -> drop (12-3)*3 = 27. + ids2 = [5, 6] + [7, 8, 9] * 12 + assert fsd._trailing_runaway_drop(ids2) == 27 + + +def test_trailing_runaway_drop_is_conservative(): + # fewer than min_reps (12) back-to-back -> no trim. + assert fsd._trailing_runaway_drop([9] * 11) == 0 + # legitimate non-repeating tail -> no trim. + assert fsd._trailing_runaway_drop(list(range(40))) == 0 + # a period that does not tile the very tail -> no trim. + assert fsd._trailing_runaway_drop([1, 2] * 10 + [3]) == 0 + # empty / short -> no trim. + assert fsd._trailing_runaway_drop([]) == 0 + + +def test_fused_loop_stops_on_runaway_repeat(): + # Drafter keeps proposing the same token; the fake verifier's "+1" truth is + # defeated by making the bonus re-loop: we feed a drafter that always drafts + # the marker token and a verifier that greedily agrees, so the committed + # stream becomes a runaway single-token loop the guard must cut. + class _LoopAdapter(_FakeAdapter): + def forward_block(self, candidate): + # verifier greedily predicts the SAME marker token (42) forever. + if self._capture_aux: + L = len(candidate) + self._last_aux = [torch.zeros(L, self.hidden)] + return [42 for _ in candidate] + + adapter = _LoopAdapter(prompt_len=5, first_token=42) + drafter = _FakeDrafter(drafts=[[42, 42, 42]] * 60) + res = fsd.fused_specdecode_generate( + adapter, drafter, gen_tokens=400, block_size=4, eos_ids=(), + allow_greedy_fallback=False, **_loop_kwargs(drafter)) + assert res["stopped_on_runaway"] is True + # stopped early with a short clean tail, nowhere near the 400 budget. + assert len(res["tokens"]) < 40 + assert set(res["tokens"]) == {42} + + +def test_fused_loop_runaway_guard_can_be_disabled(): + class _LoopAdapter(_FakeAdapter): + def forward_block(self, candidate): + if self._capture_aux: + self._last_aux = [torch.zeros(len(candidate), self.hidden)] + return [42 for _ in candidate] + + adapter = _LoopAdapter(prompt_len=5, first_token=42) + drafter = _FakeDrafter(drafts=[[42, 42, 42]] * 200) + res = fsd.fused_specdecode_generate( + adapter, drafter, gen_tokens=120, block_size=4, eos_ids=(), + allow_greedy_fallback=False, stop_on_runaway=False, + **_loop_kwargs(drafter)) + assert res["stopped_on_runaway"] is False + assert len(res["tokens"]) == 120 # ran to the full budget + + def test_sliding_ring_would_wrap_detects_wrap(): # offset + n_new >= max_size -> the rotating ring becomes non-trimmable. cache = [_FakeRotating(offset=1022, max_size=1024)] From d10aac96bd44406ba999c122cf2aabbbd5a57cc5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 18 Jun 2026 04:23:54 +0000 Subject: [PATCH 5/8] fix(probe): drop env KAKEYA_KDBG prefix (broke venv python3 -> no mlx_lm); add guard-ON validation preset The 'env KAKEYA_KDBG=1 python3' prefix resolved a python3 without mlx_lm on the runner (ModuleNotFoundError). Drop it (KDBG instrumentation is now inert, which is also what we want for the final PR). The native_ref/text/stopped_on_runaway signals in the JSON are sufficient to characterize + validate. Add mlx-kakeya-codegen-guard-validate (guard ON) to prove the clean stop. Co-authored-by: FluffyAIcode --- inference_engine/bridge/manifest.py | 29 ++++++++++++++++++- .../inference_engine/bridge/test_manifest.py | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 5977c77..76e9ee1 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -788,7 +788,6 @@ def _harness_preset( "degenerate ⇒ bounded-greedy pathology the engine must guard.", command_templates=( ( - "env", "KAKEYA_KDBG=1", "python3", "scripts/research/k3_integrated_niah_eval_mac.py", "--verifier-path", "${ENV:KAKEYA_MAC_VERIFIER_PATH}", "--drafter-id", "${ENV:KAKEYA_MAC_DRAFTER_ID}", @@ -806,6 +805,34 @@ def _harness_preset( params={"max_new_tokens": ("int:max_new_tokens", "192")}, validate_reports=False, ), + Preset( + name="mlx-kakeya-codegen-guard-validate", + description="Validate the runaway-loop guard end-to-end: full f_θ fused " + "engine on the same long code prompt (pow_codegen_longprompt" + ".txt) with the guard ENABLED (default). The fused answer " + "must NOT collapse into a marker wall — the guard stops the " + "runaway (stopped_on_runaway) leaving a clean tail — while " + "the native-greedy control (no guard) degenerates, proving " + "the guard is what saves the engine from greedy pathology.", + command_templates=( + ( + "python3", "scripts/research/k3_integrated_niah_eval_mac.py", + "--verifier-path", "${ENV:KAKEYA_MAC_VERIFIER_PATH}", + "--drafter-id", "${ENV:KAKEYA_MAC_DRAFTER_ID}", + "--f-theta-dir", "${ENV:KAKEYA_MAC_FTHETA_DIR}", + "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", + "--sink-size", "4", "--window-size", "64", "--block-size", "4", + "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", + "--chat", "--chat-native-ref", + "--chat-scripted-file", + "scripts/research/pow_codegen_longprompt.txt", + "--output", "results/research/codegen_guard_validate_2815.json", + ), + ), + timeout_minutes=90, + params={"max_new_tokens": ("int:max_new_tokens", "256")}, + validate_reports=False, + ), Preset( name="mlx-kakeya-degen-probe", description="Long-decode regression probe: full f_θ fused engine on a " diff --git a/tests/inference_engine/bridge/test_manifest.py b/tests/inference_engine/bridge/test_manifest.py index cce242d..cfea538 100644 --- a/tests/inference_engine/bridge/test_manifest.py +++ b/tests/inference_engine/bridge/test_manifest.py @@ -82,6 +82,7 @@ def test_allowlist_contains_exactly_the_documented_presets(): "mlx-env-probe", "mlx-kakeya-chat-smoke", "mlx-kakeya-codegen-degen-probe", + "mlx-kakeya-codegen-guard-validate", "mlx-kakeya-degen-probe", "mlx-kakeya-fused-chat-ftheta", "mlx-kakeya-fused-chat-smoke", From f8a7a9ae517fc6d542572b5dc60c578f46ae3436 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 18 Jun 2026 05:30:52 +0000 Subject: [PATCH 6/8] debug(probe): long single-decode A/B (drop native-ref for memory, budget 1100) to reach the ~978-tok collapse onset Co-authored-by: FluffyAIcode --- inference_engine/bridge/manifest.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 76e9ee1..0953833 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -795,14 +795,14 @@ def _harness_preset( "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", - "--chat", "--chat-native-ref", "--fused-no-loop-guard", + "--chat", "--fused-no-loop-guard", "--chat-scripted-file", "scripts/research/pow_codegen_longprompt.txt", "--output", "results/research/codegen_degen_2815_longprompt.json", ), ), - timeout_minutes=90, - params={"max_new_tokens": ("int:max_new_tokens", "192")}, + timeout_minutes=120, + params={"max_new_tokens": ("int:max_new_tokens", "1100")}, validate_reports=False, ), Preset( @@ -823,14 +823,14 @@ def _harness_preset( "--s5-exact-full-attn", "--fused-specdecode", "--force-f-theta", "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", - "--chat", "--chat-native-ref", + "--chat", "--chat-scripted-file", "scripts/research/pow_codegen_longprompt.txt", "--output", "results/research/codegen_guard_validate_2815.json", ), ), - timeout_minutes=90, - params={"max_new_tokens": ("int:max_new_tokens", "256")}, + timeout_minutes=120, + params={"max_new_tokens": ("int:max_new_tokens", "1100")}, validate_reports=False, ), Preset( From 85abe81834b584b507eefc9a99830ed9ded1b9e1 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 18 Jun 2026 05:44:50 +0000 Subject: [PATCH 7/8] debug(probe): multi-turn (explanation->code) guard-off/on A/B, no native-ref, budget 900 (matches the user's high-accept regime) Co-authored-by: FluffyAIcode --- inference_engine/bridge/manifest.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 0953833..76794be 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -796,13 +796,13 @@ def _harness_preset( "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", "--chat", "--fused-no-loop-guard", - "--chat-scripted-file", - "scripts/research/pow_codegen_longprompt.txt", + "--chat-scripted", + "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", "--output", "results/research/codegen_degen_2815_longprompt.json", ), ), timeout_minutes=120, - params={"max_new_tokens": ("int:max_new_tokens", "1100")}, + params={"max_new_tokens": ("int:max_new_tokens", "900")}, validate_reports=False, ), Preset( @@ -824,13 +824,13 @@ def _harness_preset( "--sink-size", "4", "--window-size", "64", "--block-size", "4", "--max-new-tokens", "{max_new_tokens}", "--ignore-turn-stop", "--chat", - "--chat-scripted-file", - "scripts/research/pow_codegen_longprompt.txt", + "--chat-scripted", + "请详细解释POW的工作原理||实现一个PoW的代码,用c语言完成", "--output", "results/research/codegen_guard_validate_2815.json", ), ), timeout_minutes=120, - params={"max_new_tokens": ("int:max_new_tokens", "1100")}, + params={"max_new_tokens": ("int:max_new_tokens", "900")}, validate_reports=False, ), Preset( From 772c8dfe571bb4bfdfb85a1d634d3c8eed39e4cf Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 18 Jun 2026 06:17:18 +0000 Subject: [PATCH 8/8] cleanup(mlx-fused): strip inert KDBG probe instrumentation; finalize codegen presets - Remove the KAKEYA_KDBG-gated debug instrumentation (helpers + per-block emission + prefill_state/turn_compare) from fused_specdecode.py and k3_integrated_niah_eval_mac.py. Investigation complete. - Keep the production fix (runaway-loop guard) + the --chat-scripted-file / --fused-no-loop-guard / --chat-native-ref flags. - Repoint the two codegen presets to the multi-turn 'explain||code' chat (guard-off probe + guard-on validate), accurate descriptions; drop the now- unused pow_codegen_longprompt.txt fixture. On-device (Mac M4): across short/long/multi-turn regimes the engine is coherent (fused==native); guard-on and guard-off outputs are byte-identical on the multi-turn code scenario -> the guard is inert on healthy output (no regression) and the systematic degeneration was already resolved by the wrap fix (#146). Co-authored-by: FluffyAIcode --- .../backends/mlx/fused_specdecode.py | 79 ------------ inference_engine/bridge/manifest.py | 33 +++-- .../research/k3_integrated_niah_eval_mac.py | 113 ------------------ scripts/research/pow_codegen_longprompt.txt | 85 ------------- 4 files changed, 14 insertions(+), 296 deletions(-) delete mode 100644 scripts/research/pow_codegen_longprompt.txt diff --git a/inference_engine/backends/mlx/fused_specdecode.py b/inference_engine/backends/mlx/fused_specdecode.py index 9fafdd7..3d44d79 100644 --- a/inference_engine/backends/mlx/fused_specdecode.py +++ b/inference_engine/backends/mlx/fused_specdecode.py @@ -35,69 +35,6 @@ restored_prefill_cache, ) -# region agent log (fused-codegen-degeneration-2815 probe; strip after fix) -import os as _kdbg_os -import sys as _kdbg_sys -import json as _kdbg_json - -_KDBG = bool(_kdbg_os.environ.get("KAKEYA_KDBG")) - - -def _kdbg(hyp: str, msg: str, **data: Any) -> None: - """Emit one NDJSON probe line to stderr (prefix ``KDBG ``) and, best-effort, - to /opt/cursor/logs/debug.log. No-op unless ``KAKEYA_KDBG`` is set, so - production behaviour is unchanged.""" - if not _KDBG: - return - rec = {"hypothesisId": hyp, "location": "fused_specdecode.py", - "message": msg, "data": data} - try: - _kdbg_sys.stderr.write("KDBG " + _kdbg_json.dumps(rec, ensure_ascii=False) + "\n") - _kdbg_sys.stderr.flush() - except Exception: - pass - try: - with open("/opt/cursor/logs/debug.log", "a") as _f: - _f.write(_kdbg_json.dumps(rec) + "\n") - except Exception: - pass - - -def _kdbg_cycle(ids: Sequence[int], window: int = 80) -> Tuple[float, int]: - """Short-unit cycle metric on the tail of ``ids``: returns - ``(cyc_frac, cyc_p)`` where ``cyc_p`` is the period (1..window//3) whose - back-to-back repetition covers the largest fraction ``cyc_frac`` of the - trailing ``window`` tokens. ~1.0 => the tail is a tight repeating loop.""" - w = list(ids[-window:]) - n = len(w) - if n < 6: - return 0.0, 0 - best_frac, best_p = 0.0, 0 - for p in range(1, n // 3 + 1): - run, i = 0, n - 1 - while i - p >= 0 and w[i] == w[i - p]: - run += 1 - i -= 1 - if run > 0: - frac = (run + p) / n - if frac > best_frac: - best_frac, best_p = frac, p - return round(best_frac, 3), best_p - - -def _kdbg_cache_offsets(cache: Any) -> Tuple[Optional[int], Optional[int]]: - """(first full-attn KVCache offset, first sliding RotatingKVCache offset).""" - off_full = off_rot = None - for c in (cache or []): - nm = type(c).__name__ - if off_rot is None and "Rotating" in nm: - off_rot = int(getattr(c, "offset", -1)) - elif off_full is None and "Rotating" not in nm: - off_full = int(getattr(c, "offset", -1)) - return off_full, off_rot -# endregion - - # --------------------------------------------------------------------------- # # Component A: capture verifier aux-layer hidden states (no transformers # `output_hidden_states` on MLX → patch the decoder-layer __call__). @@ -888,22 +825,6 @@ def fused_specdecode_generate( commit = candidate[:accepted] + [correction] generated += commit accepts.append(accepted) - # region agent log (fused-codegen-degeneration-2815 probe) - if _KDBG: - off_full, off_rot = _kdbg_cache_offsets(getattr(adapter, "_cache", None)) - cyc_frac, cyc_p = _kdbg_cycle(generated) - # H-D: cache.offset must track committed length (past_len). - # off_rot lags by the sliding window (bounded), off_full == past_len. - _kdbg("AD", "block", - blk=len(accepts) - 1, base=cstart, - past_len=int(adapter._past_len), gen=len(generated), - off_full=off_full, off_rot=off_rot, - bonus=int(bonus), cand=[int(x) for x in candidate], - n_cand=len(candidate), accepted=int(accepted), - commit=[int(x) for x in commit], - next_argmax=int(argmax_fn(adapter.next_token_logits)), - cyc_frac=cyc_frac, cyc_p=cyc_p) - # endregion if any(t in eos for t in commit): break # Greedy decoding can collapse into a runaway short-period loop (e.g. diff --git a/inference_engine/bridge/manifest.py b/inference_engine/bridge/manifest.py index 76794be..fbcdbb0 100644 --- a/inference_engine/bridge/manifest.py +++ b/inference_engine/bridge/manifest.py @@ -773,19 +773,13 @@ def _harness_preset( ), Preset( name="mlx-kakeya-codegen-degen-probe", - description="DEBUG: full f_θ fused engine on a LONG single-turn prompt " - "(~2k-char PoW explanation + a 'write C code' request, from " - "the committed fixture pow_codegen_longprompt.txt) so the " - "native RotatingKVCache ring is ALREADY WRAPPED at prefill " - "(would_wrap_block0). Short single-turn prompts were proven " - "token-identical to native & coherent; this isolates the " - "long-prompt-prefill regime cheaply (tiny 192-tok budget). " - "KAKEYA_KDBG logs prefill state (prompt_len, any_wrapped, " - "would_wrap_block0, rot/full offsets) + per-block offsets + " - "turn_compare_fused_vs_native. Native-greedy control " - "(--chat-native-ref): native coherent + fused garbled ⇒ " - "long-prompt prefill corrupts logits (engine bug); both " - "degenerate ⇒ bounded-greedy pathology the engine must guard.", + description="Regression probe (guard DISABLED): full f_θ fused engine " + "on the multi-turn 'explain PoW || write PoW in C' chat " + "that originally degenerated, with --fused-no-loop-guard so " + "any greedy markdown-marker collapse is observable. Pairs " + "with mlx-kakeya-codegen-guard-validate (guard ENABLED) to " + "show the guard is what keeps the answer clean. On current " + "code (post wrap-fix) both turns stay coherent.", command_templates=( ( "python3", "scripts/research/k3_integrated_niah_eval_mac.py", @@ -808,12 +802,13 @@ def _harness_preset( Preset( name="mlx-kakeya-codegen-guard-validate", description="Validate the runaway-loop guard end-to-end: full f_θ fused " - "engine on the same long code prompt (pow_codegen_longprompt" - ".txt) with the guard ENABLED (default). The fused answer " - "must NOT collapse into a marker wall — the guard stops the " - "runaway (stopped_on_runaway) leaving a clean tail — while " - "the native-greedy control (no guard) degenerates, proving " - "the guard is what saves the engine from greedy pathology.", + "engine on the multi-turn 'explain PoW || write PoW in C' " + "chat with the guard ENABLED (production default). The " + "answer must stay coherent and never collapse into a marker " + "wall — if a runaway starts, the guard stops it " + "(stopped_on_runaway) leaving a clean tail. Confirmed " + "coherent on current code; byte-identical to the guard-off " + "probe (the guard is inert on healthy output).", command_templates=( ( "python3", "scripts/research/k3_integrated_niah_eval_mac.py", diff --git a/scripts/research/k3_integrated_niah_eval_mac.py b/scripts/research/k3_integrated_niah_eval_mac.py index 345de2b..a6fc2eb 100644 --- a/scripts/research/k3_integrated_niah_eval_mac.py +++ b/scripts/research/k3_integrated_niah_eval_mac.py @@ -230,7 +230,6 @@ def main() -> int: MLXRestoredIncrementalVerifier, capture_aux_hidden, make_bridge_embed_lm_head, fused_specdecode_generate, fused_specdecode_generate_mlx, fused_specdecode_generate_mlx_trim, - _sliding_ring_would_wrap, # region agent log (fused-codegen-degeneration-2815) ) from inference_engine.v04.kv_compressor import make_default_compressor from inference_engine.bench.k3_report_gate import ( @@ -779,54 +778,6 @@ def _encode_chat(history: List[Dict[str, str]]) -> List[int]: history, add_generation_prompt=True) return list(cids.tolist() if hasattr(cids, "tolist") else cids) - # region agent log (fused-codegen-degeneration-2815 prefill probe) - import os as _kos_chat - _KDBG_CHAT = bool(_kos_chat.environ.get("KAKEYA_KDBG")) - - def _kdbg_emit(rec: Dict[str, Any]) -> None: - try: - sys.stderr.write("KDBG " + json.dumps(rec, ensure_ascii=False) + "\n") - sys.stderr.flush() - except Exception: - pass - try: - with open("/opt/cursor/logs/debug.log", "a") as _f: - _f.write(json.dumps(rec) + "\n") - except Exception: - pass - - def _kdbg_cache_summary(cache: Any) -> Dict[str, Any]: - """rot/full offset+max_size rollup + wrap/trimmable flags. The - decisive prefill signal: is the sliding RotatingKVCache already - wrapped (off>=max_size) BEFORE decode starts, and does full-attn - off == prompt_len? A pre-wrapped ring at prefill means the very - first speculative block's trim is refused (offset desync).""" - rot_off = rot_ms = full_off = None - any_wrapped = False - all_trimmable = True - n = 0 - for c in (cache or []): - n += 1 - nm = type(c).__name__ - off = int(getattr(c, "offset", -1)) - ms = getattr(c, "max_size", None) - ms = int(ms) if ms is not None else None - is_rot = "Rotating" in nm - if is_rot and ms is not None and off >= ms: - any_wrapped = True - trim_fn = getattr(c, "is_trimmable", None) - trim = bool(trim_fn()) if callable(trim_fn) else None - if trim is False: - all_trimmable = False - if is_rot and rot_off is None: - rot_off, rot_ms = off, ms - if (not is_rot) and full_off is None: - full_off = off - return {"n_layers": n, "rot_off": rot_off, "rot_ms": rot_ms, - "full_off": full_off, "any_wrapped": any_wrapped, - "all_trimmable": all_trimmable} - # endregion - def _gen_turn(pid: List[int]) -> Dict[str, Any]: # Opt-in A/B control (--chat-native-ref): a plain NATIVE greedy # AR decode of the SAME prompt for --max-new-tokens. Captured as @@ -837,14 +788,6 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: nref_tokens: List[int] = [] if args.chat_native_ref: nref_cache, nref_logits = native_prefill(list(pid)) - # region agent log (fused-codegen-degeneration-2815 prefill probe) - if _KDBG_CHAT: - _turn = sum(1 for h in history if h.get("role") == "user") - _kdbg_emit({"hypothesisId": "AE", - "message": "prefill_state_native", - "data": {"turn": _turn, "prompt_len": len(pid), - "cache": _kdbg_cache_summary(nref_cache)}}) - # endregion while len(nref_tokens) < args.max_new_tokens: tok = int(mx.argmax(nref_logits).item()) nref_tokens.append(tok) @@ -875,23 +818,6 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: restored_v_per_layer=_pad(rv, tsrc, T), evicted_positions=evicted, prefill_chunk_size=args.prefill_chunk_size, full_kv=args.cuda_trim) - # region agent log (fused-codegen-degeneration-2815 prefill probe) - if _KDBG_CHAT: - _turn = sum(1 for h in history if h.get("role") == "user") - _kdbg_emit({"hypothesisId": "AE", - "message": "prefill_state_fused", - "data": {"turn": _turn, "prompt_len": T, - "evicted_count": len(evicted), - "block_size": int(args.block_size), - "would_wrap_block0": bool( - _sliding_ring_would_wrap( - getattr(adapter, "_cache", None), - int(args.block_size))), - "past_len": int(adapter._past_len), - "f_theta_ran": bool(f_theta_ran), - "cache": _kdbg_cache_summary( - getattr(adapter, "_cache", None))}}) - # endregion t0 = time.perf_counter() _guard = not args.fused_no_loop_guard if mlx_drafter is not None and args.cuda_trim: @@ -937,45 +863,6 @@ def _gen_turn(pid: List[int]) -> Dict[str, Any]: res["native_ref_tokens"] = len(nref_tokens) res["resident_kv_bytes"] = int( sum(int(getattr(c, "nbytes", 0)) for c in (adapter._cache or []))) - # region agent log (fused-codegen-degeneration-2815 probe) - import os as _kos - if _kos.environ.get("KAKEYA_KDBG"): - ftoks = [int(t) for t in res.get("tokens", [])] - ntoks = [int(t) for t in nref_tokens] - div = None - for j, (a, b) in enumerate(zip(ftoks, ntoks)): - if a != b: - div = j - break - if div is None: - div = min(len(ftoks), len(ntoks)) - - def _dec(seq): - try: - return tokenizer.decode(seq, skip_special_tokens=True) - except TypeError: - return tokenizer.decode(seq) - rec = { - "hypothesisId": "AC", - "message": "turn_compare_fused_vs_native", - "data": { - "turn": sum(1 for h in history if h.get("role") == "user"), - "fused_n": len(ftoks), "native_n": len(ntoks), - "first_divergence_idx": div, - "fused_div_ctx": ftoks[max(0, div - 8):div + 16], - "native_div_ctx": ntoks[max(0, div - 8):div + 16], - "fused_div_text": _dec(ftoks[max(0, div - 8):div + 16]), - "native_div_text": _dec(ntoks[max(0, div - 8):div + 16]), - "fused_tail": ftoks[-48:], - "native_tail": ntoks[-48:], - "fused_tail_text": _dec(ftoks[-48:]), - "native_tail_text": _dec(ntoks[-48:]), - }, - } - sys.stderr.write( - "KDBG " + json.dumps(rec, ensure_ascii=False) + "\n") - sys.stderr.flush() - # endregion return res print(f"[chat] FULL fused engine: verifier={args.verifier_path} " diff --git a/scripts/research/pow_codegen_longprompt.txt b/scripts/research/pow_codegen_longprompt.txt deleted file mode 100644 index fa1ae08..0000000 --- a/scripts/research/pow_codegen_longprompt.txt +++ /dev/null @@ -1,85 +0,0 @@ -**PoW (Proof of Work,工作量证明)** 是区块链技术中最核心的共识机制之一。它的核心目的是:**在没有中心化机构(如银行)的情况下,让分布在世界各地的计算机能够达成一致,决定谁有权记账,并防止有人通过伪造数据来欺骗网络。** - -为了深入理解,我们可以从“核心逻辑”、“工作流程”、“数学原理”和“经济博弈”四个维度来详细解释。 - ---- - -### 1. 核心逻辑:用“算力”换取“信任” - -在去中心化网络中,大家面临一个问题:**如果每个人都说自己记了一笔账,听谁的?** - -PoW 的逻辑是:**谁付出了巨大的计算资源(工作量),谁就有权提议下一块账本。** -* **工作量(Work)**:指消耗的计算资源和时间。 -* **证明(Proof)**:当计算完成后,结果是公开且易于验证的。 - -这种机制确保了:**作恶的成本极高,而维护网络的收益(奖励)很诱人。** - ---- - -### 2. 工作流程:从“挖矿”到“验证” - -假设我们正在运行一个像比特币这样的 PoW 网络,流程如下: - -#### 第一步:收集交易 -矿工(节点)从网络中收集待处理的交易,并将它们打包成一个“候选区块”。 - -#### 第二步:寻找“随机数”(核心环节) -为了让区块生效,矿工必须解决一个数学难题。这个难题通常是: -> “找到一个数字(称为 **Nonce**),使得:**区块头部数据的哈希值(Hash) < 目标难度值(Target)**。” - -* **哈希函数(Hash Function)**:像是一个“数字粉碎机”。你输入任何内容,它都会输出一串固定长度的乱码。只要输入变一点点,输出就会天差地别。 -* **不可逆性**:你无法通过结果反推输入。 - -#### 第三步:竞争与“挖矿” -矿工们开始疯狂尝试。他们不断改变区块里的 `Nonce` 值,重新计算哈希值。 -* 矿工 A 尝试:`Hash(数据 + 1) = 0xabc...` (不符合要求) -* 矿工 B 尝试:`Hash(数据 + 2) = 0xdef...` (不符合要求) -* ... -* 矿工 C 运气好/算力强:`Hash(数据 + 999) = 0x000...` (**符合要求!**) - -一旦有人找到了满足条件的哈希值,他就“挖到了矿”。 - -#### 第四步:广播与验证 -获胜的矿工将新区块广播给全网。其他节点收到后,只需进行一次简单的计算(把 Nonce 代入哈希函数),发现结果确实符合要求,就会接受这个区块,并更新自己的账本。 - ---- - -### 3. 数学原理:为什么它是安全的? - -#### A. 难度调整机制 (Difficulty Adjustment) -如果每个人都买更快的显卡,挖矿速度会变快,导致区块产生太频繁。 -为了保持稳定的出块时间(例如比特币约10分钟一个块),系统会**自动调整难度**: -* 如果出块太快 $\rightarrow$ 提高目标值(让要求的哈绪值前导零更多,变得更难)。 -* 如果出块太慢 $\rightarrow$ 降低难度。 - -#### B. 概率与公平性 -由于哈希值是随机分布的,寻找答案的过程就像“在茫茫大海里捞针”。 -* **算力越高**,意味着你每秒尝试的次数越多,捞到针的概率就越大。 -* 这保证了在宏观上,算力占比与奖励分配是公平的。 - -#### C. 抵抗“双花”与篡改 -如果有人想修改 10 分钟前的一个交易,他必须重新计算那个区块的哈希,以及**之后所有区块**的哈希。 -由于后面的区块都包含了前一个区块的哈希值(形成链式结构),修改历史意味着要重算后面所有的工作量。除非攻击者的算力超过全网总和的 51%,否则这在经济上是不可能的。 - ---- - -### 4. 总结:PoW 的优缺点 - -#### 优点: -1. **极高的安全性**:通过物理世界的能源消耗(电力)为数字世界筑起防线。 -2. **去中心化**:任何人只要有设备就可以加入,不需要许可。 -3. **公平性**:基于数学概率,不存在人为干预。 - -#### 缺点: -1. **能源消耗大**:为了维持安全,全球范围内的矿机都在消耗大量电力(这是最受争议的一点)。 -2. **扩展性差(TPS低)**:为了保证全球同步,出块速度不能太快,导致处理交易的速度受限。 -3. **算力集中风险**:如果出现大规模矿池,可能导致权力向少数人手中集中。 - -### 通俗比喻总结 -想象一个**“数字猜数字游戏”**: -全网的人都在玩一个游戏,目标是猜出一个符合特定规则的数字。 -* **挖矿**就是不停地尝试不同的数字。 -* **难度**就是规则越来越严苛(比如要求数字必须以 10 个零开头)。 -* **验证**就是别人看到你猜中了,只需要看一眼你的数字是否符合规则,瞬间就能确认你没撒谎。 - -基于以上说明,用C语言写一个简短的PoW示例(只写核心循环,不超过30行)。 \ No newline at end of file