Skip to main content
By default, each session gets its own instance, with its own GPUs and its own copy of the weights. You can also serve several sessions from one instance. Do this when one copy of the weights fills most of the GPU’s memory, or when one session leaves the GPU partly idle. The instance then runs one model server and several session runtimes:
  • The model server loads the weights once and holds the GPU.
  • Each session runtime serves one session. It runs your application code, and it has no GPU.
  • At each step, a session runtime sends one request to the model server, and waits for the result.
On the left, three clients each connect to their own instance, and each instance has its own GPU and weights. On the right, three clients connect to three session runtimes inside one instance. All three session runtimes send their requests to one model server, which holds the only GPU and the only copy of the weights.

Left: by default, each session has its own instance, GPU, and copy of the weights. Right: with several sessions in one instance, one model server holds the GPU and the weights for every session.

Every session in an instance runs the same model and the same image. You need reactor-runtime 3.12.0 or later, and an up-to-date reactor CLI, which sends the sessions when it deploys.

What changes in an existing model

Three things change. Your inputs, outputs and clients stay the same.
  1. The code that uses the GPU moves into a ModelServer subclass. It has load(), and a step() that takes a list of requests.
  2. Your ReactorApp keeps its inputs and outputs. In place of the model, it holds a ModelClient and calls step() on it.
  3. reactor.yaml names the ModelServer in runtime.model_server, and sets the number of sessions in model.resources.sessions.count.

Does this fit your model?

It is a good fit when one of these is true:
  • One copy of the weights fills most of the GPU’s memory, so only one copy fits.
  • One session uses only part of the GPU’s time, for example a small model at 24 frames per second.
It is a poor fit when:
  • One session already keeps the GPU busy. More sessions then only wait longer.
  • One step() takes most of the frame period, so a second session misses its frames, and the model cannot batch requests.
  • The application half needs the GPU. A session runtime has no GPU.

What you give up

  • GPU time is shared. The sessions take turns on one GPU, so each extra session adds to every session’s wait for its frame. Choose the number of sessions shows how to size this.
  • A model server restart interrupts every session in the instance. Each one gets ModelServerLost, as the failure table shows. With one session per instance, a crash affects one session.
  • Requests and results cross processes. The runtime copies them through shared memory each step, so keep them small: send parameters, not tensors, when you can.

How the halves split

Your model is split in two halves, as Writing a good model describes. Here, the two halves run in different processes: A session has at most one request on the model server at a time. The model server takes the requests in the order they arrive.

Build the model

This section builds a complete example: a small network that draws a 256×256 animated frame for each session. Each session sets its own seed, speed, and warmth. Four sessions share one model server, and they take turns: the model server runs one request at a time. If your model can run several requests in one forward pass, Batch requests shows the one change that makes the sessions step together. Put these files in one folder: lava_types.py, lava_model.py, lava_net.py, lava.py, requirements.txt, and reactor.yaml.
1

Define the data that crosses between the halves

The two halves run in different processes, so the runtime pickles each request and each result. Use plain data: frozen dataclasses of numbers, strings, and NumPy arrays. Do not send a CUDA tensor. A session runtime has no GPU, so it cannot rebuild one.Put the types in a module that both halves import. Keep torch out of it.
lava_types.py
2

Write the model half

Subclass ModelServer from reactor_runtime.model_server. The model server calls load() once, before any session runtime connects. self.device is "cuda" when torch sees a GPU. The runtime checks this when it builds your class, so torch must be imported by then. Here lava_model.py imports lava_net.py, which imports torch.The model server calls step() with a list of requests. Each request has a payload, which is the LavaRequest that one session sent. Return one output per request, in the same order. On this page the list holds one request. The same code runs batches on Batch requests. self.max_batch is the most requests one step takes. It is 1 on this page, and the batching page raises it.
lava_model.py
render() stacks every request into one tensor and runs the network once. It returns a uint8 array with one frame per request. The network is ordinary PyTorch code. This one is a small decoder with fixed random weights, so the example needs no weight files. Copy it as it is; nothing in it is specific to Reactor.
lava_net.py
This model keeps no state for a session. Each request carries everything the model needs. When your model keeps state for each session, see Keep state for each session.
3

Write the application half

The application half is a normal ReactorApp, with one change. It holds a ModelClient in place of the model, and generate() calls step() on the client.
lava.py
Three calls do the work:
  • ModelClient.connect() in load() waits until the model server is ready. on_lost=self.crash ends the session runtime if the link to the model server breaks while no step runs.
  • self.model.reset() in the @session_started hook gives each new session a clean start on the model server. Until the application calls it, step() raises ResetRequired.
  • self.model.step(input) sends this session’s request and waits for its result.
The runtime builds a fresh LavaState for each session, so _t starts at 0 every time.
4

Name the model server in reactor.yaml

Name your ModelServer in runtime.model_server, and set the number of sessions in model.resources.sessions.count. List the dependencies in requirements.txt. Both halves run from the same image.
requirements.txt
reactor.yaml
The sessions block has defaults for everything else, and they fit most models. A session’s CPU, memory and shared memory are in the reactor.yaml reference, if a step ever reports that it needs more.
5

Deploy and test

Deploy from the folder, as you deploy any model. The first deploy names the GPU; later deploys keep it:
The deploy builds the folder’s code and sends model.resources.sessions as the sessions of the environment it deploys, so each environment, such as staging, can serve its own number.Each instance runs one model server and four session runtimes. Reactor sends each new session to a free session runtime. To test, connect several clients at the same time, as in the quickstart. Then read the model’s logs:
The mean batch size lines come from LavaModel.step(). Here they show 1.00, because the sessions take turns.Lines that your model logs from step() name no session. Read them in the model’s logs, as above, not in the view of one session (--session).

Choose the number of sessions

Every session needs a frame in each frame period. At 24 frames per second, the period is about 42 ms. The sessions take turns, so with N sessions, a session can wait for N steps for its frame. Keep N step times under the frame period.
  1. Measure how long one step() takes.
  2. Find the largest N that meets the rule above. Also make sure the session runtimes fit in the instance’s CPU and memory.
  3. Set model.resources.sessions.count to N.
  4. Deploy, connect that many clients, and read the logs.
If N comes out small, check whether your model can batch. A batch serves several sessions in about the time of one step. See Batch requests.

What a failure ends

The example re-raises every error in process_output(), so a failed step ends its session. To recover from an error your model knows, handle that error in process_output() and re-raise the rest. The model server already reports itself unhealthy when a step stops making progress, and the deployment then restarts it. To report your model’s own failures as well, override health() on your ModelServer and return a Health from reactor_runtime.core. It runs on the health check’s thread, so keep it quick and do not block on the step.

Keep state for each session

A model that keeps state for each session, such as a cache, stores that state by slot. Each request has a slot, from 0 to the number of sessions minus one, and a generation. The generation changes when a session runtime connects and at every reset. Two hooks run between steps, on the same thread as step():
  • session_opened(slot, generation) sets up the state for a slot. It runs when a session runtime connects, and again at every reset.
  • session_closed(slot, generation) drops that state. It runs when a session runtime disconnects, and before a reset opens the slot again.
So the state of a session that ended stays until the next session on that slot resets, or until the session runtime disconnects.

Next

Batch requests

Step several sessions together in one forward pass.

Writing a good model

The application and model halves, and the rules that keep them apart.

reactor.yaml reference

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

Multi-GPU inference

Run one session across several GPUs, the opposite direction.