Is your feature request related to a problem? Please describe.
I'm requesting this feature after trying to use the GPT2-style tokenizer I trained using HuggingFace in my .NET code. I had trained a model and converted the model to ONNX, but the tokenizer didn't transfer. An exact description of the problem is listed down below.
Describe the solution you'd like
Add support for a flag indicating that the tokenizer came from the HuggingFace BPE trainer, and behind the scenes handle the minor changes required.
Describe alternatives you've considered
Currently I have a class I wrote which wraps a BPE trainer and applies the adjustments before every call to the ML.NET BPE tokenizer.
Additional context
In the HuggingFace BPE code they have a dictionary bytes_to_unicode() which is list of utf-8 byte and a mapping to unicode strings. They run every byte in the string through the mapping before running the BPE encoder/decoder. Examples of where it's used can be found here and here and in other places.
Before the encoding, they treat the string as bytes and map all the bytes to representative unicode strings, and the same thing during after the decoding.
Real example:
I trained a BPE tokenizer using HuggingFace's tokenizers.ByteLevelBPETokenizer. The merges.txt and vocab.json can be found here: https://gist.github.com/shaltielshmid/58b7c1109639eefcd714eb6bfc3eb602.
Sample python code:
fromtransformersimportGPT2Tokenizertokenizer=GPT2Tokenizer.from_pretrained('/path/to/tokenizer')
print(tokenizer.encode('שלום וברכה')); // [150, 662, 426, 1396]
print(tokenizer.decode([150, 662, 426, 1396])); //שלוםוברכהSample C# code:
varbpe=newBpe("/path/to/vocab.json","/path/to/merges.txt");stringphrase="שלום וברכה";Console.WriteLine(string.Join(", ",bpe.Tokenize(phrase).Select(t =>t.Id.ToString())));// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0stringdecoded=Bpe.Decoder.Decode(newList<int>{150,662,426,1396}.Select(id =>bpe.IdToToken(id)!));Console.WriteLine(decoded);// ש׾×ķ×ĿĠ×ķ×ijר׼×Ķ// with proposed solution from down belowphrase=newstring(Encoding.UTF8.GetBytes(phrase).Select(b =>hf_encoder[b]).ToArray());Console.WriteLine(string.Join(", ",bpe.Tokenize(phrase).Select(t =>t.Id.ToString())));// 150, 662, 426, 1396decoded=Encoding.UTF8.GetString(decoded.Select(c =>(byte)hf_decoder[c]).ToArray());Console.WriteLine(decoded);// שלום וברכהProposed Solution
Create a static dictionary in the BPE class, which is initialized once:
varhf_encoder=newDictionary<int,char>();for(intc='!';c<='~';c++)hf_encoder.Add(c,(char)c);for(intc='¡';c<='¬';c++)hf_encoder.Add(c,(char)c);for(intc='®';c<='ÿ';c++)hf_encoder.Add(c,(char)c);intn=0;for(intc=0;c<256;c++){if(!hf_encoder.ContainsKey(c))hf_encoder.Add(c,(char)(256+n++));}varhf_decoder=hf_encoder.ToDictionary(kvp =>kvp.Value, kvp =>kvp.Key);Then, in the BPE.cs class in the Tokenize function here, add the following check:
if(_isHFFormat){sequence=newstring(Encoding.UTF8.GetBytes(sequence).Select(b =>hf_encoder[b]).ToArray())}And then in the BPEDecoder.cs file, in the Decode function here
stringret=string.Join("",tokens);if(_suffix!=null){ret=ret.Replace(_suffix," ");}if(_isHFFormat){ret=Encoding.UTF8.GetString(ret.Select(c =>(byte)hf_decoder[c]).ToArray())}returnret;Would be happy to compile this into a PR, if relevant.
@luisquintanilla
Is your feature request related to a problem? Please describe.
I'm requesting this feature after trying to use the GPT2-style tokenizer I trained using HuggingFace in my .NET code. I had trained a model and converted the model to ONNX, but the tokenizer didn't transfer. An exact description of the problem is listed down below.
Describe the solution you'd like
Add support for a flag indicating that the tokenizer came from the HuggingFace BPE trainer, and behind the scenes handle the minor changes required.
Describe alternatives you've considered
Currently I have a class I wrote which wraps a BPE trainer and applies the adjustments before every call to the ML.NET BPE tokenizer.
Additional context
In the HuggingFace BPE code they have a dictionary
bytes_to_unicode()which is list of utf-8 byte and a mapping to unicode strings. They run every byte in the string through the mapping before running the BPE encoder/decoder. Examples of where it's used can be found here and here and in other places.Before the encoding, they treat the string as bytes and map all the bytes to representative unicode strings, and the same thing during after the decoding.
Real example:
I trained a BPE tokenizer using HuggingFace's
tokenizers.ByteLevelBPETokenizer. The merges.txt and vocab.json can be found here: https://gist.github.com/shaltielshmid/58b7c1109639eefcd714eb6bfc3eb602.Sample python code:
Sample C# code:
Proposed Solution
Create a static dictionary in the BPE class, which is initialized once:
Then, in the BPE.cs class in the
Tokenizefunction here, add the following check:And then in the BPEDecoder.cs file, in the
Decodefunction hereWould be happy to compile this into a PR, if relevant.
@luisquintanilla