File size: 3,354 Bytes
b4b069f | 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | """
Derived from Andrej Karpathy's nanochat project.
MIT License
Copyright (c) 2025 Andrej Karpathy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"""
from __future__ import annotations
from pathlib import Path
from typing import Iterable
from tokenizers import Regex
from tokenizers import Tokenizer as HFTokenizer
from tokenizers import decoders, pre_tokenizers
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
SPECIAL_TOKENS = [
"<|bos|>",
"<|user_start|>",
"<|user_end|>",
"<|assistant_start|>",
"<|assistant_end|>",
"<|python_start|>",
"<|python_end|>",
"<|output_start|>",
"<|output_end|>",
]
SPLIT_PATTERN = (
r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,2}|"""
r""" ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+"""
)
class BpeTokenizer:
"""Minimal HuggingFace BPE wrapper following nanochat's GPT-4-style splitter."""
def __init__(self, tokenizer: HFTokenizer):
self.tokenizer = tokenizer
@classmethod
def train_from_iterator(
cls, text_iterator: Iterable[str], vocab_size: int
) -> "BpeTokenizer":
tokenizer = HFTokenizer(BPE(byte_fallback=True, unk_token=None, fuse_unk=False))
tokenizer.normalizer = None
tokenizer.pre_tokenizer = pre_tokenizers.Sequence(
[
pre_tokenizers.Split(
pattern=Regex(SPLIT_PATTERN),
behavior="isolated",
invert=False,
),
pre_tokenizers.ByteLevel(add_prefix_space=False, use_regex=False),
]
)
tokenizer.decoder = decoders.ByteLevel()
trainer = BpeTrainer(
vocab_size=vocab_size,
show_progress=True,
min_frequency=0,
initial_alphabet=pre_tokenizers.ByteLevel.alphabet(),
special_tokens=SPECIAL_TOKENS,
)
tokenizer.train_from_iterator(text_iterator, trainer)
return cls(tokenizer)
@classmethod
def from_file(cls, path: str | Path) -> "BpeTokenizer":
return cls(HFTokenizer.from_file(str(path)))
def save(self, path: str | Path) -> None:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
self.tokenizer.save(str(path))
@property
def vocab_size(self) -> int:
return self.tokenizer.get_vocab_size()
@property
def bos_id(self) -> int:
bos = self.tokenizer.token_to_id("<|bos|>")
if bos is None:
raise ValueError("tokenizer is missing <|bos|>")
return bos
def encode(self, text: str, prepend_bos: bool = False) -> list[int]:
ids = self.tokenizer.encode(text, add_special_tokens=False).ids
if prepend_bos:
ids.insert(0, self.bos_id)
return ids
|