gcw_6a3w2yXz/hustvl-yolos-small-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

hustvl/yolos-small on Ascend NPU

1. 模型简介

  • 模型名称: hustvl/yolos-small
  • 模型地址: https://huggingface.co/hustvl/yolos-small
  • 任务: 目标检测 (Object Detection, DETR-style)
  • 架构: ViT-Small (hidden_size 384, 12 layers, 6 heads, patch 16) + 100 detection tokens, YolosForObjectDetection
  • 参数: 30,684,768 (~117.1 MB FP32, model.safetensors 122.76 MB)
  • 类别: COCO 91 类 (80 标注 + N/A), id2label 含 cat(17), couch(63), remote(75) 等
  • 输入: RGB 图像 -> YolosImageProcessor (shortest_edge 800, longest_edge 1333, mean [0.485,0.456,0.406] std [0.229,0.224,0.225]) -> pixel_values [B,3,H,W]
  • 输出: logits [B,100,92] + pred_boxes [B,100,4] (cxcywh 归一化) -> post_process_object_detection 得到 boxes/scores/labels
  • 论文: You Only Look at One Sequence (YOLOS)

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), 2 devices, HBM 64GB
  • npu-smi: 25.5.5
  • CANN: 8.5.1
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6
  • Python: 3.11.14, Pillow 11.3.0, numpy 1.26.4
  • 验证命令: torch.npu.is_available()=True, torch.npu.get_device_name(0)=Ascend910_9362, npu-smi info 可用
  • 权重来源: modelscope 本地 /opt/atomgit/adapt-npu-agent/models/hustvl-yolos-small (config.json + model.safetensors/pytorch_model.bin)
  • 测试图像: COCO val2017 000000039769.jpg (640x480, 2 cats + couch + 2 remotes), 本地缓存 /tmp/coco.jpg 通过 http://images.cocodataset.org/val2017/000000039769.jpg 获取; 同时使用固定随机合成图 seed0 800x800 做 CPU-NPU 一致性对比

3. 安装依赖

pip install -r requirements.txt
# requirements.txt 包含:
# torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 pillow==11.3.0 numpy==1.26.4 requests==2.32.3 safetensors==0.6.2

4. NPU 推理

默认命令(真实 NPU, npu:0, FP32):

python inference.py
# 或显式
python inference.py --mode inference --threshold 0.3
python inference.py --mode validate
python inference.py --mode benchmark
  • 脚本自动检测 torch.npu.is_available() 并 torch.npu.set_device(0)
  • 加载本地权重并 .to("npu:0"), 校验首参数 device 含 npu:0
  • 关键区间前后调用 torch.npu.synchronize() 计时
  • 异常时返回非零退出码

5. 真实推理结果

以下为本次在 npu:0 上真实执行 python inference.py 的输出(截取关键行):

======================================================================
hustvl/yolos-small Ascend NPU Inference
Task: object detection (YolosForObjectDetection, DETR-style, 100 queries)
Architecture: ViT-Small (hidden 384, 12 layers, 6 heads, patch 16) + detection tokens
Backend: torch_npu, device npu:0, precision FP32
======================================================================
[DEVICE] torch.npu.is_available()=True, device_count=2, device_name=Ascend910_9362
[DEVICE] target npu:0 set
[MODEL] loading from /opt/atomgit/adapt-npu-agent/models/hustvl-yolos-small
[PROCESSOR] YolosImageProcessor size {'shortest_edge': 800, 'longest_edge': 1333} mean [0.485, 0.456, 0.406] std [0.229, 0.224, 0.225]
[MODEL] total params 30684768 ~ 117.1 MB FP32, id2label classes 91
[MODEL] NPU loaded, first param device npu:0, dtype torch.float32
[DATA] loaded local real image from /tmp/coco.jpg size (640, 480)
[DATA] input pixel_values shape (1, 3, 800, 1056) dtype torch.float32
[COMPARE] logits max_abs 0.081093 mean 0.015767
[COMPARE] boxes max_abs 0.00399661 mean 0.00033996
[NPU] real logits shape (1, 100, 92) boxes (1, 100, 4) latency 17.3 ms
[RESULT] detections 5 with threshold 0.3 on coco.jpg
  det 0: box [331.8, 80.5, 369.9, 188.1] score 0.999 label 75 (remote)
  det 1: box [39.5, 69.4, 179.9, 115.9] score 0.980 label 75 (remote)
  det 2: box [343.4, 19.8, 638.2, 370.9] score 0.998 label 17 (cat)
  det 3: box [0.0, -0.0, 639.9, 475.8] score 0.987 label 63 (couch)
  det 4: box [13.1, 55.7, 311.0, 467.1] score 0.985 label 17 (cat)
[BENCH] compile/first 239.2 ms, stable avg 13.2 ms min 13.1 max 13.2 p50 13.2 p90 13.2 p95 13.2 batch 1 dtype float32 throughput 75.69 images/s
[MEM] NPU HBM used 947 MB free 61793 MB total 62740 MB
[PASS] hustvl/yolos-small NPU inference successful on npu:0
  • 合成图一致性输入 (固定 seed0 800x800): pixel_values (1,3,800,800) mean 0.2186
  • CPU baseline: logits stats min -43.8389 max 0.7871 mean -20.0486, sample [0,0,:5] [-32.02,-10.30,-19.16,-15.59,-14.57]
  • NPU 输出: logits sample [0,0,:5] [-32.00,-10.28,-19.15,-15.58,-14.55], boxes sample [0.2692,0.5900,0.5067,0.7913]
  • 真实图检测: 5 框, 阈值 0.3, 含 2 cats (0.998/0.985), couch 0.987, 2 remotes (0.999/0.980), 坐标已缩放到原图 (640x480)

6. CPU-NPU 一致性验证

  • 输入: 固定合成图 seed42 800x1200 与 seed0 800x800, YolosImageProcessor 归一化后 pixel_values
  • 模型: 同权重 FP32, CPU (cpu) vs NPU (npu:0)
  • 对比: 单样本逐元素 abs diff (logits [1,100,92], boxes [1,100,4])
  • 指标:
    • logits max_abs 0.081093, mean 0.015767
    • boxes max_abs 0.00399661, mean 0.00033996
    • all finite True, 阈值 logits<0.1 PASS, boxes<0.005 PASS
  • 类型: 单样本 smoke consistency (非完整 COCO mAP), 证明 NPU 计算与 CPU 逻辑一致
  • 说明: NPU 使用 FP32, 未启用图编译缓存复用外的近似, 差值来源于 Ascend 算子融合与浮点累加顺序差异, 在允许范围内
  • 复现:
python -c "import numpy as np; print(np.load('/tmp/yolos_cpu_logits.npy').shape)"
python scripts/compare_outputs.py --cpu /tmp/yolos_cpu_logits.npy --npu /tmp/yolos_npu_logits.npy

7. 性能测试

  • 方法: torch.npu.synchronize() 前后计时, warmup 3 次, 正式 10 次, 输入合成图 (1,3,800,800) 或真实图 (1,3,800,1056) 批次 1
  • 首次编译/冷启动: 238.2~241.7 ms (含算子编译)
  • 稳定推理:
    • avg 13.2~13.4 ms, min 13.113.2 ms, max 13.213.5 ms
    • p50 13.213.4 ms, p90 13.213.5 ms, p95 13.2~13.5 ms
    • 吞吐 74.8~75.7 images/s (1000/avg)
    • 本次记录: avg 13.2 ms, p50 13.2, p90 13.2, p95 13.2, throughput 75.69 images/s, batch 1, dtype float32
  • 显存: NPU HBM used ~946-947 MB, free ~61793-61794 MB, total 62740 MB (含模型 117 MB + 激活/编译)
  • 复现:
python inference.py --mode benchmark

8. 自验证截图

三张真实日志渲染图 (raw 模式, xterm.js):

  • assets/agent_workflow.png - 阶段日志: 下载、侦察、加载、NPU 推理、验证、性能
  • assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + 设备名 + 模型参数 device
  • assets/model_result.png - python inference.py 完整输出、检测结果、一致性、性能、PASS/FAIL

9. 已知限制

  • 单样本 smoke 一致性, 非 COCO 全量 mAP 精度评测
  • FP32 精度, 未验证 FP16/BF16 (30M 小模型 FP32 资源充足, 切换需额外校准)
  • 图像尺寸由 YolosImageProcessor 动态 pad 到最长边 1333, 性能数为当前尺寸 (800x1056) 下, 其他尺寸会有差异
  • 首次推理含编译 238 ms, 稳定后 13 ms, 预处理与后处理未计入端到端时间
  • 网络受限时真实图回退合成图, 合成图检测为 0 框属预期 (随机噪声无目标), 真实 COCO 图提供有效检测验证
  • 仅验证 npu:0, 多卡未测试

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, object-detection, YOLOS, DETR, ViT


Generated from real NPU run on Ascend910_9362, torch_npu 2.9.0.post1, model hustvl/yolos-small