KV memory
Pages, and the memory cap.
The scheduler checks for room for KV cache before admitting a new request. 80 GB on the card, 16 GB of weights, and everything else is room for KV cache. Running requests fill it a token per step.
The memory arithmetic, once
Per token, the cache costs , 128 KB for Llama-3-8B in bf16. The batch cap is
SGLang does not hand the cache all of : it sizes the pool to , where is --mem-fraction-static (about 0.83 for this model on one H100), and keeps the rest for activations and CUDA graphs. That makes the pool about 55 GB here; the stage leaves out.
Reserving the maximum length per request wastes whatever is never written, typically well over half, and fragments as requests finish. Paging with page size wastes at most tokens per request and never fragments: a request’s cache is a list of page numbers, and the attention kernel gathers through that list. In SGLang, MHATokenToKVPool holds the K and V, TokenToKVPoolAllocator hands out free slots (page size 1 by default), and req_to_token holds the page tables.
Admission (PrefillAdder) counts more than the pages a step writes. It sets aside each running request’s expected remaining output, times a ratio that starts at 0.7 and decays to about 0.1 as requests finish early, and it charges a newcomer its prompt plus . Retraction is a separate check: when the running batch’s own next step does not fit and nobody is finishing, retract_decode sends back the request with the fewest output tokens, to be prefilled again later. Sharing pages across requests is the next chapter.