Serving trained policies¶
decisionrl.serving turns a trained agent into a portable artifact and a tiny
inference service. The serving runtime needs only onnxruntime + FastAPI — no
PyTorch — so deployment images stay small.
Install the extra:
pip install "decisionrl[serve]"
Export¶
from decisionrl.algorithms import PPO
from decisionrl.envs import CartPole
from decisionrl.serving import export_onnx, export_torchscript, OnnxPolicy
agent = PPO(CartPole(), seed=0).learn(50_000)
export_onnx(agent, "policy.onnx") # writes policy.onnx + policy.onnx.json
export_torchscript(agent, "policy.pt") # TorchScript alternative
policy = OnnxPolicy("policy.onnx") # inference with onnxruntime only
action = policy.predict(obs)
The exporter freezes the deterministic policy (argmax for discrete agents; the squashed/clamped mean for continuous ones). Supported agents: PPO, A2C, GRPO, SAC, DDPG, TD3 and DQN.
Serve over HTTP¶
DECISIONRL_MODEL=policy.onnx uvicorn decisionrl.serving.server:app --port 8000
| Method | Path | Description |
|---|---|---|
| GET | /health |
liveness probe |
| GET | /info |
policy metadata (obs dim, action type, bounds) |
| POST | /predict |
{"observation": [...]} → {"action": ...} |
from decisionrl.serving import create_app # FastAPI app for the model
app = create_app("policy.onnx")
Docker¶
deploy/Dockerfile builds a slim image (onnxruntime + FastAPI, no torch and no decisionrl
package) around the standalone deploy/serve.py entrypoint. It bakes in a default policy at
/models/policy.onnx; override DECISIONRL_MODEL to serve your own. The image is built and
smoke-tested on every push by the docker-serve CI job.
docker build -f deploy/Dockerfile -t decisionrl-serve .
docker run -p 8000:8000 decisionrl-serve
curl -X POST localhost:8000/predict -H 'content-type: application/json' \
-d '{"observation": [0, 0, 0, 0]}'