#NPU
Freepik/nsfw_image_detectorworking/nsfw_image_detector (通过 HF_ENDPOINT=https://hf-mirror.com 缓存, 无手动代理)TimmWrapperForImageClassification wrapping eva02_base_patch14_448.mim_in22k_ft_in22k_in1k
num_classes 4, num_features 768, pretrained_cfg input_size [3,448,448], crop_pct 1.0, interpolation bicubicmodel.safetensors 172,725,672 bytes (BF16, 86,351,620 params), config.json + README.mdtorch 2.9.0+cpu + torch_npu 2.9.0.post1 + transformers 4.57.6 + timm 1.0.28| 项 | 值 |
|---|---|
| 任务 | 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 |
| dtype | bfloat16 (权重、输入、输出、计算一致) |
| 输入 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 |
| 项 | 值 |
|---|---|
| Python | 3.11.14 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| 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 |
| Ascend Toolkit | /usr/local/Ascend/cann-8.5.1 |
| NPU | Ascend910_9362 ×2 (target npu:0, 64GB HBM) |
| npu-smi | 25.5.5 |
| HF_ENDPOINT | https://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.2python inference.pyinference.py 自包含, 默认完成:
working/nsfw_image_detector 加载权重 (BF16, 86M) → to("npu:0"), 打印 param device/dtypecreate_transform(resolve_data_config(eva02_base_patch14_448)) 预处理torch.npu.synchronize())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 |
| dtype | bfloat16 |
| batch | 1, 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.png: 侦察、环境、加载、NPU 推理、一致性、benchmark 全流程npu_device_call.png: npu-smi info + torch.npu.is_available() + device_count + param device npu:0model_result.png: python inference.py 真实完整输出 (logits、耗时、一致性、SUCCESS)npu:0 BF16 推理, 未测 FP32/FP16; 双精度算子在 NPU 上自动 cast 为 float (warning 已记录)HF_ENDPOINT=https://hf-mirror.com 下载 (172MB)submission/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
├── agent_workflow.png
├── npu_device_call.png
└── model_result.pngHF_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