m
mengsihan/ppo-seals-CartPole-v0-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PPO seals/CartPole-v0 - Ascend NPU Adaptation

Model: HumanCompatibleAI/ppo-seals-CartPole-v0
Revision: a86541dd3744275227cb16d07192967fc6b7775e (fixed snapshot tree hash)
Original Source: https://huggingface.co/HumanCompatibleAI/ppo-seals-CartPole-v0
Task: Reinforcement Learning - Discrete control on CartPole (seals/CartPole-v0)
Route: rl-policy (Discrete 2-action MlpPolicy, extended from offline-rl / demonstration-policy)
Backend: stable-baselines3 2.9.0 + torch_npu 2.9.0
Device: npu:0 (Ascend910_9362)

Model Summary

PPO agent trained with Stable-Baselines3 on seals/CartPole-v0 (Gymnasium + SEALS 0.2.1 suite). Policy is MlpPolicy with MlpExtractor:

  • pi: [64, 64] ReLU, vf: [64, 64] ReLU
  • action_net: Linear 64 -> 2 (Discrete logits), value_net: Linear 64 -> 1
  • Observation: Box(4,) float32 [cart_pos, cart_vel, pole_angle, pole_vel] , action Discrete(2) (push left/right)
  • Training hyperparameters: batch 256, gamma 0.9999, lr 0.00124, clip 0.4, n_steps 512, n_envs 8, verified mean_reward 500.0 +/-0.0.

Observation / Action Contract

  • Observation schema: np.ndarray float32 shape (4,) raw CartPole state. Batch inference supports (B,4). No image preprocessor.
  • Action schema: Raw logits shape (B,2) -> argmax deterministic action {0,1}. Horizon=1 (single step), coordinate_frame=cartpole-discrete.
  • Sampler: deterministic argmax (no stochastic sampling, seed N/A). For stochastic, Categorical sampling would be equivalent on CPU/NPU.

NPU Adaptation

  • Weight source fixed to local ./model_download/ppo-seals-CartPole-v0.zip (139KB) or HF snapshot_download fallback; no model substitution.
  • torch_npu used: model.policy.to("npu:0"), all float tensors (obs, feats, latent_pi, logits) kept on npu:0. Verified:
    • policy param device: npu:0
    • input device: npu:0
    • output logits device: npu:0
    • action device: npu:0
    • torch.npu.synchronize() wrapped timing.
  • No CPU fallback: all critical matmuls/ReLU on NPU. Categorical(logits) on NPU normalizes logits internally (logits - logsumexp), so raw action_net logits used for consistency (probs diff <1e-9).
  • Reviewed stable_baselines3 source; trust_remote_code not needed (no custom code).

CPU-NPU Consistency

  • Input: 5 fixed CartPole states (traceable, from gymnasium.make("seals/CartPole-v0") reset seed 0 and manual samples).
  • Compared object: raw action_net logits (shape 5x2, 10 values) + deterministic actions.
  • Result: max_abs=4.77e-07, tol=5.69e-03 (atol=1e-4, rtol=1e-3), finite=True, shape_ok=True, actions_match=True => PASS
    • Validation command: python scripts/validate_policy_outputs.py --cpu cpu_values.json --npu npu_values.json
    • Probs diff <1e-9, actions identical [0,0,1,1,0].
  • Bounds: actions in [0,2) PASS, finite PASS.

Performance

  • Environment: Ascend910_9362, 2 devices, CANN 8.5.1, torch 2.9.0+cpu + torch_npu, Python 3.11
  • Timing: torch.npu.synchronize() before/after, warmup 3, measured 10 iterations batch=5
  • Results:
    • first_compile_latency_ms: 0.549
    • avg_ms: 0.525, min 0.501, max 0.574, p50 0.522, p90 0.574, p95 0.574
    • decisions_per_s: 9527.1 (5 decisions per call)
    • actions_per_s: 9527.1
    • dtype float32, horizon 1, batch 5, actions 2 logits
  • Resource: weight bytes 139KB (zip) + 40KB policy.pth, peak memory <100 MB, fits single NPU (65536 MB).

Usage

pip install -r requirements.txt
python inference.py

Prints model/revision/route/backend/device/dtype, input summary, CPU vs NPU logits/probs/actions, synchronized latency, consistency and RESULT PASS/FAIL. No weights are bundled; they are downloaded once from HuggingFace HumanCompatibleAI/ppo-seals-CartPole-v0 or reused from ./model_download.

Limitations

  • Requires Ascend NPU with torch_npu and npu:0. CPU fallback is treated as failure.
  • Only seals/CartPole-v0 discrete action verified; other envs not tested.
  • Stochastic sampling not benchmarked (deterministic argmax is primary metric).
  • Rollout reward (500) not used as consistency proof; only single-step logits/actions compared.

Evidence

agent workflow npu device call model result