atlasleong/ppo-pendulum-v1-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PPO Pendulum-v1 — Ascend NPU Inference Candidate

中文简介:本仓库是 HumanCompatibleAI/ppo-Pendulum-v1(Stable-Baselines3 PPO,MlpPolicy + gSDE) 在华为昇腾 Ascend910B4(物理 npu:1)上的推理示例。CPU 仅用于编排/反序列化/预处理,策略与价值网络的 前向计算全部在 npu:1 上执行,并对输入/模型/输出的设备位置做严格断言。发布文件集不包含权重字节; 推理脚本从固定的本地不可变检查点严格加载并校验字节数与 SHA256。

  • Model / source: HumanCompatibleAI/ppo-Pendulum-v1
  • Pinned source revision: 6a0fe838119427278252dde3bf723ed7a3266df5
  • Checkpoint: ppo-Pendulum-v1.zip — 137832 bytes, SHA256 d2a5572df0c6d6022519615b76c9e6ca5bce0e7d5cf859ec145cbf99a437b8b0
  • Architecture route: Stable-Baselines3 PPO (ActorCriticPolicy, MlpPolicy, use_sde=True)
  • Hardware: Ascend910B4-1, physical device npu:1, CANN cann-8.5.1, engine torch_npu
  • License: the upstream model card does not declare a license field; the front matter above therefore uses AtomGit-valid license: other to denote a non-standard / undeclared license. The weights are treated as license-not-declared; do not assume a permissive license for redistribution without upstream confirmation.

Model introduction

This is a trained PPO agent playing Pendulum-v1 using the stable-baselines3 library and the RL Zoo framework (HumanCompatibleAI/ppo-Pendulum-v1). The observation space is Box([-1 -1 -8], [1 1 8], (3,), float32) (cos(theta), sin(theta), angular velocity) and the action space is a single continuous torque Box(-2.0, 2.0, (1,), float32). The deterministic test observation is [0, 0, 0].

The deterministic inference produces:

  • action (Gaussian mean): [[0.11721252650022507]], shape (1, 1)
  • value (critic state-value; PPO has no Q-function): [[-74.04744720458984]], shape (1, 1)
  • log_prob: [-0.16488760709762573], shape (1,)
  • entropy: [0.6648876070976257] (self-check: log_prob + entropy = 0.5, the exact Gaussian-at-mean identity)
  • distribution (DiagGaussian/gSDE Normal): loc = [[0.11721252650022507]], scale = [[0.47045689821243286]], shape (1, 1)

All tensors are on npu:1. Timing is NPU-synchronized.

Environment dependencies

Verified working image (aarch64 / openEuler 22.03 SP4):

ComponentVersion
Python3.11.14
CANNcann-8.5.1
torch2.9.0+cpu (preinstalled, must not be replaced)
torch_npu2.9.0 (preinstalled, must not be replaced)
stable-baselines32.7.0
gymnasium1.2.0
numpy1.26.4
cloudpickle3.1.2
scipy1.17.1
pandas2.3.2
matplotlib3.10.5
psutil7.2.2
decorator5.3.1

Important: torch==2.9.0+cpu and torch_npu==2.9.0 are provided by the container and are matched to CANN 8.5.1. Do not reinstall them from ordinary PyPI wheels — that would break the NPU runtime.

Installation (Aliyun mirror only)

export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
export PIP_TRUSTED_HOST=mirrors.aliyun.com

# Only non-torch runtime dependencies are installed from the Aliyun mirror.
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

requirements.txt intentionally does not list torch / torch_npu.

Inference steps / 分步推理操作

  1. Source the CANN environment (required for the NPU runtime):
    source /usr/local/Ascend/ascend-toolkit/set_env.sh
  2. Confirm the physical device is visible:
    npu-smi info
  3. Obtain the pinned checkpoint (the published delivery does not bundle weight bytes). Either use the immutable local copy at /work/pipeline/models/rl26/ppo-pendulum-v1/ppo-Pendulum-v1.zip, or download ppo-Pendulum-v1.zip from the HF repo at revision 6a0fe838119427278252dde3bf723ed7a3266df5 and pass it with --model-path.
  4. Run inference on npu:1:
    python3 inference.py --device npu:1 --hold-seconds 8 --result-json logs/npu_result.json \
        > logs/npu_inference.log 2>&1
  5. Inspect the result:
    cat logs/npu_result.json

inference.py fails nonzero on any of: missing NPU, wrong NPU index, checkpoint bytes/SHA256 mismatch, or any input/model/output placement that is not npu:1.

Complete test case and output / 完整测试用例与输出

cd /root/ascend-model-pipeline/jobs/rl26/ppo-pendulum-v1/ppo-pendulum-v1-npu
source /usr/local/Ascend/ascend-toolkit/set_env.sh
python3 inference.py --device npu:1 --hold-seconds 8 --result-json logs/npu_result.json \
    > logs/npu_inference.log 2>&1
echo "exit=$?"

Actual output (exit=0, PID 7567, captured from the real final run):

[INFO] python=3.11.14 platform=Linux-5.10.0-325.0.0.226.oe2203sp4.aarch64-aarch64-with-glibc2.38
[INFO] device=npu:1 hardware=Ascend910B4-1 cann=cann-8.5.1
[INFO] package torch=2.9.0+cpu
[INFO] package torch_npu=2.9.0
[INFO] package stable_baselines3=2.7.0
[INFO] package gymnasium=1.2.0
[INFO] package numpy=1.26.4
[INFO] package cloudpickle=3.1.2
[INFO] package scipy=1.17.1
[INFO] model_path=/work/pipeline/models/rl26/ppo-pendulum-v1/ppo-Pendulum-v1.zip bytes=137832 sha256=d2a5572df0c6d6022519615b76c9e6ca5bce0e7d5cf859ec145cbf99a437b8b0
[INFO] loading SB3 PPO checkpoint onto npu:1 ...
[INFO] policy device verified: all 13 parameters + buffers on npu:1
[INFO] observation_space=Box([-1. -1. -8.], [1. 1. 8.], (3,), float32) action_space=Box(-2.0, 2.0, (1,), float32) use_sde=True
[INFO] observation value=[[0.0, 0.0, 0.0]] shape=(1, 3) dtype=torch.float32 device=npu:1
MODELAGENT_NPU_READY pid=7567 device=npu:1 hardware=Ascend910B4-1 revision=6a0fe838119427278252dde3bf723ed7a3266df5
[INFO] npu-smi correlation (PID present):
    | 1       0                 | 7567          | python3                  | 120                     |
[INFO] sanitized raw npu-smi log -> logs/npu_smi.log
[INFO] holding 8.0s for external npu-smi correlation ...

===== NPU INFERENCE RESULT (deterministic) =====
engine          : torch_npu
npu_available   : True
npu_device_name : Ascend910B4-1
device          : npu:1 (Ascend910B4-1)
input obs       : [[0.0, 0.0, 0.0]] shape=(1, 3) device=npu:1
action (mean)   : shape=(1, 1) value=[[0.11721252650022507]] device=npu:1
value (critic)  : shape=(1, 1) value=[[-74.04744720458984]] device=npu:1
log_prob        : shape=(1,) value=[-0.16488760709762573] device=npu:1
entropy         : [0.6648876070976257] device=npu:1
dist loc        : [[0.11721252650022507]]
dist scale      : [[0.47045689821243286]]
elapsed (s)     : 0.042375
pid             : 7567
===============================================
[INFO] result written to logs/npu_result.json
MODELAGENT_NPU_DONE

logs/npu_result.json exposes the required top-level fields: model_id, engine: "torch_npu", exit_code: 0, npu_available: true, npu_device_name: "Ascend910B4-1", source_revision: "6a0fe838119427278252dde3bf723ed7a3266df5", input_device: "npu:1", model_device: "npu:1", output_device: "npu:1", plus a top-level output object with continuous action, value, log_prob, entropy and the Gaussian distribution (loc/scale) — each with shapes, npu:1 device, dtype and scalar statistics (mean/std/min/max) — together with timing (forward seconds + wall seconds) and the inference pid. logs/npu_smi.log is the sanitized raw device log whose process line PID equals the inference PID during the hold window.

Evidence screenshots

All screenshots are rendered only from the real final transcript/log/result (no fabricated values):

  • assets/agent_workflow.png — ModelAgent workflow steps from the real session transcript.
  • assets/npu_device_call.png — NPU device call / placement evidence from logs/npu_inference.log.
  • assets/model_result.png — model result panel from the real result JSON/log.
  • assets/modelagent_live_session.png — live ModelAgent session summary (READMEY/npu-smi correlation and result).

Limitations

  • Published set does not bundle weights. The checkpoint bytes (ppo-Pendulum-v1.zip) are not included in the published file set; supply the pinned checkpoint via --model-path.
  • Do not reinstall torch/torch_npu from PyPI; only the container's CANN-matched pair works.
  • Ascend910 does not support fp64; torch_npu down-casts fp64 to fp32 automatically.
  • PPO is actor-critic; there is no Q-function. The value output is the critic state-value.
  • The deterministic test uses the Gaussian mean action (get_actions(deterministic=True)), so output is reproducible across runs; gSDE exploration matrices are resampled on NPU but do not affect the deterministic action.
  • Two benign SB3 warnings are suppressed with targeted filters (training-only schedule callables that cannot be unpickled on Python 3.11, and a spurious "running PPO on the GPU" message for the NPU). They do not affect the strict weight load or the NPU forward.
  • No Gym/Unity/robot hardware is launched; the loaded policy network is invoked directly.
  • The two benign deserialization warnings and the "GPU" message do not change model weights.

Files

  • inference.py — deterministic SB3 PPO inference on npu:1 with strict pinned-artifact verification and placement assertions.
  • requirements.txt — non-torch runtime dependencies (Aliyun mirror).
  • logs/npu_inference.log — raw inference log.
  • logs/npu_result.json — structured result JSON.
  • logs/npu_smi.log — sanitized raw npu-smi info device log.
  • README.md / readme.md — this document (byte-identical).
  • SHA256SUMS — SHA-256 manifest of the published file set.
  • assets/*.png — evidence screenshots.