返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Phi-3:小模型,大未来!(附魔搭社区推理、微调实战教程)

[复制链接]
链载Ai 显示全部楼层 发表于 4 小时前 |阅读模式 打印 上一主题 下一主题
01

导读



近期, Microsoft 推出 Phi-3,这是 Microsoft 开发的一系列开放式 AI 模型。Phi-3 模型是一个功能强大、成本效益高的小语言模型(SLM),在各种语言、推理、编码和数学基准测试中,在同级别参数模型中性能表现优秀。为开发者构建生成式人工智能应用程序时提供了更多实用的选择。


从今天开始,Phi-3-mini(参数量3.8B)发布,可在魔搭社区上下载使用:

  • Phi-3-mini 有两种上下文长度变体- 4K 和 128K 令牌,支持128K 个令牌的上下文窗口的模型。


  • 支持指令微调,通过指令微调可以遵循反映人们正常沟通方式的不同类型的指令,保障了模型可以开箱即用。


  • 针对ONNX进行了优化,支持 GPU、CPU 甚至移动硬件的跨平台支持。


在未来几周内,Phi-3 系列将添加更多型号,为客户在质量成本曲线上提供更大的灵活性。Phi-3-small(7B)和Phi-3-medium(14B)很快就会提供。  


小尺寸的突破性性能

Phi-3 模型在关键基准测试中具有显著的优势(请参阅下面的基准数据,越高越好)。

注意:Phi-3 模型在事实知识基准(例如 TriviaQA)上的表现不佳,因为较小的模型大小会导致保留事实的能力较低。


02

模型链接和下载



Phi-3系列模型现已在ModelScope社区开源:

模型名称

模型链接

Phi-3-mini-128k-instruct

https://www.modelscope.cn/models/LLM-Research/Phi-3-mini-128k-instruct

Phi-3-mini-4k-instruct

https://modelscope.cn/models/LLM-Research/Phi-3-mini-4k-instruct

Phi-3-mini-4k-instruct-onnx

https://modelscope.cn/models/LLM-Research/Phi-3-mini-4k-instruct-onnx

Phi-3-mini-128k-instruct-onnx

https://modelscope.cn/models/LLM-Research/Phi-3-mini-128k-instruct-onnx


社区支持直接下载模型的repo:


frommodelscopeimportsnapshot_downloadmodel_dir=snapshot_download("LLM-Research/Phi-3-mini-128k-instruct")


03

模型体验



创空间体验链接:

https://modelscope.cn/studios/LLM-Research/Phi-3-mini-128k-instruct-demo


开脑洞问题:


四则运算:


中文问答会有比较多的一些重复回答:


04

模型推理



本文使用的模型为Phi-3-mini-128k-instruct 模型,在PAI-DSW运行(单卡A10)。


模型推理

import torchfrom modelscope import snapshot_downloadfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
torch.random.manual_seed(0)
model_dir = snapshot_download("LLM-Research/Phi-3-mini-128k-instruct")
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="cuda", torch_dtype="auto", trust_remote_code=True, )tokenizer = AutoTokenizer.from_pretrained(model_dir)
messages = [{"role": "system", "content": "You are a helpful digital assistant. Please provide safe, ethical and accurate information to the user."},{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},{"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},{"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},]
pipe = pipeline("text-generation",model=model,tokenizer=tokenizer,)
generation_args = {"max_new_tokens": 500,"return_full_text": False,"temperature": 0.0,"do_sample": False,}
output = pipe(messages, **generation_args)print(output[0]['generated_text'])


资源消耗:


ONNX格式模型推理

在魔搭社区的免费CPU算力体验ONNX模型推理:


gitclonehttps://www.modelscope.cn/LLM-Research/Phi-3-mini-4k-instruct-onnx.git

pipinstall--preonnxruntime-genaicurlhttps://raw.githubusercontent.com/microsoft/onnxruntime-genai/main/examples/python/model-qa.py-omodel-qa.py

pythonmodel-qa.py-mPhi-3-mini-4k-instruct-onnx/cpu_and_mobile/cpu-int4-rtn-block-32-l2048

05

模型微调和微调后推理



我们使用SWIFT来对模型进行微调,SWIFT是魔搭社区官方提供的LLM&AIGC模型微调推理框架.


微调代码开源地址:

https://github.com/modelscope/swift


环境准备:

gitclonehttps://github.com/modelscope/swift.gitcdswiftpipinstall-e.[all]


我们使用混合数据集来增强模型的中文能力和Agent能力


使用到的数据集有

- COIG-CQIA:

https://modelscope.cn/datasets/AI-ModelScope/COIG-CQIA/summary 该数据集包含了中国传统知识、豆瓣、弱智吧、知乎等中文互联网信息


-魔搭通用Agent训练数据集:

https://modelscope.cn/datasets/AI-ModelScope/ms-agent-for-agentfabric/summary


-alpaca-en:

https://modelscope.cn/datasets/AI-ModelScope/alpaca-gpt4-data-en/summary


-ms-bench魔搭通用中文问答数据集:

https://modelscope.cn/datasets/iic/ms_bench/summary


微调脚本:

LoRA+ddp

CUDA_VISIBLE_DEVICES=0,1,2,3NPROC_PER_NODE=4\swiftsft\--model_typephi3-4b-4k-instruct\--datasetms-agent-for-agentfabric-defaultalpaca-enms-benchms-agent-for-agentfabric-additioncoig-cqia-ruozhibacoig-cqia-zhihucoig-cqia-examcoig-cqia-chinese-traditionalcoig-cqia-logi-qacoig-cqia-segmentfaultcoig-cqia-wiki\--batch_size2\--max_length2048\--use_loss_scaletrue\--gradient_accumulation_steps16\--learning_rate5e-5\--use_flash_attntrue\--eval_steps500\--save_steps500\--train_dataset_sample-1\--dataset_test_ratio0.1\--val_dataset_sample10000\--num_train_epochs2\--check_dataset_strategynone\--gradient_checkpointingtrue\--weight_decay0.01\--warmup_ratio0.03\--save_total_limit2\--logging_steps10\--sft_typelora\--lora_target_modulesALL\--lora_rank8\--lora_alpha32


训练过程支持本地数据集,需要指定如下参数:

--custom_train_dataset_pathxxx.jsonl\--custom_val_dataset_pathyyy.jsonl\


自定义数据集的格式可以参考:

https://github.com/modelscope/swift/blob/main/docs/source/LLM/%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8E%E6%8B%93%E5%B1%95.md


微调后推理脚本:(这里的ckpt_dir需要修改为训练生成的checkpoint文件夹)

#Experimentalenvironment:A100CUDA_VISIBLE_DEVICES=0\swiftinfer\--ckpt_dir"/path/to/output/phi3-4b-4k-instruct/vx-xxx/checkpoint-xxx"\--load_dataset_configtrue\--max_new_tokens2048\--temperature0.1\--top_p0.7\--repetition_penalty1.\--do_sampletrue\--merge_lorafalse\

训练loss


我们可以部署训练后的模型与Modelscope-Agent联合使用,搭建一个可以调用API的LLMAgent


更详细的内容参考我们的官方文档https://github.com/modelscope/swift/blob/main/docs/source/LLM/Agent%E5%BE%AE%E8%B0%83%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.md#%E5%9C%A8%E5%91%BD%E4%BB%A4%E8%A1%8C%E4%B8%AD%E4%BD%BF%E7%94%A8agent


效果展示

模型部署后,在AgentFabric中体验Agent


存在的问题:

最终微调后的模型虽然具备调用API能力,但能力较弱,输入需要给出较强提示调用正确的API。


模型回复存在叠词现象,原模型同样存在这个问题,可能的原因是模型预训练的中文能力不足,需要更大的中文语料训练或者扩充词表来解决。


欢迎开发者来SWIFT尝试更多训练配置


资源占用

微调lora+ddp


部署




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ