Buckets:
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = ["datasets==5.1.0", "huggingface-hub==2.2.0", "numpy==2.2.6", "rank-bm25==0.2.2"] | |
| # /// | |
| """Prepare the frozen public inputs for the encoder comparison.""" | |
| from __future__ import annotations | |
| import gzip | |
| import hashlib | |
| import json | |
| import logging | |
| import random | |
| import re | |
| from pathlib import Path | |
| import numpy as np | |
| from datasets import load_dataset | |
| from huggingface_hub import HfApi | |
| from rank_bm25 import BM25Okapi | |
| REPO = "tomaarsen/encoder-comparison-20261014" | |
| SOURCE = "sentence-transformers/msmarco-co-condenser-margin-mse-sym-mnrl-mean-v1" | |
| MODELS = [ | |
| "answerdotai/ModernBERT-base", | |
| "jhu-clsp/ettin-encoder-150m", | |
| "dmis-lab/UltraBERT-150M", | |
| "answerdotai/ModernBERT-large", | |
| "jhu-clsp/ettin-encoder-400m", | |
| "dmis-lab/UltraBERT-380M", | |
| ] | |
| def words(text): | |
| return re.findall(r"\w+", text.lower()) or [""] | |
| def norm(text): | |
| return " ".join(text.lower().split()) | |
| def ranked(index, query, count): | |
| scores = index.get_scores(words(query)) | |
| return np.argsort(-scores, kind="stable")[:count].tolist() | |
| def main(): | |
| logging.basicConfig(level=logging.INFO, force=True) | |
| for name in ("httpx", "httpx2", "httpcore", "huggingface_hub", "urllib3", "filelock", "fsspec"): | |
| logging.getLogger(name).setLevel(logging.WARNING) | |
| api = HfApi() | |
| manifest = {"seed": 12, "models": {m: api.model_info(m).sha for m in MODELS}, "datasets": {}} | |
| tests = {} | |
| excluded_queries = set() | |
| for name in ("NanoNQ", "NanoFiQA2018", "NanoSciFact"): | |
| source = f"zeta-alpha-ai/{name}" | |
| revision = api.dataset_info(source).sha | |
| manifest["datasets"][source] = revision | |
| subsets = { | |
| s: load_dataset(source, s, split="train", revision=revision) for s in ("corpus", "queries", "qrels") | |
| } | |
| corpus = {str(r["_id"]): r["text"] for r in subsets["corpus"]} | |
| queries = {str(r["_id"]): r["text"] for r in subsets["queries"]} | |
| qrels = {qid: {} for qid in queries} | |
| for row in subsets["qrels"]: | |
| qid, did = str(row["query-id"]), str(row["corpus-id"]) | |
| if qid in qrels and did in corpus and float(row.get("score", 1)) > 0: | |
| qrels[qid][did] = float(row.get("score", 1)) | |
| assert all(qrels.values()) | |
| ids = list(corpus) | |
| index = BM25Okapi([words(corpus[did]) for did in ids]) | |
| candidates = {qid: [ids[i] for i in ranked(index, text, 100)] for qid, text in queries.items()} | |
| tests[name] = {"corpus": corpus, "queries": queries, "qrels": qrels, "candidates": candidates} | |
| excluded_queries.update(norm(q) for q in queries.values()) | |
| print(f"Prepared {name}: {len(queries)} queries, {len(corpus)} documents", flush=True) | |
| revision = api.dataset_info(SOURCE).sha | |
| manifest["datasets"][SOURCE] = revision | |
| stream = load_dataset(SOURCE, "triplet-hard", split="train", revision=revision, streaming=True) | |
| stream = stream.shuffle(seed=12, buffer_size=10_000) | |
| rows, seen, inspected = [], set(), 0 | |
| for row in stream: | |
| inspected += 1 | |
| row = {k: row[k].strip() for k in ("query", "positive", "negative")} | |
| q = norm(row["query"]) | |
| if q in seen or q in excluded_queries or not all(row.values()): | |
| continue | |
| if norm(row["positive"]) == norm(row["negative"]): | |
| continue | |
| seen.add(q) | |
| rows.append(row) | |
| if len(rows) == 32_768 + 1_024: | |
| break | |
| assert len(rows) == 33_792 | |
| random.Random(12).shuffle(rows) | |
| dev, train = rows[:1024], rows[1024:] | |
| assert not {norm(r["query"]) for r in train} & {norm(r["query"]) for r in dev} | |
| dev_docs = list(dict.fromkeys(r[k] for r in dev for k in ("positive", "negative"))) | |
| index = BM25Okapi([words(t) for t in dev_docs]) | |
| validation = [] | |
| for row in dev: | |
| candidates = [row["positive"], row["negative"]] | |
| for i in ranked(index, row["query"], 40): | |
| if dev_docs[i] not in candidates: | |
| candidates.append(dev_docs[i]) | |
| if len(candidates) == 20: | |
| break | |
| random.Random(hashlib.sha256(row["query"].encode()).hexdigest()).shuffle(candidates) | |
| validation.append({"query": row["query"], "positive": row["positive"], "documents": candidates}) | |
| manifest.update( | |
| { | |
| "train_size": len(train), | |
| "validation_size": len(dev), | |
| "rows_inspected": inspected, | |
| "sampling": "seeded buffered shuffle, one unique normalized query per triplet", | |
| "validation_candidates": "positive, supplied hard negative, BM25 distractors from validation passages", | |
| "test_candidates": "BM25Okapi k1=1.5 b=0.75, lowercase word tokens, stable ties, no positive injection", | |
| } | |
| ) | |
| output = Path("prepared") | |
| output.mkdir(exist_ok=True) | |
| for name, value in (("train", train), ("validation", validation), ("tests", tests)): | |
| payload = json.dumps(value, ensure_ascii=False).encode() | |
| manifest[f"{name}_sha256"] = hashlib.sha256(payload).hexdigest() | |
| with gzip.open(output / f"{name}.json.gz", "wb") as handle: | |
| handle.write(payload) | |
| (output / "manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8") | |
| (output / "README.md").write_text( | |
| "# Encoder comparison, 14 October 2026\n\n" | |
| "Frozen public research inputs and results for ModernBERT, Ettin and UltraBERT.\n\n" | |
| "Training contains 32,768 query-disjoint MS MARCO triplets from " + SOURCE + " (triplet-hard). " | |
| "Validation contains 1,024 separate queries. Test data comes from zeta-alpha-ai/NanoNQ, " | |
| "zeta-alpha-ai/NanoFiQA2018 and zeta-alpha-ai/NanoSciFact. Original dataset terms apply. " | |
| "See manifest.json for source revisions, sampling, hashes and candidate construction.\n\n" | |
| "Validation rankings include the known positive. Test BM25 top-100 rankings do not inject positives. " | |
| "These small research runs do not reproduce the UltraBERT paper or establish production quality.\n", | |
| encoding="utf-8", | |
| ) | |
| api.create_repo(REPO, repo_type="dataset", private=False, exist_ok=True) | |
| commit = api.upload_folder( | |
| repo_id=REPO, | |
| repo_type="dataset", | |
| folder_path=output, | |
| commit_message="Freeze comparison data and model revisions", | |
| ) | |
| print(f"PREPARED_REPO={REPO} REVISION={commit.oid}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 6.48 kB
- Xet hash:
- f26706a96cb714c9d55b4ded53520cd3824f891d7435ea764e9820c78f65ba37
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.