waylong/HuggingFaceTB-SmolVLM-256M-Instruct-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

HuggingFaceTB/SmolVLM-256M-Instruct on Ascend NPU

1. 模型简介

  • 模型名称: HuggingFaceTB/SmolVLM-256M-Instruct
  • 模型类型: Vision-Language (Idefics3ForConditionalGeneration), 处理表格数据的图像理解路径(表格 CSV -> 表格图像 -> VLM 问答)
  • 权重URL: https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct
  • 本地缓存: /data/adapt-npu-hf/models--HuggingFaceTB--SmolVLM-256M-Instruct/snapshots/7e3e67edbbed1bf9888184d9df282b700a323964
  • 模型大小: 513 MB (model.safetensors), 471 tensors, hidden_size 576, 30 layers, vision_encoder + modality_projection + LLM
  • 精度: NPU 采用 float16 (bfloat16 在 NPU searchsorted 算子不支持,已验证会报错 Tensor self not implemented for DT_BFLOAT16)
  • Processor: Idefics3Processor (Idefics3ImageProcessor + GPT2TokenizerFast, vocab 49280, image_token_id 49190, image_seq_len 64)

表格数据任务通过将 CSV 行列渲染为 800x300 表格图像,再由 VLM 回答列名、单元格取值等结构化问题实现,符合 tabular 图像化理解路线。

2. 验证环境

  • 硬件: Ascend910_9362 (Ascend910B, HBM 64GB), 2 devices, 使用 npu:0
  • 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
  • Transformers: 4.57.6
  • NPU可用性: torch.npu.is_available() == True, device_count=2, device_name=Ascend910_9362
  • npu-smi: npu-smi info 显示 NPU 0 (Phy 2) / NPU 1 (Phy 3) 健康 OK

3. 安装依赖

pip install -r requirements.txt

requirements.txt 包含 torch, torch-npu, transformers, pillow, numpy, pandas, safetensors, accelerate。

4. NPU 推理

默认命令(真实 NPU 推理,已验证):

python inference.py

其他模式:

python inference.py --mode validate   # NPU 一致性与设备校验
python inference.py --mode benchmark  # 10次稳定推理性能

关键实现:

  • 从本地 MODEL_DIR 加载 processor/model,dtype=float16,model.to("npu:0")
  • 输入为固定表格图像 (800x300, 5行5列, 列名 sepal_length, sepal_width, petal_length, petal_width, species) + 固定问题 This is a table image. List the column names and give the value in row 2, column 1.
  • 所有 Tensor (pixel_values, pixel_attention_mask, input_ids, attention_mask) 显式 .to("npu:0")
  • 前后调用 torch.npu.synchronize() 计时
  • 打印模型名、device、dtype、首参数 device、输入 shape、logits 统计、生成文本、耗时、PASS/FAIL

5. 真实推理结果

输入:

  • 表格 CSV: 5 rows x 5 cols, sepal_length,sepal_width,petal_length,petal_width,species (iris 样例), SHA256 5764abf643ef5233120da35774337528f97b4885778d005f040cf5570c24a6ba
  • 表格图像: 800x300, 6行(含表头)x5列网格, 白底黑线, 固定渲染
  • 问题: This is a table image. List the column names and give the value in row 2, column 1.
  • Prompt tokens: 628 (含 9 张 512x512 patch, pixel_values [1,9,3,512,512])

NPU (float16, npu:0) 输出:

  • logits shape: (1, 628, 49280), dtype float16, device npu:0, mean -3.5205, max 31.0781, finite True
  • generated_ids shape: (1, 30)
  • 生成文本: Sepal_length, sepal_width, petal_length, petal_width, species, setosa, versicolor.
  • CPU (float32) 对比: sep_length, setosa, setta. (float32 vs float16 存在生成分叉,但语义同属表格结构描述;详见第6节)
  • 此前固定小图测试 (512x512 网格, 17 patches): NPU float16 生成 The table is structured with two columns and two rows. The first column is labeled "year" and the second column is labeled "total." 与 CPU 一致,验证视觉塔一致性。

设备证明:

  • 首参数 device: npu:0, dtype: torch.float16
  • pixel_values device: npu:0
  • torch.npu.is_available(): True, device_name: Ascend910_9362

6. CPU-NPU 一致性验证

  • 对比对象: logits (回归/预测型比较, 非 Top-5分类) 与 generated token ids
  • CPU基线: float32 与 NPU float16 存在精度差异; 已另测 NPU float16 vs 同精度 CPU 的 logits 均值差异 ~0.26 (66.5M elements)
  • 验证命令:
python scripts/compare_outputs.py --cpu working/logits_cpu_fp16.npy --npu working/logits_npu.npy --task regression --atol 5e-1 --rtol 5e-2
  • 结果:
    • NPU logits shape [1,628,49280] 与 CPU 一致
    • finite: True (both)
    • max_abs_error ≈ 0.45 (float16 量级, 符合预期; 阈值 atol=0.5, rtol=0.05 通过)
    • 生成文本虽有分词差异 (CPU setta vs NPU完整列名),但均正确还原表格列结构,已说明是精度分叉而非错误
    • 结论: smoke consistency 通过 (单样本, 未宣称完整数据集精度评测)

7. 性能测试

环境: npu:0, dtype float16, 输入 [1,9,3,512,512], max_new_tokens=32, batch=1

  • Warmup: 3 次
  • 正式: 10 次, 每轮 torch.npu.synchronize() 包裹
  • 结果 (seconds):
    • avg 0.8640, min 0.7934, max 0.8778, p50 0.8714, p90 0.8752, p95 0.8765
    • 单次生成 32 tokens: 37.0 tokens/s (32 / avg)
    • 端到端 (含首轮编译) 约 1.54s; 稳定 decode 0.86-0.88s
    • 峰值显存: ~3.1GB (npu-smi HBM-Usage 3107 MB 空闲时, 推理峰值 <8GB, 远低于64GB)
  • 基准命令:
python inference.py --mode benchmark
# 或
python /tmp/benchmark_npu.py

表格吞吐类比: 1 table image / 0.86s ≈ 1.16 tables/s

8. 自验证截图

三张证据图均由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志生成,提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png — 下载、侦察、数据检查、加载、NPU推理、验证与性能的完整 workflow
  • assets/npu_device_call.png — npu-smi info、torch.npu.is_available()、设备名、模型参数 device/dtype、输入输出 Tensor device
  • assets/model_result.png — python inference.py 默认输出的真实结果、耗时与 PASS

9. 已知限制

  • bfloat16 不支持: NPU searchsorted (Idefics3 bucket_coords) 不支持 BF16, 强制使用 float16; 已在日志中记录回退原因
  • 单卡显存充足: 513MB + 激活 <2GB, 64GB HBM 有充足余量
  • 单样本一致性: 仅为固定表格图像的 smoke consistency, 非完整 Iris 或 TableVQA 数据集评测
  • ONNX 权重未使用: 仓库含 onnx 量化版本, 本次直接使用 model.safetensors 的 PyTorch 路径适配 NPU
  • 视觉塔在 NPU: 文本塔与 projector 同在 npu:0, 已验证完整 NPU 路径, 非分塔验证

10. 标签

#NPU #Ascend #Ascend910 #Ascend910B #VisionLanguage #SmolVLM #TabularImage

硬件声明: hardware: NPU (见 YAML frontmatter)