Phi-3-Vision-128K-Instruct 是一款轻量级、最先进的开放式多模态模型,其构建基础包括合成数据和经过筛选的公开网站数据,重点关注文本和视觉领域高质量、推理密集型数据。该模型属于 Phi-3 模型家族,其多模态版本支持 128K 上下文长度(以 tokens 为单位)。模型经过严格的增强流程,融合了监督微调与直接偏好优化技术,以确保精准遵循指令并具备稳健的安全措施。
资源与技术文档:
| 短上下文 | 长上下文 | |
|---|---|---|
| Mini | 4K [HF] ; [ONNX] ; [GGUF] | 128K [HF] ; [ONNX] |
| Small | 8K [HF] ; [ONNX] | 128K [HF] ; [ONNX] |
| Medium | 4K [HF] ; [ONNX] | 128K [HF] ; [ONNX] |
| Vision | 128K [HF] |
主要使用场景
该模型旨在面向英语的广泛商业和研究用途。它适用于具备视觉和文本输入能力的通用人工智能系统及应用,尤其适合以下场景:
我们的模型旨在加速高效语言模型和多模态模型的研究,并作为生成式人工智能驱动功能的基础组件。
使用场景考量
我们的模型并非针对所有下游用途进行专门设计或评估。开发人员在选择使用场景时,应考虑语言模型的常见局限性,并在特定下游用途中使用之前,对准确性、安全性和公平性进行评估和缓解,尤其是在高风险场景中。 开发人员应了解并遵守与其使用场景相关的适用法律或法规(包括隐私、贸易合规法律等)。
本模型卡片中的任何内容均不应被解释为或视为对模型发布所依据的许可的限制或修改。
Phi-3-Vision-128K-Instruct 已集成到 transformers 的开发版本(4.40.2)中。在通过 pip 发布官方版本之前,请确保执行以下操作之一:
加载模型时,确保将 trust_remote_code=True 作为 from_pretrained() 函数的参数传入。
将本地 transformers 更新至开发版本:pip uninstall -y transformers && pip install git+https://github.com/huggingface/transformers。此命令是克隆并从源代码安装的替代方法。
可通过 pip list | grep transformers 验证当前 transformers 版本。
所需软件包示例:
flash_attn==2.5.8
numpy==1.24.4
Pillow==10.3.0
Requests==2.31.0
torch==2.3.0
torchvision==0.18.0
transformers==4.40.2Phi-3-Vision-128K-Instruct 也可在 Azure AI Studio 中获取。
鉴于训练数据的特性,Phi-3-Vision-128K-Instruct 模型最适合处理单张图像输入,并使用如下对话格式的提示词。 你可以按照以下通用模板,将提示词与单张图像一同提供:
<|user|>\n<|image_1|>\n{prompt}<|end|>\n<|assistant|>\n 模型在 <|assistant|> 之后生成文本。在多轮对话的情况下,提示词可按以下格式设置:
<|user|>\n<|image_1|>\n{prompt_1}<|end|>\n<|assistant|>\n{response_1}<|end|>\n<|user|>\n{prompt_2}<|end|>\n<|assistant|>\n 以下代码片段展示了如何快速在 GPU 上运行模型:
from PIL import Image
import requests
from transformers import AutoModelForCausalLM
from transformers import AutoProcessor
model_id = "microsoft/Phi-3-vision-128k-instruct"
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="cuda", trust_remote_code=True, torch_dtype="auto")
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
messages = [
{"role": "user", "content": "<|image_1|>\nWhat is shown in this image?"},
{"role": "assistant", "content": "The chart displays the percentage of respondents who agree with various statements about their preparedness for meetings. It shows five categories: 'Having clear and pre-defined goals for meetings', 'Knowing where to find the information I need for a meeting', 'Understanding my exact role and responsibilities when I'm invited', 'Having tools to manage admin tasks like note-taking or summarization', and 'Having more focus time to sufficiently prepare for meetings'. Each category has an associated bar indicating the level of agreement, measured on a scale from 0% to 100%."},
{"role": "user", "content": "Provide insightful questions to spark discussion."}
]
url = "https://assets-c4akfrf5b4d3f4b7.z01.azurefd.net/assets/2024/04/BMDataViz_661fb89f3845e.png"
image = Image.open(requests.get(url, stream=True).raw)
prompt = processor.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(prompt, [image], return_tensors="pt").to("cuda:0")
generation_args = {
"max_new_tokens": 500,
"temperature": 0.0,
"do_sample": False,
}
generate_ids = model.generate(**inputs, eos_token_id=processor.tokenizer.eos_token_id, **generation_args)
# remove input tokens
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
response = processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print(response) 其他基础示例可在此处获取。
与其他模型类似,Phi 系列模型可能会表现出不公平、不可靠或冒犯性的行为。需要注意的一些局限性行为包括:
开发人员应应用负责任的 AI 最佳实践,并负责确保特定用例符合相关法律法规(例如隐私、贸易等)。需要考虑的重要领域包括:
我们的训练数据来源广泛,主要包括以下几类:
数据收集过程涉及从公开可用文档中获取信息,并采用细致的方法过滤掉不良文档和图像。为保护隐私,我们仔细筛选了各种图像和文本数据源,以从训练数据中移除或清理任何潜在的个人数据。
更多详细信息可参见 Phi-3 技术报告。
为了解模型能力,我们使用内部基准测试平台,在各种零样本基准测试中对 Phi-3-Vision-128K-Instruct 与一系列模型进行了比较。
| 基准测试 | Phi-3 Vision-128K-In | LlaVA-1.6 Vicuna-7B | QWEN-VL Chat | Llama3-Llava-Next-8B | Claude-3 Haiku | Gemini 1.0 Pro V | GPT-4V-Turbo |
|---|---|---|---|---|---|---|---|
| MMMU | 40.4 | 34.2 | 39.0 | 36.4 | 40.7 | 42.0 | 55.5 |
| MMBench | 80.5 | 76.3 | 75.8 | 79.4 | 62.4 | 80.0 | 86.1 |
| ScienceQA | 90.8 | 70.6 | 67.2 | 73.7 | 72.0 | 79.7 | 75.7 |
| MathVista | 44.5 | 31.5 | 29.4 | 34.8 | 33.2 | 35.0 | 47.5 |
| InterGPS | 38.1 | 20.5 | 22.3 | 24.6 | 32.1 | 28.6 | 41.0 |
| AI2D | 76.7 | 63.1 | 59.8 | 66.9 | 60.3 | 62.8 | 74.7 |
| ChartQA | 81.4 | 55.0 | 50.9 | 65.8 | 59.3 | 58.0 | 62.3 |
| TextVQA | 70.9 | 64.6 | 59.4 | 55.7 | 62.7 | 64.7 | 68.1 |
| POPE | 85.8 | 87.2 | 82.6 | 87.0 | 74.4 | 84.2 | 83.7 |
请注意,默认情况下,Phi-3-Vision-128K模型使用Flash Attention,这需要特定类型的GPU硬件才能运行。我们已在以下GPU类型上进行了测试:
要在这些环境中启用模型,您可以考虑按照以下步骤操作:
步骤1:在modeling_phi3_v.py文件中,注释掉第52行至第56行的Flash Attention导入代码。
# if is_flash_attn_2_available():
# from flash_attn import flash_attn_func, flash_attn_varlen_func
# from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
# _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)步骤 2:在 config.json 中将 _"attn_implementation" 从 "flash_attention_2" 更改为 "eager",或者在创建模型时按以下方式禁用 flash attention。
model = AutoModelForCausalLM.from_pretrained('microsoft/Phi-3-vision-128k-instruct', device_map="cuda", trust_remote_code=True, torch_dtype="auto", _attn_implementation="eager")本模型根据 MIT 许可协议 进行许可。
本项目可能包含项目、产品或服务的商标或徽标。Microsoft 商标或徽标的授权使用受 Microsoft 商标与品牌指南 约束,且必须遵循该指南。在本项目的修改版本中使用 Microsoft 商标或徽标不得造成混淆或暗示 Microsoft 的赞助。任何第三方商标或徽标的使用均受该第三方政策的约束。