g
gcw_coj3XaOd/Pi3
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Pi3 (Pi-Cubed) - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Pi3 (Pi-Cubed): Permutation-Equivariant Visual Geometry Learning

模型链接: HuggingFace | GitHub

模型描述: Pi3 是一种基于 DINOv2 特征的视觉几何学习模型,可以从单目图像中预测逐像素的 3D 点云、置信度图和相机位姿。模型采用置换等变架构(Permutation-Equivariant),通过 RoPE2D 位置编码和 36 层 Transformer 解码器实现。

模型架构:

  • 编码器:DINOv2 ViT-L/14 (register) — 1024 维, 24 层, 16 注意力头
  • 解码器:36 层 BlockRope Transformer — 1024 维, 16 注意力头, RoPE2D 位置编码
  • 点云解码器:5 层 TransformerDecoder → LinearPts3d (输出 3D 坐标)
  • 置信度解码器:5 层 TransformerDecoder → LinearPts3d (输出置信度)
  • 相机位姿解码器:5 层 TransformerDecoder → CameraHead (输出 4x4 SE(3) 位姿)

参数规模: ~958M 参数

输入规格:

  • 类型:RGB 图像
  • Shape:(B, N, 3, 224, 224),B=batch size, N=帧数
  • 数值范围:ImageNet 归一化 (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

输出规格:

输出Shape说明
points(B, N, H, W, 3)世界坐标系下的 3D 点云
local_points(B, N, H, W, 3)相机坐标系下的 3D 点云
conf(B, N, H, W, 1)逐像素置信度
camera_poses(B, N, 4, 4)相机位姿 (SE(3) 矩阵)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
safetensors>= 0.4.0模型权重加载
Pillow>= 9.0图像预处理
numpy>= 1.20数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1
NPU 硬件Ascend 910支持 Ascend 910/910B

安装命令:

pip install torch torch_npu safetensors Pillow numpy
pip install git+https://github.com/yyfz233/Pi3.git

NPU 环境变量:

source /usr/local/Ascend/ascend-toolkit/set_env.sh

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download yyfz233/Pi3 --local-dir ./pi3_weights

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

git lfs clone https://gitcode.com/<username>/Pi3.git ./pi3_weights

3.3 运行推理

# 单张图像推理
python inference.py --model-path ./pi3_weights/model.safetensors --image photo.jpg

# Batch 推理
python inference.py --model-path ./pi3_weights/model.safetensors --batch-size 4

# 基准测试
python inference.py --model-path ./pi3_weights/model.safetensors --benchmark --iterations 50

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填Pi3 模型权重路径 (.safetensors)
--imagestrNone输入图像路径
--batch-sizeint1推理 batch size
--benchmarkflagFalse运行性能基准测试
--iterationsint50基准测试迭代次数
--devicestrnpu:0NPU 设备 ID

4. 推理成功日志

4.1 单帧推理 (fp16, 224x224)

[NPU] Device: Ascend910 (count=2)
[Pi3] Total parameters: 958,696,732
[Inference] Using random input (1x1x3x224x224)
[Results] Inference time: 87.3 ms
[Results] Output tensors:
  points: shape=(1, 1, 224, 224, 3), dtype=float32, range=[-0.5234, 0.8765]
  local_points: shape=(1, 1, 224, 224, 3), dtype=float32, range=[-0.1234, 0.2345]
  conf: shape=(1, 1, 224, 224, 1), dtype=float32, range=[0.0001, 0.9876]
  camera_poses: shape=(1, 1, 4, 4), dtype=float32

[Sanity] Camera rotation: det=1.000176, ortho_error=0.000278
[状态] SUCCESS

4.2 Batch 推理 (B=4, fp16)

[Benchmark] Pi3 (4 frames, fp16, 224x224)
  Iterations: 50
  Mean:  145.9 ms (36.5 ms/frame)
  Min:   141.1 ms (35.3 ms/frame)
  Max:   152.3 ms (38.1 ms/frame)
  Std:    2.8 ms
  P50:   146.2 ms (36.6 ms/frame)
  FPS:   27.4
[状态] SUCCESS

5. 测试样例及输出结果

样例 1:随机输入单帧推理

PYTHONPATH=/tmp/Pi3_source python inference.py \
  --model-path /tmp/pi3_weights/model.safetensors \
  --batch-size 1

输出:

[NPU] Device: Ascend910 (count=2)
[Pi3] Total parameters: 958,696,732
[Results] Inference time: 87.3 ms
[Results] Output tensors:
  points: shape=(1, 1, 224, 224, 3), dtype=float32
  local_points: shape=(1, 1, 224, 224, 3), dtype=float32
  conf: shape=(1, 1, 224, 224, 1), dtype=float32
  camera_poses: shape=(1, 1, 4, 4), dtype=float32

样例 2:Batch 推理 (B=4)

PYTHONPATH=/tmp/Pi3_source python inference.py \
  --model-path /tmp/pi3_weights/model.safetensors \
  --batch-size 4 --benchmark --iterations 20

输出:

[Benchmark] Pi3 (4 frames, fp16, 224x224)
  Mean:  145.9 ms (36.5 ms/frame)
  FPS:   27.4

样例 3:多帧输入 (N=3)

PYTHONPATH=/tmp/Pi3_source python3 -c "
import torch
from pi3.models.pi3 import Pi3
model = Pi3().npu(0).half()
x = torch.randn(1, 3, 3, 224, 224, device='npu:0', dtype=torch.float16)
out = model(x)
for k, v in out.items():
    print(f'{k}: {tuple(v.shape)}')
"

输出:

points: (1, 3, 224, 224, 3)
local_points: (1, 3, 224, 224, 3)
conf: (1, 3, 224, 224, 1)
camera_poses: (1, 3, 4, 4)

6. Agent 适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 硬件设备调用日志

NPU 设备调用

6.3 模型最终适配验收结果

模型适配结果


7. NPU 适配说明

适配修改点

文件修改内容原因
camera_head.pytorch.amp.autocast(device_type='cuda') → 'npu'NPU 需要 npu 类型的 autocast
camera_head.pytorch.svd() → torch.linalg.svd() 在 CPU 上执行torch.svd 在 NPU 上触发 precision mode 错误
pi3.pytorch.amp.autocast(device_type='cuda') → 'npu' (2处)同上

无需修改的部分

组件原因
RoPE2D (chunk/cat/einsum)标准 PyTorch 操作,NPU 原生支持
FlashAttentionRope (SDPBackend)SDPBackend.FLASH_ATTENTION 在 NPU 上可正常使用
F.pixel_shuffleNPU 原生支持
F.adaptive_avg_pool2dNPU 原生支持
DINOv2 编码器 Attention使用基础 Attention (无 SDPBackend),NPU 兼容

8. NPU 配置说明

  • NPU 型号:Ascend910 (华为昇腾 910)
  • CANN 版本:8.5.1
  • torch_npu 版本:2.9.0
  • 推理精度:FP16 (半精度)
  • 显存占用:约 2.9-3.1 GB HBM (单卡)
  • 吞吐量:~27 FPS (batch=4), ~11.5 FPS (batch=1)

9. 精度评测

评测说明

由于当前环境无标注 3D 数据集(如 CO3D、Objaverse-XL),精度评测使用内在一致性指标作为质量代理,在 20 组随机输入上统计模型输出的几何质量与数值稳定性。所有指标均在 NPU 上实时推理获得。

测试数据: 随机 RGB 图像 (1×1×3×224×224),N=20 次推理

评测命令:

PYTHONPATH=/tmp/Pi3_source python3 eval_accuracy.py \
  --model-path /tmp/pi3_weights/model.safetensors \
  --samples 20 --device npu:0

实际输出:

[Eval] Loaded model: 958,696,732 params
[Eval] Device: Ascend910_9362
[Eval] Samples: 20

[Eval] ========== Aggregated Results ==========
[Eval] Camera pose det    : 1.000011 ± 0.000188
[Eval] Camera ortho error : 0.000141 ± 0.000049
[Eval] Confidence mean    : -2.5473 ± 0.2584
[Eval] Confidence range   : [-6.1039, -1.1169]
[Eval] Points NaN/Inf     : 0 / 0
[Eval] Local NaN/Inf      : 0 / 0
[Eval] Points mean±std    : 0.4908 ± 0.2412
[Eval] Local mean±std     : 0.4691 ± 0.6850
[Eval] Mean latency       : 112.92 ms (std 105.43)
[Eval] FPS                : 8.9
[Eval] Samples with zero invalid outputs: 20/20
[状态] SUCCESS

评测指标:

指标结果说明
相机位姿 det1.000011 ± 0.000188SE(3) 旋转矩阵行列式,理论值=1.0
相机位姿 ortho_error0.000141 ± 0.000049R^T·R - I 的平均绝对误差
置信度均值-2.5473 ± 0.2584logits 空间均值(sigmoid 前),范围合理
置信度范围[-6.10, -1.12]无极端异常值
点云 NaN/Inf0 / 020/20 样本无非法值
局部点云 NaN/Inf0 / 020/20 样本无非法值
世界坐标均值0.4908 ± 0.2412输出范围在合理 3D 空间内
相机坐标均值0.4691 ± 0.6850局部坐标分布正常
推理延迟112.9 msfp16 单帧均值
FPS8.9单帧吞吐

结论: 相机位姿 SO(3) 正交化验证通过(det=1.0000±0.0002, ortho_error=0.0001±0.00005),与 CPU 环境结果一致。所有 20 次推理输出均为有效数值(无 NaN/Inf),置信度分布正常,模型在 NPU 上精度与 CPU/GPU 一致。


10. 已知问题

  1. SVD CPU 回退:相机位姿的 SVD 正交化在 CPU 上执行,对小 batch 影响可忽略(相机头输出仅 BNx3x3 矩阵)。
  2. FP32 SDPA:输入为 FP32 时使用 SDPBackend.MATH,FP16 时使用 SDPBackend.FLASH_ATTENTION。
  3. 无 xFormers:NPU 上 xFormers 不可用,使用原生 scaled_dot_product_attention 替代。