Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
parquet
Languages:
Polish
Size:
10K - 100K
License:
Download processing/validate_cnc_dynaword.py from PiotrSty/polish-technical-mega: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/datasets/PiotrSty/polish-technical-mega/resolve/main/processing/validate_cnc_dynaword.py
- Command line
-
hf download hf://datasets/PiotrSty/polish-technical-mega/processing/validate_cnc_dynaword.py
-
curl -L -o validate_cnc_dynaword.py https://huggingface.co/datasets/PiotrSty/polish-technical-mega/resolve/main/processing/validate_cnc_dynaword.py
3.03 kB
| """Validate DynaWord Parquet schema and Slayer binary pack invariants.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import struct | |
| import pyarrow.parquet as pq | |
| import tiktoken | |
| ROOT = Path(__file__).resolve().parents[1] | |
| SCHEMA = ["id", "text", "source", "added", "created", "token_count", "license", "author"] | |
| SPLITS = {"train": "data/train.parquet", "valid": "data/validation.parquet", "test": "data/test.parquet"} | |
| PACK_DIRS = {"train": "train", "valid": "valid", "test": "test"} | |
| def validate(root: Path) -> dict: | |
| cl100k = tiktoken.get_encoding("cl100k_base") | |
| manifest = json.loads((root / "manifest.json").read_text(encoding="utf-8")) | |
| result = {} | |
| for split, relative in SPLITS.items(): | |
| parquet_files = [root / relative] | |
| if not parquet_files[0].is_file(): | |
| raise ValueError(f"missing Parquet files for {split}") | |
| rows = 0 | |
| tokens = 0 | |
| ids = set() | |
| for parquet_path in parquet_files: | |
| table = pq.read_table(parquet_path) | |
| if table.column_names != SCHEMA: | |
| raise ValueError(f"schema mismatch in {parquet_path}: {table.column_names}") | |
| for row in table.to_pylist(): | |
| if row["id"] in ids: | |
| raise ValueError(f"duplicate id in {split}: {row['id']}") | |
| ids.add(row["id"]) | |
| measured = len(cl100k.encode_ordinary(row["text"])) | |
| if row["token_count"] != measured: | |
| raise ValueError(f"cl100k token mismatch for {row['id']}: {row['token_count']} != {measured}") | |
| rows += 1 | |
| tokens += row["token_count"] | |
| expected = manifest["splits"][split] | |
| if rows != expected["documents"] or tokens != expected["tokens"]: | |
| raise ValueError(f"Parquet manifest mismatch for {split}: {rows}/{tokens} vs {expected}") | |
| pack_dir = root / "slayer_v6" / PACK_DIRS[split] | |
| bin_name = "train.bin" if split == "train" else f"{split}.bin" | |
| bin_path = pack_dir / bin_name | |
| idx_path = pack_dir / "docs.idx" | |
| raw_size = bin_path.stat().st_size | |
| if raw_size % 2: | |
| raise ValueError(f"binary token file is not uint16-sized: {bin_path}") | |
| idx_raw = idx_path.read_bytes() | |
| if len(idx_raw) != rows * 8: | |
| raise ValueError(f"index count mismatch for {split}") | |
| final_offset = struct.unpack_from("<Q", idx_raw, len(idx_raw) - 8)[0] | |
| if final_offset * 2 != raw_size: | |
| raise ValueError(f"final document offset mismatch for {split}") | |
| result[split] = {"documents": rows, "parquet_tokens": tokens, "packed_tokens_including_eos": final_offset} | |
| return result | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--root", type=Path, default=ROOT / "data" / "cnc_dynaword") | |
| args = parser.parse_args() | |
| print(json.dumps(validate(args.root), ensure_ascii=False, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |