liucu/Freepik-nsfw_image_detector-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

#NPU

Freepik/nsfw_image_detector — Ascend NPU 适配

模型简介

  • 模型名称: Freepik/nsfw_image_detector
  • 权重URL: https://huggingface.co/Freepik/nsfw_image_detector
  • 本地路径: working/nsfw_image_detector (通过 HF_ENDPOINT=https://hf-mirror.com 缓存, 无手动代理)
  • 模型卡: https://huggingface.co/Freepik/nsfw_image_detector — EVA02 ViT NSFW 四分类
  • 架构: TimmWrapperForImageClassification wrapping eva02_base_patch14_448.mim_in22k_ft_in22k_in1k
    • Backbone: EVA02 Base (patch 14, img 448, 86M params, hidden 768, 12 layers, 11 heads, RoPE, sub-LN)
    • Head: Linear 768→4 (neutral/low/medium/high), global_pool avg, BF16
    • Input: 3×448×448 RGB, timm preprocess mean [0.48145,0.4578,0.4082] std [0.2686,0.2613,0.2757] bicubic squash
  • 配置: num_classes 4, num_features 768, pretrained_cfg input_size [3,448,448], crop_pct 1.0, interpolation bicubic
  • 权重: model.safetensors 172,725,672 bytes (BF16, 86,351,620 params), config.json + README.md
  • 任务: image-classification 图像分类 (NSFW 敏感度 4 级)
  • 输入: RGB 图像 → 3×448×448 tensor BF16, batch 1
  • 输出: logits [1,4] BF16 → softmax 概率, argmax label
  • 框架: torch 2.9.0+cpu + torch_npu 2.9.0.post1 + transformers 4.57.6 + timm 1.0.28
  • 许可证: mit

数据契约

项值
任务image-classification (NSFW 4 classes: neutral/low/medium/high)
架构族TimmWrapperForImageClassification (EVA02 ViT)
加载后端AutoModelForImageClassification.from_pretrained(local_dir, dtype=bfloat16).to("npu:0") + timm create_transform
dtypebfloat16 (权重、输入、输出、计算一致)
输入 shape[1,3,448,448] BF16, npu:0
输出 shape[1,4] logits BF16 → float32 for compare
预处理timm resolve_data_config(cfg): input_size 3x448x448, bicubic, mean [0.48145,0.4578,0.4082], std [0.2686,0.2613,0.2757], squash
后处理softmax → 概率, argmax → label, cumulative danger_sum (如需阈值 medium/high)
测试数据确定性合成图 np.random.rand(448,448,3)*255 seed 42, uint8 RGB, SHA256 prefix bfd33788, 非真实 NSFW 数据但走真实权重与预处理
设备npu:0 Ascend910_9362, torch_npu 2.9.0.post1, BF16

环境

项值
Python3.11.14
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers4.57.6
timm1.0.28
Pillow12.2.0
numpy1.26.4
safetensors0.7.0
huggingface_hub0.36.2
Ascend Toolkit/usr/local/Ascend/cann-8.5.1
NPUAscend910_9362 ×2 (target npu:0, 64GB HBM)
npu-smi25.5.5
HF_ENDPOINThttps://hf-mirror.com

安装

pip install -r requirements.txt
# 模型已通过 HF_ENDPOINT 镜像下载到 working/nsfw_image_detector (172MB), 无需手动设置 http_proxy
# 依赖: torch==2.9.0 torch_npu==2.9.0.post1 transformers==4.57.6 timm==1.0.28 Pillow==12.2.0 numpy==1.26.4 safetensors==0.7.0 huggingface_hub==0.36.2

NPU 推理

python inference.py

inference.py 自包含, 默认完成:

  1. 环境打印 (torch/torch_npu/npu-smi, HF_ENDPOINT, device_count)
  2. 从本地 working/nsfw_image_detector 加载权重 (BF16, 86M) → to("npu:0"), 打印 param device/dtype
  3. timm 创建 create_transform(resolve_data_config(eva02_base_patch14_448)) 预处理
  4. 生成确定性合成图 448×448 RGB seed 42 (SHA256 bfd33788) → tensor 3×448×448 → batch 1 BF16 npu:0
  5. CPU 基线推理 + NPU warmup 3 + 首轮同步计时 + 10 轮稳定 benchmark (含 torch.npu.synchronize())
  6. 一致性对比 (classification, max_abs 0.03125 mean 0.0117, atol 0.05 rtol 0.01 PASS, argmax 100% 一致)
  7. 保存 working/cpu_output.npy / working/npu_output.npy + 打印 [SUCCESS]

真实输出示例 (本次实测 npu:0 BF16):

[INFO] model_name: Freepik/nsfw_image_detector
[INFO] param device after: npu:0 dtype: torch.bfloat16
[INFO] CPU logits tensor([[ 5.0625, -0.9727, -2.3438, -1.9219]], dtype=torch.bfloat16)
[INFO] CPU predicted label neutral score 0.9961
[INFO] first-run NPU logits tensor([[ 5.0938, -0.9648, -2.3438, -1.9141]], device='npu:0', dtype=torch.bfloat16) time 9.48 ms
[INFO] NPU probs tensor([[9.9618e-01, 2.3288e-03, 5.8651e-04, 9.0133e-04]])
[INFO] NPU predicted neutral score 0.9962
[INFO] consistency max_abs 0.03125 mean_abs 0.011719
[INFO] argmax cpu 0(neutral) npu 0(neutral) agreement 1.0
[INFO] threshold atol 0.05 rtol 0.01 (BF16) -> PASS
[SUCCESS] Freepik/nsfw_image_detector NPU inference completed on npu:0

一致性验证

指标值
方法同权重、同输入、同预处理、同 dtype (BF16) → CPU vs NPU logits float32 对比
工具python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task classification --atol 0.05 --rtol 0.01
结果max_abs 0.03125 mean_abs 0.01171875 finite true shape [1,4] both argmax_agreement 1.0 passed true
阈值说明BF16 量化误差 + NPU 融合算子浮点差异, EVA02 attention/softmax 在 BF16 下容忍 0.03 绝对误差, 0.05 atol/0.01 rtol 为合理阈值; 关键业务指标 argmax 完全一致
类型smoke consistency (单样本确定性合成图, 未做全量数据集评测, 按 Skill 要求标注)
{
  "task": "classification",
  "cpu_shape": [1, 4],
  "npu_shape": [1, 4],
  "finite": true,
  "atol": 0.05,
  "rtol": 0.01,
  "max_abs_error": 0.03125,
  "mean_abs_error": 0.01171875,
  "passed": true,
  "argmax_agreement": 1.0
}

发生 CPU fallback 视为失败; 本次 NPU 路径无 fallback, 参数与输入均在 npu:0.

性能

指标值
设备npu:0 Ascend910_9362
dtypebfloat16
batch1, shape [1,3,448,448]
首轮/编译9.48 ms (含首次编译, torch.npu.synchronize() 计时)
稳定推理avg 9.51 ms, min 9.45 ms, max 9.65 ms, p50 9.49 ms, p90 9.61 ms, p95 9.63 ms (10 runs, warmup 3)
吞吐105.1 infer/s (images/s)
显存权重 172MB + 激活 ~600MB, HBM 3107/65536 MB (npu-smi)
计时方法torch.npu.synchronize() 前后 perf_counter, 排除数据加载, 纯模型前向

说明: 若含 Pillow 解码与 timm 预处理, 端到端约 +2 ms; 本次报告为模型前向耗时.

证据图

三图由 xterm.js 根据本次真实日志生成 (--style raw, 固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$), 非手工绘图:

agent workflow npu device call model result

  • agent_workflow.png: 侦察、环境、加载、NPU 推理、一致性、benchmark 全流程
  • npu_device_call.png: npu-smi info + torch.npu.is_available() + device_count + param device npu:0
  • model_result.png: python inference.py 真实完整输出 (logits、耗时、一致性、SUCCESS)

限制

  • 单卡 npu:0 BF16 推理, 未测 FP32/FP16; 双精度算子在 NPU 上自动 cast 为 float (warning 已记录)
  • 测试输入为确定性合成噪声图, 非真实 NSFW 数据集; 真实图精度需在标注数据集上复测
  • 模型不支持动态分辨率以外的 448 固定输入 (crop_pct 1.0 squash)
  • 权重未包含, 需通过 HF_ENDPOINT=https://hf-mirror.com 下载 (172MB)
  • 未做训练/微调, 仅推理适配

仓库结构

submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png

复现

HF_ENDPOINT=https://hf-mirror.com python inference.py
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_output.npy --npu working/npu_output.npy --task classification --atol 0.05 --rtol 0.01