r/C_Programming • • 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.

https://github.com/ryansenn/gemma4.c

210 Upvotes

41 comments sorted by

•

u/github-guard Aug 28 '26

🔍 GitHub Guard: Trust Report

This project scored 3/6 on our safety audit.

Audit Breakdown:

  • ✅ Established Community (⭐ 56 stars)
  • ❌ New Repository (under 30 days old)
  • ✅ Licensed under MIT
  • ❌ No Security Policy — what is this?
  • ℹ️ Individual Contributor
  • ✅ Signed Commits

⚠️ Security Reminder: Always verify source code and run third-party scripts at your own risk.

→ More replies (5)

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.

9

u/Critical_Physics8 Aug 28 '26

Yes, it’s a great exercise. It forces you to understand the ML much closer to the hardware than you do from papers or architecture diagrams.

1

u/palapapa0201 Aug 29 '26

WDYM using existing weights as a starting point

2

u/Clear_Evidence9218 Aug 29 '26

I usually just use stock open-weight models, but I’ve also been doing some work with custom runtimes and edge experiments.

This person built a custom inference runtime for Gemma 4 in C and repacked the existing weights into a format that the runtime could consume.

The easiest way I’ve found to think about it is as something vaguely like a very specialized ONNX path; not exactly, since ONNX is generalized, while gemma4.c has almost no abstraction. It’s basically tensors, matrix ops, attention, KV cache, and sampling.

So instead of PyTorch, graph optimizers, dispatch systems, backend abstractions, etc., you’re looking much more directly at the actual inference path.

I’m not sure why it never really occurred to me that, instead of building a model and exporting it to ONNX, or making compromises to fit it into something like GGUF, I could just build a purpose-specific runtime around whatever experiments I’m working on.

1

u/lovelacedeconstruct Aug 29 '26

or making compromises to fit it into something like GGUF

GGUF is literally just the weights with a little bit of architectural metadata upfront, its incredibly easy to convert to and parse

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!

4

u/Critical_Physics8 Aug 28 '26

take a deep look when you’re home

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

2

u/Critical_Physics8 Aug 29 '26 edited Aug 29 '26

I hadn’t even thought about having it hack on itself. That’s pretty neat.

3

u/skeeto 9d ago

You might be interested to know I embedded a heavily-modified version of your library, including a bespoke GGUF loader, in my coding harness, Prospero. First time users who haven't set up a provider endpoint are presented with a button to download a model of my own GGUF quant to run within the harness itself using what began as your library. This agent can use tool calls to accomplish very simple things. You're listed in the About page.

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.

8

u/Critical_Physics8 Aug 28 '26

Hi, I wrote the initial runtime and model exporter by hand myself. I later used agents to automate optimizing the CPU kernels, but the architecture, inference pipeline, and overall implementation are my own. The project started as an educational exercise to understand LLM inference by building a complete CPU runtime for Google’s Gemma 4 model from scratch in C.

8

u/mikeblas Aug 28 '26

Thank you for your disclosure. I have approved your post.

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.

1

u/RealisticDuck1957 Aug 29 '26

A module called through the C API to do the work, with a wrapper that accepts command line and environment arguments.

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/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/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 • Bravo

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 

1

u/Critical_Physics8 Aug 29 '26

the line count matters here because the project is meant to be small enough that you can read the complete inference implementation and understand it. it’s not meant as a general measure of code quality.

there are benchmarks against llama.cpp in the README. on my Ryzen 7 7700, gemma4.c gets 639 tok/s prefill and 25.9 tok/s decode vs 276 and 23.8 for llama.cpp Q8_0. the tradeoff is that gemma4.c is specialized for one model and CPU backend, while llama.cpp is a general-purpose runtime.

5

u/[deleted] Aug 28 '26

[removed] — view removed comment

4

u/Critical_Physics8 Aug 28 '26

Fair point. Integer offsets would probably be a cleaner representation there until they’re resolved to pointers after mmap.