Measure the wasted slots of static batching
Why it matters This calculation explains why every modern LLM server switched from static to continuous batching and why length bucketing helped before that.
Static batching groups requests and keeps the whole batch busy until its longest request finishes. Short requests sit idle in their slot, which is the waste continuous batching removes.
Write static_batch_waste(out_lens, batch_size, sort_desc=False). Requests are cut into consecutive batches of batch_size in list order (sorted by length descending first if sort_desc is True); the last batch may be smaller. A batch runs max(lengths) decode steps and occupies len(batch) * max(lengths) slots. Return a dict {"steps": total decode steps over all batches, "waste": 1 - useful / slots}, where useful is sum(out_lens). If out_lens is empty return {"steps": 0, "waste": 0.0}.
def static_batch_waste(out_lens, batch_size, sort_desc=False)Example
out_lens [10, 2, 8, 4], batch_size 2 gives {"steps": 18, "waste": 0.3333}: batch [10, 2] runs 10 steps and batch [8, 4] runs 8, using 36 slots for 24 useful tokens.
Understand the idea
Static batching is like a bus that leaves only when the passenger going farthest is dropped off. Everyone else's seat stays reserved, empty, for the rest of the trip. In decoding, a seat is one slot per step. Worked example: batch [10, 2] runs 10 steps and uses 2 x 10 = 20 slots but only 12 are useful, so 8 are wasted. Rule: slots = sum over batches of len(batch) x max(batch); waste = 1 - sum(lengths) / slots. Sorting requests by length before batching groups long with long and short with short, which cuts waste. Continuous batching goes further and refills a slot the moment a request finishes, which is why it gives large throughput gains for chat traffic with very uneven output lengths.