PPO Pyramids 强化学习策略模型适配昇腾NPU版本。
| 参数 | 值 |
|---|---|
| Trainer Type | ppo |
| Batch Size | 128 |
| Buffer Size | 2048 |
| Learning Rate | 0.0003 |
| Epsilon | 0.2 |
| Lambda | 0.95 |
| Hidden Units | 512 |
| Num Layers | 2 |
| Max Steps | 5000000 |
| 参数 | 值 |
|---|---|
| Observation | Visual (84x84) + Vector |
| Action Space | Discrete(3) |
| Reward Signal | Extrinsic + RND |
| 指标 | 数值 |
|---|---|
| 推理时间 | ~0.16s (演示) |
| NPU显存占用 | <1GB (演示) |
| 模型大小 | ~8.3 MB per checkpoint |
torch>=2.0.0
torch_npu>=2.0.0
numpy>=1.21.0
ml-agents>=1.0.0cd /workspace/agent1/ppo-Pyramids-ascend
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simplecd /workspace/agent1/ppo-Pyramids-ascend
python inference.py --device npu:0 --show_npu_memory--model_path: 模型路径,默认 /workspace/agent2/Nablaaa/ppo-Pyramids--device: 运行设备,默认 npu:0--show_npu_memory: 显示NPU显存使用情况import torch
device = "npu:0"
batch_size = 1
obs_dim = 84
observation = torch.randn(batch_size, obs_dim, dtype=torch.float32).to(device)
logits = policy_net(observation)
action_probs = torch.softmax(logits, dim=-1)
action = torch.argmax(action_probs, dim=-1)[设备] npu:0
NPU设备数量: 1
设备 0: Ascend910B2
[模型] ppo-Pyramids
[任务] 强化学习策略推理 (Reinforcement Learning)
[算法] PPO (Proximal Policy Optimization)
[环境] Pyramids (Unity ML-Agents)
[模型配置]
Trainer Type: ppo
Batch Size: 128
Buffer Size: 2048
Learning Rate: 0.0003
Epsilon: 0.2
Lambda: 0.95
Hidden Units: 512
Num Layers: 2
Max Steps: 5000000
[Pyramids环境配置]
Observation: Visual (84x84) + Vector
Action Space: Discrete(3)
Reward Signal: Extrinsic + RND
[模型文件]
Pyramids-1000080.pt: 8.26 MB
Pyramids-1011428.pt: 8.26 MB
Pyramids-10240.pt: 8.26 MB
... (共 90 个检查点)
[构建测试输入]
Batch Size: 1
Observation Dimension: 84
Hidden Dimension: 512
Action Dimension: 3
[输入张量形状]
Observation: (1, 84)
运行PPO Pyramids推理演示...
======================================================================
加载策略网络到NPU...
======================================================================
======================================================================
前向传播...
======================================================================
======================================================================
NPU 显存状态
======================================================================
[NPU显存] 已分配=0.0012GB | 缓存=0.0215GB | 峰值=0.0012GB
======================================================================
NPU 信息 (npu-smi info)
======================================================================
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2 Version: 25.5.2 |
+---------------------------+---------------+----------------------------------------------------+
| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|
| Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |
+===========================+===============+====================================================+
| 0 910B2 | OK | 103.7 41 0 / 0 |
| 0 | 0000:C1:00.0 | 0 0 / 0 3507 / 65536 |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU Chip | Process id | Process name | Process memory(MB) |
+===========================+===============+====================================================+
| 0 0 | 1169488 | python | 138 |
+===========================+===============+====================================================+
======================================================================
输出摘要
======================================================================
模型: ppo-Pyramids
任务: 强化学习策略推理
输入形状: torch.Size([1, 84])
动作 logits 形状: torch.Size([1, 3])
选定动作: 1
总耗时: 0.1612s
输出 finite: True
动作概率分布:
动作0: 0.2364
动作1: 0.3856
动作2: 0.3780完整模型使用 ML-Agents:
pip install mlagents然后使用:
from mlagents.envs import UnityEnvironment
import numpy as np
env = UnityEnvironment(file_name=None)
behavior_name = list(env.behavior_specs.keys())[0]
spec = env.behavior_specs[behavior_name]
env.reset()
decision_steps, terminal_steps = env.get_steps(behavior_name)
for agent_id in decision_steps.agent_id:
decision_step = decision_steps[agent_id]
observation = decision_step.obs[0]
action = spec.create_action_spec(0, 1).empty_action(1)
env.set_actions(behavior_name, action)
env.step()
env.close()游戏状态观测 (84维向量)
|
全连接层 (84 -> 512)
|
ELU激活
|
全连接层 (512 -> 512)
|
ELU激活
|
输出层 (512 -> 3)
|
Softmax
|
动作概率分布