SmolVLM-256M-Instruct 是 HuggingFaceTB 推出的轻量级视觉-语言模型(256M 参数),基于 Idefics3ForConditionalGeneration 架构,视觉侧采用 SigLIP(vision_config hidden_size 768, 12 layers, patch_size 16)编码图像,文本侧采用 Llama 结构(text_config hidden_size 576, 30 layers, 9 attention heads, 3 kv heads),在 Ascend 910 上端到端验证。
Idefics3ForConditionalGeneration (idefics3, image_token_id 49190, scale_factor 4, tie_word_embeddings)Idefics3Processor + Idefics3ImageProcessor (longest_edge 512 → tiled 17×512×512, image_seq_len 64)processor.apply_chat_template 构造 <|im_start|>User:<image>...generate(do_sample=False)),示例 Describe this image. → 绿色圆形图形描述model.safetensors 490M,vocab 49k,tokenizer.json 3.4Mnpu-smi 25.5.5npu:0 (torch.npu.is_available() == True, device_count=2)torch.float32, NPU 原生支持,CPU-NPU 逐 token 一致),torch.npu.synchronize() 计时pip install -r requirements.txt
# requirements: torch, torch_npu, transformers>=4.45.0, accelerate, safetensors, Pillow, numpy, huggingface_hub# 基础推理(合成图 + Describe)
python inference.py --model_path ./model --device npu:0 --prompt "Describe this image." --max_new_tokens 30
# 指定外部图像
python inference.py --model_path /path/to/SmolVLM-256M-Instruct --device npu:0 --image ./my.jpg --prompt "What objects are in this image?"
# 性能压测(warmup 2 + 5 runs)
python inference.py --model_path ./model --device npu:0 --bench --runs 5
# CPU 对比
python inference.py --model_path ./model --device cpu --prompt "Describe this image."关键适配:
accelerate 的 device_map,采用 from_pretrained(...).to(npu:0),兼容 Ascend NPUAutoProcessor 与 Idefics3ForConditionalGeneration 原生支持 NPU 张量,pixel_values/pixel_attention_mask/input_ids 显式 .to("npu:0")torch_npu.npu_geglu 无需 patch(SigLIP 使用 gelu_pytorch_tanh),float32 保证精度一致torch.npu.synchronize() 前后计时,消除异步误差Prompt: Describe this image. + 512×512 合成图 (17 tiles)
NPU (npu:0, float32, 2026-08-22):
[LOAD] pretrained=/opt/atomgit/adapt-npu-agent/jobs/48091153da384c5eb5cd8b2fc4d01bf2/515b8e729cb04819a3f4e6e0646ed04f/workspace/model device=npu:0 dtype=torch.float32
[DEVICE] model device: npu:0
[DEVICE] vision param sample model.vision_model.embeddings.patch_embedding.weight: npu:0
[DEVICE] language param sample model.text_model.embed_tokens.weight: npu:0
[DEVICE] NPU name: Ascend910_9362
[INPUT] pixel_values: torch.Size([1, 17, 3, 512, 512]) -> npu:0
[PERF] latency: 1.5438s
[OUTPUT] decoded: User:\n\n\n\n\nDescribe this image.\nAssistant: The image is a simple, colorful graphic consisting of a solid green circle in the center. The circle is a perfect circle, with no visible edges or
[STATS] pixel_values mean=-0.1595 std=0.3528 min=-1.0000 max=1.0000 shape=(1, 17, 3, 512, 512)第二 prompt What objects are in this image?:
Assistant: The image contains a single green circle, which is the primary focus of the image. The green circle is positioned in the center of the image, andCPU (cpu, float32, 同输入):
[DEVICE] model device: cpu
[PERF] latency: 87.4126s
[OUTPUT] decoded: User:\n\n\n\n\nDescribe this image.\nAssistant: The image is a simple, colorful graphic consisting of a solid green circle in the center. The circle is a perfect circle, with no visible edges or→ CPU-NPU 完全一致(30 new tokens 逐 token 相同,无精度损失)
do_sample=False + max_new_tokens=30The image is a simple, colorful graphic consisting of a solid green circle in the center...output_ids shape [1,1169] 一致processor.batch_decode(..., skip_special_tokens=True) 对比,pixel_values 统计量 NPU/CPU 同为 mean -0.1595 std 0.3528Describe this image. 30 tokenstorch.npu.synchronize() + time.perf_counter(),warmup 2 + 5 runsnpu-smi 显示 3109MB 占用assets/agent_workflow.png:环境检查 → 获取模型 → 分析模型 → NPU适配 → NPU验证 → 性能测试 全流程assets/npu_device_call.png:npu-smi info + torch.npu.is_available()==True + Ascend910_9362assets/model_result.png:NPU 推理日志 + CPU 对比 + benchmark 统计assets/test_image.png:512×512 合成测试图gelu_pytorch_tanh 精度max_new_tokens=30 小规模验证,长文本生成(>100 tokens)需重新压测trust_remote_code=True(idefics3 自定义代码),离线环境需预先下载 processor 配置Hardware: NPU, Ascend, Ascend910, VLM, Vision-Language, Idefics3, SigLIP