File size: 3,028 Bytes
aae3233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""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()