Ascend NPU (torch-npu) adaptation of the stable-baselines3 PPO policy
HumanCompatibleAI/ppo-Pendulum-v1,
a reinforcement-learning controller for the Pendulum-v1 control environment.
The policy is a small MLP that maps a 3-dim observation vector to a 1-dim torque action:
obs(3) -> Linear(3,64) -> Tanh -> Linear(64,64) -> Tanh -> Linear(64,1)This is a control model, not an image model — inference takes a --state
vector (not --img).
npu:0) via torch_npu; no CUDA.policy.pth state dict (no stable-baselines3 dependency at inference time).HF_ENDPOINT=https://hf-mirror.com)
and cached under ./model_weights/ppo-Pendulum-v1/.| File | Description |
|---|---|
inference.py | Dual-mode NPU inference (CLI + FastAPI) |
README.md | This file |
assets/ | Screenshots (workflow / CLI / model result) |
model_weights/ | Cached policy weights (git-ignored) |
torch_npu, fastapi, uvicornHumanCompatibleAI/ppo-Pendulum-v1 via hf-mirrorInstall dependencies:
pip install torch torch_npu fastapi uvicorn huggingface_hubinference.py auto-downloads the weights on first run when they are missing:
HF_ENDPOINT=https://hf-mirror.com python3 inference.py --state 0.1,-0.2,0.3The policy state dict is cached at:
model_weights/ppo-Pendulum-v1/policy.pthpython3 inference.py --state 0.1,-0.2,0.3Example output:
{
"model": "ppo-Pendulum-v1",
"state": [0.1, -0.2, 0.3],
"action": 2.789349,
"action_clipped": 2.0,
"action_space": [-2.0, 2.0],
"device": "npu:0"
}action — distribution mean (deterministic action) predicted by the policy.action_clipped — the same action clipped to the Pendulum-v1 torque bounds
[-2.0, 2.0] (the value actually applied by the environment).device — npu:0 confirms the tensors run on the Ascend NPU.python3 inference.py --host 0.0.0.0 --port 8000Endpoints:
POST /predict — body {"state": [x, y, z]} (3-dim observation vector):curl -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"state": [0.1, -0.2, 0.3]}'{"model":"ppo-Pendulum-v1","state":[0.1,-0.2,0.3],"action":2.789349,"action_clipped":2.0,"action_space":[-2.0,2.0],"device":"npu:0"}GET /health — service health:{"status":"ok","model":"ppo-Pendulum-v1","device":"npu:0"}torch_npu is imported inside a try/except; the model and input tensors are
moved to npu:0 when an NPU is available, otherwise a CPU fallback is used.MlpPolicy activation function is Tanh (stable-baselines3
default). The reconstructed MLP matches the reference model's deterministic
(distribution-mean) action exactly (max abs. diff 0.0).normalize=False for this model (see model config.yml), so observations
are fed to the network as-is; no input normalization is applied.python3 inference.py --state 0.1,-0.2,0.3Expected: JSON output with "device": "npu:0" and a valid action.