竞赛提交: WinCLIP (CVPR 2023) 零样本异常检测模型在华为昇腾 910B4-1 NPU 上的适配与性能优化 提交日期: 2026-05-18 提交者: v50_ 提交说明: Opt3 npu_prefetch (权重预取) 91.6 img/s 10.9ms ~2.36x
将 WinCLIP (CVPR 2023) 零样本异常检测模型适配到 华为昇腾 910B4-1 NPU,经过 Tile Batching + 权重预取 (npu_prefetch) 两阶段优化,实现 ~2.36x 端到端推理加速,精度完全保持。
| 指标 | 串行 v1 (per-tile baseline) | Opt3 优化版 (prefetch) | 提升 |
|---|---|---|---|
| 推理延迟 (per-image) | ~25.7 ms | 10.9 ms | ~2.36x |
| FPS | ~38.9 img/s | 91.6 img/s | ~2.36x |
| VisA AUROC (avg 12cls) | 0.7876 | 0.7876* | Δ=0.0000 ✅ |

本项目遵循竞赛指定的性能基线验收标准:
| 基线类型 | 要求 | 本项指标 | 状态 |
|---|---|---|---|
| 精度基线 | 复现论文 WinCLIP VisA AUROC ≥ 78.1% | 78.76% (Δ=+0.66%) | ✅ |
| 精度无退化 | 优化版 vs baseline AUROC 差异 ≤ ±0.01 | Δ=0.0000 (12/12 类一致) | ✅ |
| 性能基线 (串行v1) | 串行 tile 推理 FPS/latency 作为起始基准 | 38.9 img/s, 25.7ms | — |
| 优化目标 | 优化版相对 baseline 须有可量化的正向加速 | 91.6 img/s, ~2.36x 加速 | ✅ |
| 精度-性能权衡 | 优化不得导致精度退化换取性能 | AUROC 完全一致 | ✅ |
WinCLIP 是基于 CLIP 的零样本异常检测模型,利用 CLIP 的视觉-语言对齐能力,在无需训练的情况下检测工业缺陷。核心流程:
优化关键: 将串行 tile 推理改为 batch 推理,消除 ViT 编码的串行瓶颈。
| 文件 | 说明 |
|---|---|
winclip_src/ | WinCLIP 源码 (open_clip 框架 + 异常检测逻辑) |
opt_winclip/benchmark_winclip.py | 基准测试脚本 (串行 tile v1) |
opt_winclip/run_optimized_v2.py | 优化版推理脚本 (Tile Batch v2) |
opt_winclip/prepare_visa_data.py | VisA 数据集准备脚本 |
scripts/compare_precision.py | 精度对比工具 |
scripts/compare_all.py | 全类别跑分脚本 |
requirements.txt | Python 依赖 |
setup_env.sh | 环境一键配置脚本 |
OPTIMIZATION_LOG.md | 完整优化日志 (含 Profiling 分析) |
baseline_npu_results.json | Baseline 性能数据 |
precision_comparison.json | 精度评估数据 |
results_screenshots/ | 评测截图、日志、可视化 |
| 组件 | 版本 |
|---|---|
| 硬件 | Ascend 910B4-1 NPU × 1 |
| CANN | 8.5.0 |
| Python | 3.9+ |
| PyTorch | 2.7.0 |
| torch_npu | 2.7.1.post4 |
# 安装依赖
pip install -r requirements.txt
# 或一键配置
bash setup_env.sh模型权重: WinCLIP 基于 OpenCLIP 预训练权重,原始模型代码见 mala-lab/WinCLIP。权重文件 vit_b_16_plus_240-laion400m_e31-8fb26589.pt:
wget https://github.com/mlfoundations/open_clip/releases/download/v0.2-weights/vit_b_16_plus_240-laion400m_e31-8fb26589.ptVisA 数据集: 从 Amazon Science / Visual Anomaly Detection 下载 VisA_20220922.tar:
# 方式一:从 AWS S3 直接下载
wget https://amazon-visual-anomaly.s3.us-west-2.amazonaws.com/VisA_20220922.tar
# 或从 GitHub Releases 下载
# https://github.com/amazon-science/visual-anomaly-detection/releases
# 解压并转换格式
tar -xf VisA_20220922.tar -C dataset/
python opt_winclip/prepare_visa_data.py# Baseline (串行 tile)
python opt_winclip/benchmark_winclip.py --obj_type candle --output results_baseline.json
# 优化版 (Tile Batch)
python opt_winclip/run_optimized_v2.py --obj_type all
# 精度对比
python scripts/compare_precision.pyimport torch
import torch_npu
from winclip_src.main_npu import WinCLIP
model = WinCLIP('ViT-B-16-plus-240', device='npu:0')
# 优化版: 自动 tile batch 推理
results = model.predict('path/to/image.jpg', category_name='candle')
print(f"Anomaly score: {results['anomaly_score']:.4f}")| 阶段 | 优化项 | FPS | Latency | 加速比 |
|---|---|---|---|---|
| Baseline | FP32 串行 tile (per-image折算) | ~38.9 img/s | ~25.7ms | 1.00x |
| Opt2 | Tile Batch v2 (堆叠batch推理) | 78.1 img/s | 12.8ms | ~2.01x |
| Opt3 | + npu_prefetch 权重预取 | 91.6 img/s | 10.9ms | ~2.36x |
使用 torch_npu.npu_prefetch API,在视觉 Transformer 推理进入循环前,异步预取全部 12 层 × 4 个 MatMul 权重(共 48 个权重张量)到 NPU On-Chip 缓存,与计算流水线重叠。
| 项目 | 值 |
|---|---|
| 预取位置 | VisionTransformer.forward() 中,self.transformer(x) 调用之前 |
| 预取张量 | 每层: attn.in_proj_weight, attn.out_proj.weight, mlp.c_fc.weight, mlp.c_proj.weight |
| 预取总数 | 12 layers × 4 = 48 个权重张量 |
| 累计权重大小 | 12 × (768×768 + 768×768 + 768×3072 + 3072×768) × 4 bytes ≈ 169 MB |
| 精度影响 | 无(纯 DMA 预取,计算语义不变) |
| 文本编码 | 不走 prefetch(小 batch 下 overhead > 收益) |
| 尝试 | 结果 | 说明 |
|---|---|---|
| AMP FP16 | ❌ 0.63x | LayerNorm 回退 FP32 导致降速 |
| torch.compile | ❌ 1.00x | 动态 shape + 图中断,无收益 |
| npu_fusion_attention | ✅ 已自动使用 | Profiling 确认已使用 FlashAttentionScore |
| GE 图模式 | ❌ 不适用 | Tile 数量随图像尺寸动态变化 |
| 算子类别 | 占比 | 说明 |
|---|---|---|
| MatMulV2 (Linear) | 41.2% | MLP + Attention 投影 |
| FlashAttentionScore | 15.1% | 已使用融合注意力算子 |
| LayerNormV3 | 10.5% | 融合 LayerNorm |
| 其他 (Transpose, Add, GeLU) | ~33.2% | 数据搬运与激活函数 |
| 类别 | Baseline AUROC | Optimized AUROC | Δ |
|---|---|---|---|
| candle | 0.9576 | 0.9576 | 0.0000 ✅ |
| capsules | 0.8448 | 0.8448 | 0.0000 ✅ |
| cashew | 0.9100 | 0.9100 | 0.0000 ✅ |
| chewinggum | 0.9544 | 0.9544 | 0.0000 ✅ |
| fryum | 0.7624 | 0.7624 | 0.0000 ✅ |
| macaroni1 | 0.7860 | 0.7860 | 0.0000 ✅ |
| macaroni2 | 0.6743 | 0.6743 | 0.0000 ✅ |
| pcb1 | 0.7874 | 0.7874 | 0.0000 ✅ |
| pcb2 | 0.5192 | 0.5192 | 0.0000 ✅ |
| pcb3 | 0.7019 | 0.7019 | 0.0000 ✅ |
| pcb4 | 0.8392 | 0.8392 | 0.0000 ✅ |
| pipe_fryum | 0.7136 | 0.7136 | 0.0000 ✅ |
| 平均 | 0.7876 | 0.7876 | 0.0000 |
✅ 所有 12 类别 AUROC 完全一致(Δ = 0.0000 < 0.01),满足精度要求。
| 来源 | VisA AUROC | 说明 |
|---|---|---|
| 论文 CLIP-AC 基线 | 59.3% | 直接 CLIP 零样本 |
| 论文 WinCLIP (Table 1) | 78.1% | CVPR 2023 |
| 本项目 NPU | 78.76% | Ascend 910B4-1 优化后 |
| Δ vs 论文 | +0.66% | 略高于论文报告值 |