solvers.ServingModel
solvers.ServingModel()Analyzes the two-phase LLM serving lifecycle: Pre-fill vs. Decoding.
LLM inference is not a single mathematical operation; it is a stateful process with two phases. Prefill is modeled with a compute bound; decode compares compute and memory bounds because the limiting resource depends on batch size and context length.
Literature Source: 1. Pope et al. (2023), “Efficiently Scaling Transformer Inference.” 2. Agrawal et al. (2024), “Sarathi-Serve” (chunked prefill scheduling). 3. Patel et al. (2024), “Splitwise” and Zhong et al. (2024), “DistServe” (prefill/decode disaggregation).
Formula contract: - TTFT is prefill compute over new prompt tokens plus dispatch overhead and, for disaggregated serving, KV-transfer time. - ITL is max(decode compute time, weight/KV memory time), plus the configured framework layer tax. Efficiency derates compute only. - KV memory always covers the full sequence, even when prompt caching skips part of the prefill computation.
Methods
| Name | Description |
|---|---|
| solve | Solve for LLM serving performance. |
solve
solvers.ServingModel.solve(
model,
hardware,
seq_len,
batch_size=1,
precision='fp16',
efficiency=0.5,
decode_hardware=None,
network_bandwidth=Q_('100 GB/s'),
draft_model=None,
draft_acceptance_rate=0.7,
cached_prefix_len=0,
prefill_chunk_tokens=None,
causal=True,
layer_overhead_ms=cal.FRAMEWORK_LAYER_TAX_MS,
)Solve for LLM serving performance.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | TransformerWorkload | The primary model to be served. | required |
| hardware | HardwareNode | The hardware node for serving (or pre-fill node in disaggregated serving). | required |
| seq_len | int | Positive context length in tokens per request, including cached prefix. | required |
| batch_size | int | Positive simultaneous requests sharing one decode iteration. | 1 |
| precision | str | Weight and KV element precision, such as fp16 or int8. | 'fp16' |
| efficiency | float | Dimensionless compute fraction in (0, 1]; memory uses peak bandwidth. | 0.5 |
| decode_hardware | HardwareNode | If provided, models Disaggregated Serving where ‘hardware’ does pre-fill and ‘decode_hardware’ does decoding. KV-cache is transferred over the network. | None |
| network_bandwidth | Quantity | Positive transfer rate in bytes/time between prefill and decode nodes. | Q_('100 GB/s') |
| draft_model | TransformerWorkload | If provided, models Speculative Decoding using this smaller draft model. | None |
| draft_acceptance_rate | float | Expected acceptance rate (0.0 to 1.0) of draft tokens per step. | 0.7 |
| cached_prefix_len | int | Nonnegative count of tokens with precomputed KV cache (prefix caching). When > 0, the prefill phase only processes (seq_len - cached_prefix_len) new tokens, reducing TTFT proportionally. The full KV-cache (including cached prefix) still occupies memory. Must be < seq_len. | 0 |
| prefill_chunk_tokens | int | If provided, split new prefill tokens into chunks of at most this size. This estimates a Sarathi-Serve-style chunked-prefill stall proxy: total TTFT keeps the same compute work plus one dispatch tax per chunk, while decode_stall_bound reports the slowest single chunk that can interfere with ongoing decode iterations. It is not a full scheduler simulation. | None |
| causal | bool | Apply causal triangular self-attention masking during prefill (2 * L * d_attn * B * S^2 at zero cached prefix) rather than full bidirectional S^2 attention. |
True |
| layer_overhead_ms | float | Nonnegative per-layer host launch overhead in milliseconds (0.0 by default for graph-captured execution). | cal.FRAMEWORK_LAYER_TAX_MS |
Returns
| Name | Type | Description |
|---|---|---|
| ServingResult | TTFT and ITL in time units, required memory, and constraint traces. Feasibility checks modeled resident storage on each device; it does not guarantee a serving engine can deploy the configuration. |
Notes
Equation: T_step = max(F_step / (eta * peak), (W + KV) / BW) + T_layer. F_step includes linear work and both attention matrix products; W is resident weight storage and KV covers all requests. The helper shared with ContinuousBatchingModel implements this bound.
Assumptions: peak memory bandwidth, one weight read per decode step, and ideal compute/memory overlap. Efficiency eta changes compute only. The per-layer tax is a separate runtime assumption. Excludes: runtime workspaces, allocator reserve, and tensor-parallel collectives. Speculation adds draft weights and KV storage; disaggregation checks prefill and decode capacities independently. For K draft proposals, target verification evaluates K+1 query positions, including the bonus-token distribution, before amortizing by expected output tokens. Prefill remains a compute-time approximation, not a kernel schedule.
References
Williams, Waterman, and Patterson (2009), “Roofline: An Insightful Visual Performance Model for Multicore Architectures.” https://doi.org/10.1145/1498765.1498785 (bibliography key: williams2009). Pope et al. (2023), “Efficiently Scaling Transformer Inference.” https://arxiv.org/abs/2211.05102 (bibliography key: pope2023efficiently). Leviathan et al. (2023), “Fast Inference from Transformers via Speculative Decoding,” Algorithm 1 (bibliography key: leviathan2023fast). https://arxiv.org/abs/2211.17192. These sources motivate the bounds; the layer tax and feasibility conventions above are MLSysIM modeling choices.
Examples
>>> from mlsysim import Hardware, Models
>>> from mlsysim.solvers import ServingModel
>>> result = ServingModel().solve(
... Models.Language.Llama3_8B, Hardware.Cloud.H100, seq_len=2048)
>>> bool(result.feasible and result.itl.m_as("ms") > 0)
True