Sub topic 1
Open-weight models and what vLLM loads
Prerequisites
This chapter assumes basic Python and command-line use, a general idea of what a prompt and a completion are, and a rough understanding that GPUs have a fixed amount of memory. You should be able to read a short Python script.
Open-weight models
An open-weight model is a language model whose trained weights are published for anyone to download, usually from the Hugging Face Hub. vLLM loads these weights and reads the model's configuration file, which lists the number of layers, attention heads, and head size. Those values determine how much GPU memory each token needs later.
How generation works: prefill and decode
Generation has two phases. Prefill processes all prompt tokens in one parallel pass and builds the model's internal state for them. Decode then produces one new token per step, feeding each token back into the model until the reply ends.
The KV cache and why memory limits throughput
The key-value (KV) cache stores attention keys and values for every token in every layer, so decode can reuse past work instead of recomputing it. The cache grows with each token of each active request. The following script estimates its size:
# Estimate KV cache size per token for a model stored in fp16.
layers = 28
kv_heads = 2
head_dim = 128
bytes_per_value = 2 # fp16
# Keys and values are both stored, hence the factor of 2.
bytes_per_token = 2 * layers * kv_heads * head_dim * bytes_per_value
print(f"KV cache per token: {bytes_per_token} bytes")
print(f"Tokens in 1 GiB: {(1024**3) // bytes_per_token}")> Note: the cache uses only the memory left after the weights load, so the cache size sets how many requests can run at once.
PagedAttention: managing the KV cache in blocks
Servers that reserve one large contiguous region per request waste memory on space the request never uses. PagedAttention splits the KV cache into fixed-size blocks, similar to memory pages in an operating system. Each request maps its tokens to whichever blocks are free, which reduces waste and lets more requests fit on the GPU.
Continuous batching and the scheduler
A batch is a group of requests processed together on the GPU. Static batching waits for the whole group to finish before starting new work. Continuous batching lets the scheduler add a waiting request at any decode step and remove a finished request right away, which keeps the GPU busy. At each step, the scheduler checks free KV blocks to decide which requests run.
Exercise
A model uses 28672 bytes of KV cache per token, and 2 GiB of GPU memory is free for the cache. Roughly how many tokens fit?
Show answer
2 GiB is 2147483648 bytes. Dividing by 28672 gives about 74898 tokens.