Skip to content

The plan and the map of the code

The route through the course with times and checkpoints, a file-by-file map of the engine code, how the pieces call each other, and a study routine that works for code-reading courses.

10 min·Verified 2026-11+50 XP
Share

#The route

  1. 1
    Start herepicture, plan, setup, a short C primer
  2. 2
    Engine Coreforward pass, weight loading, KV cache and sampling
  3. 3
    Engine Fastquantised matmul, threads and SIMD, batching and a server
  4. 4
    Labbenchmark against llama.cpp
Part Lessons Time Checkpoint
Start here 3 about 1 hour The C benchmark compiles and runs; you can describe the engine's pieces
Engine Core 3 about 3 hours Cached decoding gives the same logits as uncached; your model file round-trips
Engine Fast 4 about 4 hours INT8 matmul error is small and you can explain the speed-up; you can serve the engine over HTTP

Times assume you run the code and do the exercises.

#The map of the code

  1. 1
    server.pyan HTTP server that calls the engine for each request
  2. 2
    batching.pya simulation of static versus continuous batching
  3. 3
    quant.pyINT8 per-row and grouped 4-bit weights, and a quantised matvec
  4. 4
    gguf_mini.pywrite and read a minimal GGUF-style file, memory-mapped
  5. 5
    engine.pyConfig, weights, RMSNorm, RoPE, attention, KV cache, SwiGLU, generate

Reading from the bottom up gives the dependency order: the engine stands alone; the file format and quantisation work on its weights; batching and the server sit on top. A short C file, matvec_bench.c, measures memory-bound matrix-vector products.

#How a request flows through the files

  1. server.py receives a prompt and hands the token ids to the engine.
  2. engine.py calls forward_token once per token, reading the weights and the KVCache.
  3. Each step returns logits; the sampler picks a token; the loop repeats.
  4. If the weights came from a file, gguf_mini.py loaded them with memory-mapping.
  5. If they are quantised, quant.py supplies the matrix multiply.

#Study routine for code-reading courses

  1. Read the lesson's explanation first, then the code.
  2. Run it on a tiny input and print intermediate shapes and values.
  3. Change one thing and predict the result: width, number of heads, the quantisation group size.
  4. Check the test: every part has an equivalence check (cached equals uncached, quantised close to float).
  5. Write down the numbers that matter: memory per token, bytes per weight, tokens per second.

#What you can skip

  • Know transformers well? Skim the Engine Core forward-pass lesson for the code, and read the weight-loading and cache lessons carefully.
  • Do not know C? The C file is read-only for most readers; the setup lesson has a primer, and every lesson explains the result in words.
  • Only want performance? Go straight to Engine Fast after the forward pass.

#Exercise

You change the number of KV heads from 2 to 4 in Config. Which two things change and which file's behaviour do you expect to differ?

Answer: the KV cache becomes twice as large (more keys and values per token), and the attention reads more per step, so decode gets a little slower. The change appears in engine.py (the cache and the attention loop), and weight file sizes for the key and value matrices grow in gguf_mini.py outputs.

Next: set up, and a C primer.

Quick quiz

+10 XP per correct answer, +25 XP for a perfect score

  1. 1. Which file contains the forward pass of the model?

  2. 2. What does the server depend on?

  3. 3. What is the best first thing to do with an unfamiliar function?

  4. 4. Where does the course tell you the engine is correct?

Found this useful?

Send it to someone who is learning the same thing.

Share this lesson