上游仓库 MidnightRunner/Misc 是一个计算机视觉模型集合(upscaler / denoiser / 分割等)。本工程将其中三个代表性模型适配到华为昇腾 NPU(npu:0,Ascend 910B 系列)上完成真实推理与验证:
| 集合内权重 | 架构 | 任务 | 本工程验证方式 |
|---|---|---|---|
RealESRGAN_x4plus.pth | RRDBNet (23 blocks, x4) | 图像超分辨率 | CPU vs NPU 输出 PSNR + NPU 延迟/吞吐 |
ESRGAN_4x.pth | RRDBNet(经典 key 布局) | 图像超分辨率 | CPU vs NPU 输出 PSNR |
sam_vit_b_01ec64.pth | Segment Anything ViT-B | 点提示图像分割 | 固定点提示下 CPU vs NPU mask IoU |
其余文件为 LoRA、文本编码器、GGUF 等非 PyTorch 完整推理链路产物,不在本次端到端验证范围(见第 9 节)。
npu-smi 名称 Ascend910_9362),64 GB HBMtorch 2.9.0+cpu,torch_npu 2.9.0.post1npu:0,dtype fp32,batch size 1pip 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# 默认:依次验证三个模型(CPU-NPU 一致性 + 性能)
python inference.py
# 单模型
python inference.py --model realesrgan --mode benchmark # 或 esrgan / sam脚本加载真实权重到 npu:0 执行前向,打印模型名、任务、后端、device、dtype、输入输出契约、真实任务结果、同步计时和 PASS/FAIL;任一验证失败返回非零退出码。
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)固定种子合成输入(seed 42),同一权重分别在 CPU fp32 与 npu:0 fp32 前向:
| 模型 | 指标 | 数值 | 判定阈值 |
|---|---|---|---|
| RealESRGAN_x4plus | PSNR(CPU,NPU) / max|d| | 86.15 dB / 0.00047 | ≥30 dB 且 ≤0.05 |
| ESRGAN_4x | PSNR(CPU,NPU) / max|d| | 65.86 dB / 0.00476 | ≥30 dB 且 ≤0.05 |
| SAM ViT-B | mask IoU / |score diff| | 1.0000 / 0.00003 | ≥0.85 且 ≤0.05 |
该验证为固定样例一致性验证(smoke consistency on fixed samples),不是完整数据集精度评测。
计时以 torch.npu.synchronize() 包裹,预热 3 次、正式 10 次:
| 模型 | 输入→输出 | avg/p50/p90 latency | 吞吐 | NPU 峰值显存 |
|---|---|---|---|---|
| RealESRGAN_x4plus | 128×128→512×512 | 28.8 ms | 34.69 images/s | 344 MiB |
| ESRGAN_4x | 128×128→512×512 | 28.8 ms | 34.78 images/s | 344 MiB |
| SAM ViT-B(整图编码+解码) | 1024×1024 | ~1.13 s/image(单次实测) | — | <64 GB HBM |
assets/agent_workflow.png — 下载、侦察、加载、NPU 推理、验证、性能各阶段真实日志assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、参数所在设备与 dtypeassets/model_result.png — 默认 python inference.py 真实输出与 PASS/FAILNPU, Ascend, Ascend910B, computer-vision, super-resolution, segmentation, RealESRGAN, SAM