Docs

Should I run two replicas of a policy on one GPU?

No. Two replicas on one GPU (two loaded copies of the model, each in its own process) do not run concurrently: the GPU time-slices between the two processes. Measured with π0.5 on an L4, two replicas whose calls overlapped each took 2.1 times as long as one alone, so the GPU’s throughput was no higher than with one replica. Give each replica its own GPU, and on one GPU use one replica and batch its calls.

python
from sequence_ai.engine.replicas import ReplicaPool

# one replica per GPU; load_user() runs in the replica's process
pool_user = ReplicaPool(load_user, replicas=2, devices=[0, 1])
output_user = await pool_user.run(observation_user)  # in a request's handler: off the event loop
pool_user.close()

What was measured

Setting, π0.5 on one L4Each call took
One replica1 (the baseline)
Two replicas, their calls overlapping2.1 times as long each
Two replicas with the CUDA Multi-Process Service (MPS) enabled6 to 9% faster than without it

One model and one GPU. A model that leaves the GPU idle for most of a call could behave differently; this one did not.

What to do on one GPU

What replicas is for

  • One replica on each of several GPUs, each running one call at a time: devices=[0, 1].
  • A model that never blocks your server. Each replica runs in its own process, so a forward pass does not block the server’s event loop or hold its interpreter, even with one replica.
  • A replica that dies is replaced. ReplicaError is the model’s own error, and its replica takes the next call. ReplicaDied is a process that has exited: killed, out of memory, or past its deadline. After a few consecutive deaths the pool stops replacing, and raises PoolHalted once none is left.

Batcher(pool.run, runners=2) combines the two components: batches are formed, two run concurrently, one per replica. examples/serve_with_replicas.py does it around a small model.

One requirement

The function given as load (load_user above) must be one a new process can import: defined at the top of a module, not a lambda and not a function inside another. Each replica starts in its own process and imports it by name.