-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorizer.py
351 lines (264 loc) · 13.1 KB
/
factorizer.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from functools import lru_cache
from typing import Union, NamedTuple, List, Tuple
import torch
import numpy as np
import dawg
class Encoding(NamedTuple):
ids: List[Tuple[int, int, int]]
tokens: List[str]
perplexities: List[float]
offsets: List[Tuple[int, int]]
def whitespace_split(line):
words = line.split()
index = line.index
offsets = []
append = offsets.append
running_offset = 0
for word in words:
word_offset = index(word, running_offset)
running_offset = word_offset + len(word)
append((word, word_offset, running_offset))
return offsets
class Node:
def __init__(self, suffix, cost, predecessor, indices):
self.suffix = suffix
self.cost = cost
self.predecessor = predecessor
self.indices = indices
self.is_open = True
@lru_cache(maxsize=1024)
def char_to_bytes(ch: str):
encoded = ch.encode("utf-8")
encoded = [c for ch in encoded for c in ([0xC0 | (ch >> 3), 0x80 | (ch & 0x07)] if ch >= 0x80 else [ch])]
decoded = bytes(encoded).decode("utf-8", errors="strict")
return decoded
@lru_cache(maxsize=1024)
def bytes_to_subword(b: str):
encoded = b.encode("utf-8")
encoded = [
c if c < 0x80 else ((encoded[i - 1] << 3) & 0xFF) | (c & 0x07)
for i, c in enumerate(encoded)
if c < 0xC0
]
decoded = bytes(encoded).decode("utf-8", errors="strict")
return decoded
def optimal_search(trie, word, alpha=0.1, sigma=0.0, add_bow=True, add_eow=True, sample=False):
alignment, encoded_word = [], []
if add_bow:
w = char_to_bytes('⸥')
encoded_word.append(w)
alignment += [0] * len(w)
for i, ch in enumerate(word):
w = char_to_bytes(ch)
encoded_word.append(w)
alignment += [i] * len(w)
if add_eow:
w = char_to_bytes('⸤')
encoded_word.append(w)
alignment += [len(word) - 1] * len(w)
alignment += [len(word)]
encoded_word = ''.join(encoded_word)
if sigma == 0.0:
return dijkstra(trie, encoded_word, alignment, alpha)
for _ in range(16):
encoding = dijkstra_sigma(trie, encoded_word, alignment, alpha, sigma, sample=sample)
if encoding[1][0].startswith("⸥") and encoding[1][-1].endswith("⸤"):
return encoding
return dijkstra(trie, word, alignment, alpha)
class OOVException(Exception):
pass
def dijkstra_sigma(trie, word, alignment, alpha, sigma, sample: True):
initial_node = Node(word, 0.0, None, None)
open_nodes = [initial_node]
all_nodes = {initial_node.suffix: initial_node}
while len(open_nodes) > 0:
min_node = open_nodes.pop(0)
min_node.is_open = False
if len(min_node.suffix) == 0:
break
if '' in all_nodes and min_node.cost + alpha >= all_nodes[''].cost:
min_node = all_nodes['']
break
for prefix in trie.prefixes(min_node.suffix)[::-1]:
noise = sigma * len(prefix) * (torch.randn([]).exp().item())
suffix = min_node.suffix[len(prefix):]
if suffix != '' and '' in all_nodes and min_node.cost + noise + 2*alpha >= all_nodes[''].cost:
continue
if suffix in all_nodes:
old_node = all_nodes[suffix]
if min_node.cost + noise + alpha >= old_node.cost:
continue
values = trie.get_value(prefix)
perplexity, *indices = max(values, key=lambda item: item[0])
if min_node.cost - perplexity + noise + alpha >= old_node.cost:
continue
if suffix != '' and '' in all_nodes and min_node.cost - perplexity + noise + 2*alpha >= all_nodes[''].cost:
continue
else:
values = trie.get_value(prefix)
perplexity, *indices = max(values, key=lambda item: item[0])
if sample and len(values) > 1:
perplexities = [p for p, *_ in values]
probs = 1.0 / (torch.tensor(perplexities) - 1e-12)
probs = probs / probs.sum()
index = probs.multinomial(1).item()
perplexity, *indices = values[index]
cost = min_node.cost + alpha - perplexity + noise
if suffix != '' and '' in all_nodes and cost + alpha >= all_nodes[''].cost:
continue
if suffix not in all_nodes:
new_node = Node(suffix, cost, min_node, indices)
open_nodes.append(new_node)
all_nodes[suffix] = new_node
elif cost < old_node.cost:
assert old_node.is_open
old_node.cost, old_node.predecessor, old_node.indices = cost, min_node, indices
open_nodes = sorted(open_nodes, key=lambda node: node.cost)
if len(min_node.suffix) != 0:
raise OOVException()
indices, subwords, perplexities, offsets, offset = [], [], [], [], 1
node = min_node
while node.predecessor is not None:
indices.append(node.indices)
perplexities.append(node.cost)
subword = node.predecessor.suffix[:len(node.predecessor.suffix)-len(node.suffix)]
offsets.append((alignment[-(offset + len(subword))], alignment[-offset]))
offset += len(subword)
try:
subword = bytes_to_subword(subword)
except:
pass
subwords.append(subword)
node = node.predecessor
return indices[::-1], subwords[::-1], perplexities[::-1], offsets[::-1]
def dijkstra(trie, word, alignment, alpha):
initial_node = Node(word, 0.0, None, None)
open_nodes = [initial_node]
all_nodes = {initial_node.suffix: initial_node}
while len(open_nodes) > 0:
min_node = open_nodes.pop(0)
min_node.is_open = False
if len(min_node.suffix) == 0:
break
if '' in all_nodes and min_node.cost + alpha >= all_nodes[''].cost:
min_node = all_nodes['']
break
for prefix in trie.prefixes(min_node.suffix)[::-1]:
perplexity, *indices = max(trie.get_value(prefix), key=lambda item: item[0])
# perplexity, *indices = trie.get_value(prefix)[0]
suffix = min_node.suffix[len(prefix):]
cost = min_node.cost + alpha - perplexity
if suffix != '' and '' in all_nodes and cost + alpha >= all_nodes[''].cost:
continue
if suffix not in all_nodes:
new_node = Node(suffix, cost, min_node, indices)
open_nodes.append(new_node)
all_nodes[suffix] = new_node
elif cost < (old_node := all_nodes[suffix]).cost:
assert old_node.is_open
old_node.cost, old_node.predecessor, old_node.indices = cost, min_node, indices
open_nodes = sorted(open_nodes, key=lambda node: node.cost)
if len(min_node.suffix) != 0:
raise OOVException()
indices, subwords, perplexities, offsets, offset = [], [], [], [], 1
node = min_node
while node.predecessor is not None:
indices.append(node.indices)
perplexities.append(node.cost)
subword = node.predecessor.suffix[:len(node.predecessor.suffix)-len(node.suffix)]
offsets.append((alignment[-(offset + len(subword))], alignment[-offset]))
offset += len(subword)
try:
subword = bytes_to_subword(subword)
except:
pass
subwords.append(subword)
node = node.predecessor
return indices[::-1], subwords[::-1], perplexities[::-1], offsets[::-1]
class Factorizer:
def __init__(self, tokenizer_path: str, alpha=0.1, sigma=0.0, merge_unks=True, allow_decoding=False, sample=False):
self.special_id_to_token = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "[SOS]", "[EOS]", "[SPECIAL]"]
self.special_token_to_id = {token: i for i, token in enumerate(self.special_id_to_token)}
self.unk_id = (self.special_token_to_id["[UNK]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.cls_id = (self.special_token_to_id["[CLS]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.sep_id = (self.special_token_to_id["[SEP]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.pad_id = (self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.mask_id = (self.special_token_to_id["[MASK]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.sos_id = (self.special_token_to_id["[SOS]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.eos_id = (self.special_token_to_id["[EOS]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.special_id = (self.special_token_to_id["[SPECIAL]"], self.special_token_to_id["[PAD]"], self.special_token_to_id["[PAD]"])
self.n_special_tokens = len(self.special_token_to_id)
self.vocab_size = 256 + self.n_special_tokens
self.trie = dawg.RecordDAWG("fBBB", payload_separator=b'\xff').load(tokenizer_path)
self.alpha = alpha
self.sigma = sigma
self.merge_unks = merge_unks
self.sample = sample
self.allow_decoding = allow_decoding
if allow_decoding:
self.load_inverse_vocab()
def __call__(self, text: Union[str, List[str]]) -> Union[Encoding, List[Encoding]]:
return self.encode(text)
def load_inverse_vocab(self):
self.id_to_subword = np.zeros((256, 256, 256), dtype=object)
for subword, (_, index_1, index_2, index_3) in self.trie.iteritems():
self.id_to_subword[index_1, index_2, index_3] = bytes_to_subword(subword)
def encode(self, text: Union[str, List[str]]) -> Union[Encoding, List[Encoding]]:
if isinstance(text, (list, tuple)):
return [self.encode(t) for t in text]
assert isinstance(text, str), f"Expected str, got {type(text)}"
ids, subwords, perplexities, offsets = [], [], [], []
for word, start, end in whitespace_split(text):
if self.sigma == 0.0:
output = self.tokenize_word_cached(word)
else:
output = self.tokenize_word(word)
ids += output.ids
subwords += output.tokens
perplexities += output.perplexities
offsets += [(start + start_, start + end_) for (start_, end_) in output.offsets]
if end != offsets[-1][1]:
print(f"ERROR in offseting {text} -> {' '.join(subwords)}", flush=True)
return Encoding(ids, subwords, perplexities, offsets)
def decode(self, indices: Union[List[Tuple[int, int, int]], List[List[Tuple[int, int, int]]]], skip_special_tokens=True) -> Union[str, List[str]]:
if not self.allow_decoding:
self.load_inverse_vocab()
self.allow_decoding = True
assert isinstance(indices, (list, tuple)), f"Expected list, got {type(indices)}"
assert isinstance(indices[0], (list, tuple)), f"Expected list of tuples, got list of {type(indices[0])}"
if isinstance(indices[0][0], (list, tuple)):
return [self.decode(index) for index in indices]
assert all(len(index) == 3 for index in indices), f"Expected list of tuples of length 3"
output = []
for index_1, index_2, index_3 in indices:
if index_1 < self.n_special_tokens or index_2 < self.n_special_tokens or index_3 < self.n_special_tokens:
if not skip_special_tokens:
subword = f" {self.special_id_to_token[index_1]} "
output.append(subword)
continue
subword = self.id_to_subword[index_1 - self.n_special_tokens, index_2 - self.n_special_tokens, index_3 - self.n_special_tokens]
if subword == 0:
output.append(" [UNK] ")
continue
subword = subword.replace("⸥", " ").replace("⸤", " ")
output.append(subword)
return ' '.join(''.join(output).split())
@lru_cache(maxsize=65536)
def tokenize_word_cached(self, word: str, add_bow=True, add_eow=True):
return self.tokenize_word(word, add_bow, add_eow)
def tokenize_word(self, word: str, add_bow=True, add_eow=True):
if word.lower() == "[unk]":
return Encoding([self.unk_id], [word], [float("-inf")], [(0, len(word))])
try:
result = optimal_search(self.trie, word, self.alpha, self.sigma, add_bow, add_eow, self.sample)
except OOVException:
return Encoding([self.unk_id], [word], [float("-inf")], [(0, len(word))])
ids, subwords, perplexities, offsets = result
for i, subword in enumerate(subwords):
ids[i] = (ids[i][0] + self.n_special_tokens, ids[i][1] + self.n_special_tokens, ids[i][2] + self.n_special_tokens)
if "[unk]" in subword.lower():
if self.merge_unks:
return Encoding([self.unk_id], ["[UNK]"], [float("-inf")], [(0, len(word))])
else:
ids[i] = self.unk_id
return Encoding(ids, subwords, perplexities, offsets)