f
gcw_pCMO8DfP/ppo-Pendulum-v1
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ppo-Pendulum-v1 — Ascend NPU Inference

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).

Highlights

  • Runs entirely on the Ascend NPU (npu:0) via torch_npu; no CUDA.
  • Policy MLP reconstructed in plain PyTorch from the original SB3 policy.pth state dict (no stable-baselines3 dependency at inference time).
  • Dual mode: CLI prediction and FastAPI model service.
  • Weights fetched from the hf-mirror endpoint (HF_ENDPOINT=https://hf-mirror.com) and cached under ./model_weights/ppo-Pendulum-v1/.

Files

FileDescription
inference.pyDual-mode NPU inference (CLI + FastAPI)
README.mdThis file
assets/Screenshots (workflow / CLI / model result)
model_weights/Cached policy weights (git-ignored)

Environment

  • Python 3.11, PyTorch + torch_npu, fastapi, uvicorn
  • Ascend CANN 8.5.1 (Ascend 910 NPU)
  • Weights: HumanCompatibleAI/ppo-Pendulum-v1 via hf-mirror

Install dependencies:

pip install torch torch_npu fastapi uvicorn huggingface_hub

Weights

inference.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.3

The policy state dict is cached at:

model_weights/ppo-Pendulum-v1/policy.pth

CLI inference

python3 inference.py --state 0.1,-0.2,0.3

Example 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.

FastAPI service

python3 inference.py --host 0.0.0.0 --port 8000

Endpoints:

  • 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"}

NPU adaptation notes

  • 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.
  • No CUDA API is used anywhere in the code.
  • The SB3 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.

Verification

python3 inference.py --state 0.1,-0.2,0.3

Expected: JSON output with "device": "npu:0" and a valid action.