Live data from Hacker News

Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

github.com

1–10 of 79 posts

Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

#1
TokenDagger is a drop-in replacement for OpenAI’s Tiktoken (the tokenizer behind Llama 3, Mistral, GPT-3.*, etc.). It’s written in C++ 17 with thin Python bindings, keeps the exact same BPE vocab/special-token rules, and focuses on raw speed.

I’m teaching myself LLM internals by re-implementing the stack from first principles. Profiling TikToken’s Python/Rust implementation showed a lot of time was spent doing regex matching. Most of my perf gains come from a) using a faster jit-compiled regex engine; and b) simplifying the algorithm to forego regex matching special tokens at all.

Benchmarking code is included. Notable results show: - 4x faster code sample tokenization on a single thread. - 2-3x higher throughput when tested on a 1GB natural language text file.

Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken
github.com

Re: Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

#4

There’s something beautiful about creating a drop in replacement for something that improves performance substantially. ScyllaDB comes to mind

Agreed. I figured nobody would use it otherwise.

Put it in there readme & description. It's a big selling point.

Re: Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

#5
Kudos, I think (in the short term at least) there is a large amount of perf. optimization to be found by coding parts of the whole AI/ML infrastructure in C++ like this one, not as a rewrite (god no!) but drop in and fix key bottlenecks. Anytime I see someone (seems Chinese engineers are good at this) put something out in C++, good chance some solid engineering tradeoffs have been made and dramatic improvement will be seen.

Re: Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

#6

There’s something beautiful about creating a drop in replacement for something that improves performance substantially. ScyllaDB comes to mind

Agreed. I figured nobody would use it otherwise.

To be fair, many people have token stabbing needs.

Re: Show HN: TokenDagger – A tokenizer faster than OpenAI's Tiktoken

#9

> simplifying the algorithm to forego regex matching special tokens at all Does that mean there could be cases with less quality in terms of tokenization?

The output should be identical, assuming no bugs.

The Tiktoken implementation takes a collection of all special tokens upon initialization and compiles them into a regex by joining them with `|` [0]. Then the actual encoding process checks for matches on this expression.

Models like Llama 4 define a list of 1,135 special tokens. Notably, 1,115 of those are "reserved" special tokens! So this yields a huge regexp of special tokens that shouldn't be considered at all.

TokenDagger does not do this. Instead, simple string matching is used. This works because we don't need to consider the entire special vocabulary every time. The caller of `encode` must explicitly define which special tokens should be considered [1]. So it's faster to check against the much smaller list we _know_ is being used.

[0] https://github.com/openai/tiktoken/blob/main/src/lib.rs#L476

[1] https://github.com/openai/tiktoken/blob/main/tiktoken/core.p...

Post reply on HN