返回
RCreddit.com
14
·1天前·开发者社区 · RSS

I implemented a modern LLM in 700 lines of C

查看原文
LlamaGitHub模型发布开源代码

热度趋势

新上榜
最近 24 小时与此前 24 小时对比 · 7 天曲线

百分比基于当前可用热度信号,而非评论数或独立用户人数。

推荐理由

Llama 相关模型动态已经出现,适合跟踪能力变化、生态影响和后续可用性。

AI 摘要

一位开发者用700行C语言实现了一个名为gemma4.c的现代大型语言模型。该项目在CPU端进行了大量优化,采用了int8权重和激活、OpenMP、AVX2以及AVX-512 VNNI技术。在Ryzen 7 7700处理器上,该模型在512个token的预填充阶段达到了每秒639个token的速度,在生成阶段则达到了每秒25.9个token,性能优于llama.cpp。该项目的代码已在GitHub上开源。

The idea is pretty simple: you can download a modern language model, compile one 700-line C file, and have it generate text on an ordinary CPU. Then you can read that same file from top to bottom and understand exactly how the model generates each new token.

The model is Gemma 4 E2B, one of Google’s latest open models. The C runtime handles the tokenizer, transformer, KV cache, sampling, and CPU kernels itself. There’s no inference framework or external library doing the interesting parts underneath it.

I built it mostly because I wanted to understand LLM inference at the level where it stops being diagrams and equations and becomes actual code. Keeping everything in one file made that much easier. You can start at main(), follow a prompt all the way through the runtime, see every buffer that’s allocated, every mathematical operation that transforms the activations, and every step that eventually turns your input into new tokens.

I ended up spending a lot of time on the CPU side too. The runtime uses int8 weights and activations, OpenMP, AVX2, and AVX-512 VNNI where available. On my Ryzen 7 7700 it gets about 639 tok/s on a 512-token prefill and 25.9 tok/s during generation, making it faster than llama.cpp.

The repo stays small on purpose. It only supports this model and CPU inference, so there’s much less machinery to work through than in a general-purpose runtime.