z
zhangyuge147/HumanCompatibleAI-ppo-Pendulum-v1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

HumanCompatibleAI/ppo-Pendulum-v1 on Ascend NPU

Model Description

  • Base Model: HumanCompatibleAI/ppo-Pendulum-v1 (PPO MlpPolicy)
  • Revision: 6a0fe838119427278252dde3bf723ed7a3266df5
  • Task: reinforcement-learning | Pendulum-v1 continuous control
  • Route: offline-rl (adapted PPO continuous policy, observation -> action)
  • Original Weight: https://huggingface.co/HumanCompatibleAI/ppo-Pendulum-v1
  • Weight File: ppo-Pendulum-v1/policy.pth (40382 bytes, ~36k params, Tanh MLP 64x64)
  • License: apache-2.0
  • Ported: Pure PyTorch MLP without SB3 runtime, device-agnostic, supports npu:0 and cpu

Task Contract

  • Environment: Gymnasium Pendulum-v1 (OpenAI Gym classic control)
  • Observation: Box(3,) [cos(theta), sin(theta), theta_dot] where theta in [-pi, pi], theta_dot in [-8,8], dtype float32
  • Action: Box(1,) continuous torque in [-2.0, 2.0], dtype float32
  • Policy: PPO MlpPolicy with mlp_extractor.policy_net=[Linear(3,64)+Tanh, Linear(64,64)+Tanh] -> action_net Linear(64,1) -> mean_actions -> clamp[-2,2] and value network same shape. use_sde=True during training but deterministic inference uses mean only; log_std (64,1) is training-only and ignored for deterministic action.
  • Horizon: 1 (single-step action), no action chunk. Batch supported.
  • Preprocessing: No normalization (normalize=False in training), raw observation directly fed to MLP.

NPU Adaptation

  • Reimplemented PPO MLP in pure torch.nn (Tanh activations matching SB3) to allow direct to(npu:0) placement and avoid gymnasium/stable-baselines3 NPU incompatibilities.
  • Weights copied field-by-field from policy.pth OrderedDict: mlp_extractor.policy_net.*, mlp_extractor.value_net.*, action_net.*, value_net.*.
  • All compute stays on requested device: model params, float observation Tensor, mean/value and cloned clipped action all on npu:0 when NPU requested; verified via next(model.parameters()).device, input.device, output.device.
  • Synchronized timing via torch.npu.synchronize() before/after forward.
  • Peak memory < 50MB (model 40KB), fits single Ascend910 with 64GB HBM.

Inference

pip install -r requirements.txt
python inference.py                 # auto NPU if available
python inference.py --device npu    # force npu:0
python inference.py --device cpu    # force cpu
python inference.py --mode validate   # CPU vs NPU consistency
python inference.py --mode benchmark  # warmup 3 + 10 measured

Default python inference.py loads local policy.pth (or hf_hub_download fallback), prints model/revision/route/backend/device/dtype/input summary/action summary/synced latency/PASS/FAIL.

Real Results (Ascend910_9362, torch_npu 2.9.0.post1)

  • NPU device: Ascend910_9362, torch.npu.is_available()=True, npu:0 exists, count=2
  • Input device: npu:0 (single obs [1.0,0.0,0.0] upright, batch 8 covering upright/down/side states)
  • Output device: npu:0 shape [1,1] and [8,1]
  • Single action (upright): mean -0.267 clipped -0.267 value -0.066
  • Batch actions (clipped to [-2,2]): includes saturation at bounds: raw means exceed bounds e.g. 3.90 -> 2.0, -4.96 -> -2.0
  • Finite & Bounds: true
  • First compile latency (single, synchronized): ~130 ms includes NPU graph compilation
  • Steady-state latency: ~0.95 ms avg after 3 warmup (10 samples: min 0.93 max 0.95 p50 0.945 p90 0.954 p95 0.954) -> ~1056 decisions/s, ~1056 actions/s (batch=1, horizon=1)
  • CPU vs NPU consistency (same weights, same obs 8-batch, deterministic): max_abs_mean ~0.000000, max_abs_action 0.000000, max_abs_value 0.000008, tolerance 0.005 (atol=1e-4 rtol=1e-3) -> PASS, actions identical after clipping, finite true.

CPU-NPU Consistency Details

Shared weights, same 8 fixed observations, same Tanh MLP, deterministic mode, dtype float32. Compared raw mean and clipped action and value. Threshold FP32 atol=1e-4 rtol=1e-3. Continuous action bounds checked.

metricvalue
max_abs_mean0.000000
max_abs_action0.000000
max_abs_value0.000008
tolerance0.005063
actions_match (after clip)true

Performance

  • First inference: ~127-134 ms (NPU kernel compilation)
  • Warm steady: avg 0.946 ms / decision (batch=1)
  • Throughput: 1056 decisions/s, 1056 actions/s
  • Batch=8: ~0.7 ms for 8 obs (~11428 obs/s)
  • Memory: model 40KB, activations negligible

Environment Constraints

  • Requires torch_npu built for CANN 8.5.1, Ascend910B/910 recommended, single card sufficient.
  • python -m gymnasium not required at runtime; pure torch inference avoids env dependencies.
  • Trust remote code: not needed; all code is auditable local inference.py.

Evidence

  • assets/agent_workflow.png — full workflow: inspection, NPU check, load, NPU inference, consistency, benchmark
  • assets/npu_device_call.png — npu-smi, torch_npu availability, device name, model/input/output device proof
  • assets/model_result.png — default python inference.py showing action, latency, PASS

File Tree

inference.py
readme.md
requirements.txt
assets/agent_workflow.png
assets/npu_device_call.png
assets/model_result.png

Limitations

  • Deterministic mean only; stochastic sampling (with gSDE noise) not exposed but reproducible via adding Gaussian with learned scale if needed.
  • Real Gymnasium rollout reward not evaluated; numerical consistency is primary proof per policy contract.