waylong/decision-transformer-gym-hopper-medium-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

edbeeching/decision-transformer-gym-hopper-medium on Ascend NPU

模型与任务

  • 模型:edbeeching/decision-transformer-gym-hopper-medium
  • Revision:8224ec324200b150f10287b8c8c525224e62f319
  • 适配路线:offline-rl (Decision Transformer, state/action/return-to-go sequence -> next action)
  • 架构:DecisionTransformerModel (GPT-2 style, 3 layers, 1 head, n_embd 768, hidden_size 128)
  • 权重:pytorch_model.bin 6.30 MB, 约 4.0M 参数, FP32
  • 任务:Gym Hopper 离线强化学习,观测 dim 11,动作 dim 3,max_ep_len 1000
  • 加载后端:transformers + torch_npu, dtype float32, 无 remote code

环境依赖

{
  "torch": "2.9.0+cpu",
  "torch_npu": "2.9.0.post1+gitee7ba04",
  "transformers": "4.57.6",
  "cann": "8.5.1",
  "npu": "Ascend910_9362 x2",
  "python": "3.11.14"
}

安装:pip install -r requirements.txt

模型权重通过 HuggingFace edbeeching/decision-transformer-gym-hopper-medium 获取,需配置 hf-mirror 或本地缓存 /data/adapt-npu-hf/hub/...。

归一化系数(来自模型卡):

  • mean = [1.311279, -0.08469521, -0.5382719, -0.07201576, 0.04932366, 2.1066856, -0.15017354, 0.00878345, -0.2848186, -0.18540096, -0.28461286]
  • std = [0.17790751, 0.05444621, 0.21297139, 0.14530419, 0.6124444, 0.85174465, 1.4515252, 0.6751696, 1.536239, 1.6160746, 5.6072536]

输入观测需做 (state - mean)/std 归一化,return-to-go 为原始标量(约 1500 中位数),timesteps 为递增整数。

NPU 推理

默认执行:python inference.py

可选:

python inference.py --mode validate    # CPU-NPU 一致性
python inference.py --mode benchmark   # 性能
python inference.py --device cpu       # CPU 对比
python inference.py --batch 4 --seq-len 20

设备判断:torch.npu.is_available() 且 --device npu:0 时使用 NPU,否则 CPU;打印 Model param device: npu:0。

真实输入

{
  "batch": 4,
  "seq_len": 20,
  "state_dim": 11,
  "act_dim": 3,
  "states": "normalized Hopper state, shape (4,20,11), dtype float32",
  "actions": "shape (4,20,3), dtype float32",
  "returns_to_go": "shape (4,20,1), sample ~1500, dtype float32",
  "timesteps": "shape (4,20), dtype int64, range 0-19",
  "attention_mask": "shape (4,20), ones"
}

真实推理结果

使用 python inference.py (batch=4, seq=20, seed=42, NPU npu:0):

Model: edbeeching/decision-transformer-gym-hopper-medium
Revision: 8224ec324200b150f10287b8c8c525224e62f319
Route: offline-rl
Model dir: /data/adapt-npu-hf/hub/models--edbeeching--decision-transformer-gym-hopper-medium/snapshots/8224ec324200b150f10287b8c8c525224e62f319
Config: state_dim=11 act_dim=3 max_ep_len=1000 n_layer=3 hidden_size=128 n_embd=768
Backend: transformers + torch_npu
Device: npu:0 (Ascend910_9362)
Model param device: npu:0
Input summary: batch=4 seq_len=20 state_dim=11 act_dim=3
Output: action_preds shape (4, 20, 3) device npu:0 dtype torch.float32
Action sample last step batch0: [0.8388814330101013, 0.8591884970664978, -0.506158709526062]
Action sample last step batch1: [0.34092772006988525, 0.34669429063796997, -0.22991016507148743]
Synchronized latency: 2.997 ms (batch=4 seq=20)
Finite check: True max=0.9831
PASS
  • 输入、模型参数、输出均在 npu:0,计时前后调用 torch.npu.synchronize()。
  • 首轮编译延迟 ~240-255 ms,稳态延迟 ~2.9-3.1 ms。
  • action 为 tanh 归一化后连续动作,范围 (-1,1),shape [4,20,3],全量有限值,horizon=20, action_dim=3。

CPU-NPU 一致性

python inference.py --mode validate 共用权重、观测、seed=42,比较完整动作序列:

CPU-NPU consistency: max_abs=9.182841e-07 mae=1.300049e-07 tolerance atol=1e-4 rtol=1e-3
Consistency: PASS
{"count": 240, "finite": true, "max_abs": 9.18e-07, "passed": true, "tolerance": 0.00108}

阈值 FP32 atol=1e-4 rtol=1e-3,实际 max_abs 9.18e-07 << tolerance,验证通过 via validate_policy_outputs.py。

性能

python inference.py --mode benchmark (batch=4, seq=20=20 decisions, npu:0):

Benchmark: samples=20 avg=2.88ms min=2.85 max=2.92 p50=2.87 p90=2.91 p95=2.92 decisions/s=1387.7
First compile latency: 237.8 ms
Synchronized latency: 3.011 ms (batch=4)
  • 预热 3 次,正式测量 20 次,每次调用 torch.npu.synchronize()。
  • 单位 decisions/s = batch*1000/avg_ms。
  • 资源:模型 <10 MB 显存,单卡 Ascend910 64GB 余量充足,不涉及多卡或删减模块。

自验证截图

Agent workflow

NPU device call

Model result

三张图片均由 xterm.js 根据本次真实日志生成,包含 SHA-256 与 exit code。

已知限制

  • 仅对本次记录的模型 revision、输入契约(batch=4 seq=20 normalized Hopper state, RTG~1500)、dtype float32 与 Ascend910/CANN 8.5.1 环境完成验证。
  • 未包含闭环 Gym Hopper 环境 rollout,reward 不是一致性验收对象;轨迹采样固定 seed 42。
  • 推理为单步动作块预测,非完整 episode 生成。

标签

#NPU #Ascend #Ascend910