NVIDIA Alpamayo 1.5 是 10B 参数的 VLA (Vision-Language-Action) 模型,专为自动驾驶端到端规划设计。本仓库将其适配到华为 Ascend NPU(CANN 8.5.1 + torch_npu 2.9.0 + transformers 4.57.6),并提供单文件推理与 CPU vs NPU 6 项指标对比。
Alpamayo 1.5 是 NVIDIA 在 2026 年初推出的 10B chain-of-thought reasoning VLA 模型,用于自动驾驶车辆的端到端轨迹预测与推理:
本仓库的 NPU 适配策略:
trust_remote_code=True(本机出网受限,HF Hub 不可达)→ 自包含模型包装器 AlpamayoVLAvlm.model.visual.{patch_embed, merger}(视觉投影)+ expert.layers.{0..35} + expert.norm(动作专家)+ action_in_proj + action_out_proj(flow matching head)+ vlm.model.language_model.embed_tokens(LLM 占位)pool_proj 把 LLM 占位 embedding 池化到 expert 维度.to(npu);L≤2 时跳过 attention matmul + softmax(避开 NPU vector core 异常)环境要求:
| 组件 | 版本 |
|---|---|
| Python | 3.11.14 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| CANN | 8.5.1 |
| Ascend NPU | 910 x2 |
权重准备:
# 假设权重已下载到 /data/dl/alpamayo-1.5-10b/(约 20GB)
ls /data/dl/alpamayo-1.5-10b/
# config.json model-0000{1..5}-of-00005.safetensors model.safetensors.index.json安装依赖:
pip install -r requirements.txt# NPU 推理(默认 torch_npu.npu.set_device(0))
python inference.py --device npu
# CPU 推理(同 seed 同输入)
python inference.py --device cpu输出物:
| 文件 | 说明 |
|---|---|
npu_output.txt / cpu_output.txt | 推理输出 waypoints (1, 64, 2) flatten 后的 128 个数值 |
npu_run.log / cpu_run.log | 加载时间 / 各阶段耗时 / NPU 显存峰值 / 数值统计 |
# 6 指标对比:cos_sim / max_abs / mean_abs / sign_agreement / rel_l2 / npu_vs_cpu_speedup
python compare.py
# -> assets/compare_result.txt实测环境:CANN 8.5.1 / torch_npu 2.9.0.post1 / Ascend 910 / seed=42 / 3×224×224 输入 / bf16 / 同 seed 同输入 / flow-matching denoise 5 步。
| 指标 | NPU | CPU | 含义 / 结果 |
|---|---|---|---|
output.shape | (1, 64, 2) | (1, 64, 2) | 64 点轨迹,每点 (x, y) |
elapsed_s | 0.3672 | 35.5093 | 单次推理耗时(秒) |
npu_peak_mem_MB | 4993.71 | — | NPU 显存峰值(MB) |
| cos_sim | — | — | +0.998660(flatten 输出余弦相似度,理想 +1.0;GQA attention + fp32 Euler 积分) |
| max_abs | — | — | 0.968750(最大绝对误差) |
| mean_abs | — | — | 0.420425(平均绝对误差) |
| sign_agreement | — | — | 98.44%(符号一致率,理想 100%) |
| rel_l2 | — | — | 0.086688(L2 相对误差) |
| npu_vs_cpu_speedup | — | — | 96.70×(NPU 相比 CPU 的加速比) |
详细数据见 assets/compare_result.txt。
torch.Generator(device="npu");用 torch.Generator(device="cpu").manual_seed(seed) 生成后再 .to(device)autocast(device_type="npu", dtype=torch.bfloat16)patch_embed.proj (Conv3d kernel=(2,16,16) stride=(2,16,16)) 在 NPU 上触发 vector core 异常 → 改为 Conv2d(in=6, k=16, s=16) 并把 H 拆为 2 个时间步拍到通道expert.layers.0.{k,v}_proj.weight=(1024,2048) 而 q_proj/o_proj=(2048,2048) → 真实 GQA (16 q heads / 8 kv heads × head_dim=128);用 repeat_interleave 把 kv 复制到 q head 数后走 SDPALinear(60→512) → SiLU → LayerNorm(512) → Linear(512→512) → SiLU → LayerNorm(512) → Linear(512→2048),外层 norm 维度是 2048(不是 60)TimestepEmbedder(sinusoidal + MLP → expert_dim),按 t ∈ [1, 0] 做 5 步 Euler 积分;velocity 由 action_out_proj 输出,作用于 64 个 per-waypoint 嵌入(而非只预测一个末点后 broadcast)pred += vel * dt,再 cast 回 bf16 输出;避开 bf16 在 |v|<0.1 处的符号翻转(之前 sign_agreement 从 58% → 98%)copy_(slice),避免 .float().to(bf16) 在 NPU 上累积数值误差torch_npu.npu.reset_peak_memory_stats() + max_memory_allocated()trust_remote_code=True:本机出网受限拉不到 NVlabs/alpamayo1.5 的 modeling_* 与 processor_config;改为自包含 AlpamayoVLA wrapper 直接从 safetensors 加载关键权重n_diffusion_steps=5 是仓库自选折中:更多步会更接近真实 FlowMatching,但单次推理耗时线性放大;CPU 路径上 5 步 ≈ 35s,NPU 仅 0.37s