r/WebdevTutorials • • 2h ago

Backend I cut llama.cpp open and looked inside. Here is what's actually in there

1 Upvotes

Everyone runs llama.cpp. Almost nobody has seen inside it.

So I cut it open. One prompt ("Why is the sky blue?"), one model (Qwen3.8-27B,

UD-Q4_K_M, 16.5 GB), one machine (RTX 5090, Ryzen 9 9950X, 64 GB DDR5), and the

source pinned at commit 06230cb. Then I peeled it layer by layer, from the binary

you run down to the instructions on the GPU.

Here is what I found at each layer.

LAYER 1: THE SKIN (the binary you run)

The first cut and the first surprise. llama-cli is not really a CLI. It starts

llama-server in a std::thread on a free localhost port and talks to it over SSE,

like any web app would. You've been chatting with a web server the whole time.

LAYER 2: THE FAT (loading the file)

Nothing gets "read". The GGUF is mmap'd with MAP_SHARED + MAP_POPULATE, the kernel

pulls it into the page cache, and the GPU weights are copied straight out of that

mapping into one big VRAM buffer. Then the loader quietly munmaps everything the

CPU doesn't need. One organ never moves to the GPU, even with -ngl 99: token_embd.

The code says there is "very little benefit to offloading the input layer".

LAYER 3: THE MUSCLE (the weights)

4-bit quantization isn't 4 bits. Cut open a Q4_K block and it's 256 weights in 144

bytes: two fp16 scales, 12 bytes of 6-bit sub-scales, 128 bytes of nibbles. That's

4.5 bits per weight. And Q4_K_M isn't uniform either: the output head, attn_v and

ffn_down get Q6_K in the first 1/8, the last 1/8 and every third layer in between.

LAYER 4: THE MEMORY (KV cache)

Qwen3.8 is a hybrid. Only 16 of its 64 layers keep a KV cache; the other 48 are

Gated DeltaNet with a small fixed state. So the cache is only 64 KiB per token,

2 GiB at 32k context.

LAYER 5: THE NERVES (your prompt)

8 words go in, 58 tokens come out. The chat template injects a system line and a

<think> block before your text even begins.

LAYER 6: THE HEART (prefill and decode)

In prefill, the final layer keeps exactly ONE row. Only the last prompt token is

marked as output, so ggml_get_rows throws away the rest before the LM head runs.

The weights are never dequantized in the fast path: activations get squeezed to

Q8_1 on the fly and the dot products run in int8 with dp4a.

LAYER 7: THE BONE (the speed limit)

At the bottom there's a hard limit you can work out in your head. Every token

reads every weight once, so decode speed tops out at bandwidth / model size:

1792 GB/s / 16.5 GB = ~109 tok/s on the 5090. CPU only on DDR5: ~6 tok/s.

And one thing I found stitched into the lining: when a GGML_ASSERT fails, ggml

forks a child process that attaches gdb to its own parent, just to print you a

backtrace.

The full teardown, with every layer drawn out, the real code at file:line, and the

numbers worked through:

https://cloudmash.blog/posts/inside-llama-cpp-from-enter-to-first-token/

If I cut something wrong, tell me. The internals move fast.