本仓库将 edbeeching/decision-transformer-gym-hopper-medium(一个针对 Gym Hopper 环境的强化学习 Decision Transformer,离线 RL 模型,不是视觉模型)适配到昇腾 (Ascend) NPU(torch-npu,设备 npu:0)。权重在首次使用时自动从镜像下载,无需手动干预。
transformers.DecisionTransformerModel(model_type=decision_transformer),GPT-2 风格 transformer 编码器(n_embd=768、n_layer=3、n_head=1、n_positions=1024、max_ep_len=1000),输入 Hopper 观测(state_dim=11),输出动作(act_dim=3,action_tanh=true)。| 字段 | 值 |
|---|---|
| 模型 | edbeeching/decision-transformer-gym-hopper-medium |
| 架构 | Decision Transformer(DecisionTransformerModel,GPT-2 编码器,3 层 × 1 头) |
| 任务 | 强化学习离线决策 —— 给定 Hopper 观测与 returns-to-go,预测下一步动作(3 维) |
| 输入 | Hopper 观测向量(11 维)+ returns-to-go 标量 |
| 输出 | action_pred(3 维,action_tanh=true 时经 tanh 激活) |
| 权重文件 | config.json、pytorch_model.bin(约 6.30 MB;该仓库无 model.safetensors,请勿尝试) |
注意:这是 RL 模型,不是视觉模型。CLI 的
--img参数(沿用模板命名)接收的是状态文件,不是图片。
npu:0 上运行。init_npu() 中,失败时输出 JSON 错误并退出。model_weights/edbeeching/decision-transformer-gym-hopper-medium/,本地缓存优先;GitCode 镜像返回 429/400 错误体(本批次模型实际未在 GitCode 镜像)时自动回退到 hf-mirror.com。下载后对 pytorch_model.bin 做 torch.load + NaN/Inf 校验。states (1,1,11)、actions (1,1,3)(零)、returns_to_go (1,1,1)、timesteps (1,1)(零)、attention_mask (1,1)(全 1);取 out.action_preds[0,-1] 作为预测动作,action_tanh=true 时施加 torch.tanh。权重在首次使用时自动从镜像下载,缓存于 model_weights/edbeeching/decision-transformer-gym-hopper-medium/。后续运行优先加载本地缓存,避免重复下载。
python3 inference.py --img examples/hopper_state.txtexamples/hopper_state.txt 是一个 2 行状态文件:
100.0;缺省时取 100.0)输出 JSON 到 stdout(设备 npu:0,含 action_pred 与耗时)。
python3 inference.py --mode server --port 8783GET / — 最小化 HTML 上传页面(暗色终端风格)POST /predict — multipart 上传状态 txt 或表单字段 state(11 个逗号/空格分隔浮点)与 rtg → 预测 JSON示例:
curl -s -F file=@examples/hopper_state.txt http://127.0.0.1:8783/predict
curl -s -F "state=0.0 0.0 1.25 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0" -F "rtg=120.0" http://127.0.0.1:8783/predict{
"model": "edbeeching/decision-transformer-gym-hopper-medium",
"source": "https://ai.gitcode.com/hf_mirrors/edbeeching/decision-transformer-gym-hopper-medium",
"device": "npu:0",
"status": "success",
"elapsed_ms": 126.41,
"state": [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
"state_dim": 11,
"returns_to_go": 100.0,
"act_dim": 3,
"action_tanh": true,
"action_pred": [-0.55987, -0.548242, 0.312382]
}examples/hopper_state.txt:
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
100.0transformers==4.57.6(含 DecisionTransformerModel)、torch、torch-npu、fastapi、uvicorn、safetensors、Pillowinference.py — 双模式推理入口(CLI + FastAPI,NPU 适配)make_screenshots.py — 生成验证截图examples/hopper_state.txt — 示例 Hopper 状态文件(11 维观测 + returns-to-go)assets/ — 验证截图(agent_workflow / npu_device_call / model_result)@inproceedings{chen2021decision,
title={Decision Transformer: Reinforcement Learning via Sequence Modeling},
author={Chen, Lili and Lu, Kevin and Rajeswaran, Aravind and Lee, Kimin and Grover, Aditya and Laskin, Misha and Abbeel, Pieter and Srinivas, Aravind and Mordatch, Igor},
booktitle={Advances in Neural Information Processing Systems (NeurIPS)},
year={2021}
}