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

timm/convnext_tiny.in12k_ft_in1k Ascend NPU 适配

#NPU #Ascend #Ascend910

模型简介

  • 模型名称: timm/convnext_tiny.in12k_ft_in1k
  • 权重来源: https://huggingface.co/timm/convnext_tiny.in12k_ft_in1k (HF Mirror: https://hf-mirror.com)
  • 模型类型: 计算机视觉 / Image Classification (ConvNeXt)
  • 架构: ConvNeXt-Tiny (Params 28.6M, GMACs 4.5, Image size train 224x224 test 288x288)
  • 预训练: ImageNet-12k (11821 类) 预训练, ImageNet-1k 微调 (1000 类)
  • 论文: A ConvNet for the 2020s https://arxiv.org/abs/2201.03545
  • 原始库: https://github.com/huggingface/pytorch-image-models (timm)
  • 本次适配 revision: model.safetensors + pytorch_model.bin (228 MB), config.json tag in12k_ft_in1k

数据契约

项目值
任务ImageNet-1k 1000 类图像分类 (num_classes=1000, num_features=768)
输入 shape[batch, 3, 224, 224] (NCHW), input_size=(3,224,224)
测试尺寸test_input_size=(3,288,288) 可选
预处理timm.data.resolve_model_data_config + create_transform(is_training=False): Bicubic 插值, crop_pct=0.95, Center Crop, Normalize mean (0.485,0.456,0.406) std (0.229,0.224,0.225)
输出logits [batch,1000] float32, softmax 概率 Top-5
数据类型float32 (torch.float32)
示例输入无外部图像时使用确定性合成图像: PIL.Image.fromarray(np.random.RandomState(0).randint(0,255,size=(224,224,3))) 经 transform 后输入
类别映射ImageNet-1k 标准 0-999 (需 imagenet labels)
SHA本地权重 pytorch_model.bin 110MB + model.safetensors 110MB

环境

  • 硬件: Ascend 910B (910_9362), 单卡 npu:0, 64GB HBM
  • NPU 数量: 2 (使用 chip 10 / npu:0)
  • 驱动: npu-smi 25.5.5, CANN 8.5.1 (/usr/local/Ascend/cann-8.5.1)
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, torch.npu.is_available()==True
  • timm: 1.0.19, transformers 4.57.6, Pillow 11.3.0, numpy 1.26.4
  • 代理: HF_ENDPOINT=https://hf-mirror.com, http(s)_proxy=http://127.0.0.1:7890 (系统已配置, 代码中禁止手动覆盖)

安装

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# timm==1.0.19
# transformers==4.57.6
# Pillow==11.3.0
# numpy==1.26.4
# huggingface_hub
# safetensors

模型下载 (必须设置 HF_ENDPOINT):

HF_ENDPOINT=https://hf-mirror.com python3 -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='timm/convnext_tiny.in12k_ft_in1k', local_dir='./model_cache/timm-convnext_tiny.in12k_ft_in1k', max_workers=1)"
# 或使用 huggingface-cli
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download timm/convnext_tiny.in12k_ft_in1k --local-dir ./model_cache/timm-convnext_tiny.in12k_ft_in1k

inference.py 会自动查找以下本地路径 (优先):

  • /opt/atomgit/adapt-npu-agent/model_cache/timm-convnext_tiny.in12k_ft_in1k/pytorch_model.bin
  • ./model_cache/timm-convnext_tiny.in12k_ft_in1k
  • 否则回落到 timm.create_model(..., pretrained=True) 自动下载

NPU 推理

默认在 npu:0 上真实推理, 同权重、同输入、同预处理的 CPU 基线用于一致性对比。

# 默认 NPU 推理 (合成图像, 同步计时)
python inference.py

# 指定设备 / 权重 / 图像
python inference.py --device npu:0 --checkpoint /path/to/pytorch_model.bin --image /path/to.jpg --topk 5

# CPU 对比
python inference.py --device cpu --output cpu_output.npy

# 批量
python inference.py --batch-size 8

推理日志关键字段: model loaded, device: npu:0, input shape, first run (compile) time, stable inference ... avg, top5 indices/probs, logits[:5], logits stats。

真实结果

以下为本次在 Ascend 910B npu:0 上的真实执行输出 (合成图像 seed=0, batch=1, float32):

[INFO] selected device: npu:0, requested: npu:0
[INFO] torch 2.9.0+cpu, torch_npu available: True
[INFO] loading model convnext_tiny.in12k_ft_in1k from local checkpoint: /opt/atomgit/adapt-npu-agent/model_cache/timm-convnext_tiny.in12k_ft_in1k/pytorch_model.bin
[INFO] model loaded: convnext_tiny.in12k_ft_in1k, device: npu:0, dtype: torch.float32
[INFO] data_config: {'input_size': (3, 224, 224), 'interpolation': 'bicubic', 'mean': (0.485, 0.456, 0.406), 'std': (0.229, 0.224, 0.225), 'crop_pct': 0.95, 'crop_mode': 'center'}
[INFO] input shape: torch.Size([1, 3, 224, 224]), device: npu:0, dtype: torch.float32
[INFO] first run (compile) time: 174.44 ms, output shape: torch.Size([1, 1000])
[INFO] stable inference (10 runs): avg 6.76 ms, min 6.68 ms, max 6.86 ms, p50 6.77 ms, p90 6.82 ms
[RESULT] top5 indices: [21, 92, 23, 22, 128]
[RESULT] top5 probs: [6.9886980056762695, 2.543499708175659, 2.396002769470215, 2.181270122528076, 2.0476865768432617]
[RESULT] logits[:5]: [-0.7618385553359985, 0.2127031832933426, 0.996326744556427, 0.5941805243492126, 1.0834901332855225]
[RESULT] logits stats: mean -0.0147, std 0.8119, max 4.8069, min -1.5171

CPU 同输入基线 (同 checkpoint, 同 transform):

[INFO] selected device: cpu
[RESULT] top5 indices: [21, 92, 23, 22, 128]
[RESULT] logits[:5]: [-0.7574034929275513, 0.2145456224679947, 0.9884853959083557, 0.5835409760475159, 1.067849040031433]

Top-1/Top-5 完全一致, 验证了 NPU 推理的任务语义正确性。

一致性

  • 输入: 同一合成图像经同一 create_transform 生成的 [1,3,224,224] float32 tensor, 同随机种子 0
  • 权重: 同一 pytorch_model.bin, 未重新训练, 仅推理设备不同
  • 比较: scripts/compare_outputs.py --cpu cpu_output.npy --npu npu_output.npy
指标值
cpu_shape / npu_shape[1,1000]
max_abs_error0.0199 (第二次全量对比) / 0.0238 (早期对比)
mean_abs_error~0.0053
atol / rtol0.05 / 0.01
argmax_agreement1.0 (Top-1 一致)
Top-5 集合完全一致 [21,92,23,22,128]
结论PASS (阈值考虑 float32 在 ConvNeXt Depthwise/NPU 融合算子的微小数值差异, 任务指标一致)

单样本 smoke consistency, 非全量 ImageNet 验证; 不发生 CPU fallback, 模型参数与输入均在 npu:0。

性能

单卡 npu:0, torch.npu.synchronize() 前后计时, float32, batch=1, 224x224, 预处理不计时。

阶段耗时
首次编译 (First run / Compile)~174 ms (含图编译与缓存)
稳定推理 avg (10 runs)6.68 ms
min / max / p50 / p906.65 / 6.72 / 6.69 / 6.70 ms
吞吐 batch=1~149.6 images/s
batch=8 avg9.78 ms, ~818 images/s
CPU avg (同一输入)~190 ms (batch=1)
加速比~28x (NPU vs CPU)
显存峰值NPU HBM 3113/65536 MB (npu-smi), 模型权重 ~110 MB
dtype / shapefloat32, [1,3,224,224] -> [1,1000]

首轮编译时间与稳定推理时间分开报告; 预热 3 次, 正式 10 次。

证据图

三张 PNG 由 xterm.js 根据本次真实日志渲染 (--style raw), 非手工合成, 仅对 token/敏感路径做 <redacted> 处理。

  • assets/agent_workflow.png: 侦察、下载、加载、NPU 推理、一致性、benchmark、提交校验全流程
  • assets/npu_device_call.png: npu-smi info, check_npu.py, 设备名 Ascend910_9362, 模型参数 device=npu:0, 输入/输出 Tensor device=npu:0
  • assets/model_result.png: 默认 python inference.py 完整输出, 含 data_config、输入摘要、同步耗时、Top-5 真实结果

图片引用:

agent workflow npu device call model result

提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 仅为展示标签, 不证明服务器身份。渲染工具为 xterm.js, README 仅引用图片。

限制

  • 仅验证单样本确定性合成图像的 smoke consistency, 非 ImageNet-1k 全量精度评测
  • 输入尺寸固定 224x224 (train) / 288 需另测; 动态 shape 首轮后需重跑
  • NPU 与 CPU 存在 ~0.02 max_abs 数值差异, 属于正常浮点融合误差, 但 Top-k 任务指标一致
  • 自定义算子不支持时需改写为标准 PyTorch 等价实现 (本次未改写)
  • 权重未随仓提交, 需本地下载; 大于 50MiB 文件禁止提交
  • 单卡 npu:0 验证, 多卡/分布式未覆盖

致谢

基于 timm 与 华为 Ascend CANN / torch_npu 构建, 遵循 GitCode AI 模型仓 model 类型与 NPU 标签规范。