File size: 16,410 Bytes
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7101b
9477b5c
 
 
bc7101b
 
 
 
 
 
 
 
 
 
 
 
 
 
9477b5c
 
 
 
 
 
 
bc7101b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9477b5c
 
bc7101b
 
 
 
 
 
 
 
 
 
 
 
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7101b
 
 
 
 
 
 
 
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7101b
9477b5c
 
 
 
 
bc7101b
 
 
 
 
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7101b
 
 
 
 
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc7101b
 
 
 
 
9477b5c
 
 
bc7101b
 
 
 
 
9477b5c
 
 
 
 
 
 
 
bc7101b
9477b5c
bc7101b
 
9477b5c
 
 
 
bc7101b
 
9477b5c
 
 
 
 
bc7101b
 
9477b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""Pre-registered ablation evaluation for BLT-Reasoner.

Computes GSM8K accuracy under three conditions:
  A. normal-z   : latents from W_proj(h_{t-1}) loop
  B. random-z   : latents drawn from N(0, σ²)  with σ matched to mean ||z||
  C. zero-z     : K=0 (no latents at all; y attends directly to x, but
                  block_y_to_x is still on so y has no information path —
                  expected to be ~0% if the bottleneck is working)

H1 success: acc(A) - acc(B) >= 15pp  AND  acc(A) - acc(C) >= 25pp
"""
from __future__ import annotations

import argparse
import json
import re
import time
from pathlib import Path
from typing import List, Optional

import torch
from torch.utils.data import DataLoader

from .data import GSM8KDataset, MATHDataset, collate_batch, extract_final_number, extract_boxed_answer
from .model import BLTConfig, LatentProjector, build_base, forward_with_latent, generate_with_latent


def parse_pred(text: str, dataset: str = "gsm8k") -> Optional[str]:
    """Extract final answer from model output. Dataset-aware:
    * gsm8k: look for "#### N", fall back to last number.
    * math:  look for the LAST ``\\boxed{...}`` (handles latex), fall back to last number.
    """
    ds = (dataset or "gsm8k").lower()
    if ds == "math":
        boxed = extract_boxed_answer(text)
        if boxed is not None:
            return boxed.strip()
        # Some models emit "= ANSWER" without a box; fall back to last number.
        nums = re.findall(r"-?\d+(?:\.\d+)?", text)
        return nums[-1] if nums else None
    # gsm8k path
    m = re.findall(r"####\s*(-?\d+(?:\.\d+)?)", text)
    if m:
        return m[-1].strip()
    nums = re.findall(r"-?\d+(?:\.\d+)?", text)
    return nums[-1] if nums else None


def _normalize_math_answer(s: str) -> str:
    """Aggressively normalize MATH-style answer strings for comparison.

    Strips whitespace, LaTeX wrappers, dollar signs, common formatting noise.
    Not a complete LaTeX-equivalent checker — close to but weaker than the
    Hendrycks et al. evaluator. For our purposes we want a fast, deterministic
    string compare that catches the common-case correctness signals.
    """
    if s is None:
        return ""
    s = s.strip().replace(" ", "")
    # Strip outer $...$ math wrappers
    while s.startswith("$") and s.endswith("$") and len(s) > 2:
        s = s[1:-1]
    # Strip \text{...} wrappers (single layer)
    s = re.sub(r"\\text\{([^{}]*)\}", r"\1", s)
    # Strip leading/trailing braces
    while s.startswith("{") and s.endswith("}") and len(s) > 2:
        s = s[1:-1]
    # Strip a trailing "." (occasionally seen)
    if s.endswith("."):
        s = s[:-1]
    return s


def correct(pred: Optional[str], gold: str, dataset: str = "gsm8k") -> bool:
    if pred is None:
        return False
    ds = (dataset or "gsm8k").lower()
    if ds == "math":
        p = _normalize_math_answer(pred)
        g = _normalize_math_answer(gold)
        if p == g:
            return True
        # Also try numeric compare for the simple case
        try:
            return abs(float(p) - float(g)) < 1e-4
        except ValueError:
            return False
    # gsm8k path: numeric tolerance
    try:
        return abs(float(pred) - float(gold)) < 1e-4
    except ValueError:
        return False


def estimate_z_std(model, projector, tokenizer, val_loader, device, K) -> float:
    """Run model on a few batches to estimate the per-coordinate std of z."""
    from .model import forward_with_latent
    model.eval()
    all_z = []
    with torch.no_grad():
        for i, batch in enumerate(val_loader):
            if i >= 4: break
            x_ids = batch.x_ids.to(device)
            x_attn = batch.x_attn.to(device)
            y_ids = batch.y_ids.to(device)
            _, z, _ = forward_with_latent(model, x_ids, x_attn, y_ids, projector, K,
                                          block_y_to_x=True)
            all_z.append(z.float().cpu())
    z_cat = torch.cat(all_z, dim=0)
    return float(z_cat.std().item())


def run_condition(model, projector, tokenizer, val_loader, device, K, condition: str,
                  z_std: float, max_new_tokens: int, temperature: float,
                  dataset: str = "gsm8k",
                  block_y_to_x: bool = True) -> dict:
    """condition in {"normal", "random", "zero"}.

    `dataset` controls parsing of the gold final answer and of the prediction:
    "gsm8k" → "#### N", "math" → \\boxed{...}.
    """
    inner = model.get_base_model() if hasattr(model, "get_base_model") else model
    d_model = inner.config.hidden_size
    correct_n = 0
    total = 0
    examples = []
    model.eval()
    for batch in val_loader:
        x_ids = batch.x_ids.to(device)
        x_attn = batch.x_attn.to(device)
        B = x_ids.size(0)
        if condition == "normal":
            override_z = None
            K_eff = K
        elif condition == "random":
            override_z = torch.randn(B, K, d_model, device=device,
                                     dtype=next(projector.parameters()).dtype) * z_std
            K_eff = K
        elif condition == "zero":
            override_z = torch.zeros(B, 0, d_model, device=device,
                                     dtype=next(projector.parameters()).dtype)
            K_eff = 0
        else:
            raise ValueError(condition)

        gen = generate_with_latent(
            model, tokenizer, projector,
            x_ids=x_ids, x_attn=x_attn, K=K_eff,
            block_y_to_x=block_y_to_x, max_new_tokens=max_new_tokens,
            temperature=temperature, eos_token_id=tokenizer.eos_token_id,
            override_z=override_z,
        )
        for b in range(B):
            text = tokenizer.decode(gen[b], skip_special_tokens=True)
            pred = parse_pred(text, dataset=dataset)
            # GSM8K final_strs are "#### N"; MATH final_strs are the boxed value already.
            raw_gold = batch.final_strs[b]
            gold = raw_gold.replace("#### ", "").strip() if dataset.lower() != "math" else raw_gold.strip()
            ok = correct(pred, gold, dataset=dataset)
            correct_n += int(ok)
            total += 1
            if len(examples) < 5:
                examples.append({"text": text[:200], "pred": pred, "gold": gold, "ok": ok})
    return {"condition": condition, "K": K_eff, "acc": correct_n / max(total, 1),
            "n": total, "correct": correct_n, "examples": examples}


# ---------------------------------------------------------------------------
# Perturbation-curve evaluation (Viteri et al. ICLR 2026 analog for continuous z).
#
# At severity p ∈ [0, 1] we replace a random ⌊p·K⌋ subset of latent positions
# with Gaussian noise matched to the observed z std. At p=0 this equals
# `condition="normal"`; at p=1 this equals `condition="random"`. Intermediate
# severities give a *continuous* load-bearing measurement that we can plot
# as a curve, the way the Markovian-LM paper reports CoT-corruption sensitivity.
# A load-bearing z shows monotone, steep descent; a decorative z (your
# Abstract-CoT failure mode) shows a flat curve.
# ---------------------------------------------------------------------------

@torch.no_grad()
def _get_z_for_batch(model, projector, x_ids, x_attn, K):
    """Compute z by running the latent loop, no backprop, no y forward."""
    _, z, _ = forward_with_latent(
        model, x_ids, x_attn, y_ids=None, projector=projector, K=K,
        block_y_to_x=True, return_z=True,
    )
    return z   # [B, K, d]


def _perturb_z(z: torch.Tensor, severity: float, z_std: float, seed: int) -> torch.Tensor:
    """Replace ⌊severity·K⌋ randomly-chosen latent positions per example with
    Gaussian noise matched to z_std. Deterministic given seed for fair compare
    across severities and conditions.
    """
    B, K, d = z.shape
    if severity <= 0.0:
        return z
    n_replace = max(1, int(round(severity * K)))
    g = torch.Generator(device=z.device).manual_seed(seed)
    out = z.clone()
    for b in range(B):
        idx = torch.randperm(K, generator=g, device=z.device)[:n_replace]
        noise = torch.randn(n_replace, d, device=z.device, generator=g,
                            dtype=z.dtype) * z_std
        out[b, idx] = noise
    return out


def run_perturbation_curve(model, projector, tokenizer, val_loader, device, K,
                            z_std: float, severities, max_new_tokens: int,
                            temperature: float, seed: int = 0) -> dict:
    """For each severity p, replace fraction p of latent positions with noise
    and evaluate accuracy. severities is a list of floats in [0, 1].
    """
    inner = model.get_base_model() if hasattr(model, "get_base_model") else model
    model.eval()
    curve = []
    examples_at_p100 = []
    for p in severities:
        correct_n, total = 0, 0
        for bi, batch in enumerate(val_loader):
            x_ids = batch.x_ids.to(device)
            x_attn = batch.x_attn.to(device)
            B = x_ids.size(0)
            z = _get_z_for_batch(model, projector, x_ids, x_attn, K)
            z_pert = _perturb_z(z, severity=p, z_std=z_std, seed=seed + bi)
            gen = generate_with_latent(
                model, tokenizer, projector,
                x_ids=x_ids, x_attn=x_attn, K=K,
                block_y_to_x=True, max_new_tokens=max_new_tokens,
                temperature=temperature, eos_token_id=tokenizer.eos_token_id,
                override_z=z_pert,
            )
            for b in range(B):
                text = tokenizer.decode(gen[b], skip_special_tokens=True)
                pred = parse_pred(text)
                gold = batch.final_strs[b].replace("#### ", "").strip()
                ok = correct(pred, gold)
                correct_n += int(ok)
                total += 1
                if p == severities[-1] and len(examples_at_p100) < 3:
                    examples_at_p100.append({"text": text[:200], "pred": pred,
                                              "gold": gold, "ok": ok})
        acc = correct_n / max(total, 1)
        curve.append({"severity": p, "acc": acc, "correct": correct_n, "n": total})
        print(f"[perturb p={p:.2f}] acc={acc:.4f} ({correct_n}/{total})")
    # Compute summary statistics: monotonicity, slope.
    accs = [c["acc"] for c in curve]
    n_monotone = sum(1 for i in range(len(accs) - 1) if accs[i] >= accs[i + 1])
    return {
        "curve": curve,
        "n_pairs_monotone_decreasing": n_monotone,
        "n_pairs_total": len(accs) - 1,
        "acc_at_0": accs[0],
        "acc_at_1": accs[-1],
        "drop_0_to_1": accs[0] - accs[-1],
        "examples_at_max_severity": examples_at_p100,
    }


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--ckpt", required=True, help="path to ckpt dir containing model/, projector.pt, head.pt")
    parser.add_argument("--config", required=True)
    parser.add_argument("--n", type=int, default=200)
    parser.add_argument("--K", type=int, default=None, help="latent count to use (defaults to config end-of-curriculum)")
    parser.add_argument("--max_new_tokens", type=int, default=256)
    parser.add_argument("--temperature", type=float, default=0.0)
    parser.add_argument("--out", default=None)
    parser.add_argument("--perturbation_curve", action="store_true",
                        help="Also run a perturbation-severity sweep (Viteri-style)")
    parser.add_argument("--severities", default="0.0,0.25,0.5,0.75,1.0",
                        help="Comma-separated severities for the perturbation curve")
    parser.add_argument("--no_block_y_to_x", action="store_true",
                        help="EVALUATE without the y→only-z bottleneck mask (lets y "
                             "attend to x directly during generation). Tests "
                             "bottleneck-as-regularizer: does z's learned structure "
                             "help when the inference constraint is lifted?")
    args = parser.parse_args()

    with open(args.config) as f:
        cfg = json.load(f)
    K = args.K if args.K is not None else cfg["K_curriculum"][-1][1]

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    ckpt = Path(args.ckpt)
    # Build base without LoRA wrap, then attach trained adapter from disk.
    bcfg_nolora = BLTConfig(
        base_model=cfg["base_model"], use_lora=False,
        K_latents=K, block_y_to_x=cfg["block_y_to_x"],
        proj_init_scale=cfg["proj_init_scale"],
        dtype=cfg["dtype"], attn_impl=cfg["attn_impl"],
    )
    base_model, tokenizer = build_base(bcfg_nolora)
    from peft import PeftModel
    adapter_dir = ckpt / "model"
    if (adapter_dir / "adapter_config.json").exists():
        model = PeftModel.from_pretrained(base_model, str(adapter_dir))
        print(f"[load] adapter from {adapter_dir}")
    else:
        model = base_model
        print(f"[load] no adapter at {adapter_dir} (using base only)")
    model.to(device).eval()

    inner_base = model.get_base_model() if hasattr(model, "get_base_model") else model
    d_model = inner_base.config.hidden_size
    projector = LatentProjector(
        d_model, init_scale=cfg["proj_init_scale"],
        use_mlp=cfg.get("proj_mlp", False),
        hidden_mult=cfg.get("proj_hidden_mult", 4),
    ).to(device).to(next(model.parameters()).dtype)
    projector.load_state_dict(torch.load(ckpt / "projector.pt", map_location=device))
    projector.eval()

    dataset_name = cfg.get("dataset", "gsm8k")
    if dataset_name.lower() == "math":
        val_ds = MATHDataset(split="test", max_examples=args.n)
    else:
        val_ds = GSM8KDataset(split="test", max_examples=args.n)
    val_loader = DataLoader(val_ds, batch_size=8, shuffle=False,
                            collate_fn=lambda b: collate_batch(
                                b, tokenizer,
                                max_prompt_len=cfg["max_prompt_len"],
                                max_answer_len=cfg["max_answer_len"],
                            ))

    z_std = estimate_z_std(model, projector, tokenizer, val_loader, device, K)
    print(f"[z_std estimate] {z_std:.4f}  dataset={dataset_name}")

    eval_block_y_to_x = not args.no_block_y_to_x
    print(f"[mode] eval_block_y_to_x={eval_block_y_to_x}")
    results = {}
    t0 = time.time()
    for cond in ["normal", "random", "zero"]:
        r = run_condition(model, projector, tokenizer, val_loader, device, K,
                          cond, z_std, args.max_new_tokens, args.temperature,
                          dataset=dataset_name, block_y_to_x=eval_block_y_to_x)
        results[cond] = r
        print(f"[{cond}] acc={r['acc']:.4f} ({r['correct']}/{r['n']}) elapsed={time.time()-t0:.0f}s")

    summary = {
        "ckpt": str(ckpt), "K": K, "n": args.n, "z_std": z_std,
        "eval_block_y_to_x": eval_block_y_to_x,
        "dataset": dataset_name,
        "results": results,
        "delta_normal_minus_random": results["normal"]["acc"] - results["random"]["acc"],
        "delta_normal_minus_zero": results["normal"]["acc"] - results["zero"]["acc"],
    }
    success_random = summary["delta_normal_minus_random"] >= 0.15
    success_zero = summary["delta_normal_minus_zero"] >= 0.25
    summary["H1_supported"] = bool(success_random and success_zero)

    if args.perturbation_curve:
        severities = [float(s) for s in args.severities.split(",")]
        print(f"[perturbation_curve] severities={severities}")
        curve = run_perturbation_curve(
            model, projector, tokenizer, val_loader, device, K, z_std,
            severities=severities, max_new_tokens=args.max_new_tokens,
            temperature=args.temperature, seed=0,
        )
        summary["perturbation_curve"] = curve
        print(f"[perturbation_curve] acc(p=0)={curve['acc_at_0']:.3f} -> "
              f"acc(p=1)={curve['acc_at_1']:.3f}  drop={curve['drop_0_to_1']:.3f}  "
              f"monotone={curve['n_pairs_monotone_decreasing']}/{curve['n_pairs_total']}")

    out = args.out or str(ckpt / "ablation.json")
    with open(out, "w") as f:
        json.dump(summary, f, indent=2)
    print(f"[written] {out}")
    print(f"H1 supported? {summary['H1_supported']} "
          f"(Δ_random={summary['delta_normal_minus_random']:.3f}, "
          f"Δ_zero={summary['delta_normal_minus_zero']:.3f})")


if __name__ == "__main__":
    main()