NPU-adapted inference for the reinforcement-learning control policy
HumanCompatibleAI/ppo-seals-CartPole-v0
— a stable-baselines3 PPO (MlpPolicy) agent for the CartPole balancing task.
The policy MLP is reconstructed from the checkpoint and executed on an Ascend NPU
via torch-npu. No CUDA is used anywhere.
| Item | Value |
|---|---|
| Environment | CartPole (obs dim 4, discrete actions 2) |
| Algorithm | PPO (MlpPolicy) |
| Network | obs(4) → Linear(4,64)+ReLU → Linear(64,64)+ReLU → Linear(64,2) |
| Activation | ReLU |
| Device | npu:0 (Ascend), CPU fallback |
The checkpoint is fetched from HF (via hf-mirror) and cached locally:
mkdir -p model_weights/ppo-seals-CartPole-v0
HF_ENDPOINT=https://hf-mirror.com python3 -m huggingface_hub.snapshot_download \
HumanCompatibleAI/ppo-seals-CartPole-v0 \
--local-dir model_weights/ppo-seals-CartPole-v0The SB3 zoo repo nests the checkpoint one folder deep, so the policy weights end up at
model_weights/ppo-seals-CartPole-v0/ppo-seals-CartPole-v0/policy.pth.
inference.py probes both the flat and nested paths automatically.
Weights are git-ignored; only code and
assets/are published.
Given a 4-dim CartPole observation vector, the model prints the predicted action as JSON:
python3 inference.py --state 0.1,-0.2,0.3,0.4Example output:
{"model": "ppo-seals-CartPole-v0", "device": "npu:0", "action": 0, "logits": [0.9, -0.4], "probabilities": [0.787, 0.213]}action is the deterministic (greedy) action: argmax(logits).
Serve the model over HTTP:
python3 inference.py --serve --port 8000Health check:
curl http://127.0.0.1:8000/health
# {"status":"ok","model":"ppo-seals-CartPole-v0","device":"npu:0"}Prediction:
curl -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"state": [0.1, -0.2, 0.3, 0.4]}'
# {"model":"ppo-seals-CartPole-v0","device":"npu:0","action":0,"logits":[...],"probabilities":[...]}Request schema: {"state": [4 values]} — the observation vector for CartPole
[cart_position, cart_velocity, pole_angle, pole_angular_velocity].
npu:0; NPU init is wrapped in try-except with a CPU fallback.cuda (case-insensitive) in inference.py.[-4.8, 4.8]; raw values are used directly..
├── inference.py # dual-mode NPU inference (CLI + FastAPI)
├── README.md
├── assets/ # 3 screenshots (NPU workflow / CLI / web)
└── model_weights/ # git-ignored weight cache
└── ppo-seals-CartPole-v0/policy.pth