w
gcw_uQ09W7jl/MidnightRunner-Misc-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

MidnightRunner/Misc on Ascend NPU

1. 模型简介

上游仓库 MidnightRunner/Misc 是一个计算机视觉模型集合(upscaler / denoiser / 分割等)。本工程将其中三个代表性模型适配到华为昇腾 NPU(npu:0,Ascend 910B 系列)上完成真实推理与验证:

集合内权重架构任务本工程验证方式
RealESRGAN_x4plus.pthRRDBNet (23 blocks, x4)图像超分辨率CPU vs NPU 输出 PSNR + NPU 延迟/吞吐
ESRGAN_4x.pthRRDBNet(经典 key 布局)图像超分辨率CPU vs NPU 输出 PSNR
sam_vit_b_01ec64.pthSegment Anything ViT-B点提示图像分割固定点提示下 CPU vs NPU mask IoU

其余文件为 LoRA、文本编码器、GGUF 等非 PyTorch 完整推理链路产物,不在本次端到端验证范围(见第 9 节)。

2. 验证环境

  • 硬件:Atlas 800T A2 系列,Ascend 910B(npu-smi 名称 Ascend910_9362),64 GB HBM
  • 软件:CANN 8.5.1,Python 3.11,torch 2.9.0+cpu,torch_npu 2.9.0.post1
  • 推理设备:单卡 npu:0,dtype fp32,batch size 1

3. 安装依赖

pip install -r requirements.txt

权重不随仓库分发,需自行从上游下载到本地目录(默认查找顺序:环境变量 MISC_WEIGHTS、./model_cache/MidnightRunner_Misc、../model_cache/MidnightRunner_Misc):

HF_ENDPOINT=https://hf-mirror.com huggingface-cli download MidnightRunner/Misc \
  RealESRGAN_x4plus.pth ESRGAN_4x.pth sam_vit_b_01ec64.pth \
  --local-dir ./model_cache/MidnightRunner_Misc

4. NPU 推理

# 默认:依次验证三个模型(CPU-NPU 一致性 + 性能)
python inference.py

# 单模型
python inference.py --model realesrgan --mode benchmark   # 或 esrgan / sam

脚本加载真实权重到 npu:0 执行前向,打印模型名、任务、后端、device、dtype、输入输出契约、真实任务结果、同步计时和 PASS/FAIL;任一验证失败返回非零退出码。

5. 真实推理结果

python inference.py --mode benchmark 在 Ascend910B 上的一次真实运行(2026-08-23 UTC,日志 SHA 见 assets 截图):

===== [RealESRGAN_x4plus] x4 super-resolution =====
input contract : LR tensor (1, 3, 128, 128) fp32 in [0,1], fixed seed 42
output contract: HR tensor (1, 3, 512, 512) fp32 in [0,1]
cpu forward done in 12.1s, out (1, 3, 512, 512) range [0.2435,0.9664]
loaded real weights on npu:0 (1.3s)
npu forward done in 0.18s
consistency    : PSNR(CPU,NPU)=86.15 dB  max|d|=0.00047  mean|d|=0.000038
[RealESRGAN_x4plus] PASS

===== [ESRGAN_4x] x4 super-resolution =====
consistency    : PSNR(CPU,NPU)=65.86 dB  max|d|=0.00476  mean|d|=0.000343
[ESRGAN_4x] PASS

===== [SAM ViT-B] promptable segmentation =====
point prompt (row,col)=(194,317)
cpu best score=1.0248, mask area=26399
npu best score=1.0249, mask area=26399
consistency    : mask IoU(CPU,NPU)=1.0000  |score diff|=0.00003
[SAM ViT-B] PASS

OVERALL: PASS  (3 pipelines validated on npu:0)

6. CPU-NPU 一致性验证

固定种子合成输入(seed 42),同一权重分别在 CPU fp32 与 npu:0 fp32 前向:

模型指标数值判定阈值
RealESRGAN_x4plusPSNR(CPU,NPU) / max|d|86.15 dB / 0.00047≥30 dB 且 ≤0.05
ESRGAN_4xPSNR(CPU,NPU) / max|d|65.86 dB / 0.00476≥30 dB 且 ≤0.05
SAM ViT-Bmask IoU / |score diff|1.0000 / 0.00003≥0.85 且 ≤0.05

该验证为固定样例一致性验证(smoke consistency on fixed samples),不是完整数据集精度评测。

7. 性能测试

计时以 torch.npu.synchronize() 包裹,预热 3 次、正式 10 次:

模型输入→输出avg/p50/p90 latency吞吐NPU 峰值显存
RealESRGAN_x4plus128×128→512×51228.8 ms34.69 images/s344 MiB
ESRGAN_4x128×128→512×51228.8 ms34.78 images/s344 MiB
SAM ViT-B(整图编码+解码)1024×1024~1.13 s/image(单次实测)—<64 GB HBM

8. 自验证截图

  • assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证、性能各阶段真实日志
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、参数所在设备与 dtype
  • assets/model_result.png — 默认 python inference.py 真实输出与 PASS/FAIL

9. 已知限制

  • 仅对集合中上述三个完整可执行链路做了端到端验证;LoRA(需底模)、SDXL 文本编码器(无独立推理入口)、GGUF(量化格式,需 llama.cpp 类后端)等未纳入。
  • SAM 性能为单次实测值,未做多轮统计。
  • 一致性验证基于固定合成样例;如需业务精度请用自有数据集评测。

10. 标签

NPU, Ascend, Ascend910B, computer-vision, super-resolution, segmentation, RealESRGAN, SAM