#!/usr/bin/env python3 """Generate the J-space REPRODUCTION dataset + article aggregates on Qwen2.5-0.5B. Per reasoning probe: base NLL, J-space-ablated NLL, random-subspace-ablated NLL (control), workspace effective-rank fraction, top-8 energy, per-layer future-influence norms, and logit-lens workspace concepts by depth. All REAL measurements — feed both the bitacora post and the HF dataset. CPU/float32.""" import json, torch import jspace_advanced as J model = J.load() L = model.config.num_hidden_layers; mid = L // 2 norm, head = model.model.norm, model.lm_head PROBES = [ "Q: If a train goes 60 km in 1 hour, how far in 3 hours? 60 times 3 equals", "Q: Ana has 5 apples, buys 7 more, gives 3 away. 5 plus 7 is 12, minus 3 is", "Q: A spider has 8 legs. How many legs do 3 spiders have? 8 times 3 is", "The opposite of hot is cold. The opposite of up is down. The opposite of fast is", "Paris is to France as Rome is to", "Q: There are 12 eggs in a box. Two boxes and 3 more eggs is 24 plus 3 =", "Q: 9 times 6 is", "If all cats are animals and Tom is a cat, then Tom is an", "Q: Half of 84 is", "Water freezes at 0 and boils at 100, so the range is 100 minus 0 =", "Q: A car travels 50 km per hour for 4 hours, covering 50 times 4 =", "Big is to small as tall is to", ] def ablate_nll(ids, k=8, layer=mid, random=False): G = J.influence_jacobian(model, ids) st = J.subspace_stats(G[layer], k) Q = st["dirs"] if random: Q, _ = torch.linalg.qr(torch.randn_like(Q)) return J.reasoning_nll(model, ids, intervene=(layer, Q)), st rows = [] agg = {"base": [], "jspace": [], "random": [], "effrank": [], "energy": [], "influence": None} for p in PROBES: ids = J.tok(p, return_tensors="pt").input_ids base = J.reasoning_nll(model, ids) js, st = ablate_nll(ids, random=False) rnd, _ = ablate_nll(ids, random=True) # per-layer future-influence norms G = J.influence_jacobian(model, ids) infl = [round(G[l].norm().item(), 4) for l in range(L)] # logit-lens workspace concepts at the final position, by depth with torch.no_grad(): hs = model(ids, output_hidden_states=True).hidden_states concepts = {} for l in range(0, L + 1, max(1, L // 6)): top = head(norm(hs[l][0]))[-1].topk(4).indices.tolist() concepts[l] = [J.tok.decode([t]).strip() for t in top] row = {"prompt": p, "base_nll": round(base, 4), "jspace_ablated_nll": round(js, 4), "random_ablated_nll": round(rnd, 4), "effrank_frac": round(st["eff_rank_frac"], 5), "energy_top8": round(st["energy_topk"], 4), "per_layer_influence": infl, "workspace_concepts_by_depth": concepts} rows.append(row) agg["base"].append(base); agg["jspace"].append(js); agg["random"].append(rnd) agg["effrank"].append(st["eff_rank_frac"]); agg["energy"].append(st["energy_topk"]) agg["influence"] = [a + b for a, b in zip(agg["influence"], infl)] if agg["influence"] else list(infl) print(f" {p[:42]:42s} base={base:.2f} Jabl={js:.2f} Rabl={rnd:.2f} effrank={st['eff_rank_frac']:.3f}", flush=True) n = len(PROBES) mean = lambda x: sum(x) / len(x) summary = { "model": "Qwen2.5-0.5B-Instruct", "layers": L, "hidden": model.config.hidden_size, "n_probes": n, "subspace_k": 8, "ablation_layer": mid, "mean_base_nll": round(mean(agg["base"]), 4), "mean_jspace_ablated_nll": round(mean(agg["jspace"]), 4), "mean_random_ablated_nll": round(mean(agg["random"]), 4), "jspace_damage": round(mean(agg["jspace"]) - mean(agg["base"]), 4), "random_damage": round(mean(agg["random"]) - mean(agg["base"]), 4), "causal_necessity_nats": round((mean(agg["jspace"]) - mean(agg["base"])) - (mean(agg["random"]) - mean(agg["base"])), 4), "mean_effrank_frac": round(mean(agg["effrank"]), 5), "mean_energy_top8": round(mean(agg["energy"]), 4), "mean_per_layer_influence": [round(x / n, 4) for x in agg["influence"]], } with open("/Users/kikocisneros/coco_ppl/jspace/jspace_measurements.jsonl", "w") as f: for r in rows: f.write(json.dumps(r) + "\n") json.dump(summary, open("/Users/kikocisneros/coco_ppl/jspace/jspace_summary.json", "w"), indent=2) print("\n=== SUMMARY ===") print(json.dumps(summary, indent=2)) print("JSPACE_REPORT_DONE")