Safetensors
GGUF
English
gemma3_text
base
sml
void
pretrained-from-scratch
appvoid commited on
Commit
429dbc0
·
verified ·
1 Parent(s): 3886817

Continued pretraining: FineWeb-Edu + rewrite6 byte hybrid + no-prompt-15k

Browse files
Files changed (2) hide show
  1. hybrid_tokenizer.py +154 -0
  2. hybrid_tokenizer_config.json +301 -0
hybrid_tokenizer.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import Iterable
4
+ from transformers import AutoTokenizer
5
+
6
+
7
+ class HybridByteTokenizer:
8
+ def __init__(self, model_id_or_path: str, **kwargs):
9
+ self.tokenizer = AutoTokenizer.from_pretrained(model_id_or_path, **kwargs)
10
+ vocab = self.tokenizer.get_vocab()
11
+
12
+ self.byte_token_ids = []
13
+ for value in range(256):
14
+ token = f"<0x{value:02X}>"
15
+ if token not in vocab:
16
+ raise ValueError(f"Missing byte token: {token}")
17
+ self.byte_token_ids.append(int(vocab[token]))
18
+
19
+ self.byte_id_to_value = {
20
+ token_id: value
21
+ for value, token_id in enumerate(self.byte_token_ids)
22
+ }
23
+ self.separator_ids = self.tokenizer.encode(
24
+ "\\n\\n", add_special_tokens=False
25
+ )
26
+ self.quote_ids = self.tokenizer.encode(
27
+ '"', add_special_tokens=False
28
+ )
29
+
30
+ def encode_bytes(self, text: str) -> list[int]:
31
+ return [
32
+ self.byte_token_ids[value]
33
+ for value in str(text or "").encode("utf-8")
34
+ ]
35
+
36
+ def encode_rewrite(
37
+ self,
38
+ instruction: str,
39
+ text: str,
40
+ output: str = "",
41
+ ) -> list[int]:
42
+ ids = []
43
+ instruction = str(instruction or "").strip()
44
+
45
+ if instruction:
46
+ ids.extend(
47
+ self.tokenizer.encode(
48
+ instruction,
49
+ add_special_tokens=False,
50
+ )
51
+ )
52
+ ids.extend(self.separator_ids)
53
+
54
+ ids.extend(self.quote_ids)
55
+ ids.extend(self.encode_bytes(text))
56
+ ids.extend(self.quote_ids)
57
+ ids.extend(self.separator_ids)
58
+ ids.extend(self.quote_ids)
59
+ ids.extend(self.encode_bytes(output))
60
+ ids.extend(self.quote_ids)
61
+ return ids
62
+
63
+ def encode_text(
64
+ self,
65
+ text: str,
66
+ byte_inside_quotes: bool = True,
67
+ ) -> list[int]:
68
+ if not byte_inside_quotes:
69
+ return self.tokenizer.encode(text, add_special_tokens=False)
70
+
71
+ ids = []
72
+ normal = []
73
+ quoted = []
74
+ inside = False
75
+ escaped = False
76
+
77
+ def flush_normal():
78
+ if normal:
79
+ ids.extend(
80
+ self.tokenizer.encode(
81
+ "".join(normal),
82
+ add_special_tokens=False,
83
+ )
84
+ )
85
+ normal.clear()
86
+
87
+ def flush_quoted():
88
+ if quoted:
89
+ ids.extend(self.encode_bytes("".join(quoted)))
90
+ quoted.clear()
91
+
92
+ for character in str(text):
93
+ if inside:
94
+ if character == '"' and not escaped:
95
+ flush_quoted()
96
+ ids.extend(self.quote_ids)
97
+ inside = False
98
+ escaped = False
99
+ continue
100
+ quoted.append(character)
101
+ escaped = character == "\\\\" and not escaped
102
+ if character != "\\\\":
103
+ escaped = False
104
+ else:
105
+ if character == '"':
106
+ flush_normal()
107
+ ids.extend(self.quote_ids)
108
+ inside = True
109
+ else:
110
+ normal.append(character)
111
+
112
+ if inside:
113
+ flush_quoted()
114
+ else:
115
+ flush_normal()
116
+ return ids
117
+
118
+ def decode(self, token_ids: Iterable[int]) -> str:
119
+ output = []
120
+ normal = []
121
+ byte_buffer = bytearray()
122
+
123
+ def flush_normal():
124
+ if normal:
125
+ output.append(
126
+ self.tokenizer.decode(
127
+ normal,
128
+ skip_special_tokens=False,
129
+ clean_up_tokenization_spaces=False,
130
+ )
131
+ )
132
+ normal.clear()
133
+
134
+ def flush_bytes():
135
+ if byte_buffer:
136
+ output.append(
137
+ byte_buffer.decode("utf-8", errors="replace")
138
+ )
139
+ byte_buffer.clear()
140
+
141
+ for token_id_raw in token_ids:
142
+ token_id = int(token_id_raw)
143
+ byte_value = self.byte_id_to_value.get(token_id)
144
+
145
+ if byte_value is None:
146
+ flush_bytes()
147
+ normal.append(token_id)
148
+ else:
149
+ flush_normal()
150
+ byte_buffer.append(byte_value)
151
+
152
+ flush_bytes()
153
+ flush_normal()
154
+ return "".join(output)
hybrid_tokenizer_config.json ADDED
@@ -0,0 +1,301 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "base_model": "appvoid/void.0",
3
+ "byte_token_template": "<0x{:02X}>",
4
+ "byte_tokens": [
5
+ "<0x00>",
6
+ "<0x01>",
7
+ "<0x02>",
8
+ "<0x03>",
9
+ "<0x04>",
10
+ "<0x05>",
11
+ "<0x06>",
12
+ "<0x07>",
13
+ "<0x08>",
14
+ "<0x09>",
15
+ "<0x0A>",
16
+ "<0x0B>",
17
+ "<0x0C>",
18
+ "<0x0D>",
19
+ "<0x0E>",
20
+ "<0x0F>",
21
+ "<0x10>",
22
+ "<0x11>",
23
+ "<0x12>",
24
+ "<0x13>",
25
+ "<0x14>",
26
+ "<0x15>",
27
+ "<0x16>",
28
+ "<0x17>",
29
+ "<0x18>",
30
+ "<0x19>",
31
+ "<0x1A>",
32
+ "<0x1B>",
33
+ "<0x1C>",
34
+ "<0x1D>",
35
+ "<0x1E>",
36
+ "<0x1F>",
37
+ "<0x20>",
38
+ "<0x21>",
39
+ "<0x22>",
40
+ "<0x23>",
41
+ "<0x24>",
42
+ "<0x25>",
43
+ "<0x26>",
44
+ "<0x27>",
45
+ "<0x28>",
46
+ "<0x29>",
47
+ "<0x2A>",
48
+ "<0x2B>",
49
+ "<0x2C>",
50
+ "<0x2D>",
51
+ "<0x2E>",
52
+ "<0x2F>",
53
+ "<0x30>",
54
+ "<0x31>",
55
+ "<0x32>",
56
+ "<0x33>",
57
+ "<0x34>",
58
+ "<0x35>",
59
+ "<0x36>",
60
+ "<0x37>",
61
+ "<0x38>",
62
+ "<0x39>",
63
+ "<0x3A>",
64
+ "<0x3B>",
65
+ "<0x3C>",
66
+ "<0x3D>",
67
+ "<0x3E>",
68
+ "<0x3F>",
69
+ "<0x40>",
70
+ "<0x41>",
71
+ "<0x42>",
72
+ "<0x43>",
73
+ "<0x44>",
74
+ "<0x45>",
75
+ "<0x46>",
76
+ "<0x47>",
77
+ "<0x48>",
78
+ "<0x49>",
79
+ "<0x4A>",
80
+ "<0x4B>",
81
+ "<0x4C>",
82
+ "<0x4D>",
83
+ "<0x4E>",
84
+ "<0x4F>",
85
+ "<0x50>",
86
+ "<0x51>",
87
+ "<0x52>",
88
+ "<0x53>",
89
+ "<0x54>",
90
+ "<0x55>",
91
+ "<0x56>",
92
+ "<0x57>",
93
+ "<0x58>",
94
+ "<0x59>",
95
+ "<0x5A>",
96
+ "<0x5B>",
97
+ "<0x5C>",
98
+ "<0x5D>",
99
+ "<0x5E>",
100
+ "<0x5F>",
101
+ "<0x60>",
102
+ "<0x61>",
103
+ "<0x62>",
104
+ "<0x63>",
105
+ "<0x64>",
106
+ "<0x65>",
107
+ "<0x66>",
108
+ "<0x67>",
109
+ "<0x68>",
110
+ "<0x69>",
111
+ "<0x6A>",
112
+ "<0x6B>",
113
+ "<0x6C>",
114
+ "<0x6D>",
115
+ "<0x6E>",
116
+ "<0x6F>",
117
+ "<0x70>",
118
+ "<0x71>",
119
+ "<0x72>",
120
+ "<0x73>",
121
+ "<0x74>",
122
+ "<0x75>",
123
+ "<0x76>",
124
+ "<0x77>",
125
+ "<0x78>",
126
+ "<0x79>",
127
+ "<0x7A>",
128
+ "<0x7B>",
129
+ "<0x7C>",
130
+ "<0x7D>",
131
+ "<0x7E>",
132
+ "<0x7F>",
133
+ "<0x80>",
134
+ "<0x81>",
135
+ "<0x82>",
136
+ "<0x83>",
137
+ "<0x84>",
138
+ "<0x85>",
139
+ "<0x86>",
140
+ "<0x87>",
141
+ "<0x88>",
142
+ "<0x89>",
143
+ "<0x8A>",
144
+ "<0x8B>",
145
+ "<0x8C>",
146
+ "<0x8D>",
147
+ "<0x8E>",
148
+ "<0x8F>",
149
+ "<0x90>",
150
+ "<0x91>",
151
+ "<0x92>",
152
+ "<0x93>",
153
+ "<0x94>",
154
+ "<0x95>",
155
+ "<0x96>",
156
+ "<0x97>",
157
+ "<0x98>",
158
+ "<0x99>",
159
+ "<0x9A>",
160
+ "<0x9B>",
161
+ "<0x9C>",
162
+ "<0x9D>",
163
+ "<0x9E>",
164
+ "<0x9F>",
165
+ "<0xA0>",
166
+ "<0xA1>",
167
+ "<0xA2>",
168
+ "<0xA3>",
169
+ "<0xA4>",
170
+ "<0xA5>",
171
+ "<0xA6>",
172
+ "<0xA7>",
173
+ "<0xA8>",
174
+ "<0xA9>",
175
+ "<0xAA>",
176
+ "<0xAB>",
177
+ "<0xAC>",
178
+ "<0xAD>",
179
+ "<0xAE>",
180
+ "<0xAF>",
181
+ "<0xB0>",
182
+ "<0xB1>",
183
+ "<0xB2>",
184
+ "<0xB3>",
185
+ "<0xB4>",
186
+ "<0xB5>",
187
+ "<0xB6>",
188
+ "<0xB7>",
189
+ "<0xB8>",
190
+ "<0xB9>",
191
+ "<0xBA>",
192
+ "<0xBB>",
193
+ "<0xBC>",
194
+ "<0xBD>",
195
+ "<0xBE>",
196
+ "<0xBF>",
197
+ "<0xC0>",
198
+ "<0xC1>",
199
+ "<0xC2>",
200
+ "<0xC3>",
201
+ "<0xC4>",
202
+ "<0xC5>",
203
+ "<0xC6>",
204
+ "<0xC7>",
205
+ "<0xC8>",
206
+ "<0xC9>",
207
+ "<0xCA>",
208
+ "<0xCB>",
209
+ "<0xCC>",
210
+ "<0xCD>",
211
+ "<0xCE>",
212
+ "<0xCF>",
213
+ "<0xD0>",
214
+ "<0xD1>",
215
+ "<0xD2>",
216
+ "<0xD3>",
217
+ "<0xD4>",
218
+ "<0xD5>",
219
+ "<0xD6>",
220
+ "<0xD7>",
221
+ "<0xD8>",
222
+ "<0xD9>",
223
+ "<0xDA>",
224
+ "<0xDB>",
225
+ "<0xDC>",
226
+ "<0xDD>",
227
+ "<0xDE>",
228
+ "<0xDF>",
229
+ "<0xE0>",
230
+ "<0xE1>",
231
+ "<0xE2>",
232
+ "<0xE3>",
233
+ "<0xE4>",
234
+ "<0xE5>",
235
+ "<0xE6>",
236
+ "<0xE7>",
237
+ "<0xE8>",
238
+ "<0xE9>",
239
+ "<0xEA>",
240
+ "<0xEB>",
241
+ "<0xEC>",
242
+ "<0xED>",
243
+ "<0xEE>",
244
+ "<0xEF>",
245
+ "<0xF0>",
246
+ "<0xF1>",
247
+ "<0xF2>",
248
+ "<0xF3>",
249
+ "<0xF4>",
250
+ "<0xF5>",
251
+ "<0xF6>",
252
+ "<0xF7>",
253
+ "<0xF8>",
254
+ "<0xF9>",
255
+ "<0xFA>",
256
+ "<0xFB>",
257
+ "<0xFC>",
258
+ "<0xFD>",
259
+ "<0xFE>",
260
+ "<0xFF>"
261
+ ],
262
+ "number_token_max": 999,
263
+ "number_word_tokens": [
264
+ "zero",
265
+ "one",
266
+ "two",
267
+ "three",
268
+ "four",
269
+ "five",
270
+ "six",
271
+ "seven",
272
+ "eight",
273
+ "nine",
274
+ "ten",
275
+ "eleven",
276
+ "twelve",
277
+ "thirteen",
278
+ "fourteen",
279
+ "fifteen",
280
+ "sixteen",
281
+ "seventeen",
282
+ "eighteen",
283
+ "nineteen",
284
+ "twenty",
285
+ "thirty",
286
+ "forty",
287
+ "fifty",
288
+ "sixty",
289
+ "seventy",
290
+ "eighty",
291
+ "ninety",
292
+ "hundred",
293
+ "thousand",
294
+ "million",
295
+ "billion",
296
+ "trillion"
297
+ ],
298
+ "rewrite_layout": "instruction\\n\\n\"text bytes\"\\n\\n\"output bytes\"",
299
+ "separator": "\n\n",
300
+ "max_context_length": 4096
301
+ }