research29 min studyLesson 763 in Engineering

Context-window retention and eviction

Distinguish logical attention history from reusable physical cache storage and avoid treating surviving state as an exact archive.

On this page

What you’ll understand

  • Separate cache eviction that causes recomputation from policies that remove attention history.
  • Calculate retained KV storage for a declared model.
  • Explain indirect state influence without promising recovery of evicted facts.

The word cache can describe a reusable computation store or the live history available to the next attention operation. Evicting an inactive reusable prefix block can merely require later recomputation, while discarding a token from the active attention history can change the function being evaluated. This lesson uses versioned Transformers 4.53.2 and vLLM 0.10.1 documentation to keep those meanings separate. Neither a memory saving nor uninterrupted generation proves faithful recall of earlier evidence.

Specify what eviction removes

An ordinary full-attention decoder uses cached keys and values to avoid recomputing the prefix for each next token. If a serving cache releases reusable inactive blocks but retains the original input and can recompute under the same model and state contract, that is a storage and latency policy. vLLM’s prefix-cache design describes such block reuse and eviction. It does not imply that an active request may arbitrarily discard required history while preserving identical full-attention outputs. Name the cache object and its ownership when discussing an eviction.

A sliding-window attention policy instead limits which prior token states are directly accessible. Transformers 4.53.2 documents a sliding-window cache for models that support that architecture; it is not a universal plug-in replacement for every full-attention model. Removing old direct KV entries does not erase every influence those tokens had on later states, because retained states may already encode some of their effects. But those indirect representations are not a guaranteed lossless archive and do not establish exact retrieval of an evicted record.

MKV=2LTHKVdhbM_{\mathrm{KV}}=2LTH_{\mathrm{KV}}d_hb
For one sequence and uniform full per-layer KV layout, L is layer count, T retained token slots, H_KV key/value head count, d_h elements per head, and b bytes per element. The factor two counts keys and values. Metadata, padding, temporary storage, sharing, quantization details, and architecture-specific layouts are excluded.↔ Scroll wide equations horizontally.

Representation changes require their own quality tests

Retaining raw text outside the device cache can permit later retrieval or recomputation, while a summary or compressed state offers a different information contract. A quantized cache may preserve the same logical token set with numerical approximation; an evicted-window policy removes direct access to some tokens. These are distinct changes and need separate baselines. Check whether an apparent memory reduction comes from fewer retained positions, fewer bits per element, shared prefixes, or simply moving state to another storage tier.

Position handling also matters when a cache rolls or its blocks move. Physical slot indices are not necessarily logical token positions, and reusing a storage slot must not silently reuse the wrong positional encoding. Compare legal cached execution with an appropriate reference for the intended attention policy. When the policy itself changes, a difference from full attention is expected and should be evaluated on task quality, not dismissed as harmless because generation stays fluent. Record the visible history at each evidence-dependent test.

Ht={max⁡(1,t−W+1),…,t},∣Ht∣=min⁡(t,W)\mathcal H_t=\{\max(1,t-W+1),\ldots,t\},\qquad |\mathcal H_t|=\min(t,W)
This is a declared one-based recency-only history after t tokens, for positive integer window W. It identifies direct retained token positions, not all information encoded indirectly in states. Real layered sliding or hybrid architectures can have different effective receptive fields and storage policies.↔ Scroll wide equations horizontally.

Test recalled evidence at the actual retention boundary

Place a fresh fact before, near, and after the eviction boundary, then ask questions that require its precise value and provenance. Include a condition that retrieves the original text again and another that does not. This distinguishes an external-memory path from apparent parametric knowledge or vague continuation. Monitor cache residency, recomputation work, and task outputs together. A stable token-generation rate after hours of streaming measures operational continuity; it does not demonstrate access to every historical token.

The cache lab illustrates only reusable-entry capacity. Reset, set capacity to 4 and stride to 4, predict hits at seventeen requests, then Step once: thirteen hits. Compare a fresh Reset with both capacity at 2 and stride at 4 before Step: zero hits. The toy starts empty with sixteen requests already processed and is capped at 4,096. It has no KV tensors, logical token positions, language model, or reconstruction path, so use it only to reason about a declared working set.

Memory saved by a declared direct-history limit

A toy decoder has two layers, two KV heads, head dimension four, and two-byte elements. It has processed twelve tokens. Compare full retention with a four-token recency window, ignoring every other allocation and any indirect state influence.

  1. KV storage per retained token is 2 × 2 × 2 × 4 × 2 = 64 bytes. Full twelve-token storage is 12 × 64 = 768 bytes.

  2. The four-token window stores 4 × 64 = 256 bytes, reducing this counted component by 512 bytes. Its directly retained positions are nine through twelve under the one-based policy.

  3. A fact introduced only at token three no longer has its direct KV entry. Retained states may contain some influence, but the memory calculation cannot establish exact fact recovery; retaining source text and using a defined retrieval or recomputation path is a different contract.

The result

Storage accounting identifies retained slots, not the completeness of semantic memory.

Explore the model

Interactive lab

Interactive lab · cache

Capacity without a semantic-memory claim

Reset, set C = 4 and s = 4, predict hits at seventeen requests, then Step once: thirteen. Compare a fresh Reset with both C = 2 and s = 4 before Step: zero. This initial-sixteen, capped-4096 fully associative LRU toy has no token history, KV state, or language model.

Capacity without a semantic-memory claimA 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 forPhysical reuse and functional access to historical evidence require different contracts.

Check your understanding

A serving system evicts an inactive reusable prefix block and later recomputes it from the same original input. How does this differ from deleting old tokens from active attention history?

Try it yourself

A declared cache needs 96 bytes per retained token. After sixteen tokens, keep only the most recent six. Calculate full and retained bytes, the reduction, and the directly visible one-based positions.

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

Give me a hint

Apply the stated storage rate and recency set; do not infer a semantic recall percentage from the byte ratio.

Compare with the solution

Full storage is 16 × 96 = 1,536 bytes, while retained storage is 6 × 96 = 576, saving 960 bytes in this component. Direct positions are eleven through sixteen. Earlier influence can survive indirectly, but those states do not guarantee exact access to tokens one through ten. Other allocations and architecture-specific receptive fields are outside the toy accounting.

Take it with you

  • Name whether eviction changes reusable storage or the live logical attention history.
  • KV byte counts depend on retained slots and architecture-specific representation.
  • Surviving state and fluent continuation do not prove lossless historical recall.

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.