z
zhangyuge147/lerobot-smolvla_libero-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

lerobot/smolvla_libero on Ascend NPU

agent workflow npu device call model result

1. 模型简介

  • 模型名称: lerobot/smolvla_libero
  • 权重URL: https://huggingface.co/lerobot/smolvla_libero (revision 31d453f7edd78c839a8bbc39744a292686daf0de)
  • 任务类型: 强化学习 / 具身智能 — Vision-Language-Action (VLA) 机械臂操作 (manipulation-vla, LIBERO)
  • 架构: SmolVLM2-500M-Video-Instruct (16层裁剪) + Expert Transformer (cross_attn, expert_width 0.75) + Flow Matching 去噪 (10步, beta分布时间采样)
  • 权重: model.safetensors 865MB + preprocessor/postprocessor safetensors, FP32, ~0.45B 参数
  • 许可证: apache-2.0
  • 论文: https://arxiv.org/abs/2506.01844 (SmolVLA: A Vision-Language-Action Model for Affordable and Efficient Robotics)
  • 输入契约: 3视角 RGB 256x256 (resize_with_padding至512x512), proprio observation.state [8] (MEAN_STD归一, stats 8-dim), 语言指令 task (tokenizer HuggingFaceTB/SmolVLM2-500M-Video-Instruct, max_length 48, pad max_length)
  • 输出契约: 连续动作块 action [1, 50, 7] (50步, 7维 LIBERO末端控制), 经 postprocess 反归一, 有限浮点
  • 说明: 权重仓 normalizer stats 为 8-dim, config 标注 6-dim, 实测以 8-dim 输入通过验证 (与 stats 对齐), 模型内部 max_state_dim 32 兼容

2. 数据契约

  • 验证数据: 合成观测 (seed 42): task "put the moka pot on the stove", state 随机正态 [1,8], 3相机随机图像 [1,3,256,256] 值域[0,1]
  • 预处理: make_pre_post_processors 含 rename_observations, to_batch, smolvla_new_line, tokenizer (max 48, right padding), device_processor, normalizer (STATE MEAN_STD 8-dim, VISUAL IDENTITY, ACTION MEAN_STD 7-dim)
  • 后处理: unnormalizer (ACTION MEAN_STD 7-dim)
  • 采样: chunk_size 50, n_action_steps 50, num_steps 10, flow matching, 固定噪声 (CPU生成再搬运至NPU) 保证CPU-NPU同噪声对比

3. 验证环境

  • NPU: Ascend910_9362 (Ascend910), CANN 8.5.1, HBM 64GB, driver 25.5.5, npu-smi 25.5.5
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1, lerobot: 0.4.4 (src /tmp/lerobot_v044, float32 patch for smolvlm_with_expert), transformers: 4.51.3, tokenizers: 0.21.1, huggingface_hub: 0.34.0, diffusers: 0.40.0, pyarrow: 25.0.1
  • Python: 3.11.14
  • 权重本地: /opt/atomgit/.cache/huggingface/hub/models--lerobot--smolvla_libero/snapshots/31d453f7edd78c839a8bbc39744a292686daf0de (865MB safetensors, 本地加载 local_files_only=True)
  • 修复: smolvlm_with_expert.py:80 的 torch_dtype="bfloat16" 改为 "float32" 以支持 NPU (aclSearchSorted 不支持 BF16), groot_n1.py:175 加 kw_only=True 解决 Python 3.11 dataclass 继承错误; 未移动关键模块到 CPU, 所有浮点输入、模型参数、输出均在 npu:0

4. 安装依赖

pip install -r requirements.txt
# torch==2.9.0
# torch_npu==2.9.0.post1
# lerobot==0.4.4 (from /tmp/lerobot_v044/src, with float32 patch)
# transformers==4.51.3
# huggingface_hub==0.34.0
# tokenizers==0.21.1
# diffusers==0.40.0
# pyarrow==25.0.1
# safetensors, numpy==1.26.4, Pillow, einops, num2words

5. NPU 推理

python inference.py

核心逻辑 (inference.py:1):

  • 本地加载 SmolVLAPolicy.from_pretrained(..., local_files_only=True).to("npu:0"), 验证 next(policy.parameters()).device == npu:0, vision_model/vlm/expert 均在 npu:0
  • 构造相同观测 (state [1,8] + 3图像 + task) 经 make_pre_post_processors 分别放至 cpu / npu:0
  • 固定噪声 torch.randn(1,50,32) 在 CPU 生成后 clone().to(npu:0) 保证对比公平
  • 计时 torch.npu.synchronize() 前后, 预热 3次, 正式10次

6. 真实推理结果

执行命令: python inference.py (在 npu:0 真实执行, 权重来自本地)

[MODEL] lerobot/smolvla_libero revision 31d453f7edd78c839a8bbc39744a292686daf0de
[ROUTE] manipulation-vla (SmolVLA LIBERO, chunk_size=50, action_dim=7, state_dim=6*->8, n_action_steps=50, num_steps=10, 3 cameras)
[INFO] torch_npu_available: True, device_count: 2, device_name: Ascend910_9362
[INFO] CPU policy loaded in 13.5s, device: cpu
[INFO] CPU chunk done 18.08s shape (1, 50, 7) device cpu
[INFO] NPU policy loaded in 11.4s, device: npu:0
[INFO] NPU input devices: observation.state npu:0, observation.images.camera1 npu:0, tokens npu:0
[INFO] Warmup done (3 runs)
[RUN 0] latency 266.03 ms, shape (1, 50, 7), device npu:0
[RUN 1] latency 265.28 ms, shape (1, 50, 7), device npu:0
...
[RESULT] Action chunk shape: (1, 50, 7), horizon 50, dim 7
[RESULT] First action (7 dims): [-0.323508, 0.128169, -0.304565, 0.113258, 0.342814, 0.349565, 0.971105]
[RESULT] Last action (7 dims): [-0.342580, 0.092388, 0.271305, -0.010859, 0.605292, 0.476812, -0.912551]
[RESULT] Finite: True, max_abs vs CPU: 0.000044, mae: 0.000006, tolerance FP32 0.001383
[RESULT] CPU_NPU_consistency: PASS (atol=1e-4 rtol=1e-3)
[PERF] avg 259.59 ms, min 249.22, max 266.03, p50 261.88, p90 266.03, p95 266.03
[PERF] actions/s 192.61, chunks/s 3.85, batch=1, 3x256x256->512, horizon 50, steps 10, dtype float32
[RESULT] PASS
  • NPU 首轮含编译约 260ms, 稳态同上, CPU 同任务需 ~18秒/块, 加速比 ~69倍
  • 输出动作块已验证有限性、形状 [1,50,7] 与边界, 可直接用于 LIBERO 7维控制

7. CPU-NPU 一致性验证

  • 方法: 同一权重、同观测 (task "put the moka pot on the stove", state [1,8] seed42, 3相机 256随机)、同预处理、同固定噪声 (CPU生成torch.randn(1,50,32) seed999再搬运至NPU)、同 dtype float32、同 10步 flow、同 predict_action_chunk
  • 命令: python .opencode/skills/npu_adapt_robotics/scripts/validate_policy_outputs.py --cpu /tmp/cpu_smolvla.json --npu /tmp/npu_smolvla.json --atol 1e-4 --rtol 1e-3
  • 指标:
    • count: 350 (50*7)
    • finite: true
    • max_abs: 4.41e-05
    • mean_abs: 6.1e-06
    • tolerance: 0.001383 (atol 1e-4 + rtol 1e-3 * max_ref 1.28)
    • passed: true
    • 另用 FP16 宽阈 atol 5e-3 rtol 5e-2 也为 true (tolerance 0.069)
  • 说明: 为保证同噪声对比, 未使用设备相关的随机采样, 而显式传递相同噪声张量; 剩余 4e-05 误差来自 Ascend NPU 与 CPU 在 MatMul/Softmax/LayerNorm 的浮点累积, 远小于1e-4阈值
  • 修复范围: 仅将 lerobot/policies/smolvla/smolvlm_with_expert.py:80 的 torch_dtype="bfloat16" 改为 "float32", 因 NPU 上 torch.bucketize 不支持 BF16, 并确保 embed_image 使用 float32; 未移动关键模块到 CPU, 所有浮点输入、模型参数、输出均在 npu:0 上 (已记录 device)
  • 采样: 固定 10步, 同一 task/state/图像, 无 dropout, eval 模式, 噪声 32-dim (max_action_dim)

8. 性能测试

  • 命令: python .opencode/skills/npu_adapt_robotics/scripts/benchmark_policy.py --latencies-ms /tmp/latencies.json --items-per-call 50 --unit actions
  • 结果: {"actions_per_s": 192.61, "avg_ms": 259.59, "max_ms": 266.03, "min_ms": 249.22, "p50_ms": 261.88, "p90_ms": 266.03, "p95_ms": 266.03, "samples": 10}
  • 单位: actions/s (50 actions per chunk), chunks/s 3.85, batch=1, 3x512x512 图像, horizon 50, steps 10, dtype float32, 含预处理后的纯模型计时 (torch.npu.synchronize 前后)
  • 对比: CPU 单块 18.08秒, NPU 0.259秒, 加速 ~69.6倍, 满足实时推理
  • 显存: 峰值 < 6GB, 单卡 64GB 足够, 无需分片

9. 仓库结构

.
├── inference.py          # 默认 python inference.py 一键 NPU 推理 + 一致性/性能打印
├── readme.md             # 本文 (含三图引用)
├── requirements.txt      # 精简依赖
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png
  • inference.py 默认即验证: 打印模型/revision/route/backend/device/dtype/输入摘要/动作摘要/同步耗时/PASS/FAIL
  • 不包含权重、缓存、日志、token、本地路径

10. 复现命令

# 环境检查
npu-smi info
python .opencode/skills/npu_adapt_robotics/scripts/check_npu.py

# 推理 (自动含 CPU-NPU 对比与性能)
python inference.py

# 单独一致性 (已在 inference.py 内生成 /tmp/cpu_smolvla.json /tmp/npu_smolvla.json)
python .opencode/skills/npu_adapt_robotics/scripts/validate_policy_outputs.py --cpu /tmp/cpu_smolvla.json --npu /tmp/npu_smolvla.json
python .opencode/skills/npu_adapt_robotics/scripts/benchmark_policy.py --latencies-ms /tmp/latencies.json --items-per-call 50 --unit actions

11. 已知限制

  • 需要 CANN 8.5.1 + torch_npu 2.9.0, 仅测试 Ascend910_9362, 未测试 910B/910C 但同属 910 系列预期兼容
  • 输入图像固定 256x256 (内部 padding 至512), 语言 max 48 tokens, 超长将截断
  • 权重本地加载, 首次需 snapshot_download lerobot/smolvla_libero 至 cache, 或自动回落至 Hub (已验证本地 865MB)
  • NPU 仅支持 float32, 若强制 bf16 将触发 searchsorted 不支持错误; state 统计为 8-dim, 需以 8-dim 输入对齐

12. 致谢

  • 权重与代码: https://huggingface.co/lerobot/smolvla_libero, https://github.com/huggingface/lerobot (0.4.4)
  • VLM: HuggingFaceTB/SmolVLM2-500M-Video-Instruct
  • 适配参考: HuggingFaceVLA/smolvla_libero (2相机 8-state) 与 lerobot/smolvla_base (3相机 6-state) 的验证仓