Download tokenization_plamo.py from hscr/luxfer: direct link, hf CLI and curl.
- Browser
- Download file 22.4 kB
-
https://huggingface.co/hscr/luxfer/resolve/main/tokenization_plamo.py
- Command line
-
hf download hf://hscr/luxfer/tokenization_plamo.py
-
curl -L -o tokenization_plamo.py https://huggingface.co/hscr/luxfer/resolve/main/tokenization_plamo.py
22.4 kB
| import json | |
| import math | |
| import os | |
| import re | |
| from shutil import copyfile | |
| from typing import Any | |
| import numpy as np | |
| # NOTE: numba does not support type hints for njit: https://github.com/python/mypy/issues/16149 | |
| from numba import njit # type: ignore[attr-defined] | |
| from numba.core import types # type: ignore[import-untyped] | |
| from numba.typed import Dict | |
| from transformers.tokenization_utils import PreTrainedTokenizer | |
| from transformers.utils import logging | |
| VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.jsonl"} | |
| logger = logging.get_logger(__name__) | |
| INVALID_SCORE = -20000000 | |
| UNKNOWN_SCORE = -10000000 | |
| TABLE_PIECE_LENGTH = 0 | |
| TABLE_TOKEN_ID = 1 | |
| TABLE_SCORE = 2 | |
| TABLE_PIECE_ID = 3 | |
| PATH_TOKEN_LENGTH = 0 | |
| PATH_TOKEN_ID = 1 | |
| PATH_NUM_TOKENS = 2 | |
| # In Unicode, the character U+EE00 is a private use character that is not assigned to any specific | |
| # character. This is internally used as a boundary character in tokenization. | |
| BOUNDARY_CHAR = "\uee00" | |
| BOUNDARY_TOKEN_ID = 10000000 | |
| class AhoCorasick: | |
| def __init__(self) -> None: | |
| # List of tokens in the vocabulary. | |
| self._tokens: list[str] | |
| # A mapping from a byte code point to a token ID, used for byte fallback. | |
| self._bytes: np.ndarray | |
| # A mapping from a suffix's piece code to a suffix ID. | |
| # | |
| # Typically, the Aho-Corasick algorithm builds a Trie and adds suffix links between nodes | |
| # of the Trie. In this implementation, a suffix ID corresponds to a node in the trie, and | |
| # a piece code to an edge (in other words, a pair of a node and the next character). | |
| # | |
| # A piece code is a 64-bit integer: | |
| # - The upper 32 bits store the Unicode code point of the first character. | |
| # - The lower 32 bits store the suffix ID of the remaining suffix. | |
| # | |
| # A suffix ID is an integer indicating the starting position in the _table. | |
| self._to_suffix_id: dict[np.int64, np.int32] # numba.typed.Dict if jit is enabled | |
| # Flattened table representing the Trie structure for the Aho-Corasick algorithm. | |
| # It stores information including scores for each piece (prefix) within each suffix. | |
| # It is flattened for memory efficiency and performance. Suffixes are stored in | |
| # lexicographical order of their reversed strings, which improves memory access locality | |
| # when exploring new characters starting from the string's end. Pieces within a suffix are | |
| # stored in the decreasing order of their lengths. | |
| # | |
| # Each piece (a prefix fo the suffix) contains four pieces of information: | |
| # - TABLE_PIECE_LENGTH: Length of the piece. | |
| # - TABLE_TOKEN_ID: Token ID (or -1 if the piece is not a valid token). | |
| # - TABLE_SCORE: Score (or INVALID_SCORE if the piece is not a valid token). | |
| # - TABLE_PIECE_ID: Piece ID of the suffix. | |
| # | |
| # Each suffix also includes a sentinel row with a length of 1, a score of UNKNOWN_SCORE, | |
| # and a token ID of -1. Sentinel rows are identified by the score being UNKNOWN_SCORE. | |
| self._table: np.ndarray | |
| # Regular expression matcher for identifying special tokens in the format <|plamo:*|>. | |
| # Used to split text around special tokens during tokenization preprocessing. | |
| self._sp_token_matcher: re.Pattern[str] | None = None | |
| # Preprocessor to prevent boundary shifts in Unigram tokenization. | |
| # The global DP in Unigram can create long lookahead dependencies, causing token boundaries | |
| # to shift unexpectedly based on later context. While various sequences can trigger this, | |
| # the most common culprits are long runs of spaces or repeated characters. This matcher | |
| # finds sequences of two or more spaces or any character repeated four or more times, and | |
| # forces hard splits immediately before and after each match, treating the span as its own | |
| # token. By explicitly marking these boundaries, we eliminate most boundary jitter without | |
| # trying to cover every rare case. | |
| self._matcher: re.Pattern[str] | None = None | |
| def build( | |
| self, | |
| vocab: list[Any], | |
| *, | |
| break_around_consecutive_spaces_threshold: int | None = None, | |
| break_around_repeated_chars_threshold: int | None = None, | |
| ) -> None: | |
| """Build the Aho-Corasick data structure from vocabulary. | |
| Args: | |
| vocab: List of vocabulary entries, where each entry is [token, score, type, ...]. | |
| break_around_consecutive_spaces_threshold: Minimum number of consecutive spaces to trigger boundary splits. | |
| If None, consecutive spaces won't trigger splits. | |
| break_around_repeated_chars_threshold: Minimum number of repeated characters to trigger boundary splits. | |
| If None, repeated characters won't trigger splits. | |
| """ | |
| self._bytes = np.zeros(256, dtype=np.int32) | |
| self._to_suffix_id = Dict.empty(key_type=types.int64, value_type=types.int32) | |
| # Build suffix_to_score and token_to_token_id. | |
| # The suffix_to_score dictionary maps a suffix to its score. It also includes all suffixes | |
| # of the token for the Trie structure for the Aho-Corasick algorithm. If a suffix is not a | |
| # valid token, its score is set to math.nan. | |
| # The token_to_token_id dictionary maps a token to its token ID. | |
| suffix_to_score: dict[str, float] = {} | |
| token_to_token_id: dict[str, int] = {} | |
| self._tokens = [] | |
| for token_id, row in list(enumerate(vocab)) + [(BOUNDARY_TOKEN_ID, [BOUNDARY_CHAR, 0, "CONTROL"])]: | |
| assert isinstance(row[0], str), row | |
| assert isinstance(row[1], (int, float)), row | |
| token = str(row[0]) | |
| self._tokens.append(token) | |
| token_to_token_id[token] = token_id | |
| # Special handling for byte tokens. | |
| if len(row) > 2 and row[2] == "BYTE": | |
| assert len(token) == 6 and token.startswith("<0x") and token.endswith(">"), row[0] | |
| self._bytes[int(row[0][3:5], 16)] = token_id | |
| continue | |
| suffix_to_score[token] = float(row[1]) | |
| # Ensure that all suffixes are included in suffix_to_score. | |
| for i in range(1, len(token)): | |
| suffix_to_score[token[i:]] = suffix_to_score.get(token[i:], math.nan) | |
| # Ensure all byte tokens are set. | |
| for i in range(256): | |
| assert self._bytes[i] != 0, f"Byte token for <0x{i:02X}> is not set." | |
| # Build a matcher for special tokens. | |
| self._sp_token_matcher = re.compile(r"(<\|plamo:[^|\s]{,64}\|>)") | |
| # Build matcher pattern to prevent boundary shifts. | |
| patterns = [] | |
| if break_around_repeated_chars_threshold is not None: | |
| patterns.append(f"(.)\\2{{{break_around_repeated_chars_threshold - 1},}}") | |
| if break_around_consecutive_spaces_threshold is not None: | |
| patterns.append(f" {{{break_around_consecutive_spaces_threshold},}}") | |
| self._matcher = re.compile(f"({'|'.join(patterns)})") if patterns else None | |
| # List suffixes in lexicographical order of their reversed strings. | |
| suffixes = list(suffix_to_score.keys()) | |
| suffixes.append("") | |
| suffixes.sort(key=lambda x: x[::-1]) | |
| # Build suffix_to_id, which is a mapping from a suffix to a suffix ID, and _to_suffix_id, | |
| # which is a mapping from a piece code to a suffix ID. | |
| suffix_to_id: dict[str, int] = {} | |
| num_pieces = 0 | |
| for s in suffixes: | |
| suffix_to_id[s] = num_pieces | |
| if s != "": | |
| self._to_suffix_id[ | |
| ord(s[0]) << 32 | suffix_to_id[s[1:]] # type: ignore[index] # cast int to np.int64 | |
| ] = np.int32(num_pieces) | |
| num_pieces += 1 + sum(s[:i] in suffix_to_score for i in range(1, len(s) + 1)) | |
| assert suffix_to_id[""] == 0, suffix_to_id[""] | |
| # Build _table, which is a flattened table representing the Trie structure for the Aho-Corasick. | |
| self._table = np.zeros((num_pieces, 4), dtype=np.int32) | |
| i = 0 | |
| for suffix in suffixes: | |
| # Add all prefixes of the suffix to the table. | |
| for piece_length in range(len(suffix), 0, -1): | |
| piece = suffix[:piece_length] | |
| score = suffix_to_score.get(piece, None) | |
| if score is None: | |
| continue | |
| self._table[i, TABLE_PIECE_LENGTH] = piece_length | |
| self._table[i, TABLE_TOKEN_ID] = token_to_token_id.get(piece, -1) | |
| self._table[i, TABLE_SCORE] = round(score * 1e4) if math.isfinite(score) else INVALID_SCORE | |
| self._table[i, TABLE_PIECE_ID] = suffix_to_id[piece] | |
| i += 1 | |
| # Add a sentinel row. | |
| self._table[i, TABLE_PIECE_LENGTH] = 1 | |
| self._table[i, TABLE_TOKEN_ID] = -1 | |
| self._table[i, TABLE_SCORE] = UNKNOWN_SCORE | |
| i += 1 | |
| assert i == num_pieces, (i, num_pieces) | |
| # type: ignore[misc] # untyped decorator | |
| def _encode( | |
| to_suffix_id: dict[np.int64, np.int32], # numba.typed.Dict if jit is enabled | |
| table: np.ndarray, | |
| bytes: np.ndarray, | |
| data: np.ndarray, | |
| ) -> np.ndarray: | |
| # Initialize scores array with a high value and set the score at the end to 0. | |
| # This array keeps track of the minimum cost (best score) to encode from each position to the end. | |
| scores = np.full((len(data) + 1,), 2**60, dtype=np.int64) | |
| scores[-1] = 0 | |
| # Path array to store the best path information. | |
| # The path array keeps track of token length, token ID, and number of tokens needed to encode. | |
| path = np.zeros((len(data) + 1, 3), dtype=np.int32) | |
| # Initialize suffix_id to 0, which represents the root of the Trie. | |
| suffix_id = np.int32(0) | |
| # Process the input data from the end to the beginning. | |
| for i in range(len(data) - 1, -1, -1): | |
| c: np.int32 = data[i] | |
| # Find the next suffix ID by iterating the suffix IDs of prefixes of the current suffix. | |
| # NOTE: If no suffix ID is found, suffix_id will be set to 0. | |
| for p in range(suffix_id, len(table)): | |
| suffix_id = to_suffix_id.get(np.int64(c) << 32 | table[p, TABLE_PIECE_ID], np.int32(0)) | |
| # If a next suffix ID is found or a sentinel row is reached, break the loop. | |
| if suffix_id > 0 or table[p, TABLE_SCORE] == UNKNOWN_SCORE: | |
| break | |
| # Update the best path to the current position. If multiple paths have the same score, | |
| # this chooses the longest prefix as the best path (table is sorted in the decreasing | |
| # order of piece length). | |
| for p in range(suffix_id, len(table)): | |
| score = table[p, TABLE_SCORE] | |
| if score > INVALID_SCORE: | |
| piece_length = table[p, TABLE_PIECE_LENGTH] | |
| s = scores[i + piece_length] - score | |
| if s < scores[i]: | |
| scores[i] = s | |
| path[i, PATH_TOKEN_LENGTH] = piece_length | |
| path[i, PATH_TOKEN_ID] = table[p, TABLE_TOKEN_ID] | |
| path[i, PATH_NUM_TOKENS] = path[i + piece_length, PATH_NUM_TOKENS] + 1 | |
| if score == UNKNOWN_SCORE: | |
| # Add number of bytes to represent `c` in UTF-8 (minus 1; 1 is already | |
| # added above). | |
| path[i, PATH_NUM_TOKENS] += (c >= 0x80) + (c >= 0x800) + (c >= 0x10000) | |
| # If it reaches a sentinel row, break the loop. | |
| if score == UNKNOWN_SCORE: | |
| break | |
| # Decode the best path from the beginning to get the token IDs. | |
| pos = 0 | |
| token_ids = np.zeros(path[0, PATH_NUM_TOKENS], dtype=np.int32) | |
| token_pos = 0 | |
| while pos < len(data): | |
| if path[pos, PATH_TOKEN_ID] >= 0: | |
| token_ids[token_pos] = path[pos, PATH_TOKEN_ID] | |
| if token_ids[token_pos] != BOUNDARY_TOKEN_ID: | |
| token_pos += 1 | |
| else: | |
| # Fall back to byte tokens. | |
| c = data[pos] | |
| s = 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000) | |
| # Add byte tokens representing UTF-8 bytes. | |
| for i in range(s): | |
| b = c if s == 1 else (0xF00 >> s) & 0xFF if i == 0 else 0x80 | |
| token_ids[token_pos] = bytes[b | ((c >> (s - i - 1) * 6) & 0x3F)] | |
| token_pos += 1 | |
| # Ensure that pos should increase by at least 1. | |
| assert path[pos, PATH_TOKEN_LENGTH] > 0, (pos, path[pos]) | |
| pos += path[pos, PATH_TOKEN_LENGTH] | |
| return token_ids[:token_pos] | |
| def encode(self, data: str) -> np.ndarray: | |
| """Encodes a string into a sequence of token IDs.""" | |
| if self._sp_token_matcher is not None: | |
| data = self._sp_token_matcher.sub(BOUNDARY_CHAR + "\\1" + BOUNDARY_CHAR, data) | |
| if self._matcher is not None: | |
| data = self._matcher.sub(BOUNDARY_CHAR + "\\1" + BOUNDARY_CHAR, data) | |
| return np.asarray( | |
| self._encode( | |
| self._to_suffix_id, | |
| self._table, | |
| self._bytes, | |
| # Convert a string into a numpy array of Unicode code points. | |
| # NOTE: This skips UTF-32 BOM. | |
| np.frombuffer(data.encode("utf-32"), dtype=np.int32)[1:], | |
| ) | |
| ) | |
| def encode_as_tokens(self, data: str) -> list[str]: | |
| """Encodes a string into a sequence of tokens.""" | |
| return [self._tokens[token_id] for token_id in self.encode(data)] | |
| class Plamo3Tokenizer(PreTrainedTokenizer): | |
| vocab_files_names = VOCAB_FILES_NAMES | |
| model_input_names = ["input_ids", "attention_mask"] | |
| _save_files = [ | |
| "tokenization_plamo.py", | |
| "tokenizer.jsonl", | |
| "tokenizer_config.json", | |
| ] | |
| def __init__( | |
| self, | |
| vocab_file: str, | |
| unk_token: str = "<|plamo:unk|>", | |
| bos_token: str = "<|plamo:bos|>", | |
| eos_token: str = "<|plamo:eos|>", | |
| pad_token: str = "<|plamo:pad|>", | |
| cls_token: str | None = None, | |
| sep_token: str | None = None, | |
| mask_token: str | None = None, | |
| clean_up_tokenization_spaces: bool = False, | |
| break_around_consecutive_spaces_threshold: int | None = None, | |
| break_around_repeated_chars_threshold: int | None = None, | |
| **kwargs: Any, | |
| ) -> None: | |
| """Tokenizer for PLaMo. | |
| Args: | |
| vocab_file (str): Vocabrary file path. | |
| unk_token (str): Unknown token. | |
| bos_token (str): Beginning of sentence token. | |
| eos_token (str): End of sentence token. | |
| pad_token (str): Padding token. | |
| cls_token (str): | |
| Classification token, to extract a summary of an input sequence leveraging self-attention along the | |
| full depth of the model. | |
| sep_token (str): Separation token, to separate context and query in an input sequence. | |
| mask_token (str): Mask token, to use when training a model with masked-language modeling. | |
| clean_up_tokenization_spaces (bool): Whether or not to clean up the tokenization spaces. | |
| break_around_consecutive_spaces_threshold (int, optional): Minimum number of consecutive spaces to trigger | |
| boundary splits. If None, consecutive spaces won't trigger splits. | |
| break_around_repeated_chars_threshold (int, optional): Minimum number of repeated characters to trigger | |
| boundary splits. If None, repeated characters won't trigger splits. | |
| """ | |
| if "add_bos_token" not in kwargs: | |
| kwargs["add_bos_token"] = False | |
| if "add_eos_token" not in kwargs: | |
| kwargs["add_eos_token"] = False | |
| with open(vocab_file, encoding="utf-8") as f: | |
| self.data: list[Any] = [json.loads(line) for line in f] | |
| self.vocab: dict[str, int] = {v[0]: i for i, v in enumerate(self.data)} | |
| self.aho_corasick = AhoCorasick() | |
| self.break_around_consecutive_spaces_threshold = break_around_consecutive_spaces_threshold | |
| self.break_around_repeated_chars_threshold = break_around_repeated_chars_threshold | |
| self.aho_corasick.build( | |
| self.data, | |
| break_around_consecutive_spaces_threshold=self.break_around_consecutive_spaces_threshold, | |
| break_around_repeated_chars_threshold=self.break_around_repeated_chars_threshold, | |
| ) | |
| self.vocab_file = vocab_file | |
| self.add_bos_token = kwargs["add_bos_token"] | |
| self.add_eos_token = kwargs["add_eos_token"] | |
| super().__init__( # type: ignore[no-untyped-call] | |
| vocab_file=vocab_file, | |
| unk_token=unk_token, | |
| bos_token=bos_token, | |
| eos_token=eos_token, | |
| pad_token=pad_token, | |
| cls_token=cls_token, | |
| sep_token=sep_token, | |
| mask_token=mask_token, | |
| clean_up_tokenization_spaces=clean_up_tokenization_spaces, | |
| break_around_consecutive_spaces_threshold=break_around_consecutive_spaces_threshold, | |
| break_around_repeated_chars_threshold=break_around_repeated_chars_threshold, | |
| **kwargs, | |
| ) | |
| # the functions below are copied from hf transformers LlamaTokenizer's implementation to fix the behaviour of the tokenizer | |
| # https://github.com/huggingface/transformers/blob/v4.30.2/src/transformers/models/llama/tokenization_llama.py | |
| def __getstate__(self) -> dict[str, Any]: | |
| state = self.__dict__.copy() | |
| state["aho_corasick"] = None | |
| return state | |
| def __setstate__(self, d: dict[str, Any]) -> None: | |
| self.__dict__ = d | |
| self.aho_corasick = AhoCorasick() | |
| self.aho_corasick.build( | |
| self.data, | |
| break_around_consecutive_spaces_threshold=self.break_around_consecutive_spaces_threshold, | |
| break_around_repeated_chars_threshold=self.break_around_repeated_chars_threshold, | |
| ) | |
| def vocab_size(self) -> Any: | |
| """Returns vocab size""" | |
| return len(self.data) | |
| def token_to_score(self, token: str) -> float | None: | |
| """Returns score of the token""" | |
| token_id = self.vocab.get(token, None) | |
| return None if token_id is None else self.data[token_id][1] | |
| def get_vocab(self) -> dict[str, int]: | |
| """Returns vocab as a dict""" | |
| vocab = self.vocab.copy() | |
| vocab.update(self.added_tokens_encoder) | |
| return vocab | |
| def convert_tokens_to_string(self, tokens: list[str]) -> str: | |
| """Converts a sequence of tokens (string) in a single string.""" | |
| return b"".join( | |
| [bytes([int(t[3:5], 16)]) if t.startswith("<0x") else t.encode("utf-8") for t in tokens] | |
| ).decode("utf-8", errors="replace") | |
| def _tokenize(self, text: str, **kwargs: Any) -> list[str]: | |
| """Returns a tokenized string.""" | |
| return self.aho_corasick.encode_as_tokens(text) | |
| def _convert_token_to_id(self, token: str) -> int: | |
| """Converts a token (str) in an id using the vocab.""" | |
| return self.vocab.get(token, 0) | |
| def _convert_id_to_token(self, index: int) -> str: | |
| """Converts an index (integer) in a token (str) using the vocab.""" | |
| return self.data[index][0] # type: ignore[no-any-return] | |
| def build_inputs_with_special_tokens( | |
| self, token_ids_0: list[int], token_ids_1: list[int] | None = None | |
| ) -> list[int]: | |
| bos_token_id = [self.bos_token_id] if self.add_bos_token else [] | |
| eos_token_id = [self.eos_token_id] if self.add_eos_token else [] | |
| output = bos_token_id + token_ids_0 + eos_token_id | |
| if token_ids_1 is not None: | |
| output = output + bos_token_id + token_ids_1 + eos_token_id | |
| return output | |
| def _save_pretrained( | |
| self, | |
| save_directory: str | os.PathLike[str], | |
| file_names: tuple[str, ...], | |
| legacy_format: bool | None = None, | |
| filename_prefix: str | None = None, | |
| ) -> tuple[str, ...]: | |
| """Restore BOS/EOS flags before save_pretrained optionally uploads the files. | |
| Transformers v5 omits these flags from tokenizer_config.json, but this | |
| tokenizer needs them to preserve its behavior after saving and reloading. | |
| """ | |
| saved_files = super()._save_pretrained( | |
| save_directory, | |
| file_names=file_names, | |
| legacy_format=legacy_format, | |
| filename_prefix=filename_prefix, | |
| ) | |
| config_file = file_names[0] | |
| with open(config_file, encoding="utf-8") as f: | |
| tokenizer_config = json.load(f) | |
| tokenizer_config.update(add_bos_token=self.add_bos_token, add_eos_token=self.add_eos_token) | |
| with open(config_file, "w", encoding="utf-8") as f: | |
| f.write(json.dumps(tokenizer_config, indent=2, sort_keys=True, ensure_ascii=False) + "\n") | |
| return saved_files | |
| def save_vocabulary(self, save_directory: str, filename_prefix: str | None = None) -> tuple[str]: | |
| """ | |
| Save the vocabulary and special tokens file to a directory. | |
| Args: | |
| save_directory (`str`): | |
| The directory in which to save the vocabulary. | |
| Returns: | |
| `Tuple(str)`: Paths to the files saved. | |
| """ | |
| if not os.path.isdir(save_directory): | |
| logger.error(f"Vocabulary path ({save_directory}) should be a directory") | |
| return ("",) | |
| out_vocab_file = os.path.join( | |
| save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] | |
| ) | |
| if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file): | |
| copyfile(self.vocab_file, out_vocab_file) | |
| elif not os.path.isfile(self.vocab_file): | |
| with open(out_vocab_file, "w") as f: | |
| for token in self.data: | |
| print(json.dumps(token, ensure_ascii=False), file=f) | |
| return (out_vocab_file,) | |