r/graalvm • • 9d ago

Tok'n'Roll: Token-perfect tokenizers for Python but 100% written in Java 🤯

https://pypi.org/project/toknroll/

Tok'n'Roll is the (LLM) tokenization substrate of Quixotic AI and can be installed as follows:

pip install toknroll

But, why is a Python wheel being posted in r/java r/graalvm?

What if I told you that Tok'n'Roll is written 100% in safe Java e.g. no Unsafe.

Tok'n'Roll outperforms the industry standards: both Tiktoken and Hugging Face tokenizers (1.0rc5), which are Python wrappers over Rust codebases.

But why? The Rust codebases are riddled with unsafe usages ... not so safe after all. The Java implementation is quite nice IMHO, it just proves that Java can be extremely performant. Why wrap Rust when you can wrap Java.

This is one-of-a-kind project, inspired by u/OddEstimate1627 (Florian is that you?) work on StaticFX.

The whole Java-to-Python approach is quite interesting, the Python wheel is just ~5.6MB, supports Linux, Windows and macOS.

Benchmarks:

PS: I'm the author, everything is open source, Apache 2.0.

Note: I managed to get ~1.6K views until the r/java mods took the post down ... r/graalvm is a better fit since this use case is only possible thanks to GraalVM.

Python+Java outperforms Python+Rust while being safer ... compiled as native library, wrapped for Python, only possible with GraalVM.

6 Upvotes

3 comments sorted by

2

u/OddEstimate1627 9d ago

Yes, that's me. Awesome! Are you using annotation-based bindings like hebi-charts or did you do it differently?

2

u/mukel90 8d ago

toknroll.h is only 13 functions, so for now is all by hand ... but I hit all the odd issues with enums, C int sizes ... the C APIs must be very disciplined and stick to sane guidelines, the resulting library/header file is completely transparent, it looks like a C library, no isolates, not attach/detach ... beautiful.

This opens the door to so many use cases ... being strict on the function signatures keeps the APIs simple and clean ... double win.

2

u/OddEstimate1627 8d ago

yeah, the CEntryPoint API is amazing, but far away from being user-friendly. Even with a ton of experience with cross-platform C ABIs, I spent nearly a year on the auto-generator (pre-agents).

It's good to see others get good results with it as well. More adoption might lead to better support for missing features like static archives😉