Download tokenizer/train.py from DiChat-models/DiChat-0.1-Experimental: direct link, hf CLI and curl.
- Browser
- Download file 1.51 kB
-
https://huggingface.co/DiChat-models/DiChat-0.1-Experimental/resolve/main/tokenizer/train.py
- Command line
-
hf download hf://DiChat-models/DiChat-0.1-Experimental/tokenizer/train.py
-
curl -L -o train.py https://huggingface.co/DiChat-models/DiChat-0.1-Experimental/resolve/main/tokenizer/train.py
1.51 kB
| import argparse | |
| from pathlib import Path | |
| import sentencepiece as spm | |
| USER_SYMBOLS = ["<|system|>", "<|user|>", "<|assistant|>", "<|end|>"] | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--input", default="data/curated/tokenizer_sample.txt") | |
| parser.add_argument("--prefix", default="tokenizer/dichat") | |
| parser.add_argument("--vocab-size", type=int, default=24_000) | |
| args = parser.parse_args() | |
| input_path = Path(args.input) | |
| prefix = Path(args.prefix) | |
| prefix.parent.mkdir(parents=True, exist_ok=True) | |
| if not input_path.exists() or input_path.stat().st_size == 0: | |
| raise SystemExit("Build a balanced tokenizer sample first.") | |
| spm.SentencePieceTrainer.train( | |
| input=str(input_path), | |
| model_prefix=str(prefix), | |
| model_type="bpe", | |
| vocab_size=args.vocab_size, | |
| character_coverage=1.0, | |
| byte_fallback=True, | |
| normalization_rule_name="nfkc", | |
| split_digits=True, | |
| allow_whitespace_only_pieces=True, | |
| remove_extra_whitespaces=False, | |
| unk_id=0, | |
| bos_id=1, | |
| eos_id=2, | |
| pad_id=3, | |
| unk_piece="<unk>", | |
| bos_piece="<s>", | |
| eos_piece="</s>", | |
| pad_piece="<pad>", | |
| user_defined_symbols=USER_SYMBOLS, | |
| hard_vocab_limit=True, | |
| num_threads=8, | |
| ) | |
| print(f"Saved: {prefix}.model") | |
| print(f"Saved: {prefix}.vocab") | |
| if __name__ == "__main__": | |
| main() | |