solvers.ContinuousBatchingModel
solvers.ContinuousBatchingModel()Compares static KV reservation with PagedAttention under continuous batching.
Decode is memory-bound, so the number of requests sharing a step is the throughput lever, and the KV-cache allocator decides how many fit. A static allocator reserves a contiguous slot of max_seq_len tokens for every request; PagedAttention hands out fixed-size blocks on demand. This model sizes both from the same KV budget and a request-length distribution, then compares memory-bound decode throughput at each allocator’s concurrency.
Kwon et al. (2023) profile contiguous pre-allocation (their Orca baselines) and find that only 20.4% to 38.2% of KV-cache memory holds actual token states (Fig. 2). The rest is reserved slots for future tokens, internal fragmentation from over-provisioning for the maximum sequence length, and external fragmentation from the memory allocator (Sec. 3.1, Fig. 3). vLLM limits each request’s waste to one block (Sec. 4.2) and reaches 96.3% token-state usage (Fig. 2).
Scope and assumptions:
- The static baseline is max-length reservation, like Kwon’s “Orca (Max)”. It is modeled with no external fragmentation because every slot has the same size, although Kwon et al. still attribute 8.9% of Orca (Max) KV memory to external fragmentation and other overhead. Exact-size and power-of-two contiguous allocators, which trade reservation waste for external fragmentation, are not modeled.
- Both allocators are sized with every admitted request at its full length (peak occupancy). A request mid-generation holds fewer paged blocks, so paged capacity is conservative, while static reserves
max_seq_lenfor the whole request lifetime either way. - Request lengths are exponential, cut off at
max_seq_len, with meanmean_request_tokens(calc_capped_exponential_scale).
Literature Source: 1. Kwon et al. (2023), “Efficient Memory Management for Large Language Model Serving with PagedAttention,” SOSP ’23. 2. Yu et al. (2022), “ORCA: A Distributed Serving System for Transformer-Based Generative Models.”
Methods
| Name | Description |
|---|---|
| solve | Compare static max-length KV reservation with paged allocation. |
solve
solvers.ContinuousBatchingModel.solve(
model,
hardware,
max_seq_len,
mean_request_tokens=None,
max_batch_size=1,
page_size=16,
precision='fp16',
efficiency=0.5,
causal=True,
layer_overhead_ms=cal.FRAMEWORK_LAYER_TAX_MS,
)Compare static max-length KV reservation with paged allocation.
With KV budget M_KV (HBM capacity minus weights) and k KV bytes per token:
- Static reservation: every request reserves
max_seq_lentokens, soN_static = floor(M_KV / (k * S_max))and the unused share of each reservation is1 - S_mean / S_max. - Paged allocation: the budget is a pool of
floor(M_KV / (k * p))blocks and a request ofStokens holdsceil(S / p)of them, soN_paged = floor(pool / E[ceil(S / p)]). The unused share is the expected tail of each request’s last block (calc_expected_paged_kv_tokens), which grows with page size. - Both counts are capped by
max_batch_size. When the cap binds for both allocators, allocation policy cannot change throughput. - Throughput: each step streams weights plus filled KV tokens (reserved but empty slots are not read). Step latency is the larger of compute and memory time, plus the layer tax, using the same accounting as ServingModel. Tokens/s is
N / t_step; paging’s throughput gain can saturate at the compute ceiling. - TTFT is the worst-case prefill of a full scheduler batch (
max_batch_sizeprompts ofmax_seq_lentokens): linear2*P_active*Splus causal attention2*L*H*d*S^2FLOPs (or4*L*H*d*S^2whencausal=False), plus the dispatch tax.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | TransformerWorkload | The model being served (layers, heads, hidden_dim, kv_heads). | required |
| hardware | HardwareNode | The accelerator (HBM capacity/bandwidth, dispatch tax). | required |
| max_seq_len | int | Longest context a request may reach, prompt plus generated tokens. Static allocation reserves this much KV for every request. | required |
| mean_request_tokens | float | Mean KV tokens a request actually fills, in (0, max_seq_len]. Lengths are exponential, cut off at max_seq_len, with this mean (calc_capped_exponential_scale). None means every request fills max_seq_len; with no length variance, static and paged allocation coincide up to page rounding. |
None |
| max_batch_size | int | Positive scheduler cap on concurrent requests. | 1 |
| page_size | int | Positive PagedAttention block size in tokens; default 16. | 16 |
| precision | str | Numerical precision for weights and KV cache. | 'fp16' |
| efficiency | float | Software efficiency factor in (0, 1] applied to prefill and decode FLOPS. | 0.5 |
| causal | bool | Apply causal triangular self-attention masking during prefill. | True |
| layer_overhead_ms | float | Nonnegative per-layer host launch overhead in milliseconds (0.0 by default). | cal.FRAMEWORK_LAYER_TAX_MS |
Returns
| Name | Type | Description |
|---|---|---|
| ContinuousBatchingResult | Paged and static concurrency and token throughput, internal fragmentation of each allocator (fractions), allocated KV, TTFT/ITL, and speedup vs static. |
See Also
mlsysim.physics.memory.calc_expected_paged_kv_tokens : Page rounding. ServingModel.solve : Shared compute/memory decode accounting.
Notes
Equation: static concurrency is floor(KV_budget/(k*S_max)); paged concurrency is floor(pool_blocks/E[blocks_per_request]). Both are capped by max_batch_size. Throughput = active_requests/decode_step_time.
Assumptions: capped exponential request lengths, peak resident context per admitted request, fixed-size pages, peak memory bandwidth, and the shared decode Roofline including its layer tax. Excludes: external fragmentation, scheduler traces, arrival transients, workspace, tensor parallelism, and growing-context dynamics. Feasibility means modeled weights plus allocated KV fit, not engine deployability.
References
Kwon et al. (2023), “Efficient Memory Management for Large Language Model Serving with PagedAttention.” https://arxiv.org/abs/2309.06180 (bibliography key: kwon2023). The capped-exponential occupancy distribution is a modeling assumption, not a reproduction of vLLM scheduling.
Examples
>>> from mlsysim import Hardware, Models
>>> from mlsysim.solvers import ContinuousBatchingModel
>>> r = ContinuousBatchingModel().solve(Models.Language.Llama3_8B, Hardware.Cloud.H100,
... max_seq_len=2048, mean_request_tokens=512, max_batch_size=8)
>>> bool(r.feasible and r.max_active_requests <= 8 and r.throughput_tokens_per_sec > 0)
True