engine.solvers.serving_scale
engine.solvers.serving_scale
Serving-at-scale solvers: KV-cache admission and quantization capacity dividends.
Domain implementations behind mlsysim.solvers (the public import path, derived from engine.solvers.__init__). The KV-cache byte accounting lives in physics.memory (calc_kv_cache_size); these models size admitted request concurrency and precision dividends on one stated replica (added 2026-10-20).
Classes
| Name | Description |
|---|---|
| KvAdmissionModel | How many requests of one context length a replica’s KV budget admits. |
| PrecisionDividendModel | The maximum batch before and after lower-precision weights and KV cache. |
KvAdmissionModel
engine.solvers.serving_scale.KvAdmissionModel()How many requests of one context length a replica’s KV budget admits.
The budget is the replica’s HBM minus its weights and a runtime reserve; each request holds 2 L H_kv d_head b bytes per token for its whole context (calc_kv_cache_size). Paging changes none of these bytes, only the waste around them.
Methods
| Name | Description |
|---|---|
| solve | Divide a replica’s available KV memory by one request’s KV footprint. |
solve
engine.solvers.serving_scale.KvAdmissionModel.solve(
model,
accelerators,
hbm_per_accelerator,
weight_bytes_per_param,
reserve,
context_tokens,
kv_bytes_per_elem,
)Divide a replica’s available KV memory by one request’s KV footprint.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | TransformerWorkload | Transformer dimensions and parameter count; hidden width must be divisible by attention heads for the integer head width used here. | required |
| accelerators | int | Positive number of devices whose HBM is pooled into the replica. | required |
| hbm_per_accelerator | Quantity | Positive memory capacity per accelerator, convertible to bytes. | required |
| weight_bytes_per_param | Quantity | Positive bytes stored per weight element, expressed as a byte quantity. | required |
| reserve | Quantity | Nonnegative total replica memory reserved for other runtime state. | required |
| context_tokens | int | Positive resident context tokens per request. | required |
| kv_bytes_per_elem | Quantity | Positive bytes stored per key or value element. | required |
Returns
| Name | Type | Description |
|---|---|---|
| KvAdmissionResult | Weights and KV budget in GB, per-token/per-request cache storage, and integer request capacity. Negative budgets are not clamped; they indicate an infeasible allocation rather than a valid admission count. |
See Also
mlsysim.physics.memory.calc_kv_cache_size : Key/value tensor footprint.
Notes
Equation: KV_request = 2*layers*kv_heads*head_dim*context_tokens*element_bytes; budget = accelerators*HBM - weights - reserve; max_requests = int(budget/KV_request).
Assumptions: uniform request lengths and a perfectly pooled replica budget. For a nonnegative budget, integer truncation implements floor division. Missing kv_heads falls back to the query-head count.
Excludes: per-device imbalance, communication, block fragmentation, and workspace beyond the explicitly supplied reserve.
References
Derivation: count both key and value tensors for every layer and resident token, subtract static allocation, and divide remaining bytes by request bytes.
Examples
>>> from mlsysim import Models, Q_
>>> result = KvAdmissionModel().solve(Models.Language.Llama3_8B, 1,
... Q_("80 GB"), Q_("2 byte"), Q_("4 GB"), 1024, Q_("2 byte"))
>>> result.max_requests > 0
TruePrecisionDividendModel
engine.solvers.serving_scale.PrecisionDividendModel()The maximum batch before and after lower-precision weights and KV cache.
Weights and each request’s KV cache split evenly across tp_size devices. Lower-precision weights raise each device’s KV budget once; lower-precision KV shrinks every request’s share. The dividend is the ratio of the two maximum batches.
Methods
| Name | Description |
|---|---|
| solve | Compare KV admission limits before and after reducing storage precision. |
solve
engine.solvers.serving_scale.PrecisionDividendModel.solve(
model,
tp_size,
hbm_per_accelerator,
context_tokens,
kv_heads,
weight_bytes,
weight_bytes_low,
kv_bytes,
kv_bytes_low,
)Compare KV admission limits before and after reducing storage precision.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model | TransformerWorkload | Transformer dimensions and weight count; hidden width must divide by heads. | required |
| tp_size | int | Positive tensor-parallel degree for even weight and KV sharding. | required |
| hbm_per_accelerator | Quantity | Positive device capacity, convertible to bytes. | required |
| context_tokens | int | Positive resident tokens per request. | required |
| kv_heads | int | Positive key/value-head count in the modeled attention layout. | required |
| weight_bytes | Quantity | Positive baseline bytes per weight element. | required |
| weight_bytes_low | Quantity | Positive alternative bytes per weight element. | required |
| kv_bytes | Quantity | Positive baseline bytes per key/value element. | required |
| kv_bytes_low | Quantity | Positive alternative bytes per key/value element. | required |
Returns
| Name | Type | Description |
|---|---|---|
| PrecisionDividendResult | Weight and KV byte budgets per device, whole-request capacities, and dimensionless capacity dividend. Baseline capacity must be positive because the dividend divides by it; no feasibility flag is returned. |
See Also
mlsysim.physics.memory.calc_kv_cache_size : KV tensor footprint.
Notes
Equation: budget = HBM - weights/tp_size; batch = int(budget / (KV_request/tp_size)); dividend = batch_low/batch. Both branches use the same context and head count, varying only element sizes.
Assumptions: even sharding and identical devices; usable HBM is devoted only to weights and KV. Integer truncation is a floor only for nonnegative budgets.
Excludes: quantization accuracy, dequantization cost, workspace, allocator reserve, and communication. A memory dividend is not a throughput prediction.
References
Derivation: divide per-device free bytes by the per-device request footprint for each representation, then compare the integer capacities.
Examples
>>> from mlsysim import Models, Q_
>>> result = PrecisionDividendModel().solve(Models.Language.Llama3_8B, 1,
... Q_("80 GB"), 1024, 8, Q_("2 byte"), Q_("1 byte"), Q_("2 byte"), Q_("1 byte"))
>>> result.max_batch_low >= result.max_batch
True