Re:Understanding GPT Tokenizers
by mike_hearn on Friday June 09, 2023 @08:43AM
(#36257011)
Yes, absolutely. TikToken is quite heavily optimized. If I wanted to write a tokenizer I'd just use their Rust backend and invoke it via an FFI, or translate it mechanically into another language. Actually, GPT-4 is quite good at code language translation so I'd just ask it to do the work.
Re:Understanding GPT Tokenizers
by belladoreai on Friday June 09, 2023 @09:36AM
(#36257623)
TikToken doesn't provide a tokenizer that's compatible with LLaMA.
Re:Understanding GPT Tokenizers
by mike_hearn on Friday June 09, 2023 @02:22PM
(#36261890)
Ah interesting. What's the difference? Isn't it just finding the minimal mapping of character sequences to numbers?
Re:Understanding GPT Tokenizers
by belladoreai on Friday June 09, 2023 @03:13PM
(#36262652)
That's what I thought when I started working on this, but it turns out the answer is no! This approach - "minimal mapping of character sequences to numbers" - can be described as a greedy algorithm. Using this approach will often produce correct results, but not always. I can provide an example:
Input string: " grabbed"
Tokenize that with the greedy algorithm, you get [17229, 2580] == [" grab", "bed"]
Tokenize that with actual LLaMA tokenizer, you get [2646, 1327, 287] == [" gra", "bb", "ed"]
Note that the correct tokenizer represents this string with 3 tokens, even though it would be more efficient to represent this string with 2 tokens (yes, those 2 tokens exist in the vocabulary).
LLaMA uses SentencePiece Byte-Pair Encoding for tokenization, and it has many weird quirks like this.
Would you be willing to share a GitHub link? This seems like a fun project to read through.
Re:Understanding GPT Tokenizers
by belladoreai on Friday June 09, 2023 @11:05AM
(#36258921)
Sure. Check back to this comment after the weekend. I will post it here.
Re:Understanding GPT Tokenizers
by belladoreai on Sunday June 11, 2023 @06:27PM
(#36286368)
Published now: https://github.com/belladoreai/llama-tokenizer-js
If you want to look at mappings for individual tokens, sure, but if you actually want to tokenize text that contains more than 1 token, the process is very non trivial. I've been writing my own JavaScript LLaMA tokenizer for several days now (just the encode and decode functions, not training). I hope to release it this weekend. It's currently 400 lines of code + data.