Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Techniques

Batched Inference Optimization

About 3636 characters Updated Nov 30, 2026 22 views AI wiki

Batched inference optimization is the engineering discipline of scheduling requests and laying out memory so that one model handles more requests per unit time while keeping per-request latency within an acceptable range. It is the dividing line between a large model service that merely runs and one that makes money: on the same GPU, throughput before and after tuning often differs by more than five times.

Core principles

Language model inference has two stages: the prefill stage computes the entire input at once, which is compute-intensive matrix multiplication, and the decode stage generates one token per step, which is memory-intensive vector computation. If the two are mixed in static batches, a batch must wait for the longest sequence to finish before it can be released, short requests are held up by long ones, and the GPU idles badly.

Continuous batching solves this problem: after each decode step the scheduler checks the queue, evicts finished sequences and immediately inserts new requests, so batch composition changes from step to step. This keeps the GPU fully loaded at the cost of more complex scheduling and memory management.

Paged attention splits the key-value cache into fixed-size blocks and maps logical addresses to physical blocks much like an operating system page table; requests sharing a prefix can share blocks, achieving efficient reuse within and across sequences while eliminating the memory waste of preallocating the maximum length.

Chunked prefill cuts the prefill of a long input into several chunks and interleaves them with decode requests, preventing one long prompt from occupying the whole card for tens of milliseconds and stalling all requests currently decoding.

Engineering also requires attention to capacity sizing: stress test with the real traffic distribution first, measure the maximum arrival rate at the target tail latency, then reserve headroom for peaks; when capacity is short, prioritize length tiering and degradation rather than blindly adding cards. On caching strategy, system prompts and tool definitions should be placed at the very front of the request and kept byte-identical, otherwise prefix caching cannot hit; multi-turn conversations should keep the full history rather than reassembling it, to preserve the prefix structure.

For monitoring, record prefill token count and decode token count separately, and keep statistics per model version, so that a throughput change can be traced either to scheduling issues or to a slower model.

Key points

Distinguish throughput-first from latency-first goals: the former maximizes batch size and allows queuing, suitable for offline batch processing and overnight jobs; the latter limits batch size and queue length and, when necessary, queues long requests separately. Prefix caching is a free gain when the system prompt is fixed, and at high hit rates time to first token drops noticeably.

Frequently tuned parameters include maximum batch tokens, maximum scheduled sequences per step, block size of 16 or 32, and the memory reservation ratio. Too low a reservation causes frequent block preemption and recomputation, while too high a reservation wastes capacity.

Speculative decoding can be layered on top of batch optimization, with a small model drafting and a large model verifying; it speeds up noticeably at low concurrency, but the gain is diluted in already saturated large batches.

Common misconceptions

First, looking only at throughput and not tail latency. Evaluating service capacity by average latency hides the slowest group of users; P99 is the true boundary of experience.

Second, assuming bigger batches are always better. Beyond memory limits, preemption and recomputation are triggered and throughput collapses, producing the strange phenomenon that adding concurrency makes things slower.

Third, ignoring the request length distribution. Inputs with an extremely long tail degrade the scheduler, and in practice length tiering is often more effective than buying faster GPUs.

Application scenarios

Online question answering services, batch content generation, corpus annotation pipelines, large-scale inference over evaluation sets, concurrent load for multi-turn dialogue systems, and capacity planning for private deployments.