Continuous batching from first principles
Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill. The KV cache cuts the cost of generating each new token from O(n²) to O(n) at O(n) memory, about 16 KB per token per layer for Llama-2-7B in float16. Chunked prefill splits long prompts across forward passes. Padded batched generation wastes compute when prompts finish at different times, and dynamic batching needs heavy padding because newly inserted prompts must prefill while others decode, which motivates continuous batching's mixing of prefill and decode tokens in one forward pass.
Related posts
- Prefill and Decode for Concurrent Requests - Optimizing LLM Performance
Hugging Face mediumAdeptLLM token generation splits into a compute-bound prefill phase, which processes all prompt tokens in parallel to produce the first token, and a memory-bandwidth-bound decode phase, which produces later tokens one at a time. These map to time to first token and time per output token. Static batching leaves the GPU underutilized and delays new requests. Prefill-first continuous batching (vLLM, TGI) minimizes time to first token but pauses running decodes. Chunked prefill interleaves prefill chunks with decode steps, trading a small time-to-first-token increase for smoother decoding. At TNG it raised total throughput by about 50%, and the default chunk size is recommended when load patterns are unpredictable.
- Cache-aware prefill–decode disaggregation (CPD) for up to 40% faster long-context LLM serving
Together AI hardMasterCache-aware prefill–decode disaggregation (CPD) extends standard PD serving with a dedicated pre-prefill tier for low-reuse (cold) long prompts, a router that estimates prefix-cache reuse per request, and a three-level KV cache hierarchy (GPU memory, host DRAM, RDMA-connected distributed cache). Cold prefills compute new context and write KV state to the distributed cache, so warm requests on normal prefill nodes fetch KV blocks instead of queueing behind multi-second prefills. On B200 GPUs with a synthetic coding-agent workload, CPD raised sustainable throughput from about 0.75–0.8 to 1.1–1.15 QPS per GPU (~40%) and kept median TTFT far lower near saturation, without worsening p90 tail latency. The gains come from scheduling and cache reuse on the prefill path, not faster model execution.
- Unlocking Longer Generation with Key-Value Cache Quantization
Hugging Face mediumAdeptHugging Face Transformers adds KV cache quantization, inspired by the KIVI paper, that stores keys and values per-token in int2/int4/int8 via quanto or HQQ backends. A residual cache of the most recent tokens (128 by default) stays in full precision to avoid re-quantizing on every step and to preserve accuracy. On Llama-2-7B, int4 matches fp16 on PG-19 perplexity and LongBench, while int2 degrades noticeably. Int4 gives roughly 2.5x memory savings and, with FlashAttention on an 80GB A100, raises max context from 40k to 128k tokens, at the cost of slower generation at larger batch sizes and about 3x slowdown when combined with weight quantization.
- Making open models work for long-horizon agentic workloads
Together AI mediumAdeptAgentic coding workloads resend the whole growing transcript each turn while emitting short tool calls, so they are prefill-dominated and depend on prefix caching, which only reuses work up to the first changed token. On the harness side that means keeping the system prompt and tool list identical, appending late-loaded tool definitions at the end, fixing reasoning effort for the session, truncating tool output with explicit range markers, and choosing between inheriting context or sending briefs to subagents. On the engine side it means cache-aware and load-aware routing (prompt_cache_key or longest-prefix hashing), offloading idle caches to CPU memory, PagedAttention and continuous batching, and disaggregated prefill/decode clusters linked over RDMA. Cache memory headroom, not compute, usually limits concurrency, so both sides must cooperate to keep long sessions fast.
- KV Cache from scratch in nanoVLM
Hugging Face mediumAdeptKV caching avoids recomputing keys and values for earlier tokens during autoregressive generation, and implementing it from scratch in the pure-PyTorch nanoVLM codebase gave a 38% speedup in generation. The walkthrough shows the redundancy in self-attention with small PyTorch examples. It then makes three changes: the grouped attention block appends new K/V to a per-layer cache, the language model tracks the cache per layer and uses start_pos to keep rotary position embeddings correct, and generation is split into a prefill phase and a token-by-token decode phase. The tradeoff is memory for speed, plus more complex code that makes schemes like beam search harder.