Datasets:
Download scripts/verify_integrity.py from leo20240112/MBMamba-Eval: direct link, hf CLI and curl.
- Browser
- Download file 7.86 kB
-
https://huggingface.co/datasets/leo20240112/MBMamba-Eval/resolve/main/scripts/verify_integrity.py
- Command line
-
hf download hf://datasets/leo20240112/MBMamba-Eval/scripts/verify_integrity.py
-
curl -L -o verify_integrity.py https://huggingface.co/datasets/leo20240112/MBMamba-Eval/resolve/main/scripts/verify_integrity.py
7.86 kB
| #!/usr/bin/env python3 | |
| """Deep integrity verification for a downloaded MBMamba-Eval asset tree. | |
| Checks, for every file declared in the published manifests: | |
| * file exists and is readable | |
| * byte size matches | |
| * sha256 matches | |
| * row count matches (jsonl source banks / pickle prepared banks) | |
| * prepared shards carry the declared tokenizer and per-row token ids | |
| Usage | |
| ----- | |
| python verify_integrity.py --root /shared/mbmamba/eval_assets | |
| python verify_integrity.py --root ... --deep-tokens | |
| Exit code 0 only when every checked file passes. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import pickle | |
| import sys | |
| from pathlib import Path | |
| ROLES_DEFAULT = ("source", "prepared", "config") | |
| def sha256_file(path: Path, chunk: int = 1 << 22) -> str: | |
| h = hashlib.sha256() | |
| with path.open("rb") as fh: | |
| while True: | |
| b = fh.read(chunk) | |
| if not b: | |
| break | |
| h.update(b) | |
| return h.hexdigest() | |
| def count_jsonl(path: Path) -> int: | |
| n = 0 | |
| with path.open("rb") as fh: | |
| for line in fh: | |
| if line.strip(): | |
| n += 1 | |
| return n | |
| def check_file(root: Path, rel: str, meta: dict, deep_tokens: bool, | |
| expected_tokens: dict | None = None) -> tuple[list[str], list[str]]: | |
| """Return (problems, notes). Notes are informational, never failures.""" | |
| problems: list[str] = [] | |
| notes: list[str] = [] | |
| p = root / rel | |
| if not p.exists(): | |
| return [f"MISSING {rel}"], notes | |
| if not p.is_file(): | |
| return [f"NOT_A_FILE {rel}"], notes | |
| size = p.stat().st_size | |
| if int(meta.get("size", -1)) != size: | |
| problems.append(f"SIZE {rel}: {size} != {meta.get('size')}") | |
| return problems, notes | |
| if meta.get("sha256"): | |
| got = sha256_file(p) | |
| if got != meta["sha256"]: | |
| problems.append(f"SHA256 {rel}: {got[:12]} != {meta['sha256'][:12]}") | |
| return problems, notes | |
| role = meta.get("role") | |
| rows = meta.get("rows") | |
| if role == "source" and rows is not None: | |
| n = count_jsonl(p) | |
| if n != int(rows): | |
| problems.append(f"ROWS {rel}: {n} != {rows}") | |
| if role == "prepared" and rows is not None: | |
| with p.open("rb") as fh: | |
| items = pickle.load(fh) | |
| if len(items) != int(rows): | |
| problems.append(f"ROWS {rel}: {len(items)} != {rows}") | |
| elif deep_tokens: | |
| lengths = [len(r["input_ids"]) for r in items] | |
| if any(n == 0 for n in lengths): | |
| problems.append(f"EMPTY_INPUT_IDS {rel}") | |
| stats = {"min": min(lengths), "max": max(lengths), | |
| "mean": round(sum(lengths) / len(lengths), 2)} | |
| if expected_tokens: | |
| for key in ("min", "max"): | |
| if key in expected_tokens and int(expected_tokens[key]) != stats[key]: | |
| problems.append( | |
| f"TOKENS {rel}: {key} {stats[key]} != manifest " | |
| f"{expected_tokens[key]}") | |
| if "mean" in expected_tokens and \ | |
| abs(float(expected_tokens["mean"]) - stats["mean"]) > 0.01: | |
| problems.append( | |
| f"TOKENS {rel}: mean {stats['mean']} != manifest " | |
| f"{expected_tokens['mean']}") | |
| target = int(meta.get("length") or 0) | |
| if target and stats["max"] > target: | |
| # Re-tokenization can exceed the nominal cell length; this is | |
| # recorded in the manifest, not an integrity failure. | |
| notes.append( | |
| f"OVER_TARGET {rel}: max {stats['max']} > nominal {target} " | |
| f"(consumer must truncate)") | |
| # `example_id` is RULER's generator `index`, which is drawn from a | |
| # random range and is NOT unique inside a cell. Uniqueness must be | |
| # asserted on the positional `source_index` instead. | |
| sindex = [r.get("source_index") for r in items] | |
| if sorted(sindex) != list(range(len(items))): | |
| problems.append( | |
| f"SOURCE_INDEX {rel}: not a permutation of 0..{len(items) - 1}") | |
| bad_ids = sum(1 for r in items | |
| if str(r.get("example_id")) != str((r.get("source_row") or {}).get("index"))) | |
| if bad_ids: | |
| problems.append( | |
| f"EXAMPLE_ID {rel}: {bad_ids} rows where example_id != source_row.index") | |
| dup_ids = len(items) - len({r.get("example_id") for r in items}) | |
| if dup_ids: | |
| notes.append( | |
| f"DUPLICATE_EXAMPLE_ID {rel}: {dup_ids} rows share a generator " | |
| f"index (upstream behaviour; join on source_index)") | |
| refs_missing = sum(1 for r in items if not r.get("references")) | |
| if refs_missing: | |
| problems.append(f"NO_REFERENCE {rel}: {refs_missing} rows without references") | |
| return problems, notes | |
| def main() -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--root", required=True) | |
| ap.add_argument("--roles", default=",".join(ROLES_DEFAULT)) | |
| ap.add_argument("--deep-tokens", action="store_true", | |
| help="Also validate prepared token ids / ids / references.") | |
| ap.add_argument("--json", action="store_true", help="Emit machine-readable report.") | |
| args = ap.parse_args() | |
| root = Path(args.root).expanduser().resolve() | |
| roles = {x.strip() for x in args.roles.split(",") if x.strip()} | |
| report: dict = {"root": str(root), "checked": 0, "passed": 0, | |
| "problems": [], "notes": [], "by_benchmark": {}} | |
| for bench, mpath in (("RULER", "ruler/manifest.json"), | |
| ("HELMET", "helmet/manifest.json")): | |
| p = root / mpath | |
| if not p.exists(): | |
| report["problems"].append(f"MISSING_MANIFEST {mpath}") | |
| continue | |
| manifest = json.loads(p.read_text(encoding="utf-8")) | |
| token_lengths = manifest.get("token_lengths") or {} | |
| stats = {"files": 0, "passed": 0} | |
| for rel, meta in sorted((manifest.get("files") or {}).items()): | |
| if meta.get("role") not in roles: | |
| continue | |
| report["checked"] += 1 | |
| stats["files"] += 1 | |
| expected = None | |
| if meta.get("role") == "prepared": | |
| expected = token_lengths.get(f"{meta.get('length')}/{meta.get('task')}") | |
| problems, notes = check_file(root, rel, meta, args.deep_tokens, expected) | |
| report["notes"].extend(notes) | |
| if problems: | |
| report["problems"].extend(problems) | |
| else: | |
| report["passed"] += 1 | |
| stats["passed"] += 1 | |
| report["by_benchmark"][bench] = stats | |
| if args.json: | |
| print(json.dumps(report, indent=2)) | |
| else: | |
| print(f"root: {root}") | |
| for bench, s in report["by_benchmark"].items(): | |
| print(f" {bench:7s} {s['passed']}/{s['files']} files passed") | |
| if report["problems"]: | |
| print() | |
| for p in report["problems"][:60]: | |
| print(" FAIL", p) | |
| if len(report["problems"]) > 60: | |
| print(f" ... {len(report['problems']) - 60} more") | |
| print(f"\nRESULT: FAIL ({len(report['problems'])} problems)") | |
| return 1 | |
| if report["notes"]: | |
| print() | |
| for n in report["notes"][:60]: | |
| print(" NOTE", n) | |
| if len(report["notes"]) > 60: | |
| print(f" ... {len(report['notes']) - 60} more") | |
| print(f"\nRESULT: OK ({report['passed']}/{report['checked']} files verified)") | |
| return 0 if not report["problems"] else 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |