-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenizer_utils.py
172 lines (130 loc) · 5.19 KB
/
tokenizer_utils.py
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
import json
import logging
import pathlib
import torch
from transformers import AutoTokenizer, BartTokenizer
from tokenizers.processors import BertProcessing
from tokenizers import ByteLevelBPETokenizer, decoders
logging.basicConfig(level=logging.INFO)
def create_tokenizer(return_pretokenized, path, tokenizer_type: str = "word-level", tokenizer_ckpt: str = None):
if return_pretokenized:
print(f'*****use pretrained tokenizer*****{tokenizer_ckpt}*****')
if "bart" in tokenizer_ckpt:
tokenizer = BartTokenizer.from_pretrained(tokenizer_ckpt)
else:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_ckpt)
return tokenizer
if tokenizer_type == "byte-level":
return read_byte_level(path)
elif tokenizer_type == "word-level":
return read_word_level(path)
else:
raise ValueError(f"Invalid tokenizer type: {tokenizer_type}")
def train_bytelevel(
path, #list
save_path,
vocab_size=10000,
min_frequency=1,
special_tokens=["<s>", "<pad>", "</s>", "<unk>", "<mask>"],
):
tokenizer = ByteLevelBPETokenizer()
# Customize training
tokenizer.train(
files=path,
vocab_size=vocab_size,
min_frequency=min_frequency,
special_tokens=special_tokens,
)
tokenizer.save_model(str(pathlib.Path(save_path)))
def read_byte_level(path: str):
tokenizer = ByteLevelBPETokenizer(
f"{path}/vocab.json",
f"{path}/merges.txt",
)
tokenizer._tokenizer.post_processor = BertProcessing(
("</s>", tokenizer.token_to_id("</s>")),
("<s>", tokenizer.token_to_id("<s>")),
)
tokenizer.enable_truncation(max_length=512)
with open(f"{path}/vocab.json", "r") as fin:
vocab = json.load(fin)
# add length method to tokenizer object
tokenizer.vocab_size = len(vocab)
# add length property to tokenizer object
tokenizer.__len__ = property(lambda self: self.vocab_size)
tokenizer.decoder = decoders.ByteLevel()
print(tokenizer.vocab_size)
print(
tokenizer.encode(
"Bores can be divided into two classes; those who have their own particular subject, and those who do not need a subject."
).ids
)
print(
tokenizer.decode(
tokenizer.encode(
"Bores can be divided into two classes; those who have their own particular subject, and those who do not need a subject."
).ids,
skip_special_tokens=True,
)
)
ids = tokenizer.encode(
"Bores can be divided into two classes; those who have their own particular subject, and those who do not need a subject."
).ids
tensor = torch.tensor(ids)
print(tokenizer.decode(tensor.tolist(), skip_special_tokens=True))
print(f"Vocab size: {tokenizer.vocab_size}")
return tokenizer
def read_word_level(path: str):
from transformers import PreTrainedTokenizerFast
logging.info(f"Loading tokenizer from {path}/word-level-vocab.json")
tokenizer = PreTrainedTokenizerFast(
tokenizer_file=f"{str(pathlib.Path(path))}/word-level-vocab.json",
bos_token="[CLS]",
eos_token="[SEP]",
unk_token="[UNK]",
sep_token="[SEP]",
pad_token="[PAD]",
cls_token="[CLS]",
mask_token="[MASK]",
padding_side="right",
)
# add length property to tokenizer object
tokenizer.__len__ = property(lambda self: self.vocab_size)
return tokenizer
def train_word_level_tokenizer(
path: str,
vocab_size: int = 10000,
special_tokens=["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"],
):
from tokenizers import Tokenizer, normalizers, pre_tokenizers
from tokenizers.models import WordLevel
from tokenizers.normalizers import NFD, Lowercase, StripAccents
from tokenizers.pre_tokenizers import Digits, Whitespace
from tokenizers.processors import TemplateProcessing
from tokenizers.trainers import WordLevelTrainer
tokenizer = Tokenizer(WordLevel(unk_token="[UNK]"))
tokenizer.normalizer = normalizers.Sequence([NFD(), Lowercase(), StripAccents()])
tokenizer.pre_tokenizer = pre_tokenizers.Sequence(
[Digits(individual_digits=True), Whitespace()]
)
tokenizer.post_processor = TemplateProcessing(
single="[CLS] $A [SEP]", special_tokens=[("[CLS]", 1), ("[SEP]", 2)]
)
trainer = WordLevelTrainer(vocab_size=vocab_size, special_tokens=special_tokens)
tokenizer.train(files=[path], trainer=trainer)
tokenizer.__len__ = property(lambda self: self.vocab_size)
tokenizer.enable_truncation(max_length=512)
print(tokenizer.encode("the red.").ids)
print(tokenizer.encode("the red."))
tokenizer.save(f"{str(pathlib.Path(path).parent)}/word-level-vocab.json")
if __name__ == "__main__":
import sys
import os
if sys.argv[1] == "train-word-level":
train_word_level_tokenizer(path=sys.argv[2])
elif sys.argv[1] == "train-byte-level":
path = f"./data/{sys.argv[2]}/"
data_path = [path + item for item in os.listdir(path) if 'train' in item]
train_bytelevel(path=data_path, vocab_size=int(sys.argv[3])+5, save_path=path)
elif sys.argv[1] == "create":
create_tokenizer(path=sys.argv[2])