模型适配 · 强化学习 Vision-Language-Action (VLA) 模型 · 昇腾 NPU
Beta 版本头信息
{hardware NPU}Florence2ForConditionalGeneration(encoder-only,删除 decoder/lm_head)
SoftPromptedTransformer(24 层 Transformer,hidden 1024)+ EE6DActionSpace
(dim_action=20:xyz×2 + 6D 旋转×2 + 夹爪×2,num_actions=30 步动作序列)[B, 30, 20]
(flow-matching 迭代去噪,默认 10 步)trust_remote_code 直接加载官方 remote code,在昇腾 NPU
(torch_npu)上零源码改造推理,提供离线推理 / CPU-NPU 精度对比 / FastAPI
服务化(POST /act)三种模式。{hardware NPU} Ascend 910B(torch_npu 2.9.0.post1)| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| python | 3.11.14 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| NPU | Ascend 910B × 2(HBM 64GB/卡) |
| 依赖 | timm / pillow / fastapi / uvicorn / json-numpy / opencv-python-headless / safetensors / accelerate |
| # | 问题 | 适配方案 |
|---|---|---|
| 1 | X-VLA 是 VLA 模型(非 LLM),vLLM-Ascend 无法服务化 | 不走 vLLM-Ascend,直接 torch_npu 加载(AutoModel.from_pretrained(trust_remote_code=True) + .to("npu")) |
| 2 | Florence2 默认注意力在昇腾 NPU 不可用(flash 无、sdpa 走 aclnnFlashAttentionScore 报 ERR00100) | AutoConfig.from_pretrained(..., attn_implementation="eager") 强制 eager;transformers 只把该值写到顶层 XVLAConfig,嵌套 florence_config / text_config 需显式补丁,否则 FLORENCE2_ATTENTION_CLASSES[None] 报 KeyError |
| 3 | processor 输出 pixel_values 为 float32,模型权重为 bfloat16 → Conv2d 报 "Input type (float) and bias type (BFloat16)" | build_inputs 将浮点输入统一 cast 到模型 dtype(bfloat16) |
| 4 | 模型 remote code 顶层 import json_numpy/cv2/fastapi/uvicorn/timm,缺失直接加载失败 | requirements.txt 补齐(timm 为 DaViT 视觉骨干所需) |
| 5 | flow-matching 采样用 torch.randn 初始化噪声,CPU/NPU 结果随机 | 对比/服务模式固定 torch.manual_seed,保证可复现 |
| 6 | NPU 0 卡 HBM 常被其他会话占满(62GB) | 脚本支持 --device npu:1 / NPU_COMPARE_DEVICE 环境变量切换 |
pip install -r requirements.txt权重为单个 model.safetensors(约 3.5GB)+ 官方 remote code(config.json /
modeling_xvla.py / modeling_florence2.py / transformer.py / action_hub.py /
processing_xvla.py / tokenizer)。任选一种方式下载到本地:
# 方式 A:huggingface 镜像
HF_ENDPOINT=https://hf-mirror.com python -c "from huggingface_hub import snapshot_download; \
snapshot_download('2toINF/X-VLA-Pt', local_dir='/tmp/models/X-VLA-Pt')"
# 方式 B:gitcode 镜像(git clone,需要 git-lfs 拉取大文件)
git clone https://gitcode.com/hf_mirrors/2toINF/X-VLA-Pt.git /tmp/models/X-VLA-Ptpython inference.py --model /tmp/models/X-VLA-Pt --device npu:0 \
--instruction "pick up the red cube"输出示例(30 步 × 20 维动作序列):
[result] action shape: (1, 30, 20)
[result] latency : 495.41 ms
[result] action[0,:5]: [-0.0417, -0.1211, 0.2559, 0.1348, -0.1885]
[result] STATUS: SUCCESSpython inference.py --model /tmp/models/X-VLA-Pt --compare --seed 42 --device-ids "0,1"验证结果(固定同一噪声,对比动作序列):
[inference] CPU action shape=(1, 30, 20) mean=0.024198 std=0.230677
[inference] NPU action shape=(1, 30, 20) mean=0.024203 std=0.230766
[inference] cosine similarity = 0.999989368
[inference] max abs diff = 4.395e-03
[inference] mean abs diff = 7.007e-04
[inference] CPU latency = 59986.04 ms | NPU latency = 495.41 ms
[inference] RESULT: PASS (cos>=0.99)# 终端 1:启动服务(默认端口 8000)
python inference.py --model /tmp/models/X-VLA-Pt --serve --port 8000 --device npu:0# 终端 2:健康检查
curl -s http://127.0.0.1:8000/health
# -> {"status":"ok","model":"X-VLA-Pt","device":"npu:0","attn":"eager"}
# 动作推理(图像为本地路径或 base64,最多 3 视图)
curl -s http://127.0.0.1:8000/act -H "Content-Type: application/json" -d '{
"images": ["/path/to/cam0.png", "/path/to/cam1.png", "/path/to/cam2.png"],
"language_instruction": "pick up the red cube",
"domain_id": 0,
"steps": 10
}'
# -> {"action":[[30,20] 动作序列],"latency_ms":...,"device":"npu:0"}
# 服务自检
python inference.py --test --port 8000模型加载(NPU)
[inference] Loading X-VLA-Pt from /tmp/models/X-VLA-Pt (dtype=bfloat16, attn=eager) ...
[inference] Model loaded in 4.7s
[inference] Params total=932.002M trainable=932.002M离线推理(合成 3 视角图像 + 指令 → 动作序列)
[result] instruction : pick up the red cube
[result] device : npu:1
[result] action shape: (1, 30, 20)
[result] latency : 495.41 ms
[result] action[0,:5]: [-0.0417, -0.1211, 0.2559, 0.1348, -0.1885]
[result] STATUS: SUCCESSCPU vs NPU 精度对比
cosine similarity = 0.999989368 (PASS, cos>=0.99)
max abs diff = 4.395e-03
CPU latency = 59986.04 ms | NPU latency = 495.41 ms (NPU 快约 121×)服务化推理(GET /health 在线;POST /act 返回动作序列,~500ms/次)
NPU 设备占用(npu-smi,服务常驻时)
| 1 1 | <pid> | python3 | ~2100MB | # X-VLA-Pt 服务进程X-VLA-Pt/
├── inference.py # 推理脚本(离线 / CPU-NPU 对比 / FastAPI 服务化)
├── README.md # 部署说明文档
├── requirements.txt # 环境依赖清单
└── assets/ # 截图素材(部署/推理证据)
├── agent_workflow.png
├── npu_device_call.png
└── model_result.png本仓库仅用于 AI 模型在昇腾 NPU 上的适配与部署演示。模型权重版权归 2toINF 所有, 依据 Apache 2.0 许可证使用;本仓库不包含模型权重,权重请按上文方式自行下载。