Phi-3-Medium-128K-Instruct 是一款拥有 140 亿参数的轻量级前沿开放模型,它基于 Phi-3 数据集训练而成。该数据集包含合成数据以及经过筛选的公开网站数据,重点突出高质量和推理密集型特性。
该模型隶属于 Phi-3 系列,其中 Medium 版本包含两个变体:4k 和 128K,这代表了模型所能支持的上下文长度(以 tokens 为单位)。
模型经过了后期训练流程,其中融合了监督式微调与直接偏好优化,旨在提升指令遵循能力和安全保障措施。在针对常识、语言理解、数学、代码、长上下文及逻辑推理等方面的基准测试中,Phi-3-Medium-128K-Instruct 在同规模及更高级别模型中均展现出稳健且领先的性能。
资源与技术文档:
| 短上下文 | 长上下文 | |
|---|---|---|
| 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] ; [ONNX] |
主要使用场景
该模型旨在广泛应用于英语环境下的商业和研究用途。它适用于需要满足以下条件的通用人工智能系统和应用:
我们的模型旨在加速语言和多模态模型的研究,用作生成式人工智能驱动功能的基础组件。
使用场景考量
我们的模型并非专门为所有下游用途设计或评估。开发人员在选择使用场景时应考虑语言模型的常见局限性,并在特定下游场景中使用前,针对准确性、安全性和公平性进行评估和缓解,尤其是在高风险场景中。开发人员应了解并遵守与其使用场景相关的适用法律法规(包括隐私、贸易合规法律等)。
本模型卡片中的任何内容均不应被解释为或视为对模型发布所依据的许可的限制或修改。
Phi-3-Medium-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。前面的命令是克隆并从源代码安装的替代方法。
当前的 transformers 版本可以通过以下命令验证:pip list | grep transformers。
Phi-3-Medium-128k-Instruct 也可在 Azure AI Studio 中使用。
Phi-3-Medium-128k-Instruct 支持的词汇量最多为 32064 个标记。分词器文件 已提供占位符标记,可用于下游微调,但也可以扩展到模型的词汇量上限。
考虑到训练数据的特性,Phi-3-Medium-128k-Instruct模型最适合使用如下聊天格式的提示词。 您可以按照以下通用模板将提示词作为问题提供:
<|user|>\nQuestion <|end|>\n<|assistant|>例如:
<|user|>
How to explain Internet for a medieval knight?<|end|>
<|assistant|>其中模型会在 <|assistant|> 之后生成文本。对于少样本提示,可按以下格式设置提示:
<|user|>
I am going to Paris, what should I see?<|end|>
<|assistant|>
Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris:\n\n1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city.\n2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa.\n3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows.\n\nThese are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world."<|end|>
<|user|>
What is so great about #1?<|end|>
<|assistant|>以下代码片段展示了如何快速在 GPU 上运行模型:
from openmind import AutoModelForCausalLM,AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="zhouhui/Phi-3-medium-128k-instruct",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map=device, trust_remote_code=True)
messages = [
{"role": "user", "content": "推荐5个合肥的景点。"},
]
model_inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to(device)
model_outputs = model.generate(
model_inputs,
max_new_tokens=1024,
top_p=0.7,
temperature=0.7
)
output_token_ids = [
model_outputs[i][len(model_inputs[i]):] for i in range(len(model_inputs))
]
responses = tokenizer.batch_decode(output_token_ids, skip_special_tokens=True)[0]
print(responses)
if __name__ == "__main__":
main()部分应用程序/框架可能不会在对话开头包含 BOS 令牌(<s>)。请确保包含此令牌,因为它能提供更可靠的结果。
与其他语言模型一样,Phi 系列模型可能会表现出不公平、不可靠或冒犯性的行为。需要注意的一些局限性行为包括:
开发人员应应用负责任的 AI 最佳实践,并负责确保特定用例符合相关法律法规(例如隐私、贸易等)。需要考虑的重要方面包括:
我们的训练数据来源广泛多样,总计 4.8 万亿 tokens(包括 10% 的多语言数据),由以下部分组合而成:
我们专注于可能提升模型推理能力的数据质量,并且对公开可用文档进行筛选,以确保其包含适当水平的知识。例如,某一天英超联赛的比赛结果可能是前沿模型的良好训练数据,但对于小尺寸模型,我们需要移除此类信息,以便为推理保留更多的模型容量。有关数据的更多详细信息,请参见 Phi-3 技术报告。
我们报告了Phi-3-Medium-128k-Instruct在标准开源基准测试中的表现,这些基准测试用于衡量模型的推理能力(包括常识推理和逻辑推理)。我们将其与Mixtral-8x22b、Gemini-Pro、Command R+ 104B、Llama-3-70B-Instruct、GPT-3.5-Turbo-1106以及GPT-4-Turbo-1106(Chat)进行了对比。
所有报告的数值均通过完全相同的流程生成,以确保结果的可比性。由于评估过程中的细微差异,这些数值可能与其他已发布的结果有所不同。
按照当前标准,我们使用少样本提示(few-shot prompts)对模型进行评估,温度设定为0。这些提示和样本数量是微软内部语言模型评估工具的一部分,我们并未针对Phi-3对评估流程进行任何优化。具体而言,我们没有修改提示、选择不同的少样本示例、更改提示格式或进行任何其他形式的模型优化。
每个基准测试的k-shot示例数量会单独列出。
| 基准测试 | Phi-3-Medium-128k-Instruct 14b | Command R+ 104B | Mixtral 8x22B | Llama-3-70B-Instruct | GPT3.5-Turbo version 1106 | Gemini Pro | GPT-4-Turbo version 1106 (Chat) |
|---|---|---|---|---|---|---|---|
| AGI Eval 5-shot | 49.7 | 50.1 | 54.0 | 56.9 | 48.4 | 49.0 | 59.6 |
| MMLU 5-shot | 76.6 | 73.8 | 76.2 | 80.2 | 71.4 | 66.7 | 84.0 |
| BigBench Hard 3-shot | 77.9 | 74.1 | 81.8 | 80.4 | 68.3 | 75.6 | 87.7 |
| ANLI 7-shot | 57.3 | 63.4 | 65.2 | 68.3 | 58.1 | 64.2 | 71.7 |
| HellaSwag 5-shot | 81.6 | 78.0 | 79.0 | 82.6 | 78.8 | 76.2 | 88.3 |
| ARC Challenge 10-shot | 91.0 | 86.9 | 91.3 | 93.0 | 87.4 | 88.3 | 95.6 |
| ARC Easy 10-shot | 97.6 | 95.7 | 96.9 | 98.2 | 96.3 | 96.1 | 98.8 |
| BoolQ 2-shot | 86.5 | 86.1 | 82.7 | 89.1 | 79.1 | 86.4 | 91.3 |
| CommonsenseQA 10-shot | 82.2 | 82.0 | 82.0 | 84.4 | 79.6 | 81.8 | 86.7 |
| MedQA 2-shot | 67.6 | 59.2 | 67.9 | 78.5 | 63.4 | 58.2 | 83.7 |
| OpenBookQA 10-shot | 87.2 | 86.8 | 88.6 | 91.8 | 86.0 | 86.4 | 93.4 |
| PIQA 5-shot | 87.8 | 86.4 | 85.0 | 85.3 | 86.6 | 86.2 | 90.1 |
| Social IQA 5-shot | 79.0 | 75.3 | 78.2 | 81.1 | 68.3 | 75.4 | 81.7 |
| TruthfulQA (MC2) 10-shot | 74.3 | 57.8 | 67.4 | 81.9 | 67.7 | 72.6 | 85.2 |
| WinoGrande 5-shot | 78.9 | 77.0 | 75.3 | 83.3 | 68.8 | 72.2 | 86.7 |
| TriviaQA 5-shot | 73.9 | 82.8 | 84.5 | 78.5 | 85.8 | 80.2 | 73.3 |
| GSM8K Chain of Thought 8-shot | 87.5 | 78.3 | 83.8 | 93.5 | 78.1 | 80.4 | 94.2 |
| HumanEval 0-shot | 58.5 | 61.6 | 39.6 | 78.7 | 62.2 | 64.4 | 79.9 |
| MBPP 3-shot | 73.8 | 68.9 | 70.7 | 81.3 | 77.8 | 73.2 | 86.7 |
| Average | 77.3 | 75.0 | 76.3 | 82.5 | 74.3 | 75.4 | 85.2 |
我们在下表中深入分析了80个公开基准数据集的不同类别:
| 基准测试类别 | Phi-3-Medium-128k-Instruct 14b | Command R+ 104B | Mixtral 8x22B | Llama-3-70B-Instruct | GPT3.5-Turbo version 1106 | Gemini Pro | GPT-4-Turbo version 1106 (Chat) |
|---|---|---|---|---|---|---|---|
| 热门综合基准 | 72.3 | 69.9 | 73.4 | 76.3 | 67.0 | 67.5 | 80.5 |
| 推理能力 | 83.2 | 79.3 | 81.5 | 86.7 | 78.3 | 80.4 | 89.3 |
| 语言理解 | 75.3 | 75.7 | 78.7 | 77.9 | 70.4 | 75.3 | 81.6 |
| 代码生成 | 64.2 | 68.6 | 60.0 | 69.3 | 70.4 | 66.7 | 76.1 |
| 数学 | 52.9 | 45.3 | 52.5 | 59.7 | 52.8 | 50.9 | 67.1 |
| 事实知识 | 47.5 | 60.3 | 60.6 | 52.4 | 63.4 | 54.6 | 45.9 |
| 多语言 | 62.2 | 67.8 | 69.8 | 62.0 | 67.0 | 73.4 | 78.2 |
| 鲁棒性 | 70.2 | 57.9 | 65.5 | 78.7 | 69.3 | 69.7 | 84.6 |
请注意,Phi-3-Medium 模型默认使用 flash attention,这需要特定类型的 GPU 硬件才能运行。我们已在以下 GPU 类型上进行了测试:
如果您希望在以下设备上运行模型:
ONNX Runtime 生态系统现已支持 Phi-3 Medium 模型在各种平台和硬件上运行。 经过优化的 Phi-3 模型也以 ONNX 格式在此发布,可配合 ONNX Runtime 在 CPU 和 GPU 上跨设备运行,包括服务器平台、Windows、Linux 和 Mac 桌面以及移动 CPU,并为每个目标平台提供最适合的精度。DirectML GPU 加速支持 Windows 桌面 GPU(AMD、Intel 和 NVIDIA)。 除了 DML,ONNX Runtime 还为 Phi-3 Medium 提供跨平台支持,覆盖从 CPU、GPU 到移动设备的多种设备。 以下是我们添加的一些优化配置:
该模型根据 MIT 许可证 授权。
本项目可能包含项目、产品或服务的商标或徽标。Microsoft 商标或徽标的授权使用受 Microsoft 商标与品牌指南 约束,并且必须遵循该指南。在本项目的修改版本中使用 Microsoft 商标或徽标不得引起混淆或暗示 Microsoft 的赞助。任何第三方商标或徽标的使用均受这些第三方政策的约束。