net_arch=[dict(pi=[64,64], vf=[64,64])] activation ReLU, obs_dim=4 -> Linear(4,64)->ReLU->Linear(64,64)->ReLU -> action_net Linear(64,2) + value_net Linear(64,1), 2-layer shared MLP 64 hidden units, Discrete action 2ppo-seals-CartPole-v0.zip 解压后 policy.pth, 本地加载不替换权重torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362, npu:0 可用npu:0 推理, 同权重同输入同seed对比CPU, torch.npu.synchronize 计时pip install -r requirements.txt
# torch==2.9.0
# torch-npu==2.9.0.post1
# numpy>=1.24
# huggingface_hub>=0.20权重获取 (已本地缓存 / 可自动下载):
export HF_ENDPOINT=https://hf-mirror.com
curl -L https://hf-mirror.com/HumanCompatibleAI/ppo-seals-CartPole-v0/resolve/main/ppo-seals-CartPole-v0.zip -o /tmp/ppo.zip
unzip /tmp/ppo.zip -d /tmp/ppo_test
ls -lh /tmp/ppo_test/policy.pth # 40641 bytes
# 或 python 自动:
python -c "from huggingface_hub import hf_hub_download; print(hf_hub_download('HumanCompatibleAI/ppo-seals-CartPole-v0', filename='ppo-seals-CartPole-v0/policy.pth'))"默认命令 (真实 NPU, 自动命中本地缓存):
python inference.py
# 或
python inference.py --mode validate # CPU-NPU一致性验证
python inference.py --mode benchmark # 性能测试 (3 warmup +10)
python inference.py --device npu # 强制NPU
python inference.py --device cpu # 仅CPU对照policy.pth OrderedDict -> PPO_MLP -> to('npu:0'), 验证 first_param.device == npu:0[1,4] float32, device npu:0torch.npu.synchronize() 包围计时以下为本次 python inference.py 在 npu:0 上的真实输出 (截取关键):
Model: HumanCompatibleAI/ppo-seals-CartPole-v0
Revision: a86541dd3744275227cb16d07192967fc6b7775e
Route: offline-rl (PPO discrete policy)
Architecture: PPO MlpPolicy pi=[64,64] vf=[64,64] obs_dim=4 action_dim=2
Task: reinforcement-learning seals/CartPole-v0 observation->action
Requested device: auto -> actual device: npu:0
torch_npu available: True device_name: Ascend910_9362
dtype: float32
Weight file: /tmp/ppo_test/policy.pth (40641 bytes)
Weights loaded: OK
Model first param device: npu:0
Input: batch=5 obs_dim=4 fixed CartPole states
Input sample: [0.01, -0.02, 0.03, -0.01]
Input device: npu:0
Output logits device: npu:0 shape: [1, 2] values: [[-0.091355, 0.093317]]
Output probs: [[0.4539, 0.5460]] action: [1] value: [[196.2617]]
Action space: Discrete(2) horizon=1 action_dim=1
Inference latency (single): 133.678 ms (synchronized)
Batch inference: n=5 logits=[[0.5115, -0.5097], [-2.3264, 2.3289], [2.7183, -2.7180], [-0.9939, 0.9967], [-1.2701, 1.2691]] actions=[0, 1, 0, 1, 1] time=1.457ms
Finite check: True
PASS: PPO policy NPU inference successful[0.01, -0.02, 0.03, -0.01] -> logits [-0.091, 0.093] -> probs [0.4539, 0.5460] -> action 1 (right), value 196.26[0,1,0,1,1] 全部有限值, 无NaN/Inf, discrete bounds合法同权重同obs同deterministic argmax, 比较logits/actions:
--- validate: CPU vs NPU consistency ---
CPU logits: [[0.51155, -0.50978], [-2.32645, 2.32899], [2.71834, -2.71806], [-0.99394, 0.99677], [-1.27013, 1.26917]]
NPU logits: [[0.51155, -0.50978], [-2.32645, 2.32898], [2.71834, -2.71806], [-0.99394, 0.99677], [-1.27013, 1.26917]]
CPU actions: [0, 1, 0, 1, 1] NPU actions: [0, 1, 0, 1, 1] match=True
max_abs_logits=4.7e-07 max_abs_value=3.0e-05 tol atol=1e-4 rtol=1e-3
Consistency PASS: Truenpu:0, 无CPU fallback--mode benchmark 3次预热 + 10次正式, torch.npu.synchronize() 计时, batch=1, obs_dim=4, float32, npu:0:
Latencies ms: [0.949, 0.944, 0.944, 0.946, 0.943, 0.942, 0.944, 0.949, 0.953, 0.945]
avg=0.946 min=0.942 max=0.954 p50=0.945 p90=0.950 p95=0.954
Throughput: 1056.61 decisions/s, 1056.61 actions/s (horizon=1)
首推理(编译): 133-142ms
稳态: ~0.94mssynchronize() 真实端到端, 不含预处理 (观测已为tensor)assets/agent_workflow.png — 阶段日志: 环境检查, 模型侦察, NPU推理, 一致性, 性能assets/npu_device_call.png — npu-smi 与 torch_npu device证明assets/model_result.png — inference.py真实输出与PASStrust_remote_code=False, 纯PyTorch MLP无自定义代码