File size: 5,353 Bytes
9eb0b88 c1e1c58 b8fc4a8 9eb0b88 1fa47c0 9eb0b88 1fa47c0 9eb0b88 c1e1c58 9eb0b88 c1e1c58 9eb0b88 c1e1c58 9eb0b88 c1e1c58 b8fc4a8 c1e1c58 9eb0b88 c1e1c58 9eb0b88 |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
from transformers import PreTrainedTokenizerFast
from tokenizers import Tokenizer, normalizers, pre_tokenizers, processors, decoders
from tokenizers.models import BPE
from tokenizers.trainers import BpeTrainer
from utils import batch_text_iterator
from pretrain_datasets import tokenizer_datasets
#
# special_tokens
#
bos_token = '<|begin_of_text|>'
eos_token = '<|end_of_text|>'
special_tokens = [
bos_token,
eos_token,
'<|start_header_id|>',
'<|end_header_id|>',
'<|eom_id|>',
'<|eot_id|>',
'system',
'user',
'assistant',
# tool/function calling
'<tools>',
'</tools>',
'<tool>',
'</tool>',
'<tool_call>',
'</tool_call>',
'<tool_response>',
'</tool_response>',
'"name"',
'"arguments"',
#
# JSON Schema
#
# General Metadata Keywords
'"$schema"',
'"$id"',
'"$ref"',
'"$defs"',
'"$anchor"',
'"$dynamicAnchor"',
'"$dynamicRef"',
'"$vocabulary"',
'"$comment"',
# Data Types
'"null"',
'"boolean"',
'"object"',
'"array"',
'"number"',
'"string"',
'"integer"',
# Validation Keywords
'"type"',
'"enum"',
'"const"',
'"multipleOf"',
'"maximum"',
'"exclusiveMaximum"',
'"minimum"',
'"exclusiveMinimum"',
'"maxLength"',
'"minLength"',
'"pattern"',
'"additionalItems"',
'"items"',
'"prefixItems"',
'"contains"',
'"maxItems"',
'"minItems"',
'"uniqueItems"',
'"maxProperties"',
'"minProperties"',
'"required"',
'"properties"',
'"patternProperties"',
'"additionalProperties"',
'"dependentRequired"',
'"dependentSchemas"',
'"propertyNames"',
# Conditional Keywords
'"if"',
'"then"',
'"else"',
'"allOf"',
'"anyOf"',
'"oneOf"',
'"not"',
# Additional Keywords for Evaluation Control
'"unevaluatedItems"',
'"unevaluatedProperties"',
# Informational Keywords
'"title"',
'"description"',
'"default"',
'"deprecated"',
'"readOnly"',
'"writeOnly"',
'"examples"',
# Content-Related Keywords
'"contentEncoding"',
'"contentMediaType"',
'"contentSchema"',
# Additional Keywords
'"next"', # Typically used in reference to linked or next items
'"value"', # Represents the value of a property or item
# misc
'<input>',
'</input>',
'<output>',
'</output>',
'<query>',
'</query>',
'<key>',
'</key>',
'<value>',
'</value>',
'<text>',
'</text>',
'<code>',
'</code>',
'<image>',
'</image>',
'<file>',
'</file>',
# qa
'<question>',
'</question>',
'<answer>',
'</answer>',
# thought
'<thought>',
'</thought>',
'<plan>',
'</plan>',
'<vote>',
'</vote>',
'<passage>',
'</passage>',
# reasoning
'<reasoning>',
'</reasoning>',
'<acting>',
'</acting>',
'<action>',
'</action>',
'<observation>',
'</observation>',
'<claim>',
'</claim>',
# reflection
'<thinking>',
'</thinking>',
'<reflection>',
'</reflection>',
'<step>',
'</step>',
# graph
'<graph>',
'</graph>',
'<edge>',
'</edge>',
'<source>',
'</source>',
'<destination>',
'</destination>',
'<relation>',
'</relation>',
# '<value>',
# '</value>',
]
# for i in range(2, 25):
# special_tokens.append(' ' * i)
# for i in range(2, 25):
# special_tokens.append('\t' * i)
# for i in range(2, 25):
# special_tokens.append('\n' * i)
# for i in range(2, 25):
# special_tokens.append('\r' * i)
# for i in range(2, 25):
# special_tokens.append('\r\n' * i)
for i in range(256):
special_tokens.append(f'<0x{i:02X}>')
for i in range(64):
special_tokens.append(f'<|reserved_special_token_{i}|>')
#
# BPE Tokenizer
#
bpe = BPE(unk_token=None, byte_fallback=True)
tokenizer = Tokenizer(bpe)
# normalizer
tokenizer.normalizer = None
# pre-tokenizer
tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel(add_prefix_space=False, trim_offsets=True, use_regex=True)
# post-processor
tokenizer.post_processor = processors.ByteLevel(add_prefix_space=True, trim_offsets=False, use_regex=True)
# decoder
tokenizer.decoder = decoders.ByteLevel(add_prefix_space=True, trim_offsets=True, use_regex=True)
#
# BPE Trainer
#
trainer = BpeTrainer(
vocab_size=65536, # 64 * 1024
min_frequency=3,
special_tokens=special_tokens,
max_token_length=24,
)
tokenizer.train_from_iterator(
batch_text_iterator(tokenizer_datasets),
trainer,
)
tokenizer.save('../tokenizer.json')
tokenizer.model.save('../')
#
# PreTrainedTokenizerFast
#
CHAT_TEMPLATE = (
"{{ bos_token }}"
"{% for message in messages %}"
"{{'<|start_header_id|>' + message['role'] + '<|end_header_id|>' + message['content'] + '<|eot_id|>'}}"
"{% endfor %}"
"{% if add_generation_prompt %}"
"{{ '<|start_header_id|>assistant<|end_header_id|>' }}"
"{% else %}"
"{{ eos_token }}"
"{% endif %}"
)
fast_tokenizer = PreTrainedTokenizerFast(
tokenizer_object=tokenizer,
chat_template=CHAT_TEMPLATE,
bos_token=bos_token,
eos_token=eos_token,
clean_up_tokenization_spaces=False,
)
fast_tokenizer.save_pretrained('../')
|