anning-2026/Phi-3.5-vision-instruct
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

microsoft/Phi-3.5-vision-instruct 昇腾 NPU 部署文档

1. 模型简介

模型名称: microsoft/Phi-3.5-vision-instruct 模型链接: https://huggingface.co/microsoft/Phi-3.5-vision-instruct 模型描述: 微软 Phi-3.5 系列的多模态视觉-语言模型(VLM),支持图文问答与纯文本问答:输入图片 + 文本提示(<|image_1|> 占位),输出自然语言回答。 模型架构: Phi-3.5 Vision(CLIP ViT-L vision encoder + Phi-3 语言模型 decoder,RoPE + 长文本 rope_scaling) 参数规模: ~8.3GB(bf16,2 个 safetensors 分片:4.94GB + 3.35GB) 适配引擎: torch_npu(昇腾 NPU 推理引擎)

2. 环境依赖

依赖项版本说明
torch2.9.0
torch_npu2.9.0.post1
transformers4.46.3官方配套版本(custom code 与 4.57.6 Cache API 不兼容,需降级或补丁)
accelerate>= 0.33模型加载
pillow>= 10.0图片读取
fastapi / uvicorn / python-multipart-服务化推理(server.py)

注:本模型走 transformers 加载路径(仓内自带 custom code:modeling_phi3_v.py / configuration_phi3_v.py / processing_phi3_v.py,需 trust_remote_code=True)。config.json 的 _attn_implementation 已改为 eager(本机无 flash-attn)。

3. 推理步骤

# 纯文本问答
python inference.py --prompt "What is 1+1? Explain it." --device npu:0

# 图文问答
python inference.py --image /tmp/examples/stop_sign.jpg --prompt "What is shown in this image?" --device npu:0

推理流程:AutoProcessor.from_pretrained(trust_remote_code=True) → AutoModelForCausalLM.from_pretrained(trust_remote_code=True, _attn_implementation='eager') → processor(prompt, image, return_tensors='pt').to('npu:0') → model.generate(...) → batch_decode。

4. 测试样例及输出结果

以下输出为在昇腾 Ascend 910(CANN 8.5.1 + torch_npu 2.9.0 + transformers 4.46.3)上实测获得。

样例 1:纯文本问答 命令:

python inference.py --prompt "What is 1+1? Explain it." --max_new_tokens 60 --device npu:0

输出:

[结果] 模型回复: 1+1 is equal to 2. This is a basic arithmetic operation known as addition, which combines two numbers to produce a sum. In this case, adding 1 and 1 together results in 2. [耗时] 2.40s(生成 60 max_new_tokens)

样例 2:图文问答(stop sign 图) 命令:

python inference.py --image /tmp/examples/stop_sign.jpg --prompt "What is shown in this image?" --max_new_tokens 60 --device npu:0

输出:

[结果] 模型回复: The image displays a street scene with a prominent red stop sign in the foreground, a Chinese temple with red and gold decorations in the background, and a black SUV parked on the side of the road. [耗时] 2.85s(生成 60 max_new_tokens)

结果说明: 纯文本与图文推理均正常输出,验证了 Phi-3.5-vision-instruct 在昇腾 NPU 上的 VLM 多模态推理链路通畅。

5. Agent 适配截图

Agent 适配流程 NPU 设备调用 模型适配结果

6. 服务化推理

python server.py --port 8000 --device npu:0

接口:

  • GET /health → {"status":"ok","model":"microsoft/Phi-3.5-vision-instruct","device":"npu:0","loaded":true}
  • POST /chat(JSON:{"prompt": "...", "max_new_tokens": 256})→ 纯文本问答
  • POST /chat_img(multipart:file 图片 + prompt 文本)→ 图文问答

实测(服务化):

  • /chat {"prompt":"What is 1+1?"} → {"response":"1+1 is equal to 2.","elapsed_s":0.83}
  • /chat_img(stop_sign.jpg)→ {"response":"The image displays a street scene with a prominent red stop sign...","elapsed_s":2.07}

7. 注意事项

  • 模型权重位于 /tmp/Phi-3.5-vision-instruct/(8.3GB:model-00001-of-00002.safetensors 4.94GB sha256 校验通过 + model-00002-of-00002.safetensors 3.35GB),避免占用有限的 /opt 空间,不纳入本仓库
  • transformers 版本适配——本机 4.57.6 的 Cache API 已移除 seen_tokens/get_max_length/get_usable_length,custom code(为 4.44-4.46 编写)会报错;部署用 transformers 4.46.3(官方配套),server.py 同时内置 monkey-patch 兼容两种版本
  • attention 实现——config _attn_implementation 改为 eager(flash_attention_2 需 CUDA/无 flash-attn);推理输出 expect numerical differences 警告属正常
  • 分片1 曾因下载 sha256 不匹配导致推理乱码("Details Details..."),修复后输出正常——大文件务必校验 sha256 与 LFS oid 一致