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% |
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 # 截图生成脚本(可复现)| 依赖 | 版本 | 说明 |
|---|---|---|
| 操作系统 | 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 |
| Python | 3.11 | — |
| PyTorch | 2.9.0 | 与 torch_npu 版本配套 |
| torch_npu | 2.9.0.post1 | 昇腾 NPU 运行时插件 |
# 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)"Embodied-R1.5 为标准 transformers 架构(Qwen3VLForConditionalGeneration),
迁移到昇腾 NPU 的适配点如下:
inference.py 顶部)import torch_npu
from torch_npu.contrib import transfer_to_nputransfer_to_npu 自动完成 CUDA API → NPU API 的映射
(torch.cuda.* → torch.npu.*,device('cuda') → device('npu') 等)。
| # | 适配点 | 说明 |
|---|---|---|
| 1 | bf16 加载 | torch_dtype=torch.bfloat16(Ascend910 原生支持 bf16),显存占用 ~17GB |
| 2 | use_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 库,因此迁移工作量集中在加载与设备映射。
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| 方法 | 路径 | 说明 |
|---|---|---|
| GET | /health | 健康检查,返回设备/模型/精度信息 |
| POST | /infer | 简化推理接口(text + 可选 image) |
| POST | /v1/chat/completions | OpenAI 兼容接口 |
健康检查:
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
}'# 方式一: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 |
| T4 | OpenAI 兼容接口 /v1/chat/completions | ✅ PASS |
完整测试结果见 test_results/test_result.txt,汇总:4 通过 / 0 失败。
| 截图 | 文件 | 说明 |
|---|---|---|
| NPU 设备调用 | assets/npu_device_call.png | npu-smi info + torch_npu 探针 + 部署摘要 |
| 测试结果 | assets/model_result.png | 场景图 + 预测点标注 + 延迟指标 + 测试日志 |
| 适配过程 | assets/agent_workflow.png | 迁移适配工作流(10 步 playbook) |
截图可通过 python assets/_make_screenshots.py 重新生成(需服务运行中)。
| 问题 | 原因 | 解决方案 |
|---|---|---|
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 |
vllm serve IffYuan/Embodied-R1.5 ...(如需高吞吐可参照官方 README)