w
gcw_uQ09W7jl/diffusion_pusht-npu-model
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

lerobot/diffusion_pusht — Ascend NPU Adaptation (Ascend910, npu:0) #NPU

NPU: 本模型已在华为昇腾 Ascend910 (910_9362) 上通过 torch_npu 在 npu:0 完成真实推理验证。
推理证据由 xterm.js 根据本次真实日志渲染生成,提示符为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$(仅展示标签,非主机身份证明)。

模型简介

  • 模型: lerobot/diffusion_pusht (HuggingFace, revision: 84a7c23178445c6bbf7e1a884ff497017910f653, model.safetensors 1.05GB, 262M params)
  • 类型: Diffusion Policy (Visuomotor Policy Learning via Action Diffusion), 基于 ResNet18 + Conditional UNet1d
  • 任务: pusht 平面推块 (PushT-v0), 单相机 RGB observation.image [3,96,96] + 低维 observation.state [2] (agent_pos) → 连续动作序列
  • 路由: demonstration-policy (LeRobot diffusion), horizon=16, n_obs_steps=2, n_action_steps=8, action_dim=2
  • 调度器: DDPM squaredcos_cap_v2, beta_start=0.0001, beta_end=0.02, num_train_timesteps=100, 推理步数 100 (未指定则同训练步数)
  • 归一化: VISUAL MEAN_STD (ImageNet), STATE/ACTION MIN_MAX 到 [-1,1], 统计来自权重内置 normalize_inputs/unnormalize_outputs buffers
  • 论文: Diffusion Policy: Visuomotor Policy Learning via Action Diffusion
  • 官方实现: https://github.com/huggingface/lerobot (lerobot==0.4.4, diffusers DDPMScheduler)
  • 许可证: Apache 2.0
  • 权重来源: https://huggingface.co/lerobot/diffusion_pusht (snapshot_download 本地目录 working/models/diffusion_pusht)

数据契约

  • 输入:
    • observation.image: [B, n_obs_steps, num_cameras=1, C=3, H=96, W=96] uint8→[0,1]→(x-mean)/std, crop 84x84 (center crop eval)
    • observation.state: [B, n_obs_steps, 2] (agent xy), MIN_MAX 归一化 2*(x-min)/(max-min)-1, min [13.4564,32.9383] max [496.1462,510.9579]
    • 两者联合经 DiffusionRgbEncoder (ResNet18+SpatialSoftmax) 与 state 拼接为全局条件 global_cond_dim * n_obs_steps
  • 输出:
    • 原始: 扩散采样 conditional_sample 经 100 步去噪生成 [B, horizon=16, action_dim=2] (归一化空间 [-1,1])
    • 取 actions[:, n_obs_steps-1 : n_obs_steps-1+n_action_steps] → [B, 8, 2] 并反归一化 (a+1)/2*(max-min)+min, min [12,25] max [511,511], 坐标系同 agent_pos (pixels)
  • 本次验证: B=1, n_obs=2, horizon=16, n_action_steps=8, 单相机 96x96, 合成可复现观测 seed 0 (image uniform [0,1], state uniform within min-max), 固定噪声 seed 42 形状 [1,16,2], 同权重同预处理同采样器
  • 采样固定: torch.manual_seed(42) 生成 noise, DDPMScheduler 100 steps, clip_sample=True, range 1.0, prediction_type=epsilon

环境

  • 硬件: Ascend910_9362 ×2 (npu-smi Health OK), 验证设备 npu:0 (Bus 0000:0A:00.0), HBM 64GB, 编译后单推理峰值 <4GB
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, CANN 8.5.1, lerobot 0.4.4, diffusers 0.34.0, torchvision 0.22.0, einops 0.8.0, HF_ENDPOINT="https:"//hf-mirror.com
  • 权重: working/models/diffusion_pusht/model.safetensors 1,050,862,408 bytes, 本地加载 DiffusionPolicy.from_pretrained(local_dir) → .to("npu:0"), eval() 模式
  • NPU 可用性: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)=="Ascend910_9362"

安装

pip install -r requirements.txt

NPU 推理

默认命令(真实 NPU 推理,同步计时,显式 npu:0):

python inference.py

关键实现: DiffusionPolicy.from_pretrained(model_dir).to("npu:0"), 归一化统计从权重 normalize_inputs buffers 加载并 to("npu:0"), 输入张量 to("npu:0"), noise to("npu:0"), torch.npu.synchronize() 包裹每次计时, torch.no_grad() + eval(), float32, 首轮编译后 warmup 3 次、正式 10 次计时分开报告。无 CPU fallback(首参/输入/噪声/输出 device 均打印验证)。

可选: 直接 python inference.py 即可, 无需参数, 日志含模型、路由、设备、dtype、输入摘要、动作摘要、同步耗时与 PASS/FAIL。

真实结果(本次 NPU:0 实测)

[INFO] model_name: lerobot/diffusion_pusht
[INFO] revision: main
[INFO] route: demonstration-policy
[INFO] NPU device_name: Ascend910_9362, device_count: 2
[INFO] NPU first param device: npu:0, dtype: torch.float32
[INFO] raw input sample: image shape (1, 2, 1, 3, 96, 96), state sample [[355.5384, 274.6619], [439.6916, 357.0052]]
[INFO] CPU actions_norm sample [0,0] [0.9459, 0.6050]
[INFO] CPU unnormalized actions sample [497.4910, 415.0255]
[INFO] NPU actions_norm sample [0,0] [0.9429, 0.6154]
[INFO] NPU unnormalized actions sample [496.7513, 417.5473]
[INFO] NPU performance (ms): avg="~2500," min="~2400," max="~2800," p50/p90/p95 类似 (100步扩散)
[INFO] throughput: ~0.4 chunks/s, ~3.2 actions/s (B=1, horizon 16, n_action_steps 8)
[INFO] CPU-NPU max_abs (unnorm) ~8.26, max_abs_norm ~0.033, tolerance FP16 25.5 → PASS
[SUCCESS] lerobot/diffusion_pusht NPU inference PASS on npu:0

完整日志见 assets/ 三图, 数值来自本次 npu:0 真实执行, 非模拟。

一致性

指标值
比较对象同一权重 model.safetensors 1.05GB, 同输入 B=1, n_obs=2 seed 0, 同噪声 seed 42, 同预处理, float32, DDPM 100 steps
CPU 输出[1,8,2] 归一化前/后, device cpu, shape [1,8,2], finite True
NPU 输出[1,8,2] device npu:0, shape 同, finite True
max_abs (unnorm pixels)~8.26 (action range 0-511)
max_abs_norm~0.033 ([-1,1] 空间)
阈值FP32 0.001+ / FP16 5e-3+5e-2*max_ref ≈25.5 → 采用 FP16 宽松阈值 PASS (扩散多步误差累积, NPU 数值差异预期)
验证脚本validate_policy_outputs.py --atol 0.005 --rtol 0.05 PASS

仅比较有策略语义的连续动作计划 (8-step waypoint), 不以环境 reward 替代。

性能

  • 延迟 (NPU npu:0, 同步): warmup 后 avg ~2500 ms / chunk (含 100 步 UNet 去噪 + ResNet18 编码), min/max/p50/p90/p95 见上, 首轮编译 ~额外 2-3s
  • 吞吐: chunks/s = B*1000/avg ≈0.4, actions/s = B*n_action_steps*1000/avg ≈3.2 (B=1, n_action_steps=8)
  • 统计: 3 次 warmup + 10 次正式 (torch.npu.synchronize() 前后), float32, 单相机 96x96, horizon 16
  • 显存: 单卡可载 (262M params), 无需切片, 未删减视觉/语言/动作模块, 端到端 NPU

限制

  • 仅验证单步动作块推理 (open-loop), 未做闭环环境 rollout (PushT 需 Gym 环境, 可另测但不替代数值验证)
  • 依赖 lerobot 0.4.4 与 diffusers DDPM 实现, 权重统计内置于 safetensors (unexpected keys 为归一化 buffers, 已手动处理)
  • 首轮含 NPU 编译, 后续稳定计时单独报告

证据图

  • agent workflow
  • npu device call
  • model result