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

HumanCompatibleAI/ppo-CartPole-v1 NPU Adaptation

Model Introduction

HumanCompatibleAI/ppo-CartPole-v1 is a PPO (Proximal Policy Optimization) agent trained with the RL Zoo and stable-baselines3 on the CartPole-v1 environment. It learns to balance a pole on a cart by choosing between two discrete actions (push left / push right).

  • Original model: https://huggingface.co/HumanCompatibleAI/ppo-CartPole-v1
  • Algo: PPO (SB3 MlpPolicy)
  • Architecture: ActorCriticPolicy (MLP 64-64, Tanh) with separate actor (action_net) and critic (value_net) heads
  • Observation: 4-dim CartPole state [cart_pos, cart_vel, pole_angle, pole_ang_vel]
  • Action: 2 discrete actions (Categorical distribution)
  • Framework: PyTorch + torch_npu (Ascend 910)
  • Params: ~9.2K (FP32, 9155)
  • Seed (training): 2623485154

This repository adapts the real trained PPO policy to run on a single Ascend NPU (npu:0). All policy parameters and every input/output tensor are explicitly placed on npu:0; inference.py loads the real weights from the local checkpoint directory and produces the real policy action (action logits, probabilities and value estimate).

Policy / Action Contract

FieldValue
Routeoffline-RL policy (SB3 PPO)
Observation dim4 (cart_pos, cart_vel, pole_angle, pole_ang_vel)
Action dim2 discrete (CartPole push left/right)
ActorLinear(4→64) → Tanh → Linear(64→64) → Tanh → Linear(64→2)
CriticLinear(4→64) → Tanh → Linear(64→64) → Tanh → Linear(64→1)
Outputaction logits, action probabilities, state value, deterministic action
Samplerdeterministic (argmax) for verification
Seed (training)2623485154

Data Contract

The model is evaluated on fixed, deterministic CartPole observation batches (real observations from the environment space, no random initialization at verification time). inference.py uses a fixed 5-observation batch of float32 CartPole state vectors.

The weights are loaded from the local checkpoint directory (PPO_CARTPOLE_LOCAL_DIR) if provided, otherwise downloaded from HuggingFace Hub (HF_ENDPOINT mirror) on first run.

Environment

  • Ascend 910 NPU (single card, npu:0), CANN 8.5.1 / Ascend Toolkit
  • Python 3.11, PyTorch 2.9.0, torch_npu 2.9.0.post1
  • stable-baselines3 2.9.0, gymnasium, numpy, huggingface_hub

Installation

pip install -r requirements.txt

NPU Inference

python inference.py

To load weights from a local directory instead of downloading:

PPO_CARTPOLE_LOCAL_DIR=/path/to/model python inference.py

Real NPU Result

All computation runs on npu:0 (Ascend910_9362). The 5-observation batch produces:

  • Action logits: shape (5, 2), dtype float32, device npu:0
  • Action probs: shape (5, 2), device npu:0
  • State value: shape (5, 1), device npu:0
  • Deterministic actions: [1, 1, 1, 0, 1]
  • Action logits (flattened): [-6.35313, 6.35593, -17.81828, 17.82692, -18.07850, 18.08478, 10.21172, -10.21372, -9.73818, 9.74270]
  • CPU-NPU max_diff_logits: 0.000006 (tol 0.018), max_diff_value: 0.000008 (tol 0.050), actions_match True, finite True
  • Latency (10 samples, synchronized): avg 0.30 ms, min 0.28 ms, max 0.34 ms, p50 0.29 ms, p90 0.31 ms, p95 0.34 ms

CPU-NPU Consistency

CPU and NPU share identical weights (ppo-CartPole-v1.zip), observation batch, preprocessing, deterministic sampler, seed, dtype (FP32) and policy parameters. Verification uses FP32 tolerance atol=1e-4, rtol=1e-3. Logits and value are compared element-wise; actions are compared via torch.equal on argmax. Tolerance is satisfied (0.000006 < 0.018).

Performance

  • Batch: 5 observations, horizon 1, action_dim 2
  • Dtype: float32, device npu:0
  • Samples: 10 post-warmup (3 warmup), synchronized torch.npu.synchronize() before/after each timing
  • Throughput: ~16666 decisions/s (5 obs / 0.30ms), ~3333 batches/s
  • Peak NPU memory: <10 MB (Tiny MLP policy)

Evidence

  • agent workflow
  • npu device call
  • model result

Limitations

  • Single NPU (npu:0) only; multi-card not tested.
  • Fixed 5-observation demo batch; full Gym rollout not included (would require env loop, orthogonal to single-step policy verification).
  • FP32 only; FP16/BF16 not validated (policy trained in FP32).

License

MIT (original model). NPU adaptation code MIT.