Faster Applications: Harnessing Python with C/Zig libraries and my story of building fastest LLM tokenizer
akhil teja (~akhil02) |
10
Description:
Python is a language that is easily readable. As it is easy to read many programmers started learning it and coding to solve their problems through technology. Python's footprint can be seen in every industry and vertical tech stack like:
- Operating Systems for booting, installing packages
- Networking
- Machine learning
- Cryptography
- Web Services
For each of the above technology python provides necessary features as modules either in-built or community built packages. If we dig deeper, we can classify these technologies under two segments: Compute intensive and Input / Output few of the technologies fall in two segments, Below image gives an outline.
Let's focus more on compute intensive applications as this CFP/talk is oriented around it.
Programmers more than the Python language should be concerned about it's runtime i.e, Interpreter. In this world, everything is a trade-off. Python's language simplicity and human readability costed runtime performance i.e, as a programmer we never had to think about memory management, integer overflow errors, function stack frame size, etc..,. because everything is handled by Interpreter. It's even more restrictive while running multiple threads i.e, only one thread is run anytime because of Global Interpreter Lock and will affect programming compute intensive applications that can use multiple cores of the system. Presence of garbage collector might help in memory management but pauses execution of main program to perform housekeeping activity i.e, erasing redundant heap data.. These are few of the main drawbacks in Python Interpreter from the point of view of compute intensive applications.
But there are several libraries for Machine Learning, Data Engineering, Cryptography that provide needed performance and it was possible only through integration of Python with other low-level libraries developed in C/C++. Python's easy to read and coding helped it to become the frontend for many libraries that became popular after integration like numpy, pytorch, tensorflow, etc..,. Now there's a strong need for any library to have a python frontend to be accepted and adopted in wide range of applications.
This CFP/talk helps programmers and engineers to understand python's integration with low-level languages and speaks about
My story in building one of the fastest tokenizer in Python and Zig competing with tiktoken a rust based library created by OpenAI Team
I'll talk about Python's ctypes module that has FFI (Foreign Function Interface) constructs to interact with any language i.e, C-ABI (C-Application Binary Interface) compatible. ctypes module contains C data types and functions as signatures. Below is an example code creating C related objects in python as signatures
There'll be few slides related to
libfasttokenizer.so
, the tokenizer shared library developed using ziglang.There'll be a slide on comparison between python tokenizer libraries whose backends are developed in ziglang and rust.
I'll add few slides regarding debugging python code integrated with low level library. Debugging is the hardest part as two languages have to be dealt anytime. Using
codelldb
I'll show how to debug the backend library.I'll also share couple of slides related to performance profiling using
perf
tool and visualise the same in flamegraph.
These will be my key sub topics for the talk.
Prerequisites:
Good to have knowledge on basics of python
Speaker Info:
I'm a Data Scientist @DBS, Singapore headquartered international bank.
My work revolves around Data Engineering, Machine learning and GenAI. I'm associated with Swecha (tech community based out of Hyderabad) as a regular speaker for talks related to Rustlang and Python.
I contribute to several opensource projects and have few popular projects under my name:
Speaker Links:
Talk on Internals of Rust: https://slides.com/akhilg/palette-34e054
Talk on Podcast Summarizer built in Rust: https://slides.com/akhilg/palette
Cuda library in Zig: https://github.com/akhildevelops/cudaz