-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tokenizer.cs
152 lines (127 loc) · 4.67 KB
/
Tokenizer.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
using System.Reflection.PortableExecutable;
using System.Text.Json;
using Microsoft.ML.Tokenizers;
public class TokenizeDecoder : Microsoft.ML.Tokenizers.TokenizerDecoder
{
private const char spaceReplacement = 'Ġ';
private const char newlineReplacement = 'Ċ';
private const char carriageReturnReplacement = 'č';
private string bos = "<s>";
private string eos = "</s>";
public TokenizeDecoder(string bos = "<s>", string eos = "</s>")
{
this.bos = bos;
this.eos = eos;
}
public override string Decode(IEnumerable<string> tokens)
{
var str = string.Join("", tokens);
str = str.Replace(spaceReplacement, ' ');
str = str.Replace(newlineReplacement, '\n');
str = str.Replace(carriageReturnReplacement.ToString(), Environment.NewLine);
if (str.StartsWith(bos))
{
str = str.Substring(bos.Length);
}
if (str.EndsWith(eos))
{
str = str.Substring(0, str.Length - eos.Length);
}
return str;
}
}
public class BPETokenizer
{
private Tokenizer tokenizer;
private bool addPrecedingSpace;
public BPETokenizer(
string vocabPath,
string mergesPath,
bool addPrecedingSpace,
string uknToken,
string bosToken,
string eosToken)
{
this.addPrecedingSpace = addPrecedingSpace;
var bpe = new Bpe(vocabPath, mergesPath, endOfWordSuffix: "</w>");
this.tokenizer = new Tokenizer(bpe);
this.BosId = this.tokenizer.Model.TokenToId(bosToken) ?? throw new Exception("Failed to get bos id");
this.EosId = this.tokenizer.Model.TokenToId(eosToken) ?? throw new Exception("Failed to get eos id");
var decoder = new TokenizeDecoder(this.tokenizer.Model.IdToToken(this.BosId)!, this.tokenizer.Model.IdToToken(this.EosId)!);
this.tokenizer.Decoder = decoder;
}
public static BPETokenizer FromPretrained(
string folder,
string vocabFile = "vocab.json",
string mergesFile = "merges.txt",
string specialTokensFile = "special_tokens_map.json",
bool addPrecedingSpace = false,
string uknToken = "<|endoftext|>",
string bosToken = "<|startoftext|>",
string eosToken = "<|endoftext|>")
{
var vocabPath = Path.Combine(folder, vocabFile);
var mergesPath = Path.Combine(folder, mergesFile);
var specialTokenMapPath = Path.Combine(folder, specialTokensFile);
Dictionary<string, string>? specialTokenMap = null;
// if (File.Exists(Path.Combine(folder, specialTokensFile)))
// {
// specialTokenMap = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(specialTokenMapPath)) ?? throw new Exception("Failed to load special token map");
// }
bosToken = specialTokenMap?.GetValueOrDefault("bos_token") ?? bosToken;
eosToken = specialTokenMap?.GetValueOrDefault("eos_token") ?? eosToken;
uknToken = specialTokenMap?.GetValueOrDefault("unk_token") ?? uknToken;
return new BPETokenizer(vocabPath, mergesPath, addPrecedingSpace, uknToken, bosToken, eosToken);
}
public int VocabSize => this.tokenizer.Model.GetVocabSize();
public int ModelMaxLength { get; } = 77;
public int PadId { get; }
public int BosId { get; }
public int EosId { get; }
public string Decode(int[] input)
{
var str = this.tokenizer.Decode(input) ?? throw new Exception("Failed to decode");
if (this.addPrecedingSpace)
{
str = str.TrimStart();
}
return str;
}
public int TokenToId(string token)
{
return this.tokenizer.Model.TokenToId(token) ?? throw new Exception("Failed to get token id");
}
public int[] Encode(
string input,
bool bos = false,
bool eos = false,
string? padding = null,
int? maxLength = null)
{
if (this.addPrecedingSpace)
{
input = " " + input;
}
var tokens = this.tokenizer.Encode(input).Ids.ToArray();
if (bos)
{
tokens = new int[] { this.BosId }.Concat(tokens).ToArray();
}
if (eos)
{
tokens = tokens.Concat(new int[] { this.EosId }).ToArray();
}
if (padding == "max_length" && maxLength is int maxLen)
{
if (tokens.Length > maxLen)
{
tokens = tokens.Take(maxLen).ToArray();
}
else if (tokens.Length < maxLen)
{
tokens = tokens.Concat(Enumerable.Repeat(this.PadId, maxLen - tokens.Length)).ToArray();
}
}
return tokens;
}
}