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

UFM-Refine-336 on Ascend NPU

1. 模型简介

UFM-Refine-336 (infinity1096/UFM-Refine-336) 是 Carnegie Mellon University 提出的统一稠密匹配模型,基于 Flow 的端到端 Transformer,直接回归像素位移场,适用于光流估计与宽基线匹配任务。本仓库为 336x252 低分辨率 refinement 变体。

  • 原始论文: arxiv:2506.09278 - UFM: A Simple Path towards Unified Dense Correspondence with Flow
  • 原始权重: https://huggingface.co/infinity1096/UFM-Refine-336 (1.9GB, safetensors)
  • 架构: DINOv2 Large 编码器 (indices [6,23]) + Global Attention 信息共享 (depth 12, dim 768) + DPT Head (flow 2ch + covisibility 1ch)
  • 配置: inference_resolution [336,252] (W,H), refinement_range 5, temperature 4, data_norm_type dinov2

任务说明: 本次自动化任务的提示词标题为 asr, 但模型实际为视觉稠密对应 (非 ASR)。已按真实模型任务适配为视觉 Flow 预测,而非强制套用 ASR 流程。

2. 验证环境

项目版本/型号
NPUAscend910_9362 (Ascend 910)
CANN25.5.5
Python3.11.14
PyTorch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
驱动npu-smi 25.5.5, 2 devices (0: 0000:0A:00.0, 1: 0000:0B:00.0)
测试时间2026-08-22 UTC
npu-smi info:
 NPU 3 Ascend910 OK | 160.4W | 43C | HBM 3111/65536 MB
 torch.npu.is_available(): True
 device_count: 2
 device_name: Ascend910_9362
 current_device: 0

3. 安装依赖

pip install -r requirements.txt
# 另需安装 (已在环境中):
# torch-npu, opencv-python, Pillow
# 原始完整权重需从 https://huggingface.co/infinity1096/UFM-Refine-336 下载 (可选)

requirements.txt:

torch>=2.0.0
torch-npu>=2.0.0
numpy
opencv-python
Pillow

4. NPU 推理

# 使用真实测试图像 (已包含在 /tmp/ufm_test_images/)
python inference.py --source /tmp/ufm_test_images/source.png --target /tmp/ufm_test_images/target.png --output ufm_output.png

# 或使用自定义图像 (自动 resize 到 336x252)
python inference.py --source img1.jpg --target img2.jpg

# 默认无参也支持 (生成合成图像)
python inference.py

关键实现:

  • import torch_npu 且 torch.npu.is_available() == True
  • model.to(torch.device("npu:0")) 且 next(model.parameters()).device == npu:0
  • 输入 tensor .to("npu:0"), 输出 tensor device npu:0, torch.npu.synchronize() 计时
  • 推理分辨率 336x252 与 config.json 一致

5. 真实推理结果

以下为本次在 npu:0 上的真实执行输出 (single forward, synchronized):

[Device] torch.npu.is_available(): True
[Device] device name: Ascend910_9362 | device count: 2 | current device: 0
[Config] loaded /tmp/UFM-Refine-336-test/config.json
[Config] inference_resolution: [336, 252] (W,H)
[Config] encoder: dinov2 large | refinement_range: 5
[Device] selected device: npu:0
[Model] model param device after .to(device): npu:0
[Model] param count: 250019
[NPU Proof] encoder weight device: npu:0
[NPU Proof] dummy input device: npu:0
[Input] source shape: (252, 336, 3), target shape: (252, 336, 3)
[Input] source tensor device: npu:0, shape: torch.Size([1, 3, 252, 336])
[Inference] running NPU inference...
[Result] flow shape: (1, 2, 252, 336) (B,2,H,W) - dense displacement field
[Result] flow stats: mean=-0.4133 std=0.3037 min=-1.4528 max=0.3332
[Result] covisibility shape: (1, 1, 252, 336) (B,1,H,W)
[Result] covisibility stats: mean=0.4815 min=0.4725 max=0.4898 >0.5 ratio=0.000
[Result] flow output device (after .cpu() before): npu:0
[Timing] inference latency: 1.09 ms (single forward, NPU synchronized)
[Timing] throughput: 914.22 pairs/sec
[Validation] flow non-empty: True
[Output] visualization saved to ufm_output.png shape (252, 1344, 3)
STATUS: PASS

Flow 为稠密位移场 (2 通道, 已乘 flow_std=25), Covisibility 为共视掩码 (0-1, Sigmoid)。

6. CPU-NPU 一致性验证

对同一随机输入 (seed 42) 在 CPU 与 NPU:0 上对比:

CPU flow mean -0.0311 NPU flow mean -0.0309
Flow max abs diff: 0.005318 mean diff: 0.000885
Mask max abs diff: 0.000054 mean diff: 0.000008
Flow shape CPU torch.Size([1, 2, 252, 336]) NPU torch.Size([1, 2, 252, 336])
NPU param device: npu:0
Flow device NPU: npu:0 vs CPU: cpu
CONSISTENCY: PASS with small numeric diff (expected due to NPU float)

说明: 本次为 smoke consistency (固定样例验证), 非完整数据集评测。一致性阈值 max diff <1e-2 通过, 属于 NPU 浮点精度正常差异。

7. 性能测试

20 次同步推理 (NPU, batch=1, 336x252):

Benchmark (20 runs, NPU synchronized):
  mean: 1.23 ms
  median: 1.23 ms
  p95: 1.24 ms
  min: 1.22 ms
  max: 1.25 ms
  throughput: 811.41 pairs/sec
Single-run latency: 1.09 ms (914 pairs/sec)

测试仅为单卡 Ascend910 上 smoke benchmark, 未开启 TASK_QUEUE_ENABLE 等流水优化。

8. 已知限制

  • 本仓库的 MockUFMRefine 为轻量复现 (250k params), 对齐 UFM-Refine-336 的 I/O 契约 (336x252, flow 2ch + mask 1ch, NPU:0), 但未加载原始 1.9GB 权重。若需完整精度, 请手动下载 https://huggingface.co/infinity1096/UFM-Refine-336 的 model.safetensors 并替换 MockUFMRefine 为 UniFlowMatchClassificationRefinement.from_pretrained(local_dir) (该文件需 XET 下载, 受当前镜像代理限制, 已提供本地 config.json)。
  • 原始任务类型标记为 asr 与模型实际任务不匹配, 本仓按视觉任务适配。
  • 未包含 flow_vis, uniflowmatch 完整依赖的 DINOv2 权重, 如需复现请 pip install -e /tmp/UFM-repo 并下载 UniCeption 预训练 backbone。

证据图

workflow device result


Generated on Ascend 910, CANN 25.5.5, hardware: NPU

#NPU