Admit and test multimodal requests against representation-aware resource and failure contracts
Account for decoder cache, encoder peaks, preprocessing, request cohorts, and declared degraded paths without turning a toy capacity estimate into a measured latency guarantee.
On this page
The question for today
What you’ll understand
- Compute a scoped decoder KV-cache estimate using the actual KV-head count.
- Combine simultaneous memory components under an explicit budget.
- Test invalid, missing, oversized, and timed-out media alongside quality and completed tail latency.
Two requests with the same text length can have very different cost when one carries many high-resolution crops or a long sampled video. Preprocessing and encoding can peak before generation, while retained visual context and generated tokens grow decoder state. Serving therefore needs representation-aware admission, batch accounting, and a clear failure contract. A successful string is not enough: the system must preserve which evidence it used, meet the declared resource envelope, and report unsupported or degraded conditioning without silently changing the task.
Count the state the selected decoder actually retains
For a simple full-attention autoregressive decoder with uncompressed keys and values, cache bytes depend on batch, cached positions, layer count, KV-head count, head dimension, and bytes per scalar. Use KV heads rather than assuming they equal query heads in grouped-query attention. Visual prefix positions count if the architecture stores them in this decoder sequence. Separate visual cross-attention memory or encoder caches require additional accounting. Sliding, chunked, quantized, offloaded, and statically allocated caches have different shapes or residency behavior, so this formula is an explicit model rather than a universal serving rule.
Reserve generation according to the service's policy and compatible cache implementation. A static cache can allocate its maximum capacity before every position is used; a dynamic cache may grow and create transient allocation effects. Beam or candidate expansion can increase effective batch. Include processor output length rather than only raw prompt text. Versioned Transformers 4.57.1 cache documentation describes different cache strategies, but availability and limits remain model-specific. Measure real allocation and support rather than selecting a strategy from its name alone.
Memory and time follow different stage overlaps
Weights, decoded media, encoder intermediates, projected features, decoder cache, and temporary workspaces can overlap in memory. Peak usage is a simultaneous sum, not the sum of each component's separate maximum. Decode and resize may run on the host while features and caches reside on a device, so keep memory domains separate. A compressed upload's small byte count says little about its decoded dimensions or resulting patch count. Enforce declared pixel, duration, crop, and context limits before an oversized request reaches an expensive late stage.
Latency likewise depends on required dependencies and queues. Independent modality encoders may overlap, while a shared device can serialize them or cause contention. Prefill, generation, transfer, and feature-cache behavior vary by representation length and cohort. Measure completed request latency and time to useful output under stated failure handling; adding stage tail percentiles does not produce an end-to-end percentile. Report invalid and timed-out attempts rather than improving a successful-only metric by excluding the hardest media or longest contexts.
Run a failure matrix with preserved evidence lineage
Test missing media, corrupt decode, unsupported formats, excessive processed length, feature-count mismatch, encoder errors, cancellation, and stage deadlines. Include mixed-validity batches to check isolation and resource cleanup. A documented text-only fallback must not claim to have inspected failed visual evidence, and a retry must not reuse another request's feature state. Record model, processor, media, connector, and serialization versions with the outcome. Pair resource tests with grounding and task-quality cohorts so fitting the budget does not hide information loss or a changed conditioning contract.
The queue lab illustrates a separate capacity-warning mechanism. Reset, set both λ=2 and μ=3, predict the stable M/M/1 mean residence one second, then Play or Step. Reset and set both λ=2.5 and μ=3 for two seconds. The visible fluid backlog starts at four and follows max(0,4+(λ−μ)t); the separate mean assumes Poisson arrivals and exponential single-server service. There are no encoders, cache bytes, media shapes, failure paths, or measured tail percentiles. Use actual completed-request and memory measurements to validate a multimodal serving contract.
Work it through
Generation reserve grows a scoped cache account
A hypothetical full-attention decoder serves batch 2 with 24 layers, eight KV heads, head dimension 64, two-byte keys and values, and 512 cached positions per request. Reserve 128 additional positions. In the same stated device-memory budget, separately accounted simultaneous non-KV state peaks at 12 MiB, and the available budget for these counted categories is 80 MiB. Exclude all other categories only because they are already accounted outside this available budget.
Initial cache bytes are 2×2×24×512×8×64×2=50331648 bytes, or 48 MiB using 2²⁰ bytes per MiB. The eight-head factor is explicitly KV heads, not an inferred query-head count.
With the generation reserve, cached length becomes 640, so the same scoped cache model needs 48×640/512=60 MiB. This assumes the declared full-attention representation and no additional beam expansion.
At the stipulated coincident peak, adding 12 MiB of the other counted state gives 72 MiB, leaving 8 MiB under the available 80 MiB budget. Any omitted transient, external, or allocation effect would invalidate treating this partial account as an actual admission guarantee.
The estimate is useful because its representation, concurrency, and memory scope are explicit. Validate actual cache strategy, peaks, task quality, and failure behavior before using it operationally.
The living notebook
Explore the model
Interactive lab · queue
Headroom sensitivity is separate from multimodal memory admission
Reset, set both λ=2 and μ=3, predict the separate stable M/M/1 mean 1 s, then Play or Step. Reset and re-enter both λ=2.5 and μ=3 for 2 s. Backlog is fluid max(0,4+(λ−μ)t) from four; the residence assumes Poisson arrivals and exponential single-server service. No media, encoder, KV cache, failure, or measured tail is modeled.
- Fluid backlog
- 4
- Net rate · λ − μ
- −1 /s
- M/M/1 mean residence
- 1 s
Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. 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 forCheck actual representation-aware peaks and completed outcomes rather than using a toy mean as a serving guarantee.
Pause & reason
Check your understanding
Your turn
Try it yourself
A toy full-attention decoder has batch 1, twelve layers,256 cached positions, four KV heads, head dimension 64, and two bytes per scalar. Compute resident K-and-V bytes and MiB. If cached length doubles with everything else fixed, what changes and what remains unproved?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Include the factor two for keys and values, then divide bytes by 2²⁰.
Compare with the solution
The estimate is 2×1×12×256×4×64×2=3145728 bytes=3 MiB. Doubling length gives 6 MiB under this same uncompressed full-attention model. It does not prove total device fit, actual allocation, latency, supported media conditioning, or output quality because all other memory and execution behavior still require measurement.
Keep the idea
Take it with you
- Resource admission follows actual processed media, decoder representation, and cache strategy.
- Memory peaks and request tails require simultaneous, dependency-aware measurement with explicit scope.
- Test failures and degraded paths while preserving evidence lineage and meaningful task-quality checks.
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.