Cion-lab commited on
Commit
1b5ea42
·
verified ·
1 Parent(s): a1ae65e

Upload build/audit_contamination.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. build/audit_contamination.py +37 -4
build/audit_contamination.py CHANGED
@@ -248,15 +248,48 @@ def main():
248
  "build_mix --exclude-dir)")
249
  ap.add_argument("--out", default="", help="write the report JSON here (publish_mix expects "
250
  "mixroot/audit.json)")
 
 
 
 
 
251
  a = ap.parse_args()
252
 
253
  from tokenizers import Tokenizer
254
  from huggingface_hub import hf_hub_download
255
- tk = Tokenizer.from_file(hf_hub_download("HuggingFaceTB/SmolLM2-135M", "tokenizer.json"))
 
256
  tk.no_truncation()
257
-
258
- print("building reference gram table (only counts ever leave this process)", flush=True)
259
- ref, refmask, refcounts, errors = build_reference(tk, a.max_items_per_split)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  tasks_covered = sorted({k.split("::")[0] for k, v in refcounts.items()
261
  if v >= MIN_ITEMS_PER_SPLIT})
262
  print(f"reference: {ref.size:,} grams, {len(tasks_covered)}/8 tasks covered, "
 
248
  "build_mix --exclude-dir)")
249
  ap.add_argument("--out", default="", help="write the report JSON here (publish_mix expects "
250
  "mixroot/audit.json)")
251
+ ap.add_argument("--ref-cache", default="",
252
+ help="directory to keep the built reference gram table in. Streaming the eight "
253
+ "reference sets is the slow half of this script, and a re-audit after a filter "
254
+ "pass rebuilds exactly the same table -- so it is reused only if the task list, "
255
+ "item cap, k and tokenizer sha all still match.")
256
  a = ap.parse_args()
257
 
258
  from tokenizers import Tokenizer
259
  from huggingface_hub import hf_hub_download
260
+ tjp = hf_hub_download("HuggingFaceTB/SmolLM2-135M", "tokenizer.json")
261
+ tk = Tokenizer.from_file(tjp)
262
  tk.no_truncation()
263
+ import hashlib
264
+ tk_sha = hashlib.sha256(open(tjp, "rb").read()).hexdigest()
265
+
266
+ want_meta = {"k": K, "max_items": a.max_items_per_split, "tokenizer_sha256": tk_sha,
267
+ "refs": [[t[0], t[1], list(t[2]), list(t[3])] for t in REFS]}
268
+ ref = refmask = None
269
+ refcounts = errors = {}
270
+ if a.ref_cache and os.path.exists(os.path.join(a.ref_cache, "refmeta.json")):
271
+ try:
272
+ if json.load(open(os.path.join(a.ref_cache, "refmeta.json"))) == want_meta:
273
+ ref = np.load(os.path.join(a.ref_cache, "ref.npy"))
274
+ refmask = np.load(os.path.join(a.ref_cache, "refmask.npy"))
275
+ refcounts = json.load(open(os.path.join(a.ref_cache, "refcounts.json")))
276
+ errors = json.load(open(os.path.join(a.ref_cache, "errors.json")))
277
+ print(f"reference: loaded from cache, {ref.size:,} grams", flush=True)
278
+ except Exception as e:
279
+ print(f"reference cache unusable ({type(e).__name__}: {str(e)[:120]}); rebuilding",
280
+ flush=True)
281
+ ref = None
282
+ if ref is None:
283
+ print("building reference gram table (only counts ever leave this process)", flush=True)
284
+ ref, refmask, refcounts, errors = build_reference(tk, a.max_items_per_split)
285
+ if a.ref_cache:
286
+ os.makedirs(a.ref_cache, exist_ok=True)
287
+ np.save(os.path.join(a.ref_cache, "ref.npy"), ref)
288
+ np.save(os.path.join(a.ref_cache, "refmask.npy"), refmask)
289
+ json.dump(want_meta, open(os.path.join(a.ref_cache, "refmeta.json"), "w"))
290
+ json.dump(refcounts, open(os.path.join(a.ref_cache, "refcounts.json"), "w"))
291
+ json.dump(errors, open(os.path.join(a.ref_cache, "errors.json"), "w"))
292
+ print(f"reference cached under {a.ref_cache}", flush=True)
293
  tasks_covered = sorted({k.split("::")[0] for k, v in refcounts.items()
294
  if v >= MIN_ITEMS_PER_SPLIT})
295
  print(f"reference: {ref.size:,} grams, {len(tasks_covered)}/8 tasks covered, "