FixTheRobot Explore Robotics / LLM / LM10 / LM10-L1

This lab works on phones, but writing code is much easier on a laptop.

LLM · Modern LLM Architecture and Inference · LM10-L1 · Intermediate

KV-Cache Memory and Behaviour

Generating text one token at a time would repeat enormous amounts of work if the model recomputed the whole past at every step. The key-value cache stores the past keys and values so that each step only does the new token’s work. The price is memory, which grows with the context and with the number of users.

You will fit a serving workload into a GPU, write the cache-size formula, and count the work that the cache saves.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math (and slamkit for small matrix helpers: matmul, transpose, solve, inverse, identity). Images, point clouds and detections arrive as lists inside the dictionary D; an image is a list of rows, each row a list of pixels. There is no NumPy or OpenCV: you write the algorithm yourself, which is the point of the lab. Print with print().

    Hints

    What this lab teaches

    Stage 1 explores how key-value heads and cache precision decide whether long-context serving fits in memory. Stage 2 writes the cache-size formula. Stage 3 counts operations with and without a cache. Only formulas and operation counts are computed; no model is run. Your code is graded on hidden configurations.

    Theory for this lab

    Modern LLM Architecture and Inference labs

    1. LM10-L1: KV-Cache Memory and Behaviour: this lab.
    2. LM10-L2: Compare Attention Variants
    3. LM10-L3: Implement a KV Cache and Rotary Embeddings in PyTorch

    Educational simplification: one generic 7-billion-parameter configuration in Stage 1, a simplified operation count (projections and the two attention products of one layer, ignoring the feed-forward layers, normalisation and softmax), no paging or fragmentation of the cache. All configurations are generic.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.