Skip to main content
On Serve several sessions, the sessions share one model server and take turns. If your model can run several requests in one forward pass, the model server can step the sessions together. One forward pass then serves up to max_batch sessions, in about the time of one.
Batching helps only when one request leaves the GPU partly idle. If one request already keeps the GPU busy, a batch takes as long as its requests one after another. The GPU also needs memory for the activations of a full batch. Measure your step time first. See Choose max_batch.

How a batch forms

The model server follows these rules:
  • A session has at most one request waiting or running.
  • When the model server is free and a request waits, it waits up to 5 ms for others.
  • It stops waiting early when max_batch requests wait, or when every live session has a request waiting. A session is live when it opened, reset, sent a request, or got a result in the last 2 seconds, so a session runtime with no client stops holding batches back.
  • It then calls your step() once, with up to max_batch requests, oldest first.
  • A request that arrives while a step runs joins the next step.
Animation. Four session runtimes each send one request to the model server, at slightly different times. When all four are waiting, the model server runs one forward pass on the GPU for all four requests. Then each session gets its own frame back.

One turn of the model server, slowed down. Each session's request arrives at its own moment. When every session has a request waiting, one forward pass runs, and each session gets its own frame back.

The short wait lets requests that arrive a moment apart step together. To change it, set runtime.model_server.batch_window in seconds.

What your step() must accept

With batching on, the model server keeps this contract with your step():
  • A batch groups requests by arrival. It holds whichever sessions’ requests are waiting, oldest first, not a fixed set of sessions in slot order.
  • A batch holds any number of requests from 1 to max_batch. So the batch dimension of your tensors changes from step to step. A model that needs a fixed shape, for example for CUDA graphs, pads the batch itself.
  • Each request belongs to one session. Its slot and generation say which, so state for each session stays keyed by slot, as on the previous page.
  • Return one output per request, in the order of the requests.

Turn batching on

Start from the Lava example on the previous page, with its 4 sessions. Its step() already meets the contract: it stacks every request in the list into one tensor, runs one forward pass for any list length, and returns one frame per request. So the code does not change. Only reactor.yaml changes, from a max_batch of 1 (the default) to 4. Set max_batch under runtime.model_server, and remove the sessions block. With no sessions.count, each instance serves max_batch sessions, so it still serves 4.
reactor.yaml
Your class reads the value as self.max_batch before load() runs, so the Lava warm-up renders a full batch of 4. A warm-up of a full batch also shows at once whether the GPU has the memory for it. Deploy it, connect 4 clients at the same time, and read the logs. The environment keeps the GPU of its first deploy, and the deploy sends the 4 sessions that max_batch sets:
The mean batch size lines from LavaModel.step() now show how full the batches are. With 4 live clients, they move from 1.00 toward 4.

Choose max_batch

Every session needs a frame in each frame period. At 24 frames per second, the period is about 42 ms. A request can arrive just after a step starts. It then waits for that step to end, then up to 5 ms, then for its own step. So keep two step times plus 5 ms under the frame period.
  1. Measure how long one step() takes for 1, 2, 4, and 8 requests.
  2. Find the largest batch that meets the rule above.
  3. Set runtime.model_server.max_batch to that number.
  4. Deploy, connect that many clients, and read the logs.
For example, say one step takes 10 ms for 1 request, 12 ms for 4, and 16 ms for 8: Batching turns 4 sessions that barely fit into 8 sessions that fit. To serve more sessions than max_batch, set model.resources.sessions.count higher. A request then waits for the running step and for every full batch ahead of it. The running step holds up to max_batch sessions, which cannot send, so with up to twice max_batch sessions the rule above still holds: two step times plus 5 ms. Each further max_batch sessions adds one step time. With 12 sessions and a max_batch of 4, keep three step times plus 5 ms under the frame period.

Finish a request on your own threads

Some models run a request across their own threads, for example in stages over several GPUs. Such a model can return a concurrent.futures.Future from step() in place of a result, and complete it later. The session stays busy until the future is done, while the next batch steps. The ModelServer.step() docstring in reactor_runtime.model_server gives the rules.

Next

Serve several sessions

The model server, the session runtimes, failures, and state for each session.

reactor.yaml reference

Every key of runtime.model_server and model.resources.sessions.