Upload tokenizer
Browse files- special_tokens_map.json +30 -0
- tokenization_plamo.py +392 -0
- tokenizer.jsonl +0 -0
- tokenizer_config.json +57 -0
special_tokens_map.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<|plamo:bos|>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "<|plamo:eos|>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": {
|
17 |
+
"content": "<|plamo:pad|>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"unk_token": {
|
24 |
+
"content": "<|plamo:unk|>",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
}
|
30 |
+
}
|
tokenization_plamo.py
ADDED
@@ -0,0 +1,392 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
from shutil import copyfile
|
5 |
+
from typing import Any, Optional, Tuple
|
6 |
+
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# NOTE: numba does not support type hints for njit: https://github.com/python/mypy/issues/16149
|
10 |
+
from numba import njit # type: ignore[attr-defined]
|
11 |
+
from numba.core import types
|
12 |
+
from numba.typed import Dict, List
|
13 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
14 |
+
from transformers.utils import logging
|
15 |
+
|
16 |
+
VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.jsonl"}
|
17 |
+
logger = logging.get_logger(__name__)
|
18 |
+
|
19 |
+
INVALID_SCORE = -20000000
|
20 |
+
UNKNOWN_SCORE = -10000000
|
21 |
+
|
22 |
+
TABLE_PIECE_LENGTH = 0
|
23 |
+
TABLE_TOKEN_ID = 1
|
24 |
+
TABLE_SCORE = 2
|
25 |
+
TABLE_PIECE_ID = 3
|
26 |
+
|
27 |
+
PATH_TOKEN_LENGTH = 0
|
28 |
+
PATH_TOKEN_ID = 1
|
29 |
+
PATH_NUM_TOKENS = 2
|
30 |
+
|
31 |
+
|
32 |
+
class AhoCorasick:
|
33 |
+
def __init__(self) -> None:
|
34 |
+
# List of tokens in the vocabulary.
|
35 |
+
self._tokens: list[str]
|
36 |
+
|
37 |
+
# A mapping from a byte code point to a token ID, used for byte fallback.
|
38 |
+
self._bytes: np.ndarray
|
39 |
+
|
40 |
+
# A mapping from a suffix's piece code to a suffix ID.
|
41 |
+
#
|
42 |
+
# Typically, the Aho-Corasick algorithm builds a Trie and adds suffix links between nodes
|
43 |
+
# of the Trie. In this implementation, a suffix ID corresponds to a node in the trie, and
|
44 |
+
# a piece code to an edge (in other words, a pair of a node and the next character).
|
45 |
+
#
|
46 |
+
# A piece code is a 64-bit integer:
|
47 |
+
# - The upper 32 bits store the Unicode code point of the first character.
|
48 |
+
# - The lower 32 bits store the suffix ID of the remaining suffix.
|
49 |
+
#
|
50 |
+
# A suffix ID is an integer indicating the starting position in the _table.
|
51 |
+
self._to_suffix_id: Dict[types.int64, types.int32]
|
52 |
+
|
53 |
+
# Flattened table representing the Trie structure for the Aho-Corasick algorithm.
|
54 |
+
# It stores information including scores for each piece (prefix) within each suffix.
|
55 |
+
# It is flattened for memory efficiency and performance. Suffixes are stored in
|
56 |
+
# lexicographical order of their reversed strings, which improves memory access locality
|
57 |
+
# when exploring new characters starting from the string's end. Pieces within a suffix are
|
58 |
+
# stored in the decreasing order of their lengths.
|
59 |
+
#
|
60 |
+
# Each piece (a prefix fo the suffix) contains four pieces of information:
|
61 |
+
# - TABLE_PIECE_LENGTH: Length of the piece.
|
62 |
+
# - TABLE_TOKEN_ID: Token ID (or -1 if the piece is not a valid token).
|
63 |
+
# - TABLE_SCORE: Score (or INVALID_SCORE if the piece is not a valid token).
|
64 |
+
# - TABLE_PIECE_ID: Piece ID of the suffix.
|
65 |
+
#
|
66 |
+
# Each suffix also includes a sentinel row with a length of 1, a score of UNKNOWN_SCORE,
|
67 |
+
# and a token ID of -1. Sentinel rows are identified by the score being UNKNOWN_SCORE.
|
68 |
+
self._table: np.ndarray
|
69 |
+
|
70 |
+
def build(self, vocab: list[Any]) -> None:
|
71 |
+
self._bytes = np.zeros(256, dtype=np.int32)
|
72 |
+
self._to_suffix_id = Dict.empty(key_type=types.int64, value_type=types.int32)
|
73 |
+
|
74 |
+
# Build suffix_to_score and token_to_token_id.
|
75 |
+
# The suffix_to_score dictionary maps a suffix to its score. It also includes all suffixes
|
76 |
+
# of the token for the Trie structure for the Aho-Corasick algorithm. If a suffix is not a
|
77 |
+
# valid token, its score is set to math.nan.
|
78 |
+
# The token_to_token_id dictionary maps a token to its token ID.
|
79 |
+
suffix_to_score: dict[str, float] = {}
|
80 |
+
token_to_token_id: dict[str, int] = {}
|
81 |
+
self._tokens = []
|
82 |
+
for token_id, row in enumerate(vocab):
|
83 |
+
assert isinstance(row[0], str), row
|
84 |
+
assert isinstance(row[1], (int, float)), row
|
85 |
+
|
86 |
+
token = str(row[0])
|
87 |
+
self._tokens.append(token)
|
88 |
+
token_to_token_id[token] = token_id
|
89 |
+
|
90 |
+
# Special handling for byte tokens.
|
91 |
+
if len(row) > 2 and row[2] == "BYTE":
|
92 |
+
assert len(token) == 6 and token.startswith("<0x") and token.endswith(">"), row[0]
|
93 |
+
self._bytes[int(row[0][3:5], 16)] = token_id
|
94 |
+
continue
|
95 |
+
|
96 |
+
suffix_to_score[token] = float(row[1])
|
97 |
+
# Ensure that all suffixes are included in suffix_to_score.
|
98 |
+
for i in range(1, len(token)):
|
99 |
+
suffix_to_score[token[i:]] = suffix_to_score.get(token[i:], math.nan)
|
100 |
+
|
101 |
+
# Ensure all byte tokens are set.
|
102 |
+
for i in range(256):
|
103 |
+
assert self._bytes[i] != 0, f"Byte token for <0x{i:02X}> is not set."
|
104 |
+
|
105 |
+
# List suffixes in lexicographical order of their reversed strings.
|
106 |
+
suffixes = list(suffix_to_score.keys())
|
107 |
+
suffixes.append("")
|
108 |
+
suffixes.sort(key=lambda x: x[::-1])
|
109 |
+
|
110 |
+
# Build suffix_to_id, which is a mapping from a suffix to a suffix ID, and _to_suffix_id,
|
111 |
+
# which is a mapping from a piece code to a suffix ID.
|
112 |
+
suffix_to_id: dict[str, int] = {}
|
113 |
+
num_pieces = 0
|
114 |
+
for s in suffixes:
|
115 |
+
suffix_to_id[s] = num_pieces
|
116 |
+
if s != "":
|
117 |
+
self._to_suffix_id[ord(s[0]) << 32 | suffix_to_id[s[1:]]] = np.int32(num_pieces)
|
118 |
+
num_pieces += 1 + sum(s[:i] in suffix_to_score for i in range(1, len(s) + 1))
|
119 |
+
assert suffix_to_id[""] == 0, suffix_to_id[""]
|
120 |
+
|
121 |
+
# Build _table, which is a flattened table representing the Trie structure for the Aho-Corasick.
|
122 |
+
self._table = np.zeros((num_pieces, 4), dtype=np.int32)
|
123 |
+
i = 0
|
124 |
+
for suffix in suffixes:
|
125 |
+
# Add all prefixes of the suffix to the table.
|
126 |
+
for piece_length in range(len(suffix), 0, -1):
|
127 |
+
piece = suffix[:piece_length]
|
128 |
+
score = suffix_to_score.get(piece, None)
|
129 |
+
if score is None:
|
130 |
+
continue
|
131 |
+
self._table[i, TABLE_PIECE_LENGTH] = piece_length
|
132 |
+
self._table[i, TABLE_TOKEN_ID] = token_to_token_id.get(piece, -1)
|
133 |
+
self._table[i, TABLE_SCORE] = round(score * 1e4) if math.isfinite(score) else INVALID_SCORE
|
134 |
+
self._table[i, TABLE_PIECE_ID] = suffix_to_id[piece]
|
135 |
+
i += 1
|
136 |
+
|
137 |
+
# Add a sentinel row.
|
138 |
+
self._table[i, TABLE_PIECE_LENGTH] = 1
|
139 |
+
self._table[i, TABLE_TOKEN_ID] = -1
|
140 |
+
self._table[i, TABLE_SCORE] = UNKNOWN_SCORE
|
141 |
+
i += 1
|
142 |
+
assert i == num_pieces, (i, num_pieces)
|
143 |
+
|
144 |
+
@staticmethod
|
145 |
+
@njit
|
146 |
+
def _encode(
|
147 |
+
to_suffix_id: Dict[types.int64, types.int32],
|
148 |
+
table: np.ndarray,
|
149 |
+
bytes: np.ndarray,
|
150 |
+
data: np.ndarray,
|
151 |
+
) -> np.ndarray:
|
152 |
+
# Initialize scores array with a high value and set the score at the end to 0.
|
153 |
+
# This array keeps track of the minimum cost (best score) to encode from each position to the end.
|
154 |
+
scores = np.full((len(data) + 1,), 2**60, dtype=np.int64)
|
155 |
+
scores[-1] = 0
|
156 |
+
|
157 |
+
# Path array to store the best path information.
|
158 |
+
# The path array keeps track of token length, token ID, and number of tokens needed to encode.
|
159 |
+
path = np.zeros((len(data) + 1, 3), dtype=np.int32)
|
160 |
+
|
161 |
+
# Initialize suffix_id to 0, which represents the root of the Trie.
|
162 |
+
suffix_id = 0
|
163 |
+
|
164 |
+
# Process the input data from the end to the beginning.
|
165 |
+
for i in range(len(data) - 1, -1, -1):
|
166 |
+
c = data[i]
|
167 |
+
|
168 |
+
# Find the next suffix ID by iterating the suffix IDs of prefixes of the current suffix.
|
169 |
+
# NOTE: If no suffix ID is found, suffix_id will be set to 0.
|
170 |
+
for p in range(suffix_id, len(table)):
|
171 |
+
suffix_id = to_suffix_id.get(c << 32 | table[p, TABLE_PIECE_ID], np.int32(0))
|
172 |
+
# If a next suffix ID is found or a sentinel row is reached, break the loop.
|
173 |
+
if suffix_id > 0 or table[p, TABLE_SCORE] == UNKNOWN_SCORE:
|
174 |
+
break
|
175 |
+
|
176 |
+
# Update the best path to the current position. If multiple paths have the same score,
|
177 |
+
# this chooses the longest prefix as the best path (table is sorted in the decreasing
|
178 |
+
# order of piece length).
|
179 |
+
for p in range(suffix_id, len(table)):
|
180 |
+
score = table[p, TABLE_SCORE]
|
181 |
+
if score > INVALID_SCORE:
|
182 |
+
piece_length = table[p, TABLE_PIECE_LENGTH]
|
183 |
+
s = scores[i + piece_length] - score
|
184 |
+
if s < scores[i]:
|
185 |
+
scores[i] = s
|
186 |
+
path[i, PATH_TOKEN_LENGTH] = piece_length
|
187 |
+
path[i, PATH_TOKEN_ID] = table[p, TABLE_TOKEN_ID]
|
188 |
+
path[i, PATH_NUM_TOKENS] = path[i + piece_length, PATH_NUM_TOKENS] + 1
|
189 |
+
if score == UNKNOWN_SCORE:
|
190 |
+
# Add number of bytes to represent `c` in UTF-8 (minus 1; 1 is already
|
191 |
+
# added above).
|
192 |
+
path[i, PATH_NUM_TOKENS] += (c >= 0x80) + (c >= 0x800) + (c >= 0x10000)
|
193 |
+
|
194 |
+
# If it reaches a sentinel row, break the loop.
|
195 |
+
if score == UNKNOWN_SCORE:
|
196 |
+
break
|
197 |
+
|
198 |
+
# Decode the best path from the beginning to get the token IDs.
|
199 |
+
pos = 0
|
200 |
+
token_ids = np.zeros(path[0, PATH_NUM_TOKENS], dtype=np.int32)
|
201 |
+
token_pos = 0
|
202 |
+
while pos < len(data):
|
203 |
+
if path[pos, PATH_TOKEN_ID] >= 0:
|
204 |
+
token_ids[token_pos] = path[pos, PATH_TOKEN_ID]
|
205 |
+
token_pos += 1
|
206 |
+
else:
|
207 |
+
# Fall back to byte tokens.
|
208 |
+
c = data[pos]
|
209 |
+
s = 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000)
|
210 |
+
# Add byte tokens representing UTF-8 bytes.
|
211 |
+
for i in range(s):
|
212 |
+
b = c if s == 1 else (0xF00 >> s) & 0xFF if i == 0 else 0x80
|
213 |
+
token_ids[token_pos] = bytes[b | ((c >> (s - i - 1) * 6) & 0x3F)]
|
214 |
+
token_pos += 1
|
215 |
+
|
216 |
+
# Ensure that pos should increase by at least 1.
|
217 |
+
assert path[pos, PATH_TOKEN_LENGTH] > 0, (pos, path[pos])
|
218 |
+
pos += path[pos, PATH_TOKEN_LENGTH]
|
219 |
+
|
220 |
+
return token_ids
|
221 |
+
|
222 |
+
def encode(self, data: str) -> np.ndarray:
|
223 |
+
"""Encodes a string into a sequence of token IDs."""
|
224 |
+
return np.asarray(
|
225 |
+
self._encode(
|
226 |
+
self._to_suffix_id,
|
227 |
+
self._table,
|
228 |
+
self._bytes,
|
229 |
+
# Convert a string into a numpy array of Unicode code points.
|
230 |
+
# NOTE: This skips UTF-32 BOM.
|
231 |
+
np.frombuffer(data.encode("utf-32"), dtype=np.int32)[1:],
|
232 |
+
)
|
233 |
+
)
|
234 |
+
|
235 |
+
def encode_as_tokens(self, data: str) -> list[str]:
|
236 |
+
"""Encodes a string into a sequence of tokens."""
|
237 |
+
return [self._tokens[token_id] for token_id in self.encode(data)]
|
238 |
+
|
239 |
+
|
240 |
+
class PlamoTokenizer(PreTrainedTokenizer): # type: ignore
|
241 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
242 |
+
model_input_names = ["input_ids", "attention_mask"]
|
243 |
+
|
244 |
+
_save_files = [
|
245 |
+
"special_tokens_map.json",
|
246 |
+
"tokenization_plamo.py",
|
247 |
+
"tokenizer.jsonl",
|
248 |
+
"tokenizer_config.json",
|
249 |
+
]
|
250 |
+
|
251 |
+
def __init__(
|
252 |
+
self,
|
253 |
+
vocab_file: str,
|
254 |
+
unk_token: str = "<|plamo:unk|>",
|
255 |
+
bos_token: str = "<|plamo:bos|>",
|
256 |
+
eos_token: str = "<|plamo:eos|>",
|
257 |
+
pad_token: str = "<|plamo:pad|>",
|
258 |
+
cls_token: Optional[str] = None,
|
259 |
+
sep_token: Optional[str] = None,
|
260 |
+
mask_token: Optional[str] = None,
|
261 |
+
clean_up_tokenization_spaces: bool = False,
|
262 |
+
**kwargs: Any,
|
263 |
+
) -> None:
|
264 |
+
"""Tokenizer for PLaMo.
|
265 |
+
|
266 |
+
Args:
|
267 |
+
vocab_file (str): Vocabrary file path.
|
268 |
+
unk_token (str): Unknown token.
|
269 |
+
bos_token (str): Beginning of sentence token.
|
270 |
+
eos_token (str): End of sentence token.
|
271 |
+
pad_token (str): Padding token.
|
272 |
+
cls_token (str):
|
273 |
+
Classification token, to extract a summary of an input sequence leveraging self-attention along the
|
274 |
+
full depth of the model.
|
275 |
+
sep_token (str): Separation token, to separate context and query in an input sequence.
|
276 |
+
mask_token (str): Mask token, to use when training a model with masked-language modeling.
|
277 |
+
clean_up_tokenization_spaces (bool): Whether or not to clean up the tokenization spaces.
|
278 |
+
num_threads (int):
|
279 |
+
Number of threads. This value will be ignored if one of `PLAMO_TOKENIZER_NUM_THREADS` or
|
280 |
+
`RAYON_NUM_THREADS` is set as an environment variable.
|
281 |
+
"""
|
282 |
+
if "add_bos_token" not in kwargs:
|
283 |
+
kwargs["add_bos_token"] = False
|
284 |
+
if "add_eos_token" not in kwargs:
|
285 |
+
kwargs["add_eos_token"] = False
|
286 |
+
self.data: list[Any] = [json.loads(line) for line in open(vocab_file, "r", encoding="utf-8")]
|
287 |
+
self.vocab: dict[str, int] = {v[0]: i for i, v in enumerate(self.data)}
|
288 |
+
self.aho_corasick = AhoCorasick()
|
289 |
+
self.aho_corasick.build(self.data)
|
290 |
+
self.vocab_file = vocab_file
|
291 |
+
self.add_bos_token = kwargs["add_bos_token"]
|
292 |
+
self.add_eos_token = kwargs["add_eos_token"]
|
293 |
+
|
294 |
+
super().__init__(
|
295 |
+
vocab_file=vocab_file,
|
296 |
+
unk_token=unk_token,
|
297 |
+
bos_token=bos_token,
|
298 |
+
eos_token=eos_token,
|
299 |
+
pad_token=pad_token,
|
300 |
+
cls_token=cls_token,
|
301 |
+
sep_token=sep_token,
|
302 |
+
mask_token=mask_token,
|
303 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
304 |
+
**kwargs,
|
305 |
+
)
|
306 |
+
|
307 |
+
# the functions below are copied from hf transformers LlamaTokenizer's implementation to fix the behaviour of the tokenizer
|
308 |
+
# https://github.com/huggingface/transformers/blob/v4.30.2/src/transformers/models/llama/tokenization_llama.py
|
309 |
+
|
310 |
+
def __getstate__(self) -> dict[str, Any]:
|
311 |
+
state = self.__dict__.copy()
|
312 |
+
state["aho_corasick"] = None
|
313 |
+
return state
|
314 |
+
|
315 |
+
def __setstate__(self, d: dict[str, Any]) -> None:
|
316 |
+
self.__dict__ = d
|
317 |
+
self.aho_corasick = AhoCorasick()
|
318 |
+
self.aho_corasick.build(self.data)
|
319 |
+
|
320 |
+
@property
|
321 |
+
def vocab_size(self) -> Any:
|
322 |
+
"""Returns vocab size"""
|
323 |
+
return len(self.data)
|
324 |
+
|
325 |
+
def token_to_score(self, token: str) -> Optional[float]:
|
326 |
+
"""Returns score of the token"""
|
327 |
+
token_id = self.vocab.get(token, None)
|
328 |
+
return None if token_id is None else self.data[token_id][1]
|
329 |
+
|
330 |
+
def get_vocab(self) -> dict[str, int]:
|
331 |
+
"""Returns vocab as a dict"""
|
332 |
+
vocab = self.vocab.copy()
|
333 |
+
vocab.update(self.added_tokens_encoder)
|
334 |
+
return vocab
|
335 |
+
|
336 |
+
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
337 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
338 |
+
return b"".join(
|
339 |
+
[bytes([int(t[3:5], 16)]) if t.startswith("<0x") else t.encode("utf-8") for t in tokens]
|
340 |
+
).decode("utf-8", errors="replace")
|
341 |
+
|
342 |
+
def _tokenize(self, text: str) -> Any:
|
343 |
+
"""Returns a tokenized string."""
|
344 |
+
return self.aho_corasick.encode_as_tokens(text)
|
345 |
+
|
346 |
+
def _convert_token_to_id(self, token: str) -> Any:
|
347 |
+
"""Converts a token (str) in an id using the vocab."""
|
348 |
+
return self.vocab.get(token, 0)
|
349 |
+
|
350 |
+
def _convert_id_to_token(self, index: int) -> Any:
|
351 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
352 |
+
return self.data[index][0]
|
353 |
+
|
354 |
+
def build_inputs_with_special_tokens(
|
355 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
356 |
+
) -> List[int]:
|
357 |
+
bos_token_id = [self.bos_token_id] if self.add_bos_token else []
|
358 |
+
eos_token_id = [self.eos_token_id] if self.add_eos_token else []
|
359 |
+
|
360 |
+
output = bos_token_id + token_ids_0 + eos_token_id
|
361 |
+
|
362 |
+
if token_ids_1 is not None:
|
363 |
+
output = output + bos_token_id + token_ids_1 + eos_token_id
|
364 |
+
|
365 |
+
return output
|
366 |
+
|
367 |
+
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
368 |
+
"""
|
369 |
+
Save the vocabulary and special tokens file to a directory.
|
370 |
+
|
371 |
+
Args:
|
372 |
+
save_directory (`str`):
|
373 |
+
The directory in which to save the vocabulary.
|
374 |
+
|
375 |
+
Returns:
|
376 |
+
`Tuple(str)`: Paths to the files saved.
|
377 |
+
"""
|
378 |
+
if not os.path.isdir(save_directory):
|
379 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
380 |
+
return ("",)
|
381 |
+
out_vocab_file = os.path.join(
|
382 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
383 |
+
)
|
384 |
+
|
385 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
386 |
+
copyfile(self.vocab_file, out_vocab_file)
|
387 |
+
elif not os.path.isfile(self.vocab_file):
|
388 |
+
with open(out_vocab_file, "w") as f:
|
389 |
+
for token in self.data:
|
390 |
+
print(json.dumps(token, ensure_ascii=False), file=f)
|
391 |
+
|
392 |
+
return (out_vocab_file,)
|
tokenizer.jsonl
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": true,
|
3 |
+
"add_eos_token": false,
|
4 |
+
"added_tokens_decoder": {
|
5 |
+
"0": {
|
6 |
+
"content": "<|plamo:unk|>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false,
|
11 |
+
"special": true
|
12 |
+
},
|
13 |
+
"1": {
|
14 |
+
"content": "<|plamo:bos|>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false,
|
19 |
+
"special": true
|
20 |
+
},
|
21 |
+
"2": {
|
22 |
+
"content": "<|plamo:eos|>",
|
23 |
+
"lstrip": false,
|
24 |
+
"normalized": false,
|
25 |
+
"rstrip": false,
|
26 |
+
"single_word": false,
|
27 |
+
"special": true
|
28 |
+
},
|
29 |
+
"3": {
|
30 |
+
"content": "<|plamo:pad|>",
|
31 |
+
"lstrip": false,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false,
|
35 |
+
"special": true
|
36 |
+
}
|
37 |
+
},
|
38 |
+
"auto_map": {
|
39 |
+
"AutoTokenizer": [
|
40 |
+
"tokenization_plamo.PlamoTokenizer",
|
41 |
+
null
|
42 |
+
]
|
43 |
+
},
|
44 |
+
"bos_token": "<|plamo:bos|>",
|
45 |
+
"chat_template": "{{bos_token}}{% for message in messages %}{% if message['role'] == 'user' %}{{ '\\n\\n### 指示:\\n' + message['content'] }}{% elif message['role'] == 'system' %}{{ '以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。' }}{% elif message['role'] == 'assistant' %}{{ '\\n\\n### 応答:\\n' + message['content'] + eos_token }}{% endif %}{% if loop.last and add_generation_prompt %}{{ '\\n\\n### 応答:\\n' }}{% endif %}{% endfor %}",
|
46 |
+
"clean_up_tokenization_spaces": false,
|
47 |
+
"cls_token": null,
|
48 |
+
"eos_token": "<|plamo:eos|>",
|
49 |
+
"extra_special_tokens": {},
|
50 |
+
"local_file_only": true,
|
51 |
+
"mask_token": null,
|
52 |
+
"model_max_length": 1000000000000000019884624838656,
|
53 |
+
"pad_token": "<|plamo:pad|>",
|
54 |
+
"sep_token": null,
|
55 |
+
"tokenizer_class": "PlamoTokenizer",
|
56 |
+
"unk_token": "<|plamo:unk|>"
|
57 |
+
}
|