-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSimpleTokenizer.cs
316 lines (285 loc) · 10.7 KB
/
SimpleTokenizer.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SAMViewer
{
/// <summary>
/// BPE
/// </summary>
class SimpleTokenizer
{
static SimpleTokenizer theSingleton = null;
Dictionary<int, string> byte_encoder;
Dictionary<string, int> byte_decoder;
Dictionary<string, int> encoder = new Dictionary<string, int>();
Dictionary<int, string> decoder = new Dictionary<int, string>();
Dictionary<(string, string), int> bpe_ranks = new Dictionary<(string, string), int>();
Dictionary<string, string> cache = new Dictionary<string, string>();
System.Text.RegularExpressions.Regex pat;
int contextLength = 77;
public static SimpleTokenizer Instance()
{
if (null == theSingleton)
{
theSingleton = new SimpleTokenizer();
}
return theSingleton;
}
protected SimpleTokenizer()
{
this.Init();
}
/// <summary>
/// 初始化
/// </summary>
void Init()
{
this.byte_encoder = this.BytesToUnicode();
this.byte_decoder = this.byte_encoder.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
List<(string, string)> merges = LoadBPEMerges(default_bpe());//加载BPE
List<string> vocab = this.byte_encoder.Values.ToList();
foreach (var v in this.byte_encoder.Values.ToList())
{
vocab.Add(v + "</w>");
}
foreach ((string merge1, string merge2) in merges)
{
vocab.Add(merge1 + merge2);
}
vocab.AddRange(new List<string> { "<|startoftext|>", "<|endoftext|>" });
for (int i = 0; i < vocab.Count; i++)
{
this.encoder[vocab[i]] = i;
}
this.decoder = encoder.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
this.bpe_ranks = merges.Select((merge, index) => new { merge, index })
.ToDictionary(item => item.merge, item => item.index);
this.cache = new Dictionary<string, string>()
{
{ "<|startoftext|>","<|startoftext|>" },
{ "<|endoftext|>","<|endoftext|>" }
};
this.pat = new Regex(@" <\| startoftext\|>|<\| endoftext\|>| 's|'t | 're|'ve | 'm|'ll | 'd|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+", RegexOptions.IgnoreCase);
}
/// <summary>
/// 将字符串转换为Token
/// </summary>
/// <param name="textpromot"></param>
/// <returns></returns>
public List<Int64> tolikenlize(string textpromot)
{
int sot_token = this.encoder["<|startoftext|>"];
int eot_token = this.encoder["<|endoftext|>"];
List<string> texts = new List<string>() { textpromot };
List<Int64> allTokens = new List<Int64>();
foreach (string text in texts)
{
allTokens.Add(sot_token);
allTokens.AddRange(this.Encode(text));
allTokens.Add(eot_token);
}
if (allTokens.Count > contextLength)
{
allTokens.RemoveRange(contextLength, allTokens.Count - contextLength);
allTokens[contextLength-1] = eot_token;
}
else
{
Int64[] added = new Int64[contextLength - allTokens.Count];
allTokens.AddRange(added);
}
return allTokens;
}
/// <summary>
/// 对字符串进行编码
/// </summary>
List<Int64> Encode(string text)
{
List<Int64> bpeTokens = new List<Int64>();
text = this.whitespace_clean(this.basic_clean(text)).ToLower();
foreach (Match match in Regex.Matches(text, this.pat.ToString()))
{
string token = string.Join("", match.Value.Select(c => this.byte_encoder[c]));
string[] bpeTokenList = this.bpe(token).Split(' ');
foreach (string bpeToken in bpeTokenList)
{
bpeTokens.Add(this.encoder[bpeToken]);
}
}
return bpeTokens;
}
/// <summary>
/// 将tokens解码成字符串解码
/// </summary>
string Decode(List<int> tokens)
{
StringBuilder textBuilder = new StringBuilder();
foreach (int token in tokens)
{
textBuilder.Append(this.decoder[token]);
}
string text = textBuilder.ToString();
List<byte> byteList = new List<byte>();
foreach (char c in text)
{
byteList.Add((byte)this.byte_decoder[c.ToString()]);
}
byte[] byteArray = byteList.ToArray();
string decodedText = Encoding.UTF8.GetString(byteArray).Replace("</w>", " ");
return decodedText;
}
string bpe(string token)
{
if (cache.ContainsKey(token))
{
return cache[token];
}
List<string> word = new List<string>();
for (int i=0;i< token.Length-1;i++)
{
word.Add(token[i].ToString());
}
word.Add(token[token.Length - 1].ToString() + "</w>");
//Tuple<string, string> word = Tuple.Create( token[token.Length - 1] + "</w>", token.Substring(0, token.Length - 1));
HashSet<(string, string)> pairs = this.GetPairs(word);
if (pairs.Count == 0)
{
return token + "</w>";
}
while (true)
{
(string first, string second) = pairs.OrderBy(pair => bpe_ranks.ContainsKey(pair) ? bpe_ranks[pair] : double.PositiveInfinity).First();
if (!bpe_ranks.ContainsKey((first, second)))
{
break;
}
List<string> newWord = new List<string>();
int i = 0;
while (i < word.Count)
{
try
{
int j = word.IndexOf(first, i);
newWord.AddRange(word.GetRange(i, j - i));
i = j;
}
catch
{
newWord.AddRange(word.GetRange(i, word.Count- i));
break;
}
if (word[i] == first && i < word.Count - 1 && word[i + 1] == second)
{
newWord.Add(first + second);
i += 2;
}
else {
newWord.Add(word[i]);
i += 1;
}
}
word = newWord;
if (word.Count == 1)
break;
else
{
pairs = this.GetPairs(newWord);
}
}
string result = string.Join(" ", word);
cache[token] = result;
return result;
}
string default_bpe()
{
return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)+ "\\bpe_simple_vocab_16e6.txt";
}
List<(string, string)> LoadBPEMerges(string bpePath)
{
List<(string, string)> merges = new List<(string, string)>();
using (FileStream fileStream = File.OpenRead(bpePath))
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string content = streamReader.ReadToEnd();
string[] lines = content.Split('\n');
int startLine = 1;
int endLine = 49152 - 256 - 2 + 1;
ArraySegment<string> lineSegment = new ArraySegment<string>(lines, startLine, endLine - startLine);
foreach (string line in lineSegment)
{
string[] merge = line.Split();
merges.Add((merge[0], merge[1]));
}
}
return merges;
}
Dictionary<int, string> BytesToUnicode()
{
List<int> bs = new List<int>();
List<int> cs = new List<int>();
for (int b = (int)'!'; b <= (int)'~'; b++)
{
bs.Add(b);
cs.Add(b);
}
for (int b = (int)'¡'; b <= (int)'¬'; b++)
{
bs.Add(b);
cs.Add(b);
}
for (int b = (int)'®'; b <= (int)'ÿ'; b++)
{
bs.Add(b);
cs.Add(b);
}
int n = 0;
for (int b = 0; b < 256; b++)
{
if (!bs.Contains(b))
{
bs.Add(b);
cs.Add(256 + n);
n++;
}
}
Dictionary<int, string> byteToUnicode = new Dictionary<int, string>();
for (int i = 0; i < bs.Count; i++)
{
byteToUnicode.Add(bs[i], ((char)cs[i]).ToString());
}
return byteToUnicode;
}
HashSet<(string, string)> GetPairs(List<string> word)
{
HashSet<(string, string)> pairs = new HashSet<(string, string)>();
string prevChar = word[0];
for (int i = 1; i < word.Count; i++)
{
string currentChar = word[i].ToString();
pairs.Add((prevChar, currentChar));
prevChar = currentChar;
}
return pairs;
}
string HtmlDecode(string text)
{
// 还原 HTML 转义字符
return System.Net.WebUtility.HtmlDecode(text);
}
string basic_clean(string text)
{
text = HtmlDecode(text);
return text.Trim();
}
string whitespace_clean(string text)
{
text = System.Text.RegularExpressions.Regex.Replace(text, @"\s+", " ");
text = text.Trim();
return text;
}
}
}