超
gcw_g59KKkes/edbeeching-decision-transformer-gym-hopper-medium
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

edbeeching/decision-transformer-gym-hopper-medium · 昇腾 NPU 单步动作预测

本仓库将 edbeeching/decision-transformer-gym-hopper-medium(一个针对 Gym Hopper 环境的强化学习 Decision Transformer,离线 RL 模型,不是视觉模型)适配到昇腾 (Ascend) NPU(torch-npu,设备 npu:0)。权重在首次使用时自动从镜像下载,无需手动干预。

  • 模型权重镜像:https://ai.gitcode.com/hf_mirrors/edbeeching/decision-transformer-gym-hopper-medium
  • HuggingFace 原始仓库:https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium
  • 模型架构: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 适配说明

  1. 无任何 CUDA 代码 — 模型与所有输入 Tensor 全部在 npu:0 上运行。
  2. NPU 初始化 包裹在 init_npu() 中,失败时输出 JSON 错误并退出。
  3. 权重自动下载 — 首次运行时从 GitCode 镜像自动下载到 model_weights/edbeeching/decision-transformer-gym-hopper-medium/,本地缓存优先;GitCode 镜像返回 429/400 错误体(本批次模型实际未在 GitCode 镜像)时自动回退到 hf-mirror.com。下载后对 pytorch_model.bin 做 torch.load + NaN/Inf 校验。
  4. 单步前向: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/。后续运行优先加载本地缓存,避免重复下载。

  • GitCode 镜像(主):https://ai.gitcode.com/hf_mirrors/edbeeching/decision-transformer-gym-hopper-medium
  • 备用镜像:https://hf-mirror.com/edbeeching/decision-transformer-gym-hopper-medium

使用方法

命令行(单步推理)

python3 inference.py --img examples/hopper_state.txt

examples/hopper_state.txt 是一个 2 行状态文件:

  • 第 1 行:11 个浮点数(一个 Hopper 观测,空格或逗号分隔)
  • 第 2 行:returns-to-go(标量,如 100.0;缺省时取 100.0)

输出 JSON 到 stdout(设备 npu:0,含 action_pred 与耗时)。

FastAPI 服务

python3 inference.py --mode server --port 8783
  • GET / — 最小化 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

输出 JSON

{
  "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.0

环境

  • Python 3.11.14,PyTorch(昇腾 NPU 版),torch_npu
  • 昇腾 Ascend 910
  • 依赖:transformers==4.57.6(含 DecisionTransformerModel)、torch、torch-npu、fastapi、uvicorn、safetensors、Pillow

文件说明

  • inference.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}
}