Batch by Token Budget
Greedily group token counts into batches whose sum does not exceed budget. A single oversized item gets its own batch.
Explore the underlying concept with illustrative inputs. This experiment is separate from the problem’s test cases.
Continuous batching
Compare fixed batches with a scheduler that refills slots as requests finish.
Fixed batches · 9 ticks total
Continuous batching · 6 ticks total
All five requests arrive at tick 0. Two slots each decode one token per tick. Request lengths: 2, 4, 1, 3, 2. Bars show scheduled work; the vertical cursor marks the current tick.
Change the experiment
for each decoding_tick:
decode_one_token(active_requests)
remove_finished_requests()
fill_free_slots(queue)Try it yourself
Increase the last request length. Compare the two completion times and locate idle slots in fixed batching.
Tick 0. Fixed batches completed 0 requests; continuous batching completed 0.
Refilling free slots can improve utilization. This idealized model excludes prefill, memory limits and shared GPU contention.
Playback pauses in hidden tabs and offscreen. With reduced motion enabled, use the timeline or Next step.Examples
A guided view of the published example, not a trace of your code.
[[3,4,2,5],7][[3,4],[2,5]]Start with the input values shown in this example.
Input: token_counts = [3,4,2,5], budget = 7
Output: [[3,4],[2,5]]
Explanation: packed
Input: token_counts = [9,1], budget = 5
Output: [[9],[1]]
Explanation: oversized
Hint 1
Flush the current batch before adding an item that would overflow it.