"""Grounded natural-language targets from measured TinyChess trajectories. Every claim is traceable to a measurement. The textiser measures, then selects sentence fragments that are TRUE for this trajectory, with several natural phrasings chosen deterministically from a per-example seed so the bridge cannot memorise one rigid template. NO move explanations, NO why-claims, NO chess commentary: only statements directly implied by the measured tensors (category-1 claims in the brief). """ from __future__ import annotations BINS = "low moderate high".split() def _bin(v, q): # v in [0,1]; q = (low_cut, high_cut) return BINS[0 if v < q[0] else 1 if v < q[1] else 2] def _pick(rng, options): return rng.choice(options) def _frac(x): return f"{x:.0%}" def describe_trajectory(cap_meta: dict, seed: int = 0) -> str: """cap_meta: dict of engineered observables (see targets_from_capture): dh_norms: list per step of relative ||dh|| (vs ||h||) top_share: per-step spatial concentration of change in [0,1] settle_trend: 'increasing' | 'decreasing' | 'mixed' | 'flat' router_top: list of per-step top op names (attn/local/mlp/mem) router_entropy: list [0, log n_ops] mem_top_share: list per step halts: list of p_halt per step n_steps: int, stop_weights """ import random rng = random.Random(seed) L = [] # ---- settling / churn trend (temporal ordering) ---- dhl = cap_meta["dh_norms"] d0, d1 = dhl[0], dhl[-1] trend = cap_meta["settle_trend"] if trend == "decreasing" and d1 < 0.5 * d0: L.append(_pick(rng, [ "The internal pattern is settling: each pass is changing the board representation less than the one before.", "Successive passes are making smaller and smaller adjustments across the board.", "The computation appears to be converging, with the strongest changes early on.", ])) if trend == "increasing" and d1 > 1.5 * d0: L.append(_pick(rng, [ "The changes are growing from pass to pass; the representation keeps reorganising rather than settling.", "Rather than calming down, the internal state is being reshaped more and more.", ])) elif trend == "mixed": L.append(_pick(rng, [ "The magnitude of change fluctuates across passes rather than following one direction.", "Some passes barely move the representation while others rework it substantially.", ])) # ---- spatial concentration of change ---- ts = cap_meta["top_share"][-1] if ts > 0.5: L.append(_pick(rng, [ f"Most of the current change is concentrated in about {_frac(ts)} of the board, leaving the rest nearly untouched.", "Changes in this pass are localised to a few squares rather than spread over the whole board.", ])) elif ts < 0.2: L.append("The change is spread quite evenly across the board rather than concentrated anywhere.") # ---- router behaviour ---- tops = cap_meta["router_top"] if tops: first, last = tops[0], tops[-1] if first == last and len(set(tops)) == 1: L.append(_pick(rng, [ f"The pass-to-pass routing stays dominated by the same operation ({first}) throughout.", f"Every step leans on the {first} pathway.", ])) elif first != last: L.append(f"Routing shifts between passes, starting mostly with {first} and ending mostly with {last}.") # ---- halting ---- h_last = cap_meta["halts"][-1] if h_last > 0.8: L.append(_pick(rng, [ "The next pass looks likely to be the last: the halting signal is nearly certain.", "The computation is about to stop itself, with the halting probability very high.", ])) elif h_last < 0.2 and cap_meta["n_steps_observed"] >= 4: L.append("The halting signal remains low, so the computation does not seem ready to stop yet.") # ---- memory ---- if cap_meta["mem_top_share"] and cap_meta["mem_top_share"][-1] > 0.5: L.append(_pick(rng, [ "Memory is also becoming more selective.", "Reading has become more focused on a particular memory slot.", ])) elif cap_meta.get("mem_top_share") and all(s < 0.2 for s in cap_meta["mem_top_share"]): L.append(_pick(rng, [ "The memory slots stay essentially idle: reading remains spread evenly and nothing is being singled out.", "Memory never engages in this computation; the state changes don't involve it.", ])) # ---- repeated-change locality (from the operator's trace audit) ---- n_steps_obs = cap_meta.get("n_steps_observed", 0) if n_steps_obs >= 6: L.append(_pick(rng, [ f"The same few squares continue to change from one pass to the next across all {n_steps_obs} passes.", "A small set of squares keeps being reshaped while the rest of the board stays nearly still.", ])) return " ".join(L) def targets_from_capture(cap: dict, ops=("attn", "local", "mlp", "mem")): """Converts a capture_with_hooks() dict into the observable summary + text. All measurements use DETACHED tensors. One example per call (B=1). """ import torch steps = cap["steps"] dh_norms, top_share, router_top, router_ent, mem_share, halts = [], [], [], [], [], [] prev_norm = None for s in steps: h, hp = s["h"].detach(), s.get("h_prev", None) if hp is None: hn = torch.zeros(1) share = torch.zeros(1) else: dh = (h - hp) hn = dh.norm() / hp.norm().clamp_min(1e-6) per_sq = dh.abs().mean(-1).squeeze() share = (per_sq ** 2).sum() share = (share / (per_sq.mean() ** 2 * 64 + 1e-6)).clamp(0, 1) dh_norms.append(float(hn)) top_share.append(float(share)) r = s.get("router") if r is not None: router_top.append(ops[int(r[0].argmax())] if r.shape[-1] == len(ops) else "?") router_ent.append(float(-(r[0] * (r[0] + 1e-9).log()).sum())) ma = s.get("mem_attn") if ma is not None: mem_share.append(float(ma[0].max())) halts.append(float(s["p_halt"][0]) if s.get("p_halt") is not None else 0.0) # trend if len(dh_norms) >= 2: a, b = dh_norms[0], dh_norms[-1] if b < 0.5 * a: trend = "decreasing" elif b > 1.5 * a: trend = "increasing" elif max(dh_norms) - min(dh_norms) < 0.05: trend = "flat" else: trend = "mixed" else: trend = "flat" meta = {"dh_norms": dh_norms, "top_share": top_share, "settle_trend": trend, "router_top": router_top, "router_entropy": router_ent, "mem_top_share": mem_share, "halts": halts, "n_steps_observed": len(steps)} meta["text"] = describe_trajectory(meta, seed=int(abs(dh_norms[0] * 1000))) return meta