Maggio33 commited on
Commit
9d1ec74
·
verified ·
1 Parent(s): 39f1eac

Upload build_decontam_index.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. build_decontam_index.py +101 -0
build_decontam_index.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """RB3 decontam reference-index: 13-gram word-shingles z benchmarkow eval (Glint-parity).
3
+
4
+ Cel: zbudowac indeks n-gramow z DOKLADNIE tych zbiorow ktore ewaluujemy (WikiText-2 test,
5
+ BLiMP wszystkie configi good+bad, ARC-Easy+Challenge test), zeby filtr UFW mogl wyrzucic
6
+ zanieczyszczone dokumenty. Prawda-z-bajtow: zrodla identyczne z glint_parity_eval.py.
7
+
8
+ Reguly (RB3-spec):
9
+ - WikiText-2 + ARC (E+C): drop-UFW-doc na ANY-13gram-match (male zbiory, czystosc krytyczna).
10
+ - BLiMP: match-fraction > 0.5% (krotkie zdania, unikamy ciecia generycznych).
11
+ Indeks: blake2b-8B hash kazdego 13-gramu (word-level, znormalizowany). Zapis: decontam_index.json
12
+ {version, n13, hard_hashes:[...], blimp_hashes:[...], counts:{...}}.
13
+
14
+ Uzycie: python build_decontam_index.py [--out decontam_index.json]
15
+ """
16
+ import argparse
17
+ import hashlib
18
+ import json
19
+ import re
20
+ import sys
21
+ from pathlib import Path
22
+
23
+ # reuse dokladnych loaderow + configow z eval (te same zrodla = wlasciwa dekontaminacja)
24
+ sys.path.insert(0, str(Path(__file__).parent))
25
+ from glint_parity_eval import _rows, BLIMP_CONFIGS # noqa: E402
26
+
27
+ N = 13 # 13-gram word-level (konwencja GPT-3/FineWeb)
28
+
29
+
30
+ def normalize(text):
31
+ text = text.lower()
32
+ text = re.sub(r"[^\w\s]", " ", text) # strip punctuation
33
+ text = re.sub(r"\s+", " ", text).strip()
34
+ return text
35
+
36
+
37
+ def shingle_hashes(text, n=N):
38
+ words = normalize(text).split()
39
+ out = set()
40
+ for i in range(len(words) - n + 1):
41
+ sh = " ".join(words[i:i + n])
42
+ out.add(hashlib.blake2b(sh.encode("utf-8"), digest_size=8).hexdigest())
43
+ return out
44
+
45
+
46
+ def collect(texts, label, n=N):
47
+ idx = set()
48
+ docs = 0
49
+ for t in texts:
50
+ if not t or not t.strip():
51
+ continue
52
+ docs += 1
53
+ idx |= shingle_hashes(t, n)
54
+ print(f" [{label}] docs={docs:,} shingles({n}gram)={len(idx):,}", flush=True)
55
+ return idx
56
+
57
+
58
+ def main():
59
+ ap = argparse.ArgumentParser()
60
+ ap.add_argument("--out", default="decontam_index.json")
61
+ a = ap.parse_args()
62
+
63
+ print("=== RB3 decontam-index build (13-gram shingles) ===", flush=True)
64
+
65
+ # --- HARD sources (drop-on-any-match): WikiText-2 test + ARC-E/C test ---
66
+ hard_texts = []
67
+ print("WikiText-2 test...", flush=True)
68
+ hard_texts.extend(r["text"] for r in _rows("Salesforce/wikitext", "wikitext-2-raw-v1", "test"))
69
+ for cfg in ("ARC-Easy", "ARC-Challenge"):
70
+ print(f"ARC {cfg} test...", flush=True)
71
+ for ex in _rows("allenai/ai2_arc", cfg, "test"):
72
+ hard_texts.append(ex["question"])
73
+ hard_texts.extend(ex["choices"]["text"])
74
+ hard = collect(hard_texts, "hard-13gram", n=13)
75
+ synth_hard = collect(hard_texts, "hard-8gram-synth-paraphrase-gate", n=8)
76
+
77
+ # --- BLiMP (fraction-threshold source): all configs, good+bad ---
78
+ print(f"BLiMP ({len(BLIMP_CONFIGS)} configs) good+bad...", flush=True)
79
+ blimp_texts = []
80
+ for c in BLIMP_CONFIGS:
81
+ for e in _rows("nyu-mll/blimp", c, "train"):
82
+ blimp_texts.append(e["sentence_good"])
83
+ blimp_texts.append(e["sentence_bad"])
84
+ blimp = collect(blimp_texts, "blimp-all")
85
+
86
+ out = {
87
+ "version": 2, "n": N, "n_synth": 8,
88
+ "hard_hashes": sorted(hard), # WikiText-2 + ARC, 13-gram (drop-on-any, all sources)
89
+ "blimp_hashes": sorted(blimp), # BLiMP, 13-gram (fraction>0.5%)
90
+ "synth_hard_hashes": sorted(synth_hard), # WikiText-2 + ARC, 8-gram (drop-on-any, SYNTHETIC only)
91
+ "counts": {"hard": len(hard), "blimp": len(blimp), "synth_hard": len(synth_hard),
92
+ "hard_sources": "wikitext2-test + arc-easy/challenge-test",
93
+ "blimp_sources": f"blimp-{len(BLIMP_CONFIGS)}cfg-good+bad",
94
+ "synth_hard_note": "8-gram paraphrase-proxy for synthetic sources (Cosmopedia/Nemotron)"},
95
+ }
96
+ Path(a.out).write_text(json.dumps(out), encoding="utf-8")
97
+ print(f"=== WROTE {a.out}: hard={len(hard):,} blimp={len(blimp):,} 13-gram-hashes ===", flush=True)
98
+
99
+
100
+ if __name__ == "__main__":
101
+ main()