advanced28 min studyLesson 713 in Engineering

Reserve memory for the work an admitted request may still create

Distinguish iteration token budgets, resident KV allocation, enforceable output caps, and conservative reserve admission with explicit accounting scope.

On this page

What you’ll understand

  • Calculate rounded KV reservations under a declared maximum-length policy.
  • Distinguish a per-iteration token limit from total resident-token capacity.
  • Explain why an estimated output length cannot serve as an unqualified memory bound.

Admission decides whether the system can responsibly accept more work under its declared resource and latency policies. A request that fits its prompt today can still grow a much larger KV cache while decoding. A scheduler's per-iteration token limit also differs from the total context retained across active requests. Name each budget, its unit, and its enforcement point. Memory-safe admission needs either conservative reservations within a justified model or a tested dynamic policy with explicit preemption, rejection, or termination behavior.

Reserve against an enforceable growth policy

Let a request have P prompt tokens and an enforced maximum of O generated tokens. Under a chosen conservative policy, reserve cache capacity for P+O retained tokens, rounded to the allocator's block size. This can intentionally overreserve compared with an early EOS. If the output length is only predicted rather than capped, the reservation is an estimate and needs an overflow policy; it cannot guarantee that every admitted request fits. Count speculative slots, branches, or other extra contexts when the engine can create them.

PagedAttention explains how fixed-size KV blocks and sharing can reduce waste, but paging does not remove the need to account for real resident blocks. Shared prefixes require reference-aware accounting, and a per-request upper reservation may intentionally ignore sharing to remain conservative. Use one policy consistently rather than counting a shared block once in one dashboard and once per request in another. The reserve must correspond to the actual pool and architecture, including precision, attention variant, and supported context behavior.

ri=btok g⌈Pi+Oig⌉r_i=b_{\rm tok}\,g\left\lceil\frac{P_i+O_i}{g}\right\rceil
r_i is reserved bytes, b_tok bytes per retained KV token under a specified model, and g positive integer tokens per block. P_i and the enforced output cap O_i are nonnegative integer counts. The formula ignores sharing and includes no hidden branching; the conservative counted-token convention must cover all actual KV entries.↔ Scroll wide equations horizontally.

Keep iteration work separate from resident state

In vLLM 0.10.2, max_num_batched_tokens limits tokens processed in one iteration, while other controls and the KV allocator constrain sequences and memory. A decode iteration can process one new token for each of many requests while retaining thousands of earlier tokens per request. A small iteration budget therefore does not establish a small total resident cache. The admission decision should inspect both work and storage budgets, plus any separately enforced tenant or deadline policy.

Include fixed weights, bounded workspaces, runtime buffers, and a justified reserve outside the request KV pool. A conservative inequality is useful only if the categories cover the actual allocations and simultaneous peaks. Host output buffers and network queues may live in a different memory domain and need separate limits. Do not label a hand-estimated average as a hard cap. Validate capacity with maximum supported shapes and relevant in-flight overlap, and retain a safe behavior when an allocation still fails.

Mfixed+Mworkspace+Mreserve+∑i∈Ari≤MtotalM_{\rm fixed}+M_{\rm workspace}+M_{\rm reserve}+\sum_{i\in A}r_i\leq M_{\rm total}
All terms are bytes in the same explicitly scoped memory domain. A is the admitted reserved set. This is a sufficient accounting check only when the category bounds are justified and complete; it is not proof that every framework allocation or fragmentation pattern is covered.↔ Scroll wide equations horizontally.

Decide what happens when estimates or reservations change

An implementation can reserve worst-case caps, grow allocations dynamically, preempt and recompute, or combine strategies. Those choices trade utilization, latency, and complexity. If a request increases its cap after admission, repeat the relevant checks before accepting that growth. Cancellation and completion should release reservations according to the ownership protocol, while prefix blocks still referenced by other requests remain allocated. Track reserved, physically allocated, and reclaimable bytes separately so a quota is not confused with free physical memory.

The cache lab illustrates a finite working set fitting or failing to fit a capacity, not safe KV eviction. Reset, set both C=4,s=4, predict twelve initial hits, then Play or Step. Reset and set both C=3,s=4 for zero initial hits on the same repeating identifiers. There are sixteen initial requests and a cap of 4,096. Its LRU evictions are automatic and cheap in the toy world; an inference engine cannot discard needed context without its own supported recomputation or other correctness-preserving policy.

Round a request reserve to cache blocks

A fictional KV pool has twelve free blocks, each holding sixteen tokens at two KiB per token. A new request has ninety prompt tokens and an enforced output cap of fifty. Use the conservative P+O reservation with no sharing or extra speculative slots.

  1. The counted maximum is 90+50=140 retained tokens. Rounding to sixteen-token blocks gives ceil(140/16)=9 blocks, or 144 reserved token slots.

  2. The reservation is 144×2 KiB=288 KiB. Twelve free blocks hold 384 KiB, so the request fits this pool check and leaves three blocks.

  3. A second identical reservation needs nine more blocks and does not fit the three remaining. Its prompt length alone would underestimate the promised growth, and this pool result still does not establish deadline or total-device feasibility.

The result

One request passes the stated KV reserve check and two do not. The conclusion assumes the cap and complete per-token/block accounting are actually enforced.

Explore the model

Interactive lab

Interactive lab · cache

Inspect a working set against finite capacity

Compare fresh Reset → set both C=4,s=4 and Reset → set both C=3,s=4. Predict twelve versus zero hits over sixteen initial requests, then Play or Step. The capped 4,096-request LRU model has no KV reservations and does not make real context eviction free or safe.

Inspect a working set against finite capacityA fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Current readings: Hits / requests: 0 / 16; Hit rate: 0%; Address-stream period: 16 addresses. All parameters are adjustable with the labeled controls below.Fully associative LRU cache after 16 deterministic requests: 0 hits and 16 misses.Most recent 16 requestsaddress = i × 3 mod 160M3M6M9M12M15M2M5M8M11M14M1M4M7M10M13MCurrent cache order · oldest → newest471013Latest address 13 · miss: inserted as newestToy address stream · period 16 · starts empty
Cached addressMiss / evictionMost recent request
Hits / requests
0 / 16
Hit rate
0%
Address-stream period
16 addresses
t = 0 s

A fully associative LRU cache starts empty and requests address (i·s) mod 16. Hits move an address to the newest position; misses evict the oldest when full. Playback starts with 16 requests and adds two per second, capped at 4096 explicitly simulated requests. This toy stream is not a processor-cache benchmark. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA finite capacity must be matched to real retained state and a correctness-preserving growth policy.

Check your understanding

A system processes at most 2,000 tokens per iteration. What does that alone establish about resident KV memory?

Try it yourself

The request's output cap increases from fifty to one hundred after it reserved nine blocks. Keep P=90, block size sixteen, and two KiB per token. What new reserve is required, and can the original reservation simply be assumed sufficient?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Recompute the conservative token total before rounding. A changed promise is a new admission obligation.

Compare with the solution

The new counted maximum is 90+100=190 tokens, requiring ceil(190/16)=12 blocks or 192 token slots. That is 384 KiB, three blocks and 96 KiB more than before. The original nine-block reservation is insufficient for this policy. Growth must be accepted only after the additional resource check or handled by the documented rejection or dynamic-memory policy; changing a cap in metadata does not create capacity.

Take it with you

  • Admission must cover enforceable retained-state growth rather than prompt length alone.
  • Per-iteration token limits do not bound total resident KV memory.
  • Reservations, physical allocation, sharing, and safe release require consistent scoped accounting.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.