HJ.
← All writing

DOWN THE STACK / PHASE 2

Phase 2: From GPU kernels to an inference server

"The first principle is that you must not fool yourself - and you are the easiest person to fool."
Richard Feynman · Cargo Cult Science, 1974

After building attention and HNSW on the CPU, I wanted to see what changes when the same ideas meet a GPU, and what happens around the model when you actually serve it.

Conceptual illustration of tiled GPU computation, a cache and an inference server
Conceptual illustration, not a benchmark or a hardware diagram.

Starting with Triton

Phase 1 taught me to write the naive version and then find the bottleneck. In Phase 2, I moved to a T4 on Colab and learned Triton by writing vector addition and matrix multiplication. The matmul ran at about 1 TFLOPS against roughly 2.7 TFLOPS for cuBLAS on the same setup. My kernel worked, but a working kernel and a tuned library kernel are very different things.

The useful shift was thinking in tiles: which block of data each program instance owns, what gets reused, and what has to travel to and from memory. That prepared me to revisit FlashAttention instead of treating the NumPy version as the end of the exercise.

FlashAttention, this time on a GPU

In Phase 1, I used online softmax and tiled Q, K and V so I never had to hold the entire attention matrix. In Triton I implemented the same idea as a GPU kernel. Each tile updates a running maximum and sum, rescales the previous accumulator when the maximum changes, and adds the contribution from the new K/V block. The bookkeeping that felt abstract in NumPy became the heart of the kernel.

At sequence length 16,384, my kernel beat the PyTorch comparison. At 65,536, the PyTorch version ran out of memory while the tiled kernel still ran. The reported maximum output difference was about 3.57e-07. These are results from my Colab experiment, not a claim that my kernel beats every production implementation.

The win was not that I had discovered a new attention algorithm. It was seeing how avoiding the N×N allocation changes what fits at all.

Quantization and the memory question

I also wrote a Triton quantization kernel and compared it with a NumPy implementation. At large sizes, the GPU path showed a 1,000x-plus speedup in that comparison. It's a useful result, but not a fair general GPU-versus-CPU headline: Python/NumPy and a compiled GPU kernel have very different overheads. The exercise was about moving the work into a kernel and understanding where the data lives.

That led naturally to the memory used during generation. A model does not recompute every past key and value for each new token if it can keep a KV cache. I built a simple cache and benchmarked it on CPU: about 2.2x at 30 generated tokens and 6.7x at 200. The longer the generation, the more repeated work the cache avoids, while the cache itself takes memory.

A small inference server

To connect the kernels and cache ideas to an actual request, I put GPT-2 behind a FastAPI server on Colab. The sampling path made temperature concrete: take the logits for the next token, divide them by temperature, apply softmax, then sample. I had seen softmax in attention; here I could watch the same probability machinery affect a generated response.

Then I read through vLLM's request path, from the API server through the engine and scheduler to KVCacheManager and BlockPool. That was the first time my small cache exercise looked like a piece of a much larger serving system. I also read the PagedAttention paper and connected its block table to an OS page table: token sequences need not own one continuous run of memory. The analogy helped me follow the code, but it is still an analogy, not a full account of how the production system works.

What I'm taking away

  • A correct kernel is only the start. Compare against a tuned baseline and say what was actually measured.
  • Tiling is a memory decision as much as a compute decision. The sequence that runs at all can matter more than a small speed win.
  • The model is only one part of inference. Caching, scheduling and allocation decide how requests share limited memory.
  • Build a small version first, then read production code with something familiar to map it to.

Next I want to go deeper on the serving side: raw memory allocation, grouped-query attention, llama.cpp's quantized formats, and the details that make the tiny examples hold up under real workloads.