Paged Attention a show in 11 chapters
Goal: more parties in the same room now 6.8 in flight
2 of 11 The problem
Why this matters
A chat server keeps notes on every conversation in scarce GPU memory. In 2023 Woosuk Kwon's team at UC Berkeley measured how much of that memory held any notes at all.
Only 20.4 to 38.2 percent held notes, so 61.8 to 79.6 percent, about 60 to 80, sat held and empty. It was held for words not yet written, or never written. Their fix, PagedAttention, fit more conversations in the same memory, for two to four times the throughput.
Your goal: seat more parties (conversations) in the same room (the memory), and finish every one. In this lesson's small room the old way keeps 6.8 in flight on average. The next chapter shows where that number comes from.
Kwon et al., "Efficient Memory Management for Large Language Model Serving with PagedAttention", SOSP 2023, arXiv:2309.06180. The quote: "only 20.4% - 38.2% of the KV cache memory is used to store the actual token states in the existing systems."
- party
- one conversation
- chair
- the notes for one character
- slab
- 32 chairs, one party's until it leaves
- room
- 8 slabs × 32 = 256 chairs
At the door: party 0. Its prompt, the text it arrives with, fills 12 chairs. How long its reply runs: nobody knows. 63 more behind it
held but empty: 0 of 256 chairs
Full
3 of 11 Try it
Be the seater
Seat each party in a free slab. You can't know how long it will talk, so it gets all 32 chairs.
Tap a free slab, or press Seat. Each seat is one step: everyone seated writes one chair.
You did what the old servers did.
A typical step, 24 of 84: 7 parties seated in 8 slabs.
37% of held chairs sit empty, counted at every step.
11,483 chairs written of 18,272 held, counted at every one of 84 steps. 1 − 11,483 ÷ 18,272 = 37% emptyIn flight: 571 seated, counted at every one of 84 steps. 571 ÷ 84 = 6.8 in flight
4 of 11 Your guess
Your guess
Before fixing anything, find out how much there is to win back.
Guess the share of what the slabs hold that sits empty.
This run: 37% empty. The paper in chapter 2: 61.8 to 79.6%, about 60 to 80. Same leak, bigger there: real servers held room for the longest reply the model allows, thousands of tokens, where a slab here is 32 chairs for parties of 14 to 32.
- You, one seat a step
- 0
- Slabs, the real run
- 6.8
- Benches of 4
- 9.7
Parties in flight, on average
- bench
- 4 chairs in a row. A party gets one more bench only when its last one is full. Same room: 64 benches × 4 = 256 chairs.
party 0 holds 4 × 4 = 16 chairs: 13 written, 3 held but empty
5 of 11 The fix
Same parties, benches of 4
The move toward the goal: the same parties, and benches handed out as each one fills.
Party 0 sits with 13 chairs written, in 4 benches of 4.
A bench is a block. Handing them out like this is paged attention.
A typical step, 48 of 57: 10 parties seated in 64 benches. In flight: 555 seated, counted at every one of 57 steps. 555 ÷ 57 = 9.7 in flight
8% of held chairs sit empty.
11,225 chairs written of 12,136 held, counted at every one of 57 steps. 1 − 11,225 ÷ 12,136 = 8% empty
6 of 11 Your guess
Guess again
Less held and empty means more parties fit.
Guess the share of what benches of 4 hold that sits empty.
Not zero, because a party's last bench is usually part full.
What changed
- Held chairs sitting empty: 37% → 8%
- Parties in flight, on average: 6.8 → 9.7
7 of 11 The idea
Where the chairs went
Take one party from that typical step, step 48: party 61, with 5 benches. Each time its last bench filled, it got whichever bench was spare right then. So its benches are 62, 17, 12, 39 and 9, scattered across the room. The numbers on the picture are bench numbers, 0 to 63.
That is the price of more parties. Its seating chart lists its benches in the order it got them, so nothing is lost.
13 = 3 × 4 + 1 Three full benches, then chair 1 of entry 3: bench 39.
The seating chart is paged attention's block table.
Party 61's seating chart
chart = [62, 17, 12, 39, 9]
BS = 4 # chairs per bench
def find(pos):
entry = ... # your line
chair = pos % BS
return entry, chair
entry, chair = find(13)
if entry is ...:
print("Fill in the blank: replace ... with the chart entry for character pos.")
elif entry == 13 % BS:
print("Not yet: pos % BS is the chair inside the bench, 1. The entry counts the whole benches before pos.")
elif entry == 13 / BS:
print("Close: pos / BS gives 3.25, a fraction. A chart entry is a whole number, so round down with //.")
elif entry == 13 // BS + 1:
print("Off by one: entries count from 0, so 13 // 4 = 3 is already the right entry.")
elif entry == chart[13 // BS]:
print("That is the bench number. entry is a place in the chart, and chart[entry] is the bench.")
elif all(find(p)[0] == p // BS for p in range(17)):
print(f"Right: character 13 is chart entry {entry}, chair {chair}, bench {chart[entry]}.")
elif entry == 13 // BS:
print("Right for 13, but not for character 2 or 9. Write it with pos and BS, not a number.")
else:
print(f"Not yet: your line put character 13 in entry {entry}. Count the full benches of {BS} before it.")
8 of 11 Try it
Your turn
Spread-out benches only work if every chair can still be found. Replace the ... with the line that finds the chart entry for any character, then run it.
Stuck? Character 13 was 3 full benches plus 1.
Change the bench size
The pool stays 256 chairs. Drag to cut it into bigger or smaller benches and watch what the same 64 parties do.
- In flight
- 9.7
- parties, on average
- Steps
- 57
- to finish all 64
- Empty
- 8%
- of held chairs
- Preempted
- 16
- times
9 of 11 The cost
The trade
How far can you push the goal? Small benches waste less but need a longer chart. Big benches are the slab again.
Watch the goal line as you drag. At 32 the room is 8 slabs again, 37% empty.
10 of 11 Your guess
Your picture now
Goal met, 6.8 to 9.7 in flight. Which picture is closest now?
Pick one.
11 of 11 Keep this
Keep this
Hand memory out in small benches as it fills, and the only waste left is each party's last, part-empty bench.
The price: the pool ran dry 16 times, and each time the newest party stepped out and was seated again later.
Coming up Every bench in this room was filled from scratch, even when parties opened with the same standing instructions. Next: name each bench by a fingerprint of everything before it, and compute a shared opening once.