Skip to content

RL with boxfork

Reinforcement learning needs many environments at the same time, and each episode must start from the same state. A snapshot gives that state. A pool or a fork gives many copies of it.

snapshotthe start statereset()sandboxstep 1: run actionstep 2: run actionstep 3: run actionepisodeAgentpolicyactionrewardnext episode: the same start state
Each episode starts from the same snapshot. The agent takes steps in the sandbox. A reset puts the sandbox back to the snapshot for the next episode.
Item State
Snapshot of a prepared environment built
Fork: many copies of one sandbox built
Pool of a snapshot: copies kept started, a create in 1.6 ms built
A whole GPU for each sandbox built
Reset a sandbox to a snapshot, with the same ID (#13): on a daemon, not on Kubernetes built
Async Python SDK (#14) planned
Environment wrapper with reset() and step() (#15) planned
A “trusted” isolation level for your own code (#16) planned
Shared-GPU mode for trusted sandboxes (#17) planned

The reset() in the diagram is sandbox.reset(snapshot). On a daemon, a reset took a median of 167.9 ms, and 63.9 ms from a pool of the snapshot. See Reset. The loop below creates a sandbox for each episode instead: it also works on Kubernetes.

  1. Prepare one sandbox: install the packages, copy the task.
  2. Save it as a snapshot.
  3. Create a pool of the snapshot, so that each new episode starts at once.
  4. For each episode, create a sandbox from the snapshot, take the steps, and kill it.
from boxfork import Pool, Sandbox
with Sandbox.create("python:3.12-slim") as env:
env.run("pip install --quiet gymnasium")
env.files.write("/work/task.py", task_code)
snap = env.snapshot() # the start state
Pool.create(snapshot=snap, size=8).wait() # eight episodes kept ready
for episode in range(1000):
with Sandbox.create(snapshot=snap) as sbx: # from the pool
for step in range(max_steps):
action = policy(observation)
result = sbx.run(f"python3 /work/task.py --action {action}")
observation, reward = parse(result.stdout)

policy, parse, task_code and max_steps are your code. boxfork gives the sandbox, the snapshot and the pool.

  • sbx.fork(n) makes n copies of a sandbox that runs, with the same files.
  • A pool of the snapshot keeps copies started. On the test machine, a create from it took a median of 1.6 ms.
  • Each copy has its own processes. A change in one copy does not go to the others.
  • A snapshot keeps files, not processes or memory. Each episode starts its processes again.
  • A simulator that uses the GPU is not proven under gVisor (#59).
  • The number of sandboxes that one machine holds is not measured yet (#58).