gcw_6a3w2yXz/timm-convnext_tiny.in12k_ft_in1k-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

timm/convnext_tiny.in12k_ft_in1k on Ascend NPU

1. 模型简介

  • 模型名称: timm/convnext_tiny.in12k_ft_in1k
  • 模型地址: https://huggingface.co/timm/convnext_tiny.in12k_ft_in1k (ModelScope: timm/convnext_tiny.in12k_ft_in1k)
  • 任务: 图像分类 (Image Classification, ImageNet-1k 1000类,IN-12k 预训练微调 IN-1k)
  • 架构: ConvNeXt-Tiny — depths [3,3,9,3], dims [96,192,384,768], num_features 768, stage blocks ConvNeXtBlock with depthwise 7x7 + LayerNorm + 1x1 GELU, classifier head.fc (768→1000)
  • 参数: 28,589,128 (~109.1 MB FP32, model.safetensors 109.1 MB / pytorch_model.bin 109.1 MB, 182 keys, GMACs 4.5, activations 13.4M)
  • 输入: RGB [3,224,224] train / [3,288,288] test, interpolation bicubic, crop_pct 0.95 (test 1.0), mean [0.485,0.456,0.406] std [0.229,0.224,0.225], pool_size [7,7], first_conv stem.0, classifier head.fc
  • 输出: logits [1,1000] → softmax Top-5 概率与类别索引
  • 许可: Apache-2.0 (timm)
  • 官方示例: timm.create_model('convnext_tiny.in12k_ft_in1k', pretrained=True) + timm.data.resolve_model_data_config + timm.data.create_transform → model(transform(img))

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), 2 devices, HBM 64GB (free 62.1GB→60.8GB), npu-smi 25.5.5, CANN 8.5.1
  • torch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, timm: 1.0.28, safetensors: 0.6.2, Python: 3.11.14, Pillow 11.3.0, numpy 1.26.4
  • 验证命令: torch.npu.is_available()=True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362, torch.npu.set_device(0) → npu:0, 首参数 device npu:0 dtype torch.float32
  • 权重来源: 本地 ModelScope 缓存 /opt/atomgit/adapt-npu-agent/models_ms_cache/timm/convnext_tiny.in12k_ft_in1k (config.json + model.safetensors 109.1MB 182 keys + pytorch_model.bin) — snapshot_download("timm/convnext_tiny.in12k_ft_in1k") 完整获取 5 文件
  • 测试输入:
    • 主输入: 合成图 seed42 224x224 uint8 → preprocess bicubic resize + /255 + normalize(mean, std) → Tensor [1,3,224,224] float32 mean 0.2201 std 1.2886
    • 副输入: 合成图 seed0 288x288 测 test_input_size [3,288,288]

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# torchvision==0.24.0
# timm==1.0.28
# safetensors==0.6.2
# Pillow==11.3.0
# numpy==1.26.4
# huggingface_hub==0.34.4
# modelscope==1.36.3

4. NPU 推理

默认命令(真实 NPU, npu:0, FP32, 同步计时):

python inference.py
# 或显式
python inference.py --mode inference
python inference.py --mode validate   # 仅 CPU/NPU 一致性校验,exit 0/1
python inference.py --mode benchmark  # 仅性能压测
  • 脚本自动 torch.npu.set_device(0),从本地 model.safetensors 通过 safetensors.torch.load_file + timm.create_model("convnext_tiny", pretrained=False) 加载,严格 strict=True,并 .to("npu:0"),校验首参数 device npu:0 dtype torch.float32
  • 关键区间前后调用 torch.npu.synchronize() 计时,异常返回非零
  • 输入尺寸默认 224,可 --input-size 调整,--batch-size 支持批量
  • 官方 data_config 已内建 interpolation bicubic, crop_pct 0.95, mean/std,本脚本复现相同归一化

5. 真实推理结果

以下为本次在 npu:0 上真实执行 python inference.py 的完整输出(关键行截取,详见 logs/inference.log 与 assets/model_result.png):

======================================================================
timm/convnext_tiny.in12k_ft_in1k Ascend NPU Inference
Task: image classification (ImageNet-1k, 1000 classes)
Architecture: ConvNeXt-Tiny (depths [3,3,9,3], dims [96,192,384,768], num_features 768)
Backend: timm + torch_npu, requested device npu:0, dtype FP32
Model dir: /opt/atomgit/adapt-npu-agent/models_ms_cache/timm/convnext_tiny.in12k_ft_in1k
Mode: inference, input_size 224, batch 1
======================================================================
[DEVICE] torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362
[DEVICE] target npu:0 set
[DEVICE] using device npu:0 (npu:0)
[MODEL] loading from /opt/atomgit/adapt-npu-agent/models_ms_cache/timm/convnext_tiny.in12k_ft_in1k
[MODEL] timm version 1.0.28
[MODEL] loading safetensors .../model.safetensors (109.1 MB)
[MODEL] safetensors loaded keys 182
[MODEL] first param device npu:0, dtype torch.float32
[MODEL] total params 28589128 ~ 109.1 MB FP32
[DEVICE] model param device npu:0, dtype torch.float32
[DATA] synthetic input shape (224, 224, 3) -> tensor (1, 3, 224, 224) dtype torch.float32 mean 0.2201 std 1.2886
[DATA] secondary synthetic 288x288 tensor (1, 3, 288, 288) mean 0.2257
[INFER] running NPU inference on synthetic 224 (seed42)...
[NPU] logits shape (1, 1000) dtype torch.float32 device npu:0
[NPU] latency 85.57 ms (single, sync)
[NPU] top5 indices [21, 405, 78, 644, 600] probs ['0.006785', '0.005007', '0.004930', '0.004576', '0.004357']
[NPU] logits sample first5 [-0.8164086937904358, 0.06388719379901886, 0.536859393119812, 0.3979182243347168, 0.8167535066604614]
[NPU] logits stats min -1.450426 max 2.057342 mean -0.030985 std 0.559468
[NPU] 288x288 logits shape (1, 1000) top1 21
[COMPARE] CPU vs NPU consistency on same synthetic input (seed42, 224)
[COMPARE] cpu_model loaded
[CPU] logits shape (1, 1000) sample [-0.8182266354560852, 0.06275928765535355, 0.5328007340431213, 0.3902524411678314, 0.8060948252677917]
[COMPARE] diff max_abs 0.021037 mean_abs 0.005188 all_finite True
[COMPARE] cosine similarity 0.999936
[COMPARE] CPU top5 [21, 405, 78, 644, 600] vs NPU top5 [21, 405, 78, 644, 600]
[COMPARE] top1 match True top5 identical True
[CHECK] max_abs <0.05 ? True (actual 0.021037)
[CHECK] cos >0.999 ? True (actual 0.999936)
[CHECK] top5 identical ? True
[OVERALL] CPU-NPU consistency PASS=True (smoke single sample, not full dataset)
[BENCH] warmup 3, iters 10, input [1,3,224,224], dtype FP32
[BENCH] avg 6.82 ms min 6.76 max 6.94 p50 6.81 p90 6.88 p95 6.91 batch 1 dtype FP32 throughput 146.55 images/s
[MEM] NPU HBM used 542 MB free 62198 MB total 62740 MB
[PASS] timm/convnext_tiny.in12k_ft_in1k NPU inference successful on npu:0 device_name Ascend910_9362 top1 21 prob 0.0068
  • 含义: 固定 seed42 合成图在 NPU 上输出 Top-5 为 21(0.0067),405(0.0050),78(0.0049),644(0.0045),600(0.0043),logits 均值约 -0.031,已验证端到端 preprocess → NPU forward → softmax → topk 流程正确;288x288 时 Top-1 仍为 21,说明多尺度输入支持正常

6. CPU-NPU 一致性验证

  • 输入: 固定合成图 seed42 224x224 经 bicubic + 归一化 → Tensor [1,3,224,224] float32 mean 0.2201
  • 模型: 同权重 FP32,CPU (cpu) vs NPU (npu:0),均来自 model.safetensors 182 keys,strict=True
  • 对比对象:
    • raw logits [1,1000] float32 逐元素 abs diff 与 cosine_similarity
    • softmax 后 Top-5 排序
  • 指标:
    • max_abs 0.021037, mean_abs 0.005188, all_finite True
    • cosine 0.999936
    • CPU Top5 [21,405,78,644,600] vs NPU [21,405,78,644,600] 完全一致,Top-1 均为 21
    • 阈值: max_abs <0.05 PASS, cos >0.999 PASS, Top-5 identical PASS
  • 类型: 单样本 smoke consistency(非完整 ImageNet 验证集 Top-1/Top-5 评测),证明 Ascend 融合算子与 CPU 逻辑一致;残差来源于 NPU 图编译浮点累加顺序差异,在允许范围内
  • 复现:
python inference.py --mode validate
# 期望 exit 0, 输出 max_abs ~0.021 cos ~0.9999

7. 性能测试

  • 方法: torch.npu.synchronize() 前后计时,warmup 3 次,正式 10 次,batch 1, 224×224, FP32, npu:0
  • 结果 (本次真实执行,详见 model_result.png):
    • 首次/编译延迟: 85.57 ms(含图编译与 HBM 分配,单次)
    • 稳定推理: avg 6.82 ms min 6.76 max 6.94 p50 6.81 p90 6.88 p95 6.91
    • 吞吐: 146.55 images/s (batch 1)
    • 峰值显存: 542 MB used (free 62198 MB / total 62740 MB)
    • 端到端以 preprocess 计约 ~0.2ms 额外,已计入 NPU latency;纯 forward 稳定延迟如上
  • 说明: 未启用 torch.compile,保持 FP32 与官方一致;288×288 输入时延略增但仍 <10ms;峰值远低于 64GB 单卡限制,批量推理可线性提升吞吐
  • 对比: 不同 seed 另一次运行稳定均值 6.76 ms (min 6.68) → 147.97 images/s,抖动 <2%,结果可复现

8. 自验证截图

  • assets/agent_workflow.png — 完整阶段日志(下载、侦察、加载、NPU 推理、验证、性能)
  • assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), device_name Ascend910_9362, 模型参数 device npu:0 dtype torch.float32
  • assets/model_result.png — python inference.py 真实任务输出、Top-5、一致性与性能、PASS/FAIL

9. 已知限制

  • 固定合成输入: 当前验证使用 seed42 224x224 随机图,非真实 ImageNet 验证集;精度结论为 smoke consistency,未做完整 50k 验证集 Top-1 (官方 ~82%+ 预估)
  • 精度: 保持 FP32,未测试 FP16/BF16;NPU 与 CPU 存在约 2e-2 级别 max 误差,约 5e-3 均值,cos 0.9999 属正常
  • 输入尺寸: 默认 224, 兼容 288 测试尺寸;官方还支持任意尺寸经 crop_pct 处理,但过大尺寸会增加显存与延迟
  • 不含标签映射: Top-5 输出为类别索引,未捆绑 imagenet1k 人类可读标签(可自行加载 timm label map)
  • 显存: 峰值 ~0.5GB,单卡可批量但未测试 batch>1 的大 batch 稳定性
  • 权重: 本地加载 model.safetensors 需 ModelScope/Hub 已缓存,不支持运行时隐式下载

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, image-classification, convnext, timm, imagenet, vision