Bug_Factory_w/Embodied-R1.5
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Embodied-R1.5 昇腾 NPU 迁移与服务化推理指南

Embodied-R1.5 — 基于 Qwen3-VL-8B-Instruct 的具身基础模型(Embodied Foundation Model), 支持空间认知推理、任务规划与纠错、具身指代定位(2D/3D 点、轨迹)三大核心能力。 本项目将其从通用 PyTorch 环境迁移到 华为昇腾 Ascend910 NPU,并基于 FastAPI 提供 服务化推理(HTTP 接口)。

项目内容
模型Embodied-R1.5(Qwen3-VL-8B 架构,约 17GB bf16 权重)
硬件Ascend910(本环境 16 卡,部署使用 npu:0)
软件CANN 8.5.1 / Python 3.11 / PyTorch 2.9.0 / torch_npu 2.9.0.post1 / transformers 5.9.0
推理服务FastAPI + uvicorn(HTTP 服务化推理)
测试结果4/4 用例通过,空间定位精度 < 1%

1. 目录结构

Embodied-R1.5-npu/
├── inference.py              # 服务化推理脚本(FastAPI HTTP 服务)★ 核心交付
├── test_inference.py         # 测试用例(HTTP 模式 / 离线模式)
├── requirements.txt          # 运行环境依赖清单
├── README.md                 # 本文档
├── test_results/
│   └── test_result.txt       # 测试结果记录
└── assets/
    ├── test_scene.jpg        # 测试用合成场景图(机器人操作场景)
    ├── npu_device_call.png   # 截图1:NPU 设备调用
    ├── model_result.png      # 截图2:模型测试结果
    ├── agent_workflow.png    # 截图3:适配过程工作流
    └── _make_screenshots.py  # 截图生成脚本(可复现)

2. 环境要求

依赖版本说明
操作系统openEuler / Ubuntu(aarch64)本机 openEuler 2203 SP4
昇腾硬件Ascend910(≥1 卡,推荐 64GB HBM)bf16 权重约占 17GB
CANN≥ 8.0(本机 8.5.1)/usr/local/Ascend/ascend-toolkit/set_env.sh
Python3.11—
PyTorch2.9.0与 torch_npu 版本配套
torch_npu2.9.0.post1昇腾 NPU 运行时插件

3. 环境准备

# 1) 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh

# 2) 指定可见 NPU 卡
export ASCEND_RT_VISIBLE_DEVICES=0

# 3) 安装依赖(华为镜像源)
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt

# 4) 校验 NPU 环境
npu-smi info
python -c "import torch, torch_npu; a=torch.randn(3,4).npu(); print(a+a)"

4. 模型迁移适配说明

Embodied-R1.5 为标准 transformers 架构(Qwen3VLForConditionalGeneration), 迁移到昇腾 NPU 的适配点如下:

4.1 自动迁移注入(inference.py 顶部)

import torch_npu
from torch_npu.contrib import transfer_to_npu

transfer_to_npu 自动完成 CUDA API → NPU API 的映射 (torch.cuda.* → torch.npu.*,device('cuda') → device('npu') 等)。

4.2 关键适配点(本次迁移实际遇到的问题与修复)

#适配点说明
1bf16 加载torch_dtype=torch.bfloat16(Ascend910 原生支持 bf16),显存占用 ~17GB
2use_cache 参数transformers 5.x 中 from_pretrained(use_cache=True) 会报 TypeError(Qwen3VL __init__ 不接受该参数),改为加载后设置 model.config.use_cache = True
3设备映射model.to("npu:0"),inputs.to(model.device)
4推理串行化NPU 推理非线程安全,threading.Lock 保护 generate
5并发控制服务单进程预热加载模型(uvicorn startup 事件),避免重复加载

本项目不涉及 CUDA 自定义算子(无 .cu 文件)、DP/DDP 分布式改造、 flash_attn 等三方 CUDA 库,因此迁移工作量集中在加载与设备映射。

5. 服务化推理(inference.py)

5.1 启动服务

source /usr/local/Ascend/ascend-toolkit/set_env.sh
export ASCEND_RT_VISIBLE_DEVICES=0

python inference.py \
  --model-path /mnt/old_data/whl/models/1/IffYuan/Embodied-R1.5 \
  --host 0.0.0.0 --port 8000

启动日志确认模型加载到 NPU:

[inference] Loading model (bf16) -> npu:0 ...
[inference] Model loaded in 4.7s, device=npu:0
INFO:     Uvicorn running on http://0.0.0.0:8000

5.2 接口文档

方法路径说明
GET/health健康检查,返回设备/模型/精度信息
POST/infer简化推理接口(text + 可选 image)
POST/v1/chat/completionsOpenAI 兼容接口

5.3 调用示例

健康检查:

curl -s http://127.0.0.1:8000/health
# {"status":"ok","model":"Embodied-R1.5","device":"npu:0","dtype":"torch.bfloat16"}

图像空间定位(核心场景):

curl -s http://127.0.0.1:8000/infer -H "Content-Type: application/json" -d '{
  "text": "You are a robot performing manipulation tasks. The task instruction is: move the blue cube on top of the yellow cube. Use 2D points to mark the target location.",
  "image": "data:image/jpeg;base64,<BASE64>",
  "max_new_tokens": 512
}'

返回示例:

{
  "output": "```json\n[{\"point_2d\": [506, 300]}]\n```",
  "latency_ms": 2561.81
}

坐标约定:point_2d 归一化到 [0, 1000] 范围,与图像原始分辨率无关。 例如输出 [506, 300] 映射到 640×480 图像为像素 (324, 144), 与目标蓝色方块中心 (320, 145) 误差 < 5px(<1%)。

OpenAI 兼容接口:

curl -s http://127.0.0.1:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
  "model": "Embodied-R1.5",
  "messages": [{"role": "user", "content": [
    {"type": "image", "image": "/path/to/scene.jpg"},
    {"type": "text", "text": "Where is the red cube? Answer with 2D points."}
  ]}],
  "max_tokens": 256
}'

6. 测试用例

# 方式一:HTTP 模式(默认,需服务已启动)
python test_inference.py --url http://127.0.0.1:8000

# 方式二:离线模式(直接加载模型验证,不依赖服务)
python test_inference.py --offline

用例覆盖:

用例内容结果
T1健康检查 /health,确认 device=npu:0✅ PASS
T2纯文本推理(数学问答)✅ PASS
T3图像 + 文本推理(空间定位,输出 point_2d)✅ PASS
T4OpenAI 兼容接口 /v1/chat/completions✅ PASS

完整测试结果见 test_results/test_result.txt,汇总:4 通过 / 0 失败。

7. 交付截图

截图文件说明
NPU 设备调用assets/npu_device_call.pngnpu-smi info + torch_npu 探针 + 部署摘要
测试结果assets/model_result.png场景图 + 预测点标注 + 延迟指标 + 测试日志
适配过程assets/agent_workflow.png迁移适配工作流(10 步 playbook)

截图可通过 python assets/_make_screenshots.py 重新生成(需服务运行中)。

8. 常见问题(FAQ)

问题原因解决方案
TypeError: Qwen3VLForConditionalGeneration.__init__() got an unexpected keyword argument 'use_cache'transformers 5.x 不接受 from_pretrained(use_cache=...)加载后设置 model.config.use_cache = True
No module named 'decorator'torch_npu 运行时依赖缺失pip install decorator
精度 / 算子报错CANN 环境未加载source /usr/local/Ascend/ascend-toolkit/set_env.sh
显存不足(OOM)HBM 不足缩小 max_new_tokens,或换 64GB 卡
服务启动慢首次运行算子编译 + 17GB 权重加载属正常现象,二次加载 < 10s
并发请求报错NPU 推理非线程安全已内置 threading.Lock 串行化,勿调大 --workers

9. 参考

  • 模型主页:https://huggingface.co/IffYuan/Embodied-R1.5
  • 项目代码:https://github.com/pickxiguapi/Embodied-R1.5
  • 官方部署建议(vLLM):vllm serve IffYuan/Embodied-R1.5 ...(如需高吞吐可参照官方 README)
  • 迁移 Skill:ai4s-basic(ascend-model-agent-plugin)