Skip to content
Inference Academy

Capstone: make a real layer faster, and prove it

Take one decoder layer of a real open model, measure it at a decode shape and a prefill shape, profile it, choose the kernel changes the profile justifies, implement them with tests, and write a short report with before and after numbers, a profile that explains the gain, and the limits of what you showed.

By

20 min · verified 2026-11Pro
Share
Open in ColabRuns on a free T4 GPU: pick Runtime → Change runtime type → T4 GPU.

Time to use the whole course on something real. You'll take one decoder layer of an open model, find out where its time goes, make it faster with kernels you write, and prove the result to a sceptical reader. The goal is a measured, explained gain, not a record. A 15% gain you can explain is worth more than a 2x you can't.

#The setup

Use one decoder layer from a small Llama-style model, with real shapes and random weights (you don't need the trained weights to measure speed). For example, the shapes of a 1B to 8B model:

Parameter Example (8B-class)
Hidden size 4,096
Query heads, KV heads, head dimension 32, 8, 128
MLP intermediate size 14,336
Data type FP16 (BF16 if your GPU supports it)

On a T4, a full 8B layer in FP16 has about 218M parameters, about 436 MB, which fits easily. You may want a 1B-class shape too (hidden size 2,048) for faster runs.

Build the layer in plain PyTorch: RMSNorm, QKV projection, rotary embedding, attention over a KV cache, output projection, residual add, RMSNorm, gate-up projection, SwiGLU, down projection, residual add. This is your baseline. Keep it simple and correct; it's also your reference for testing.

Pick two shapes to report:

  • Decode: batch 8 (8 new tokens, one per sequence), each with a KV cache of 2,048 tokens.
  • Prefill: one sequence of 2,048 tokens.

#Step 1: the baseline, measured properly

python
import torch, triton
def bench(fn):
    ms = sorted(triton.testing.do_bench(fn, rep=200) for _ in range(5))
    return ms[2], ms[4] - ms[0]            # median of 5 runs (each already averaged), and the spread

Record the median and the spread, at both shapes, in eager mode and with torch.compile. torch.compile is a strong baseline: if your kernels don't beat it, that's an important, honest result.

Also write down the predicted floor for decode: the bytes the layer must read (all weights, plus the KV cache for 8 sequences of 2,048 tokens), divided by your measured copy bandwidth. For prefill, the floor is the FLOPs divided by your measured FP16 matmul throughput. Your gain is bounded by the gap between the baseline and these floors.

Pro content

Keep reading with Inference Academy Pro

Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lesson

  1. Step 2: profile before choosing
  2. Step 3: implement, with tests
  3. Step 4: the report
  4. How to judge your own result
  5. What you can now do
  6. Going deeper

You will be able to answer

  • Why does the capstone measure both a decode shape and a prefill shape?
  • Your fused kernel makes the layer 8% faster at decode but changes outputs by up to 5% relative. What should you do?
  • What makes a capstone result believable to someone else?