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

HumanCompatibleAI/ppo-CartPole-v1 on Ascend NPU

Model Description

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

Task Contract

  • Environment: Gymnasium CartPole-v1 (classic control)
  • Observation: Box(4,) [cart_pos, cart_vel, pole_angle, pole_ang_vel] where cart_pos in [-4.8,4.8], pole_angle in [-0.418,0.418] rad, dtype float32
  • Action: Discrete(2) 0=push left, 1=push right, logits [2] -> argmax -> discrete, prob via softmax
  • Policy: PPO MlpPolicy with mlp_extractor.policy_net=[Linear(4,64)+Tanh, Linear(64,64)+Tanh] -> action_net Linear(64,2) -> logits -> argmax and value network same shape. Deterministic inference uses argmax, stochastic would sample from softmax.
  • Horizon: 1 (single-step action), no action chunk. Batch supported.
  • Preprocessing: No normalization, 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, logits/value and cloned 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 [0,0,0,0] centered upright, batch 8 covering centered/tilted/cart-offset states)
  • Output device: npu:0 shape [1,2] logits and [8,2] batch, action shape [1] and [8]
  • Single logits (upright): [-6.353, 6.356] -> probs [3.0e-06, 0.999997] -> action 1 (push right) value 49.98
  • Batch logits/actions: actions [1,1,1,1,0,1,0,1] with high confidence (>0.93 even near decision boundary, saturates to 1.0 for far states)
  • Batch value: varies 37.5-50.0 with max at upright
  • Finite & Bounds: true, discrete bounds [0,1] satisfied
  • First compile latency (single, synchronized): ~138 ms includes NPU graph compilation
  • Steady-state latency: ~0.237 ms avg after 3 warmup (10 samples: min 0.229 max 0.275 p50 0.232 p90 0.244 p95 0.275) -> ~4217 decisions/s, ~4217 actions/s (batch=1, horizon=1)
  • CPU vs NPU consistency (same weights, same obs 8-batch, deterministic argmax): max_abs_logits 0.000004, max_abs_value 0.000004, tolerance 0.018 (atol=1e-4 rtol=1e-3) -> PASS, discrete actions identical, probs diff <1e-06, finite true.

CPU-NPU Consistency Details

Shared weights, same 8 fixed observations, same Tanh MLP, deterministic argmax, dtype float32. Compared logits, probs, argmax action and value. Threshold FP32 atol=1e-4 rtol=1e-3. Discrete action bounds checked.

metricvalue
max_abs_logits0.000004
max_abs_probs0.000000
max_abs_value0.000004
tolerance0.018267
actions_match (argmax)true

Performance

  • First inference: ~138-141 ms (NPU kernel compilation)
  • Warm steady: avg 0.237 ms / decision (batch=1)
  • Throughput: 4217 decisions/s, 4217 actions/s
  • Batch=8: ~0.6 ms for 8 obs (~13300 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 logits, 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 argmax only; stochastic sampling via softmax + multinomial not exposed by default but reproducible (probs available).
  • Real Gymnasium rollout reward not evaluated; numerical consistency is primary proof per policy contract. Original HF eval reports mean_reward 500 +/-0 over 10 episodes deterministic.