Datasets:
Tasks:
Text Generation
Modalities:
Text
Formats:
parquet
Languages:
Polish
Size:
10K - 100K
License:
Download processing/pack_cnc_slayer.py from PiotrSty/polish-technical-mega: direct link, hf CLI and curl.
- Browser
- Download file 4.36 kB
-
https://huggingface.co/datasets/PiotrSty/polish-technical-mega/resolve/main/processing/pack_cnc_slayer.py
- Command line
-
hf download hf://datasets/PiotrSty/polish-technical-mega/processing/pack_cnc_slayer.py
-
curl -L -o pack_cnc_slayer.py https://huggingface.co/datasets/PiotrSty/polish-technical-mega/resolve/main/processing/pack_cnc_slayer.py
4.36 kB
| """Pack DynaWord-compatible CNC Parquet into Slayer uint16/doc-index files.""" | |
| from __future__ import annotations | |
| import argparse | |
| from array import array | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import shutil | |
| import struct | |
| import sys | |
| import pyarrow.parquet as pq | |
| from tokenizers import Tokenizer | |
| ROOT = Path(__file__).resolve().parents[1] | |
| INPUT_DIRS = {"train": "data/train.parquet", "valid": "data/validation.parquet", "test": "data/test.parquet"} | |
| OUTPUT_DIRS = {"train": "train", "valid": "valid", "test": "test"} | |
| def sha256(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(1 << 20), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def pack_split(input_root: Path, output_root: Path, split: str, tokenizer: Tokenizer) -> dict: | |
| parquet_file = input_root / INPUT_DIRS[split] | |
| if not parquet_file.is_file(): | |
| raise FileNotFoundError(f"missing Parquet file for {split}: {parquet_file}") | |
| parquet_files = [parquet_file] | |
| output_root.mkdir(parents=True, exist_ok=True) | |
| bin_path = output_root / "train.bin" | |
| if split != "train": | |
| bin_path = output_root / f"{split}.bin" | |
| idx_path = output_root / "docs.idx" | |
| documents = 0 | |
| tokens = 0 | |
| with bin_path.open("wb") as bin_file, idx_path.open("wb") as idx_file: | |
| offset = 0 | |
| for parquet_path in parquet_files: | |
| table = pq.read_table(parquet_path, columns=["id", "text"]) | |
| for record_id, text in zip(table["id"].to_pylist(), table["text"].to_pylist()): | |
| ids = tokenizer.encode(text).ids | |
| if any(token_id > 65535 for token_id in ids): | |
| raise ValueError(f"token id exceeds uint16 for {record_id}") | |
| values = array("H", ids) | |
| values.append(0) | |
| if sys.byteorder != "little": | |
| values.byteswap() | |
| if values.itemsize != 2: | |
| raise RuntimeError("uint16 packing requires two-byte unsigned array items") | |
| bin_file.write(values.tobytes()) | |
| offset += len(values) | |
| idx_file.write(struct.pack("<Q", offset)) | |
| documents += 1 | |
| tokens += len(values) | |
| return {"documents": documents, "tokens_including_eos": tokens, "bin_sha256": sha256(bin_path), "idx_sha256": sha256(idx_path)} | |
| def pack(input_root: Path, output_root: Path, tokenizer_path: Path, dataset_name: str) -> dict: | |
| tokenizer = Tokenizer.from_file(str(tokenizer_path)) | |
| if tokenizer.token_to_id("<|endoftext|>") != 0: | |
| raise ValueError("expected Slayer EOS token <|endoftext|> to have id 0") | |
| output_root.mkdir(parents=True, exist_ok=True) | |
| shutil.copy2(tokenizer_path, output_root / "tokenizer.json") | |
| stats = {} | |
| for split in INPUT_DIRS: | |
| stats[split] = pack_split(input_root, output_root / OUTPUT_DIRS[split], split, tokenizer) | |
| manifest = { | |
| "dataset": dataset_name, | |
| "version": json.loads((input_root / "manifest.json").read_text(encoding="utf-8")).get("version", "0.1.0"), | |
| "format": "little-endian uint16 token ids; one EOS token 0 after every document", | |
| "index": "little-endian uint64 document end offsets in tokens including EOS", | |
| "tokenizer": "Slayer1B-1.06 tokenizer.json", | |
| "tokenizer_sha256": sha256(tokenizer_path), | |
| "vocab_size": tokenizer.get_vocab_size(), | |
| "eos_id": 0, | |
| "source": "../data/{train,validation,test}.parquet", | |
| "splits": stats, | |
| } | |
| (output_root / "manifest.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") | |
| return manifest | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--dataset-name", default="CNC/CAM Slayer pretraining pack") | |
| parser.add_argument("--input", type=Path, default=ROOT / "data" / "cnc_dynaword") | |
| parser.add_argument("--output", type=Path, default=ROOT / "data" / "cnc_dynaword" / "slayer_v6") | |
| parser.add_argument("--tokenizer", type=Path, default=ROOT / "models" / "Slayer1B-1.06" / "tokenizer.json") | |
| args = parser.parse_args() | |
| print(json.dumps(pack(args.input, args.output, args.tokenizer, args.dataset_name), ensure_ascii=False, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |