gcw_6a3w2yXz/nvidia-llama-nemotron-embed-1b-v2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

dima806/fairface_age_image_detection NPU 适配

#NPU

本仓库将 HuggingFace 上的 dima806/fairface_age_image_detection(基于 google/vit-base-patch16-224-in21k 微调的 9 类年龄分组图像分类模型,ViT-B/16 224×224,86M 参数 float32)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持人脸年龄分组预测(0-2、3-9、10-19、20-29、30-39、40-49、50-59、60-69、more than 70)。

  • 模型原始仓库:https://huggingface.co/dima806/fairface_age_image_detection
  • 权重:model.safetensors 343 MB(86M 参数,float32),config.json model_type vit,hidden_size 768,patch_size 16,image_size 224,num_hidden_layers 12,num_attention_heads 12,id2label 9 类
  • 源实现:transformers ViTForImageClassification + ViTImageProcessor(do_resize 224, do_rescale 1/255, do_normalize mean 0.5 std 0.5)
  • Revision:main(本地缓存 /tmp/fairface_final,checkpoint-4688 权重与根 config 一致,复制为 model.safetensors + preprocessor_config.json)

任务与路由

项值
模型dima806/fairface_age_image_detection(86M, 343 MB float32, revision main)
架构ViT-B/16 (google/vit-base-patch16-224-in21k) 12层 768 hidden + classification head 9-way
任务图像分类 image-classification(年龄分组 9 类)
输入pixel_values shape [B,3,224,224] float32,经 ViTImageProcessor 预处理:resize 224×224 (bilinear resample 2), rescale 1/255, normalize mean 0.5 std 0.5
输出logits shape [B,9] float32 → softmax probabilities [B,9] → argmax label 0-8 对应 0-2 等
数据契约输入为 RGB 图像(224×224 或任意尺寸自动 resize),轴顺序 [batch, channel, height, width],dtype float32,通道 3,归一化到 [-1,1];缺失值不适用;输出为 9 维 logits,需经 softmax 得概率,阈值 argmax;本仓使用确定性合成图像验证(seed 42 随机图 + gradient 图),batch 2
路由计算机视觉 图像分类(验收 logits/probabilities/label、argmax 一致性与任务完成度)

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,numpy 1.26.4,Pillow,safetensors
  • 依赖见 requirements.txt

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件

权重已缓存在 /tmp/fairface_final,inference.py 默认从本地加载(LOCAL_CANDIDATES 优先);缺失时自动尝试从 HuggingFace 拉取(约 343 MB,需 HF_HUB_ENABLE_HF_TRANSFER=1 加速)。

NPU 推理

inference.py 为自包含脚本,默认使用确定性合成图像(batch 2:seed 42 随机图 + gradient 图)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png),计时前后均调用 torch.npu.synchronize(),通过 copy.deepcopy 保持 CPU/NPU 权重一致,输出为 logits + probs + pred。

# 权重已缓存在本地(离线,推荐)
python inference.py

# 指定本地权重目录
MODEL_DIR=/path/to/dima806-fairface_age_image_detection python inference.py

设备选择固定为 npu:0(可用时),输入输出均校验 device 为 npu:0,无 CPU fallback。

真实结果(Ascend 910B 单卡,npu:0)

以确定性合成数据 batch 2(seed 42 随机 224×224 + gradient 224×224)为例:

  • 输入:pixel_values shape (2,3,224,224) float32,mean -0.004 std 0.527 min -1.0 max 1.0;sample pixel [0,0,0:2,0:2] [[-0.20, -0.25],[0.22, -0.63]]
  • CPU 基线:logits sample0 [-0.464, -0.069, -0.113, 0.043, 0.013, 0.205, 0.047, 0.023, -0.148] pred 5 (40-49);sample1 [0.015, -0.022, -0.148, 0.130, -0.224, -0.126, 0.080, -0.110, 0.153] pred 8 (more than 70);probs 均约 0.07-0.14 分布
  • NPU 输出:相同输入在 npu:0 上 logits sample0 [-0.463, -0.070, -0.113, 0.044, 0.014, 0.204, 0.047, 0.023, -0.148] pred 5;sample1 [0.016, -0.023, -0.148, 0.130, -0.223, -0.125, 0.080, -0.110, 0.152] pred 8;数值与 CPU 接近,误差在 float32 容差内,argmax 完全一致
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback,参数 device 全为 npu:0,输入输出 device 均为 npu:0

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy)、相同输入(processor batch 2 固定)、相同预处理(ViTImageProcessor 同参)、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始 logits,用 scripts/compare_outputs.py 比较:

python scripts/compare_outputs.py --cpu /tmp/fairface_cpu.npy --npu /tmp/fairface_npu.npy --task classification --atol 5e-3 --rtol 5e-3
  • logits:shape (2,9) 一致,max_abs 1.47e-03 mean_abs 6.69e-04 在 atol=5e-3 rtol=5e-3 内 PASS(float32 ViT 卷积与注意力在 NPU 上的微小数值差异)
  • argmax:0.0-1.0 完全一致 1.0,两样本预测 label 不变(5 40-49 与 8 more than 70)
  • 阈值:atol=5e-3 rtol=5e-3(float32 模型,ViT 12层 GELU + LayerNorm,误差远低于阈值且保证分类不变)

单批次一致性为 smoke consistency,非完整 FairFace 10k 精度评测;原始仓库报告 accuracy 0.589 仅作参考。

性能

至少预热 3 次、正式测试 10 次,计时前后调用 torch.npu.synchronize(),首轮编译单独报告:

  • 预热:3 次(首轮 237 ms 含首次编译,后续 ~6 ms)
  • 测试:10 次,报告 avg/min/max/p50/p90/p95、batch 2、shape [3,224,224]、dtype float32、throughput
  • 结果:avg 5.97 ms min 5.88 max 6.13 p50 5.95 p90 6.06 p95 6.09 throughput 167.56 batches/s (335.12 images/s),batch 2
  • 吞吐:images/s = batch/avg_time(含 processor + model forward,不含磁盘)
  • 显存:权重 343 MB + 激活,峰值约 1-2 GB(64GB 单卡充足,npu-smi baseline 3114 MB + 模型)

详见 model_result.png。

证据图

三张 PNG 由 xterm.js 根据本次真实日志生成(scripts/render_xterm_evidence.mjs --style raw),固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png:侦察、环境检查、模型下载、加载、NPU 推理、一致性与提交校验的真实工作流日志
  • assets/npu_device_call.png:npu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的真实完整输出,含 schema、真实预测、同步耗时与状态

限制

  • 当前仅验证 batch 2 确定性合成图 smoke test,未覆盖真实 FairFace 数据集全量评测与多 batch/多分辨率
  • 模型为 float32,NPU 上存在 1e-3 量级数值差异属预期,未启用量化或 fp16 优化
  • 受单卡显存与编译时间限制,更大 batch(>32)或动态 shape 未测试
  • 权重来源为 checkpoint-4688/model.safetensors 与根 config.json 组合,已验证与原始仓库配置一致;若上游更新 revision 需重新校验