#!/usr/bin/env python3 """Reconstruct a paper scaling corpus as a prefix of the archived 10M corpus. The canonical collection is ordered as 100,195 BrowseComp-Plus documents followed by the deterministic FineWeb stream. The paper's lower-scale corpora are nested prefixes. The exact observed document counts are: 100k -> 100,195 200k -> 200,197 400k -> 400,195 800k -> 800,195 10m -> 10,000,195 Output uses Pyserini JsonCollection-compatible JSONL shards. """ from __future__ import annotations import argparse import contextlib import hashlib import json import subprocess from pathlib import Path from typing import BinaryIO, Iterator SIZES = { "100k": 100_195, "200k": 200_197, "400k": 400_195, "800k": 800_195, "10m": 10_000_195, } @contextlib.contextmanager def open_source(path: Path) -> Iterator[BinaryIO]: if path.suffix != ".zst": with path.open("rb") as handle: yield handle return process = subprocess.Popen( ["zstd", "-dc", "--long=31", str(path)], stdout=subprocess.PIPE, ) if process.stdout is None: process.kill() raise RuntimeError(f"Could not open zstd output stream for {path}") try: yield process.stdout finally: process.stdout.close() return_code = process.wait() # A requested prefix can end inside a compressed shard. Closing the # pipe then gives zstd SIGPIPE, which is expected and not corruption. if return_code not in (0, -13, 141): raise RuntimeError(f"zstd failed with status {return_code}: {path}") def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--source", type=Path, required=True) parser.add_argument("--size", choices=SIZES, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--docs-per-shard", type=int, default=100_000) args = parser.parse_args() target = SIZES[args.size] compressed_files = sorted(args.source.glob("docs_*.jsonl.zst")) source_files = compressed_files or sorted(args.source.glob("docs_*.jsonl")) if not source_files: raise SystemExit( f"No docs_*.jsonl.zst or docs_*.jsonl files found under {args.source}" ) if args.docs_per_shard <= 0: raise SystemExit("--docs-per-shard must be positive") args.output.mkdir(parents=True, exist_ok=True) copied = 0 shard_index = 0 in_shard = 0 output_handle = None digest = hashlib.sha256() try: for source_file in source_files: with open_source(source_file) as source_handle: for line in source_handle: if copied >= target: break if output_handle is None or in_shard >= args.docs_per_shard: if output_handle is not None: output_handle.close() output_path = args.output / f"docs_{shard_index:05d}.jsonl" output_handle = output_path.open("wb") shard_index += 1 in_shard = 0 output_handle.write(line) digest.update(line) copied += 1 in_shard += 1 if copied >= target: break finally: if output_handle is not None: output_handle.close() if copied != target: raise SystemExit(f"Source exhausted at {copied:,}; expected {target:,}") manifest = { "variant": args.size, "doc_count": copied, "docs_per_shard": args.docs_per_shard, "shard_count": shard_index, "source": str(args.source), "construction": "ordered prefix of canonical 10M collection", "content_sha256": digest.hexdigest(), } (args.output / "manifest.json").write_text( json.dumps(manifest, indent=2) + "\n", encoding="utf-8" ) print(json.dumps(manifest, indent=2)) if __name__ == "__main__": main()