Download scripts/tokenize_shard.py from Infinity08/KAWK50M-Project: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/scripts/tokenize_shard.py
- Command line
-
hf download hf://datasets/Infinity08/KAWK50M-Project/scripts/tokenize_shard.py
-
curl -L -o tokenize_shard.py https://huggingface.co/datasets/Infinity08/KAWK50M-Project/resolve/main/scripts/tokenize_shard.py
2.11 kB
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from transformers import AutoTokenizer | |
| from kawk50m.cli import configure_utf8_stdio | |
| from scripts.tokenize_corpus import tokenize_split | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description="Tokenize one clean JSONL shard into a uint16 token part." | |
| ) | |
| parser.add_argument("--input", required=True) | |
| parser.add_argument("--tokenizer-dir", required=True) | |
| parser.add_argument("--output", required=True) | |
| parser.add_argument("--batch-size", type=int, default=64) | |
| return parser.parse_args() | |
| def main() -> None: | |
| configure_utf8_stdio() | |
| args = parse_args() | |
| input_path = Path(args.input) | |
| output_path = Path(args.output) | |
| metadata_path = output_path.with_suffix(".json") | |
| if not input_path.is_file(): | |
| raise FileNotFoundError(input_path) | |
| if output_path.exists() or metadata_path.exists(): | |
| raise FileExistsError(f"Refusing to overwrite token part: {output_path}") | |
| if args.batch_size <= 0: | |
| raise ValueError("--batch-size must be positive") | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| args.tokenizer_dir, use_fast=False, legacy=True | |
| ) | |
| tokenizer.model_max_length = int(1e30) | |
| if len(tokenizer) > np.iinfo(np.uint16).max: | |
| raise ValueError("uint16 output requires a vocabulary no larger than 65,535") | |
| if tokenizer.eos_token_id is None: | |
| raise ValueError("Tokenizer must define eos_token_id") | |
| summary = tokenize_split( | |
| split=input_path.stem, | |
| paths=[input_path], | |
| tokenizer=tokenizer, | |
| output_path=output_path, | |
| batch_size=args.batch_size, | |
| max_tokens=None, | |
| ) | |
| summary["input"] = str(input_path) | |
| with metadata_path.open("w", encoding="utf-8") as handle: | |
| json.dump(summary, handle, ensure_ascii=False, indent=2) | |
| handle.write("\n") | |
| print(json.dumps(summary, ensure_ascii=False)) | |
| if __name__ == "__main__": | |
| main() | |