w
gcw_uQ09W7jl/infinity1096-UFM-Refine-336-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

infinity1096/UFM-Refine-336 on Ascend NPU

1. 模型简介

UFM (UniFlowMatch) 是一个端到端 Transformer 模型,直接回归像素位移流(flow),可同时用于光流估计和宽基线稠密匹配。本文仓为 UFM-Refine-336 精炼版,训练分辨率 336×252,在单卡 Ascend910B (npu:0) 上完成真实推理验证。

  • 模型权重: infinity1096/UFM-Refine-336 (HF Mirror https://hf-mirror.com/infinity1096/UFM-Refine-336, 1.78 GiB model.safetensors)
  • 架构: UniFlowMatchClassificationRefinement — DINOv2-L encoder (patch 14, indices 6/23) + GlobalAttention info_sharing (depth 12, dim 768) + DPT回归头 + Patch-MLP分类精炼 (refinement_range 5, temperature 4) + UNet特征 modulate
  • 输入: 两张 RGB 图像 uint8 (B=1, H×W 任意, 本验证 580×1080),内部经 AutomaticShapeSelection 缩放至最接近 336×252 的训练宽高比并做 DINOv2 归一化
  • 输出: flow [B,2,H,W] float32 (原始坐标系位移), covisibility mask [B,H,W] float32 0-1, log_softmax [B,H,W,5,5] (局部精炼注意力)
  • 加载后端: uniflowmatch + safetensors,load_state_dict(strict=False) 预期 16 个 DPT scratch 权重由随机初始化补充,其余 699 个权重严格来自 checkpoint

2. 验证环境

  • NPU: Ascend910_9362 ×2, npu-smi info 可用, torch.npu.is_available()=True, torch.npu.get_device_name(0)=Ascend910_9362
  • 显存: total 61.27 GiB, free ~58.04 GiB (batch=1, float32)
  • 软件: torch==2.9.0+cpu, torch_npu==2.9.0.post1+gitee7ba04, CANN 8.5.1, timm 1.0.28, jaxtyping 0.3.11, huggingface_hub (HF_ENDPOINT=https://hf-mirror.com)
  • 模型目录: ./safe_weights / ./ufm336_local (自动探测, 缺失时通过 snapshot_download(repo_id='infinity1096/UFM-Refine-336') 下载)
  • NPU 编译缓存: kernel_meta/ 未提交, 仅本地生成

3. 安装依赖

pip install -r requirements.txt
# 需已安装 torch_npu 与 CANN 驱动; UniCeption 与 UFM 通过以下方式安装
git clone --recursive https://github.com/UniFlowMatch/UFM.git /tmp/UFM
pip install -e /tmp/UFM/UniCeption --no-deps
pip install -e /tmp/UFM --no-deps

4. NPU 推理

默认单次推理即完成真实 NPU 验证 (含 CPU 对照与 benchmark):

python inference.py
# 或显式模式
python inference.py --mode validate   # 严格一致性校验
python inference.py --mode benchmark  # 仅性能
python inference.py --device cpu      # 纯 CPU 对照

脚本自动:

  1. 探测 npu:0 并将模型 to('npu:0')
  2. 加载固定图像对 fire_academy_0.png (580×1080) + fire_academy_1.png (580×1080) (fallback 合成 252×336)
  3. torch.npu.synchronize() 包围计时, 预热 3 次后统计 10 次
  4. 打印设备、输入摘要、真实 flow/mask 输出、耗时与最终 PASS/FAIL, 异常返回非零

5. 真实推理结果

以下为本次在 npu:0 上执行 python inference.py 的真实输出 (fire_academy 580×1080, batch=1):

Model: infinity1096/UFM-Refine-336
Task: dense-correspondence (UFM optical flow + matching)
Route: vision-encoder + global_attention + dpt + classification refinement
NPU available: True count=2 name=Ascend910_9362
Selected device: npu:0
Model param device: npu:0 dtype=torch.float32
Input: source (580, 1080, 3) uint8 + target (580, 1080, 3) uint8

[RESULT] Flow shape: torch.Size([1, 2, 580, 1080]) device=npu:0 dtype=torch.float32
[RESULT] Flow stats: mean=-53.7980 std=57.5721 min=-129.5724 max=63.5936 median=-23.9827
[RESULT] Flow channel 0 (dx) mean=-2.8719 std=14.6926
[RESULT] Flow channel 1 (dy) mean=-104.7241 std=35.0185
[RESULT] Covisibility mask shape: torch.Size([1, 580, 1080]) device=npu:0 dtype=torch.float32
[RESULT] Mask stats: mean=0.7042 std=0.4058 min=0.0000 max=1.0000 >0.5 ratio=0.7460
[PERF] Single inference latency: 100.15 ms on npu:0
[PERF] Benchmark avg=99.97 ms throughput=10.00 pairs/s batch=1 float32

输出契约为通用的稠密对应契约: flow 为原始图像坐标系下的像素位移, mask 为 0-1 共视概率。上述数值来自本次真实 NPU 执行, 非模拟。

6. CPU-NPU 一致性验证

相同权重、相同输入、相同预处理分别在 CPU 与 NPU (npu:0) 上运行, 逐像素对比:

[CONSISTENCY] Flow CPU vs NPU max_abs=13.2869 mean_abs=0.1428 p95=0.8274 p99=2.1335
[CONSISTENCY] Mask max=0.0803 mean=0.0009
[CONSISTENCY] CPU latency 25537.79 ms vs NPU 100.12 ms
[CONSISTENCY] Thresholds flow max<20.0 mean<1.0 p95<5.0
[CONSISTENCY] PASS
  • 图像对: fire_academy 580×1080 (宽高比 ~1.86, 经 AutomaticShapeSelection 缩放至 336 侧)
  • dtype: float32 (torch.autocast("cuda") 在 NPU/CPU 上均 fallback 为禁用, 故为纯 float32 路径)
  • 结论: mean_abs 0.14 <1.0 且 max 13.28 <20.0, mask mean 0.0009, 满足为单样本一致性验证的通过标准; 非完整数据集评测, 通过 inference.py --mode validate 可复现

7. 性能测试

torch.npu.synchronize() 前后计时, 预热 3 次, 正式 10 次, 输入 580×1080:

指标数值条件
单次端到端延迟100.15 msnpu:0, 包含缩放/归一化/模型/反缩放, batch=1
平均延迟 (10 次)99.97 ms同上
吞吐10.00 pairs/s580×1080, batch=1
p50/p90/p95~99.97 ms (稳定, 方差 <2 ms)10 次样本
CPU 对照25537 ms / pair同输入, 慢 ~255×
显存 free/total58.04 / 61.27 GiBbatch=1 float32, 峰值未单独采集
首次编译已计入 100 ms 稳定值 (首次与稳定差异 <5 ms)

输入尺寸为 580×1080 实拍图像, 若以训练分辨率 336×252 或合成小图测试, 延迟会更低。不同分辨率的性能需单独报告。

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染, PNG 真实有效且 SHA-256 与源日志一致:

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证与性能的完整阶段日志
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), device_name, 模型参数 npu:0 证据
  • assets/model_result.png — python inference.py 真实任务输出、精度与性能 PASS

9. 已知限制

  • 仅在 npu:0 单卡、batch=1、float32 下验证; 未测试多卡、BF16/FP16 或更大 batch
  • DPT scratch 的 16 个权重 (head/uncertainty_head) 非 checkpoint 提供, 为随机初始化, 可能导致与官方完全一致权重在这些层存在微小差异, 但已被验证不影响主 flow 输出
  • torch.autocast("cuda", ...) 在 NPU 上被禁用, 故未启用 bfloat16 加速; 若启用 torch.autocast("npu", bfloat16) 可能进一步提速但未在本次验证
  • 宽高比极端或分辨率远大于 1080p 的图像未经测试, AutomaticShapeSelection 会选最接近 336×252 的缩放, 但可能增加显存
  • 完整数据集评测 (Sintel/KITTI/ETH3D) 未执行, 本仓仅提供单对固定图像的一致性与性能基准

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, UFM, optical-flow, dense-correspondence, DINOv2, computer-vision