Generation request batching and cancellation
Schedule compatible active trajectories while preserving per-request solver state, random streams, and GPU buffer lifetimes.
On this page
The question for today
What you’ll understand
- Distinguish padded slot work from useful active trajectory work.
- Identify batching constraints beyond equal image shape.
- Define cancellation as a lifecycle transition rather than immediate device-memory reuse.
A generation request is an evolving trajectory with its own grid, history, conditioning, random state, and output contract. Batching can share a denoiser invocation, but it does not turn independent requests into one interchangeable state. Different step budgets and cancellations create empty slots unless scheduling handles them explicitly. The serving objective is useful completed requests under latency and quality constraints, not a large batch dimension by itself.
Group work only when the execution contract supports it
Compatible image shape, model revision, precision, and conditioning layout are basic grouping requirements. The current noise coordinate, prediction parameterization, guidance branches, and solver state also matter. Some implementations can evaluate different timesteps per batch element; others assume one shared step or maintain a single scheduler index. Verify the actual kernel and pipeline support rather than replacing independent trajectories with a shared loop counter. A batch assembled from newly arriving requests needs an explicit map from slots to request-owned states.
For requests with different evaluation budgets, a fixed batch running until the longest trajectory ends may waste slot work on already finished entries. Compacting the active set can avoid those evaluations in a suitable implementation, but it changes batch shapes, launch behavior, and sometimes numerical kernel selection. A smaller active batch may be less hardware-efficient per image. Count useful evaluations and measure end-to-end completion time, including compaction and decode queues, before concluding that removing padded slots produces a proportional latency reduction.
Cancellation has logical and physical completion points
When a client cancels, mark the request so no new unnecessary work is scheduled under the policy. Work already submitted to a GPU stream may still access its buffers. A callback that stops future denoising steps is not a general primitive for interrupting a running kernel. Keep the slot’s storage and identity protected until the relevant operations complete, then release histories, conditioning references, latents, and outputs safely. Generation or epoch tags can prevent a late completion from being mistaken for a newly admitted request in a recycled slot.
Diffusers v0.35.1 documents step-end interruption callbacks for specific pipelines, and the classic implementation checks interruption between denoising iterations. That behavior does not automatically provide per-element cancellation in a batch, asynchronous multi-request isolation, or removal of later decode work. Inspect the returned-output and postprocessing path too. A production scheduler may need its own cancellation result and cleanup state machine rather than exposing a partially denoised image as a normal completed response. Preserve request-specific random streams so canceling one request does not shift another’s future draws.
Measure useful work and reserve the later stages
Admission should account for trajectory state and eventual decode/output memory, not just current denoiser occupancy. A busy decode stage can make completed latents accumulate even when denoising slots turn over quickly. Apply backpressure at a known boundary and report queue delay, denoise service, decode service, canceled work, and time to usable output. Test mixed step counts, canceled long requests, slow clients, and interleaved scheduler histories. A throughput number from identical uncanceled requests does not characterize that lifecycle.
The queue instrument illustrates admission pressure only. Reset, set both λ=4 and μ=3, predict backlog six at t=2 from four, then Step four times. Separately Reset, set both λ=2 and μ=3, predict drainage at t=4, then Step eight times. These fresh runs compare offered and service rates in a fixed fluid model. It has no batch slots, solver coordinates, cancellation events, GPU completion, or decode memory. Lower offered rate in the diagram is not evidence that an actual cancellation implementation safely reclaimed device buffers or improved generation goodput.
Work it through
Count padded and useful trajectory evaluations
Four compatible hypothetical requests need 10, 20, 20, and 30 equally counted denoiser evaluations. Compare a fixed four-slot loop through thirty rounds with ideal active-slot compaction, ignoring compaction cost and guidance.
The fixed loop schedules 4×30=120 slot evaluations. Useful requested work is 10+20+20+30=80, leaving forty evaluations on completed slots under this simplistic padded policy.
The avoidable fraction of padded work is 40/120=1/3, or about 33.33 percent. Ideal compaction counts eighty evaluations, but its changing batch sizes can change efficiency and kernel behavior.
If a separate twenty-evaluation request is canceled after six have completed, fourteen future evaluations are logically avoidable. Already submitted work and retained buffers still require completion-aware cleanup; fourteen is not an immediate wall-time or memory-release guarantee.
Active-slot accounting reveals one source of wasted work. A correct scheduler must preserve request state and physical lifetimes before turning that accounting into a serving benefit.
The living notebook
Explore the model
Interactive lab · queue
Offered work and completed service need a defined boundary
Reset, set both λ=4 and μ=3, predict backlog six at t=2, then Step four times. Separately Reset, set both λ=2 and μ=3, predict backlog zero at t=4, then Step eight times. The fluid model starts with four units and has no request slots, cancellation, denoising grid, or GPU buffer lifecycle.
- Fluid backlog
- 4
- Net rate · λ − μ
- −1 /s
- M/M/1 mean residence
- 1 s
Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forValidate cancellation semantics and measure useful completions separately from an abstract rate comparison.
Pause & reason
Check your understanding
Your turn
Try it yourself
Three requests need 8, 12, and 20 evaluations. Under the same simplified fixed-slot policy, compute padded work, useful work, and avoidable slot evaluations.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Compare three times the longest budget with the sum of individual budgets.
Compare with the solution
Padded work is 3×20=60, useful work is 8+12+20=40, and twenty slot evaluations are avoidable under ideal compaction. The calculation omits guidance, internal calls, varying batch efficiency, state movement, cancellation, and decode service; it is not a measured latency prediction.
Keep the idea
Take it with you
- Batch compatible numerical states while retaining request-owned histories and streams.
- Distinguish useful evaluations from padded slots and measured latency.
- Cancel future work logically and reclaim storage only after physical completion.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.