hums6666/lerobot-smolvla-base
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

lerobot/smolvla_base — 昇腾 NPU 适配

昇腾NPU

原模型:lerobot/smolvla_base(HuggingFace / GitCode 镜像)

  • 模型类型:SmolVLA(Vision-Language-Action) 机器人策略(SmolVLM2 骨干 + Action Expert)
  • 原始权重:https://ai.gitcode.com/hf_mirrors/lerobot/smolvla_base
  • 适配框架:torch-npu(CANN + torch_npu),推理设备 npu:0

适配要点(CUDA 清零 + NPU 迁移)

  • 全局 CUDA 清零:工程代码不使用任何 torch.cuda / .cuda() / device='cuda' 调用, 模型与输入统一迁移 npu:0。
  • NPU 初始化保护:torch.npu.set_device(0) 包在 try-except 中,失败即抛明确错误。
  • lerobot 运行时打补丁:不改第三方库源码,通过补丁让 is_torch_device_available / get_safe_torch_device / auto_select_torch_device / is_amp_available 识别 npu 设备, 使 config.device 自动回退为 npu:0。
  • 全量权重本地加载:smolvla_base 的 model.safetensors(906MB,含 VLM 骨干)一次性 加载,关闭 load_vlm_weights 避免联网下载 VLM 权重。
  • 预/后处理管线设备改写:device_processor 覆盖为 npu:0,tokenizer 走本地 VLM 组件。
  • VLM 组件本地化:SmolVLM2 的 config / tokenizer / processor 缓存于 vlm_deps/, 推理时不依赖外网。

模型输入 / 输出

模态键名形状说明
输入observation.image(3, 256, 256)camera1 相机图像 (C,H,W),值域 [0,1]
输入observation.image2(3, 256, 256)camera2 相机图像 (C,H,W),值域 [0,1]
输入observation.image3(3, 256, 256)camera3 相机图像 (C,H,W),值域 [0,1]
输入observation.state(6,)机器人关节状态向量
输入taskstr语言指令(默认 "Pick up the object and place it on the plate")
输出action(6,)单步动作(关节位置,去归一化)
输出action chunk(50, 6)动作序列 chunk

权重缓存目录:./model_weights/lerobot/smolvla_base(本地,不入库) 归一化统计量已内嵌于 policy_preprocessor_* / policy_postprocessor_*,推理脚本自动加载。

环境要求

  • Python 3.11
  • CANN / torch_npu(已就绪的昇腾 NPU 环境)
  • pip install -r requirements.txt

快速开始

0. 下载权重(缓存到本地,命中跳过)

python3 download_weights.py

1. 命令行推理(NPU)

python3 inference.py \
  --img test_images/camera1.png \
  --img-camera2 test_images/camera2.png \
  --img-camera3 test_images/camera3.png \
  --state "0,0,0,0,0,0" \
  --task "Pick up the object and place it on the plate"

输出示例:

{
  "status": "success",
  "model": "lerobot/smolvla_base",
  "device": "npu:0",
  "task": "Pick up the object and place it on the plate",
  "action": [0.022895, -0.044025, -0.120563, 0.12122, -0.071435, -0.040008],
  "action_dim": 6,
  "action_chunk_size": 50
}

2. FastAPI 服务(NPU)

python3 -m uvicorn inference:app --host 0.0.0.0 --port 8000

调用推理接口:

curl -s -X POST http://127.0.0.1:8000/predict \
  -F "camera1=@test_images/camera1.png" \
  -F "camera2=@test_images/camera2.png" \
  -F "camera3=@test_images/camera3.png" \
  -F "state=0,0,0,0,0,0" \
  -F "task=Pick up the object and place it on the plate"

返回示例:

{"status":"success","model":"lerobot/smolvla_base","device":"npu:0","action":[0.005313,-0.117961,-0.131012,0.037106,0.041844,-0.039565],"action_dim":6,"action_chunk_size":50}

文件说明

文件说明
inference.pyNPU 推理主脚本(CLI + FastAPI 双模式,共用同一套 NPU 加载/推理逻辑)
download_weights.py权重下载脚本(本地缓存,命中跳过)
make_screenshots.py取证截图生成脚本(assets/*.png)
assets/取证截图(agent_workflow.png / npu_device_call.png / model_result.png)
vlm_deps/SmolVLM2 VLM 组件本地缓存(config/tokenizer/processor)
test_images/示例测试图像(三路相机场景)