Upload train.py
Browse files
train.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from tokenizers import (
|
3 |
+
decoders,
|
4 |
+
models,
|
5 |
+
normalizers,
|
6 |
+
pre_tokenizers,
|
7 |
+
processors,
|
8 |
+
trainers,
|
9 |
+
Tokenizer,
|
10 |
+
Regex,
|
11 |
+
)
|
12 |
+
from transformers import PreTrainedTokenizerFast, PreTrainedTokenizerBase
|
13 |
+
from tqdm import tqdm
|
14 |
+
|
15 |
+
dataset = load_dataset(
|
16 |
+
"parquet", data_dir="Mxode/IndustryCorpus-Subset-zh-en", split="train")
|
17 |
+
dataset = dataset.shuffle(seed=3407)
|
18 |
+
|
19 |
+
ds = dataset[:1000000]
|
20 |
+
ds_val = dataset[-10000:]
|
21 |
+
char_len = sum(len(x) for x in ds_val['text'])
|
22 |
+
|
23 |
+
|
24 |
+
def get_training_corpus():
|
25 |
+
for i in range(0, len(ds), 1000):
|
26 |
+
yield ds["text"][i: i + 1000]
|
27 |
+
|
28 |
+
|
29 |
+
def train():
|
30 |
+
tokenizer = Tokenizer(models.BPE())
|
31 |
+
tokenizer.normalizer = normalizers.NFC()
|
32 |
+
tokenizer.pre_tokenizer = pre_tokenizers.Sequence([
|
33 |
+
pre_tokenizers.Split(
|
34 |
+
pattern=Regex(
|
35 |
+
"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+"),
|
36 |
+
behavior="isolated",
|
37 |
+
invert=False,
|
38 |
+
),
|
39 |
+
pre_tokenizers.ByteLevel(
|
40 |
+
add_prefix_space=False,
|
41 |
+
use_regex=False,
|
42 |
+
trim_offsets=False
|
43 |
+
)
|
44 |
+
])
|
45 |
+
trainer = trainers.BpeTrainer(
|
46 |
+
vocab_size=16000,
|
47 |
+
special_tokens=["<|endoftext|>", "<|im_start|>", "<|im_end|>"]
|
48 |
+
)
|
49 |
+
tokenizer.train_from_iterator(get_training_corpus(), trainer=trainer)
|
50 |
+
tokenizer.post_processor = processors.ByteLevel(
|
51 |
+
add_prefix_space=False,
|
52 |
+
use_regex=False,
|
53 |
+
trim_offsets=False
|
54 |
+
)
|
55 |
+
tokenizer.decoder = decoders.ByteLevel(
|
56 |
+
add_prefix_space=False,
|
57 |
+
use_regex=False,
|
58 |
+
trim_offsets=False
|
59 |
+
)
|
60 |
+
wrapped_tokenizer = PreTrainedTokenizerFast(
|
61 |
+
tokenizer_object=tokenizer,
|
62 |
+
bos_token="<|endoftext|>",
|
63 |
+
eos_token="<|im_end|>",
|
64 |
+
pad_token="<|endoftext|>",
|
65 |
+
model_max_length=4096,
|
66 |
+
clean_up_tokenization_spaces=False,
|
67 |
+
errors="replace",
|
68 |
+
split_special_tokens=False,
|
69 |
+
)
|
70 |
+
wrapped_tokenizer.chat_template = """{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' }}{% endif %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"""
|
71 |
+
wrapped_tokenizer.save_pretrained(
|
72 |
+
'Mxode/Bilingual-Tokenizer/BilingualTokenizer-16K')
|
73 |
+
return wrapped_tokenizer
|
74 |
+
|
75 |
+
|
76 |
+
def eval(tokenizer: PreTrainedTokenizerBase):
|
77 |
+
def get_compress_len(tokenizer):
|
78 |
+
return sum(len(tokenizer(x, return_tensors=None)['input_ids']) for x in tqdm(ds_val['text']))
|
79 |
+
|
80 |
+
compress_len = get_compress_len(tokenizer)
|
81 |
+
compression_rate = compress_len / char_len * 100
|
82 |
+
print(f'{len(tokenizer):<40} {compression_rate:.2f}%')
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
tokenizer = train()
|
87 |
+
eval(tokenizer)
|