NHacker Next
login
▲GigaToken: ~1000x faster Language model tokenizationgithub.com
297 points by syrusakbary 5 hours ago | 56 comments
Loading comments...
maxdo 5 hours ago [-]
Interesting :

Q: Did you just way over-optimize for a specific CPU and tokenizer? How is it so fast? No, I way over-optimized for every combination of these! The results are very consistent across CPUs (modern x86 and ARM), and across specific tokenizers.

The major improvements are in optimizing heavily an implementation that usually is outsourced to a Regex engine (pretokenization) using SIMD, minimizing branching and other tricks, as well as heavily optimizing caching of pretoken mappings (if a word has been seen before, look it up its encoded tokens efficiently). Caching is a very hard problem in this domain since the cache grows very quickly, and pretoken distributions are very long-tailed.

Finally, interactions with Python are minimized, and threads have minimal interactions with each other.

onlyrealcuzzo 4 hours ago [-]
This is awesome, but tokenization is typically <0.1% of total inference time.

Presumably there's a host of applications that just need to tokenize, though, and this would be great for those!

scottcha 3 hours ago [-]
I run an AI platform and we need to tokenize fast and early to make a lot of decisions on the subsequent steps (things like routing, rate limiting and such). Its really important to do this efficiently even though its not a large % of total end to end time for the request.
jaggederest 2 hours ago [-]
To concur it's "latency critical", not "performance critical", people often confuse those two - optimize it all, but especially the chained critical path latency!
quietfox 28 minutes ago [-]
‘I run an AI platform’ I have so many genuine questions I don’t even know where to start.
NuclearPM 12 minutes ago [-]
Pick one. I would like to hear it.
pipsterwo 4 hours ago [-]
1/1000 of inference compute is a non-trivial workload at scale. Gartner estimates ~$28B in inference spend for 2026 making this a $28 million dollar per year workload (edit: based on the assumption above)

Source: https://www.gartner.com/en/newsroom/press-releases/2026-07-2...

boroboro4 3 hours ago [-]
The issue is it’s cpu compute which is underutilized in gpu clusters anyway, so practically it’s not really 1/1000.
pipsterwo 3 hours ago [-]
Totally, edited my comment to specify "based on the assumption above." The main takeaway I was going for was 0.1% is not a small number in this context
GenerocUsername 4 hours ago [-]
Always good to make it 0.001%
noahbp 2 hours ago [-]
Time to first token, especially for smaller models, can be sharply reduced.

Latency can be just as important as overall throughput, especially for inference providers like Groq and Cerebras.

fastball 2 hours ago [-]
Tokenization is <0.1% of the inference time for the first token in the same way it is <0.1% for the last.
marcelroed 2 hours ago [-]
Time to first token refers to the time until the model outputs one token, which includes the time to process the entire prompt (doing prefill). The GPU time per token is much lower when doing prefill, so the significance of tokenization is higher.
dingdingdang 24 minutes ago [-]
Have you done preliminary numbers on replacing tokenizer on, say, llama-server?
brcmthrowaway 3 hours ago [-]
[dead]
apollopower 2 hours ago [-]
Cool stuff. From my understanding, this is less valuable at inference time and more useful when running offline pre-training data prep.

When tokenizing terabytes of text for your training corpus, the speedup here is probably doing real work in saving you time (and money?). You get a faster iteration cycle when figuring out and adjusting your datasets.

luciana1u 2 hours ago [-]
engineering effort to make something 1000x faster that accounts for 0.1% of total runtime is the most software developer thing imaginable
minimaxir 6 minutes ago [-]
I initially had a Rust-based word cloud generator that generates word clouds in high resolution in ~100 milliseconds whereas it would take other generators a couple seconds to do the same thing. Does the world need a super-fast word cloud generator? No. Do I want a super-fast word cloud generator? Yes.

I later found enough optimizations to reduce the generation speed all the way down to ~16ms. Do I need a word cloud generator that fast? No. But if I have a word cloud generator it's going to be as fast as possible dammit.

piker 2 hours ago [-]
"The pursuit of excellence does not need justification."

https://x.com/mitchellh/status/2074225453217505494

michaelmior 2 hours ago [-]
This depends on what your workflow is. There are use cases for tokenization that don't always involve immediately feeding the text into a model.
NuclearPM 8 minutes ago [-]
1000x faster on 0.1% of runtime = 0.1% saved. Amdahl remains undefeated.

Globally that’s ~50 GWh/yr, or ~5.7 MW continuous:

• 4,700 American homes

• a week of British tea

alansaber 2 hours ago [-]
It's pretty funny but then again, why not if it's as trivial to simplify as it appears
0xnyn 4 hours ago [-]
I had to stare at that chart for a minute just to let the numbers sink in. It's genuinely mind-bending, incredible ship OP
swiftcoder 3 hours ago [-]
So the question becomes, how many other parts of the inference pipeline have left 1000x optimization opportunities lying on the table?
fastball 2 hours ago [-]
The problem with the rest of inference is that changes are not trivially correct or incorrect, as they are with the tokenization layer.
nixon_why69 1 hours ago [-]
Eh, linear algebra changes are still easy to measure correctness, it's just that you're competing with 50 years of research for most of them, less low hanging fruit.
parineum 2 hours ago [-]
I'm sure there's been a lot more effort put into the other, more consequential, portions of inference time.
ProofHouse 2 hours ago [-]
the answer is many! This would take hours to write. Full teams and research on nearly every part. So many 'unlocks' coming.
chocrates 2 hours ago [-]
Practically I would need to wait for hugging face models to adopt this? My harness tokenizer is just an estimate since the model tokenizes on my api calls?
fwip 5 hours ago [-]
What sort of setups do people have that are bounded by the speed of the tokenizer?
marcelroed 4 hours ago [-]
Author here! In my case it's mostly pretraining experiments, where you might want to change your data mixture/filtering/processing of training data, and splits are usually done at a token-level instead of a text level. In this case we usually run for days on a huge number of CPUs to finish tokenizing something like DCLM.

From what I can tell it's also useful for inference when considering time-to-first-token (TTFT) as reported by fastokens.[0]

I'm not sure about the proprietary inference engines, but in the open source ones tokenization is done before looking up if a text sequence is present in the KV-cache. If you have a long prefix that's been seen before (say a system prompt), the time for tokenizing that will be a large part of your TTFT. The tokenizer cache should be warmed up in this case, so the throughput for Gigatoken would be significantly higher than reported in the repo.

[0] https://github.com/crusoecloud/fastokens

wren6991 3 hours ago [-]
> I'm not sure about the proprietary inference engines, but in the open source ones tokenization is done before looking up if a text sequence is present in the KV-cache

Is this necessary? Tokenisation is deterministic, so for a hit/miss check you can lookup on (a hash of) the source text instead of the tokens. You only need the tokens once you're seeking for the exact token index having determined there is a hit. That means tokenisation can proceed in parallel with your cache query, and since these caches are distributed in production systems I imagine the query itself could be slow.

I'm not trying to undermine the utility, and this is obviously excellent work. Being able to tokenise faster on the client also seems useful (precise token counts for context pruning heuristics, instead of `chars / 4`), and on a phone your work translates directly to energy savings. I'm just curious about the cache lookup point.

marcelroed 1 hours ago [-]
It's usually not as binary as "hit" or "miss" with a prefix cache, and you need to know the token boundaries to know where the cache hit ends.

The current structures used for KV-caching in vLLM and SGLang work by chunking the KV-cache tokens into prefix trees, and you need to hash chunks of tokens in order to look up in these, meaning you need to be able to slice up your tokens by token count.

Again I have no idea what proprietary engines are doing, but this is why open source stuff needs to tokenize before cache lookup at least.

fwip 4 hours ago [-]
Very cool, thanks.
lostmsu 4 hours ago [-]
Can't you tokenize in preloading on demand?
marcelroed 4 hours ago [-]
You can, but this usually results in sequences with padding/truncation, since you won't know how many tokens your inputs map to before you actually tokenize them. This also makes shuffling difficult.

In practice every training project I've worked on does tokenization in a separate data processing phase.

SnowflakeOnIce 11 minutes ago [-]
I worked on a system a couple years ago with a BERT-based model (64M parameters) used for classification. The rest of the system could process data at gigabytes per second, and so here tokenization at a measly few megabytes per second really slowed things down. The model inference was more expensive than tokenization, but tokenization was still >10% of total runtime.
andersa 5 hours ago [-]
Wait, since when does it matter whether something being hyper-optimized is useful? The computer going brrrr on an interesting problem is in itself the goal!
fwip 4 hours ago [-]
That's fair, I just figure there are useful scenarios as well. Apologies if I came off as dismissive!
ac2u 4 hours ago [-]
It didn’t come off as dismissive to me. I was curious as well as to where such optimizing helps and knew that the answers to your question would help me discover use cases I didn’t think of
janalsncm 4 hours ago [-]
If you are training an LLM, you need to tokenize the text before it’s trained on. A lot of time this can be done in parallel with the GPU though.

I have spent way too much time waiting 10-15 minutes tokenizing my training dataset only for the run to crash over some minor bug after that. (If I was smarter, I’d test on a smaller batch first.)

rhdunn 5 hours ago [-]
It can be useful for checking input token usage before sending it to the model, e.g. preventing calls above a given token bound or grouping requests into batches.

It can also be used by the LLMs to provide the input and output token counts on the different APIs, though I'm not sure if this is how llama.cpp or other OpenAI-like APIs calculate the input/output tokens of a request.

charcircuit 4 hours ago [-]
But are those bounded on the speed of tokenization?
avereveard 4 hours ago [-]
I've data where i cannot store metadata that i need to search semantically so i embed it on the fly at every search with static embedding and tokenizing was more than 99% of the cpu time. Granted that was due the naive implementation of the default tokenizer which was o^2 with document length and just switching to a proper scanner solved most of it without going to simd and whatnot, but still.
imperio59 4 hours ago [-]
Pre-training data is pre-tokenized ahead of time before being used to not waste any GPU compute.

A massive speedup like this is a nice efficiency savings on some of these data pipelines for sure.

sashank_1509 5 hours ago [-]
This is really cool, great work!
anonymousmoos 3 hours ago [-]
Quality software here.
dmezzetti 4 hours ago [-]
Very interesting project! Are there benchmarks for the "compatibility mode" or are all the numbers for the Gigatoken API?
marcelroed 4 hours ago [-]
Numbers are for the Gigatoken API, but compatibility mode just means eating a bunch of Python overhead (creating lists, reading strings to bytes). You can expect a modest ~200-300x speedup with compatibility mode depending on how you use it.
robotresearcher 1 hours ago [-]
> a modest ~200-300x speedup with compatibility mode

marcelroed is modest, this speedup is not. Good work.

marcelroed 4 hours ago [-]
I can add some benchmarks for compatibility mode in the future. I have a little more juice to squeeze out of the Python interop though, so not quite ready for it yet.
zerolines 4 hours ago [-]
wow, best release all week.
semiinfinitely 4 hours ago [-]
quite excellent software
luck7710 1 hours ago [-]
[dead]
vmware508 3 hours ago [-]
We should just rewrite everything in Rust, especially bloated Python code, and the world would be a better place. ;) Disclosure: I'm a Rust advocate!
minimaxir 3 hours ago [-]
Both the example libraries compared (tokenizers and tiktoken) are Rust-based with Python bindings. There's just a few levers in Rust that can speed it up even more particularly with LLM assistance as the AI Use Discloure here notes:

> Final profiling stages and the last ~4x worth of performance from eliminating branching and improving the pretoken cache hierarchy

SOLAR_FIELDS 3 hours ago [-]
1 year ago everyone would have called you insane for suggesting this. Now we all shrug and say yeah maybe we can do this and it’s actually a good idea?
3 hours ago [-]
weberer 2 hours ago [-]
We should rewrite all Rust code in Python. Not for any technical reason. I'm just sick of the Rust cult at this point.