liuhongwei-2026/X-VLA-Pt
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

X-VLA-Pt on Ascend NPU

模型适配 · 强化学习 Vision-Language-Action (VLA) 模型 · 昇腾 NPU

Beta 版本头信息

{hardware NPU}

1. 简介

  • 模型来源: 2toINF/X-VLA-Pt (AtomGit 镜像: https://ai.gitcode.com/hf_mirrors/2toINF/X-VLA-Pt)
  • 模型类型: X-VLA(X-VLA-Pt / Foundation Edition) —— 2toINF 开源的 0.9B 参数 强化学习 (RL) 后训练 Vision-Language-Action(VLA) 模型,基于 论文 arXiv:2510.10274(Zheng et al., 2025, "X-VLA: Soft-Prompted Transformer as Scalable Cross-Embodiment Vision-Language-Action Model")。 以 Florence2 encoder-only 多模态骨干(vision-language)+ SoftPromptedTransformer (flow-matching 动作去噪器,带 per-embodiment 可学习 soft prompt)+ Action Hub (ee6d 末端执行器动作空间)构建,跨具身训练于 Bridge Data,评测覆盖六个仿真 + 三个真实机器人。
  • 参数量: 932,001,792(0.93B ≈ 0.9B)
  • 架构: Florence2ForConditionalGeneration(encoder-only,删除 decoder/lm_head)
    • SoftPromptedTransformer(24 层 Transformer,hidden 1024)+ EE6DActionSpace (dim_action=20:xyz×2 + 6D 旋转×2 + 夹爪×2,num_actions=30 步动作序列)
  • 输入/输出: 多视角图像(默认 3 视图,CLIP 预处理 224×224)+ 语言指令 (Bart tokenizer)+ 本体感受 + 具身 domain_id → 30 步动作序列 [B, 30, 20] (flow-matching 迭代去噪,默认 10 步)
  • 适配状态: SUCCESS
  • 适配说明: X-VLA 是非自回归的 VLA(多模态动作策略)模型,不适用 vLLM-Ascend; 通过 transformers trust_remote_code 直接加载官方 remote code,在昇腾 NPU (torch_npu)上零源码改造推理,提供离线推理 / CPU-NPU 精度对比 / FastAPI 服务化(POST /act)三种模式。
  • 适配时间: 2026-08-21
  • 硬件: {hardware NPU} Ascend 910B(torch_npu 2.9.0.post1)

2. 验证环境

组件版本
torch2.9.0
torch-npu2.9.0.post1
python3.11.14
transformers4.57.6
CANN8.5.1
NPUAscend 910B × 2(HBM 64GB/卡)
依赖timm / pillow / fastapi / uvicorn / json-numpy / opencv-python-headless / safetensors / accelerate

3. 关键 NPU 适配点

#问题适配方案
1X-VLA 是 VLA 模型(非 LLM),vLLM-Ascend 无法服务化不走 vLLM-Ascend,直接 torch_npu 加载(AutoModel.from_pretrained(trust_remote_code=True) + .to("npu"))
2Florence2 默认注意力在昇腾 NPU 不可用(flash 无、sdpa 走 aclnnFlashAttentionScore 报 ERR00100)AutoConfig.from_pretrained(..., attn_implementation="eager") 强制 eager;transformers 只把该值写到顶层 XVLAConfig,嵌套 florence_config / text_config 需显式补丁,否则 FLORENCE2_ATTENTION_CLASSES[None] 报 KeyError
3processor 输出 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 视觉骨干所需)
5flow-matching 采样用 torch.randn 初始化噪声,CPU/NPU 结果随机对比/服务模式固定 torch.manual_seed,保证可复现
6NPU 0 卡 HBM 常被其他会话占满(62GB)脚本支持 --device npu:1 / NPU_COMPARE_DEVICE 环境变量切换

4. 快速开始

4.1 环境准备

pip install -r requirements.txt

4.2 下载权重(本仓不内置权重)

权重为单个 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-Pt

4.3 离线推理

python 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: SUCCESS

4.4 CPU vs NPU 精度对比

python 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)

4.5 启动 FastAPI 服务化推理

# 终端 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

5. 验证结果(昇腾 NPU)

模型加载(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: SUCCESS

CPU 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 服务进程

6. 项目结构

X-VLA-Pt/
├── inference.py        # 推理脚本(离线 / CPU-NPU 对比 / FastAPI 服务化)
├── README.md           # 部署说明文档
├── requirements.txt    # 环境依赖清单
└── assets/             # 截图素材(部署/推理证据)
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

7. 参考

  • 模型卡: https://huggingface.co/2toINF/X-VLA-Pt
  • 官方代码: https://github.com/2toINF/X-VLA
  • 论文: X-VLA: Soft-Prompted Transformer as Scalable Cross-Embodiment Vision-Language-Action Model
  • 项目主页: https://thu-air-dream.github.io/X-VLA/

8. 免责声明

本仓库仅用于 AI 模型在昇腾 NPU 上的适配与部署演示。模型权重版权归 2toINF 所有, 依据 Apache 2.0 许可证使用;本仓库不包含模型权重,权重请按上文方式自行下载。