btx-e001 T-2 control: vanilla t5-small = wordpiece parity (0.47/0.61) through the same pushforward; flan sentinel infill EOS-degenerate — channel exonerated, flan indicted
Browse files- t2_control_vanilla_t5.py +140 -0
t2_control_vanilla_t5.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""C3 causal-attribution control: pretrained t5-small (span-corruption
|
| 2 |
+
objective, no flan tuning) vs flan-t5-small on the same T-2 sites.
|
| 3 |
+
Same seed, same pools, first 150 of each domain's 600 sampled sites.
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
from collections import Counter, defaultdict
|
| 7 |
+
|
| 8 |
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
| 9 |
+
import numpy as np
|
| 10 |
+
import torch
|
| 11 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 12 |
+
import transformers.utils.logging as hlog
|
| 13 |
+
|
| 14 |
+
hlog.set_verbosity_error()
|
| 15 |
+
CODEX = r"E:\mirel\data\bytelex\codex_v1.txt"
|
| 16 |
+
N_PER_DOMAIN = 600
|
| 17 |
+
N_SUB = 150
|
| 18 |
+
rng = np.random.default_rng(20260820)
|
| 19 |
+
|
| 20 |
+
tkA = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 21 |
+
tkB = AutoTokenizer.from_pretrained("google/bert_uncased_L-4_H-256_A-4")
|
| 22 |
+
tkC = AutoTokenizer.from_pretrained("google-t5/t5-small")
|
| 23 |
+
assert tkC.get_vocab() == tkA.get_vocab(), "t5-small vocab != flan vocab"
|
| 24 |
+
mA = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")
|
| 25 |
+
mC = T5ForConditionalGeneration.from_pretrained("google-t5/t5-small")
|
| 26 |
+
mA.eval()
|
| 27 |
+
mC.eval()
|
| 28 |
+
|
| 29 |
+
raw = open(CODEX, "rb").read().decode("ascii")
|
| 30 |
+
lines, story, arith, n = raw.split("\n"), [], [], 0
|
| 31 |
+
for ln in lines:
|
| 32 |
+
if not ln:
|
| 33 |
+
n += 1
|
| 34 |
+
continue
|
| 35 |
+
(story if n < 200_000 else arith).append(ln)
|
| 36 |
+
n += len(ln) + 1
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def enc(tk, line):
|
| 40 |
+
e = tk(line, add_special_tokens=False, return_offsets_mapping=True)
|
| 41 |
+
return [(i, s, t) for i, (s, t)
|
| 42 |
+
in zip(e["input_ids"], e["offset_mapping"]) if t > s]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def sites(lines_):
|
| 46 |
+
out = []
|
| 47 |
+
for ln in lines_:
|
| 48 |
+
A, B = enc(tkA, ln), enc(tkB, ln)
|
| 49 |
+
bA = {s for _, s, _ in A} | {t for _, _, t in A}
|
| 50 |
+
bB = {s for _, s, _ in B} | {t for _, _, t in B}
|
| 51 |
+
shared = sorted(bA & bB)
|
| 52 |
+
for lo, hi in zip(shared, shared[1:]):
|
| 53 |
+
ca = [i for i, s, t in A if s >= lo and t <= hi]
|
| 54 |
+
cb = [i for i, s, t in B if s >= lo and t <= hi]
|
| 55 |
+
span = ln[lo:hi].lstrip(" ")
|
| 56 |
+
if ca and cb and span:
|
| 57 |
+
shape = "1x1" if (len(ca) == 1 and len(cb) == 1) \
|
| 58 |
+
else "nm"
|
| 59 |
+
out.append((ln, lo, hi, shape))
|
| 60 |
+
return out
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
sys.path.insert(0, r"E:\mirel\geolip-bytelex")
|
| 64 |
+
from geolip.bytelex.extract import token_byte_table
|
| 65 |
+
|
| 66 |
+
rows = token_byte_table(tkA)
|
| 67 |
+
cbA = np.full(len(tkA), -1, dtype=np.int64)
|
| 68 |
+
specA = np.zeros(len(tkA), dtype=bool)
|
| 69 |
+
for r in rows:
|
| 70 |
+
if r["is_special"]:
|
| 71 |
+
specA[r["id"]] = True
|
| 72 |
+
if r["is_special"] or not r["hex"]:
|
| 73 |
+
continue
|
| 74 |
+
e = bytes.fromhex(r["hex"]).lstrip(b" ")
|
| 75 |
+
if e:
|
| 76 |
+
cbA[r["id"]] = e[0]
|
| 77 |
+
|
| 78 |
+
EXTRA0 = tkA.convert_tokens_to_ids("<extra_id_0>")
|
| 79 |
+
EX1 = tkA.convert_tokens_to_ids("<extra_id_1>")
|
| 80 |
+
EOSA = tkA.eos_token_id
|
| 81 |
+
PAD = tkA.pad_token_id
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def push(probs, cb):
|
| 85 |
+
overflow = float(probs[len(cb):].sum())
|
| 86 |
+
probs = probs[:len(cb)]
|
| 87 |
+
keep = cb >= 0
|
| 88 |
+
dropped = overflow + float(probs[~keep].sum())
|
| 89 |
+
marg = np.zeros(256)
|
| 90 |
+
np.add.at(marg, cb[keep], probs[keep])
|
| 91 |
+
s = marg.sum()
|
| 92 |
+
return (marg / s if s > 0 else marg), dropped
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@torch.no_grad()
|
| 96 |
+
def probe(model, ln, lo, hi):
|
| 97 |
+
src = ln[:lo] + "<extra_id_0>" + ln[hi:]
|
| 98 |
+
ids = tkA(src, return_tensors="pt")
|
| 99 |
+
dec = torch.tensor([[PAD, EXTRA0]])
|
| 100 |
+
lg = model(input_ids=ids["input_ids"],
|
| 101 |
+
attention_mask=ids["attention_mask"],
|
| 102 |
+
decoder_input_ids=dec).logits[0, -1]
|
| 103 |
+
p = torch.softmax(lg.float(), -1).numpy()
|
| 104 |
+
(marg, drop) = push(p, cbA)
|
| 105 |
+
return marg, drop, p
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
res = defaultdict(lambda: defaultdict(float))
|
| 109 |
+
cnt = Counter()
|
| 110 |
+
top1 = defaultdict(Counter)
|
| 111 |
+
for dom, pool in (("story", sites(story)), ("arith", sites(arith))):
|
| 112 |
+
idx = rng.choice(len(pool), size=min(N_PER_DOMAIN, len(pool)),
|
| 113 |
+
replace=False)
|
| 114 |
+
for k in idx[:N_SUB]:
|
| 115 |
+
ln, lo, hi, shape = pool[int(k)]
|
| 116 |
+
gold = ord(ln[lo:hi].lstrip(" ")[0])
|
| 117 |
+
key = f"{dom}:{shape}"
|
| 118 |
+
cnt[key] += 1
|
| 119 |
+
for tag, model in (("flan", mA), ("t5", mC)):
|
| 120 |
+
marg, drop, p = probe(model, ln, lo, hi)
|
| 121 |
+
res[key][f"{tag}_gold"] += int(marg.argmax() == gold)
|
| 122 |
+
res[key][f"{tag}_drop"] += drop
|
| 123 |
+
res[key][f"{tag}_eos"] += float(p[EOSA])
|
| 124 |
+
res[key][f"{tag}_ex1"] += float(p[EX1])
|
| 125 |
+
top1[f"{key}:{tag}"][
|
| 126 |
+
tkA.convert_ids_to_tokens(int(p.argmax()))
|
| 127 |
+
if int(p.argmax()) < len(tkA)
|
| 128 |
+
else f"PHANTOM_{int(p.argmax())}"] += 1
|
| 129 |
+
|
| 130 |
+
for key in sorted(cnt):
|
| 131 |
+
n_ = cnt[key]
|
| 132 |
+
r = res[key]
|
| 133 |
+
print(f"[CTRL {key}] n={n_}")
|
| 134 |
+
for tag in ("flan", "t5"):
|
| 135 |
+
print(f" {tag:4s}: gold={r[f'{tag}_gold']/n_:.4f} "
|
| 136 |
+
f"drop={r[f'{tag}_drop']/n_:.4f} "
|
| 137 |
+
f"eos={r[f'{tag}_eos']/n_:.4f} "
|
| 138 |
+
f"ex1={r[f'{tag}_ex1']/n_:.4f}")
|
| 139 |
+
print(f" top1: {top1[f'{key}:{tag}'].most_common(8)}")
|
| 140 |
+
print("[CTRL] DONE", flush=True)
|