-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalignment.py
More file actions
168 lines (128 loc) · 6 KB
/
alignment.py
File metadata and controls
168 lines (128 loc) · 6 KB
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
import unicodedata
import multiprocessing
import json
import os
from tqdm import tqdm
from transformers import AutoTokenizer
import editdistance
class TokenMapper:
def __init__(self, draft_name, target_name) -> None:
self.tokenizer_draft = AutoTokenizer.from_pretrained(draft_name)
self.tokenizer_target = AutoTokenizer.from_pretrained(target_name)
self.draft_tag = (draft_name + "x" + target_name).replace("/", "_")
self.target_tag = (target_name + "x" + draft_name).replace("/", "_")
self.draft_index_mapping = self.align_token(self.tokenizer_draft, self.tokenizer_target, self.draft_tag)
self.draft_index_prob_map = self.align_prob(self.tokenizer_draft, self.tokenizer_target, self.draft_tag)
self.target_index_mapping = self.align_token(self.tokenizer_target, self.tokenizer_draft, self.target_tag)
def get_correspond(self, index: int) -> list:
index = str(index)
if index in self.draft_index_mapping:
return self.draft_index_mapping[index]
print("not found in draft", index)
return []
def get_reverse(self, index: int) -> list:
index = str(index)
if index in self.target_index_mapping:
return self.target_index_mapping[index]
print("not found in target", index)
return []
def normalize(self, text: str) -> str:
normalized_text = unicodedata.normalize('NFKD', text).lower()
return ''.join(char for char in normalized_text if char.isalnum())
def align_token(self, tokenizer_first: AutoTokenizer, tokenizer_second: AutoTokenizer, tag: str) -> dict:
print(tag + ".json")
if os.path.exists(tag + ".json"):
with open(tag + ".json", "r") as infile:
return json.load(infile)
vocab = tokenizer_first.get_vocab()
tokenizer_first_normalized = {}
for token, index in vocab.items():
tokenizer_first_normalized[(token, tokenizer_first.convert_tokens_to_string([token]))] = index
vocab = self.tokenizer_target.get_vocab()
tokenizer_second_normalized = {}
for token, index in vocab.items():
tokenizer_second_normalized[tokenizer_second.convert_tokens_to_string([token])] = index
first_to_second = {}
for (token, str_rep), index in tqdm(tokenizer_first_normalized.items(), desc="Aligning tokens", unit="token"):
# lst = self.get_best(str_rep, tokenizer_second_normalized)
lst = []
if len(lst) == 0:
first_to_second[str(index)] = tokenizer_second(str_rep)["input_ids"]
else:
word, num = zip(*lst)
first_to_second[str(index)] = list(num)
json_object = json.dumps(first_to_second, indent=4)
with open(tag + ".json", "w") as outfile:
outfile.write(json_object)
MAX_SPLIT = 10
statistics = {
"exact_match": 0,
"no_match": 0,
"split": {
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
MAX_SPLIT: 0
}
}
for index, lst in first_to_second.items():
if len(lst) == 0:
statistics["no_match"] += 1
elif len(lst) == 1:
statistics["exact_match"] += 1
else:
if len(lst) < MAX_SPLIT:
statistics["split"][len(lst)] += 1
else:
statistics["split"][MAX_SPLIT] += 1
json_object = json.dumps(statistics, indent=4)
with open("statistics.json", "w") as outfile:
outfile.write(json_object)
return first_to_second
def align_prob(self, tokenizer_first: AutoTokenizer, tokenizer_second: AutoTokenizer, tag: str) -> dict:
if os.path.exists(tag + "_prob.json"):
with open(tag + "_prob.json", "r") as infile:
return json.load(infile)
vocab = tokenizer_first.get_vocab()
tokenizer_first_normalized = {}
for token, index in vocab.items():
tokenizer_first_normalized[(token, tokenizer_first.convert_tokens_to_string([token]))] = index
vocab = self.tokenizer_target.get_vocab()
tokenizer_second_normalized = {}
for token, index in vocab.items():
tokenizer_second_normalized[tokenizer_second.convert_tokens_to_string([token])] = index
first_to_second = {}
for (token, str_rep), index in tqdm(tokenizer_first_normalized.items(), desc="Aligning tokens", unit="token"):
best = self.get_best(str_rep, tokenizer_second_normalized, tokenizer_second)
first_to_second[str(index)] = best
json_object = json.dumps(first_to_second, indent=4)
with open(tag + "_prob.json", "w") as outfile:
outfile.write(json_object)
return first_to_second
def get_best(self, cur_token: str, vocab: dict, tokenizer_second: AutoTokenizer) -> list:
best_score = 0
best_match = 0
for token, index in vocab.items():
cur_score = self.compute_score(tokenizer_second.convert_ids_to_tokens(index), cur_token)
if cur_score > best_score:
best_score = cur_score
best_match = index
return best_match
def compute_score(self, str1: str, str2: str) -> float:
return editdistance.eval(str1, str2)
# reawakening
# Daft: reawaken ing'
# Target: re awakening
# Add statistics for the alignment
# How many tokens found exact, match, how many split, how many didn't have match
# Want general matching: learn a mapping, take into account of byte-level sampling (letter by letter)
# Learn a mapping function, use histogram
# Neural network for generating mapping
# Read more about the token alignment
# Look at LLM Fusion, Zero-shot Tokenizer
# Look at papers that cited LLM Fusion