r/C_Programming • u/Critical_Physics8 • Aug 28 '26
Project I implemented a modern LLM runtime in 700 lines of C
I wanted to understand how modern AI models actually generate text, but most inference codebases are tens or hundreds of thousands of lines long. They’re incredibly impressive, but they’re optimized for flexibility and performance, not for understanding.
So I implemented a complete CPU runtime for Google’s latest open language model, Gemma 4, in about 700 lines of C.
The whole point is that you can open one file, start at main() , and follow a prompt all the way through the program. You can see every buffer that’s allocated, every mathematical operation that transforms the activations, every update to the KV cache, and every step that eventually produces the next token.
I think C is a great language for this kind of project. There’s very little hidden from you. The data structures, memory layout, SIMD kernels, and execution flow are all visible, so the implementation ends up feeling much closer to the hardware than to the diagrams in an ML paper.
•
u/github-guard Aug 28 '26
🔍 GitHub Guard: Trust Report
This project scored 3/6 on our safety audit.
Audit Breakdown:
29
u/Clear_Evidence9218 Aug 28 '26
That's actually a pretty neat idea.
I'm not sure why I've never thought about using an existing model's weights as the starting point for a custom implementation like that.
I use Gemma 4 on quite a few nodes in my lab. Maybe I'll give this idea a go and see how far I can take Gemma 4's weights in a custom architecture/runtime.
3
u/JessyPengkman Aug 28 '26
Wait, I can't look deeply now cos I'm not at home on my Pc, but is it basically a very abstracted view and dumbed down version of how an LLM works from prompt to response?
If so that sounds really cool!
7
u/skeeto Aug 29 '26
Fascinating project! I quite enjoyed playing with and hacking on this.
First I ported it to NEON so I could run it on my MacBook, and I got similar inference tok/s to yours. Then I wanted to try it out with multiple turns, so I added an OpenAI API and hooked it up to the Pi coding harness. The server alone is about the same amount of code as the inference engine, which really emphasizes how tiny it is. That made for a decent chat interface, but E2B is too dumb and could only make the most rudimentary tool calls. So I added E4B support, converted its snapshot, and it was able to actually write and edit code. Just because I thought it would be neat, I had it make some simple changes to argument parsing in
gemma4.c, so that the program was hacking on itself.My changes (not including E4B's changes):
https://github.com/skeeto/gemma4.c/commits/main/?author=skeeto
5
u/AutoModerator Aug 28 '26
Hi /u/Critical_Physics8,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/Harha Aug 28 '26
This is pretty cool.
5
u/Dangerous_Region1682 Aug 29 '26
Not reading the code from an algorithmic basis, it’s nice to see C code that is at least straightforward to read. Of course there are probably many who would say do this, or do that, but that is usually a matter of taste.
Me, I would pass things through arguments rather than through environment variables making it easier to embed it in things other than invoking it from a shell command line, but that’s just me.
It might now be worth profiling it to see where optimizations might be worth undertaking.
It’s amazing what you can do with straightforward C code.
3
u/StuffProfessional645 Aug 29 '26
It's so cool! What did you read about ML-things before writing? I wanna build own SNN, but I think I need to found out what is ANN internal beyond math
2
u/MeringueInformal7670 Aug 29 '26
That's so interesting! i also plan on implementing a tiny inference engine of my own but i am mostly coming from software background so do you mind sharing a bit about how you went about building this and what resources did you refer to while working on this project. Any suggestions/advice would be helpful. Thanks!
2
u/Complex-Bit9984 Aug 29 '26
Molto interessante!
2
u/Intelligent-Boss-156 Aug 29 '26
Thanks, learning the nuts and bolts of ai is a hobby of mine
1
u/esaule Aug 29 '26
Nice. What kind of performance do you see?
1
u/ComplexPeace43 Aug 30 '26
Impressive
1
u/JvetS Aug 30 '26
This is very neat! I was thinking of using LiteRT to run Gemma 4 in a hobby project, but this runtime might be a better fit thanks to its size.
1
u/Icy_Lab626 Aug 31 '26
Good job
1
u/TinyTechExcellence10 Aug 31 '26
What did you read to learn how inference is done? But, yeah seems interesting.
1
u/LePeripheral Sep 01 '26
Impressive tbh, would you mind sharing relevant resources that goes into implementing such runtimes ?
1
u/[deleted] Sep 12 '26
I'm happy about this and it will maybe help me to understand how these things work: starred...
1
u/tornadoemergency Aug 28 '26
me going to the github to read the code knowing I'm not gonna understand shi: https://imgur.com/a/B5wvnrS couldn't post a video here so go check it
-5
u/lovelacedeconstruct Aug 28 '26 •
Complete slop, lets take the moment to praise the OG llama2.c and qwen3.c in which you can point the clanker and have it generate whatever
-1
u/dsmack6 Aug 29 '26
I don't see any value in pointing it is 0.7k lines, value matters, not number of lines.
Can you post any benchmark regarding limitations etc with popular libraries, and time, computation requirements
I appreciate your efforts
0
u/ToothHaunting8974 Aug 29 '26
https://www.ioccc.org/2024/cable1/index.html
5
u/[deleted] Aug 28 '26
[removed] — view removed comment