Skip to main content

solvers.BatchingOptimizer

solvers.BatchingOptimizer()

Finds the maximum batch size that satisfies a P99 latency SLA.

Searches the continuous batching design space using an M/M/c queueing model to find the optimal balance between throughput and tail latency.

Methods

Name Description
solve Determine the maximum batch size that satisfies a P99 latency SLA.

solve

solvers.BatchingOptimizer.solve(
    model,
    hardware,
    seq_len,
    sla_latency_ms,
    arrival_rate_qps,
    num_replicas=1,
    precision='fp16',
    efficiency=0.5,
    max_search_batch=256,
)

Determine the maximum batch size that satisfies a P99 latency SLA.

Exhaustively scans batch sizes 1..max_search_batch. For each candidate b, the per-batch service latency is TTFT + ITL * seq_len from ServingModel, and the batch arrival rate is arrival_rate_qps / b (requests are grouped into batches). A candidate is feasible when the model fits in memory, the M/M/c queue is stable, and the queueing P99 meets the SLA. Among feasible candidates the largest batch wins by the chosen objective; this is not a separate optimization of saturation throughput.

Parameters

Name Type Description Default
model TransformerWorkload The model being served. required
hardware HardwareNode The serving accelerator. required
seq_len int Tokens generated per request (used as the decode count). required
sla_latency_ms float P99 latency SLA in milliseconds. required
arrival_rate_qps float Aggregate request arrival rate in queries per second. required
num_replicas int Number of serving replicas (the ‘c’ in M/M/c). 1
precision str Weight and KV element precision, such as fp16 or int8. 'fp16'
efficiency float Software efficiency factor in (0, 1]. 0.5
max_search_batch int Upper bound of the batch-size search range. 256

Returns

Name Type Description
BatchingOptimizerResult Best batch size, resulting throughput (tokens/s), P99 latency, and SLO headroom; is_feasible=False if no batch size works.

See Also

ServingModel.solve : Candidate service and capacity calculation. TailLatencyModel.solve : Queueing estimate for grouped arrivals.

Notes

Equation: service_time(b) = TTFT(b) + seq_len*ITL(b), with batch arrival rate qps/b. Search maximizes b among memory-feasible, stable candidates whose estimated P99 is within sla_latency_ms.

Assumptions: positive SLA, arrival rate, sequence length, replica count, and search cap; fixed context and grouped arrivals. seq_len is used as both prompt/context length and generated token count in this scenario. Excludes: time spent forming batches, growing KV contexts, and dynamic scheduling. Returned max_throughput is offered qps*seq_len, not a measured or independently optimized saturation capacity. No feasible candidate returns is_feasible=False and batch zero.

References

Derivation: enumerate the integer batch domain, apply modeled capacity and latency constraints, and choose the largest admitted batch.

Examples

>>> from mlsysim import Hardware, Models
>>> from mlsysim.solvers import BatchingOptimizer
>>> r = BatchingOptimizer().solve(Models.Language.Llama3_8B, Hardware.Cloud.H100,
...     seq_len=8, sla_latency_ms=10000, arrival_rate_qps=1, max_search_batch=2)
>>> bool(r.is_feasible and 1 <= r.best_batch_size <= 2)
True
Back to top