Mistral AI发布首个开源MoE模型,魔搭社区推理微调最佳实践来啦!

本文主要是介绍Mistral AI发布首个开源MoE模型,魔搭社区推理微调最佳实践来啦!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文:Mistral AI发布首个开源MoE模型,魔搭社区推理微调最佳实践来啦! - 知乎

导读

继Mistral 7B 后,Mistral AI 近日又放出一记大招——发布了引爆开源社区的首个 MoE 开源模型 Mixtral 8x7B,在 Apache 2.0 许可证下可商用。Mixtral-8x7B 是一款混合专家模型(Mixtrue of Experts),由8个拥有70亿参数的专家网络组成,这种结构不仅提高了模型处理信息的效率,还降低了运行成本。

在能力上,Mixtral-8x7B 支持 32k token 上下文长度,支持英语、法语、意大利语、德语和西班牙语,拥有优秀的代码生成能力,可微调为指令跟随模型(Mixtral 8x7B Instruct,已同步开源),在 MT-Bench 上达到 8.3 分,达到了可媲美GPT3.5的水平。

Mixtral-8x7B 在大多数Benchmarks中表现

与 Llama2 70B 和 GPT3.5相当,甚至部分项上更优于二者

Mixtral 拥有46.7B的总参数量,但每个token只使用 12.9B参数,也就是说,Mixtral的实际执行速度和所需的成本和一个12.9B的模型相当。下图展示了官方公布的模型生成质量与推理消耗成本的关系,与Llama 2相比,Mistral 7B和Mixtral 8x7B表现出自己高能效的优势。

目前魔搭社区已经支持 Mixtral-8x7BMixtral-8x7B-Instruct 的下载、推理、微调一站式体验,并提供对应最佳实践教程,欢迎感兴趣的开发者小伙伴们来玩

环境配置与安装

  1. python 3.8及以上版本
  2. pytorch 1.12及以上版本,推荐2.0及以上版本
  3. 建议使用CUDA 11.4及以上
  4. transformers>=4.36.0

本文主要演示的模型为 Mixtral-8x7B-v0.1 和 Mixtral-8x7B-Instruct-v0.1 两个MoE模型。这两个模型参数量大概是47B左右。半径度训练和推理均需要两张A100,或同等显存(约90G~120G显存)。

模型链接和下载

Mixtral-MoE系列模型现已在ModelScope社区开源,包括:

Mixtral-8x7B-v0.1模型:

https://www.modelscope.cn/models/AI-ModelScope/Mixtral-8x7B-v0.1/summary

Mixtral-8x7B-Instruct-v0.1模型:

https://www.modelscope.cn/models/AI-ModelScope/Mixtral-8x7B-Instruct-v0.1/summary

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

from modelscope import snapshot_download
model_dir1 = snapshot_download("AI-ModelScope/Mixtral-8x7B-v0.1", revision = "master")
model_dir2 = snapshot_download("AI-ModelScope/Mixtral-8x7B-Instruct-v0.1", revision = "master")

值得一提的是,魔搭社区同步上线了Mistral-7B-Instruct-v0.2的新模型:

https://www.modelscope.cn/models/AI-ModelScope/Mistral-7B-Instruct-v0.2/summary

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

from modelscope import snapshot_download
model_dir1 = snapshot_download("AI-ModelScope/Mistral-7B-Instruct-v0.2", revision = "master")

Mixtral模型推理

Mixtral-8x7B-v0.1推理代码:

from modelscope import AutoModelForCausalLM, AutoTokenizer
import torchmodel_id = "AI-ModelScope/Mixtral-8x7B-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.float16)text = "Hello my name is"
inputs = tokenizer(text, return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Mixtral-8x7B-Instruct-v0.1推理代码:

from modelscope import AutoModelForCausalLM, AutoTokenizer
import torchmodel_id = "AI-ModelScope/Mixtral-8x7B-Instruct-v0.1"
tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.float16)text = "Hello my name is"
inputs = tokenizer(text, return_tensors="pt")outputs = model.generate(**inputs, max_new_tokens=20)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

资源消耗:

Mixtral模型微调和微调后推流

微调代码开源地址:

https://github.com/modelscope/swift/tree/main/examples/pytorch/llm

clone swift仓库并安装SWIFT(魔搭官方提供的训练推理框架)

# 设置pip全局镜像和安装相关的python包
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
git clone https://github.com/modelscope/swift.git
cd swift
pip install .[llm]
# 下面的脚本需要在此目录下执行
cd examples/pytorch/llm

模型微调脚本

由于模型尺寸较大,因此我们支持了基于LoRA的训练,精度使用了半精度。

  1. Mixtral-8x7B-v0.1模型
# Experimental environment: 2 * A100
# 2 * 50GB GPU memory
PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0,1 \
python llm_sft.py \--model_id_or_path AI-ModelScope/Mixtral-8x7B-v0.1 \--model_revision master \--sft_type lora \--tuner_backend swift \--dtype AUTO \--output_dir output \--ddp_backend nccl \--dataset dureader-robust-zh \--train_dataset_sample -1 \--num_train_epochs 2 \--max_length 512 \--check_dataset_strategy warning \--lora_rank 8 \--lora_alpha 32 \--lora_dropout_p 0.05 \--lora_target_modules ALL \--batch_size 1 \--weight_decay 0.01 \--learning_rate 1e-4 \--gradient_accumulation_steps 16 \--max_grad_norm 0.5 \--warmup_ratio 0.03 \--eval_steps 300 \--save_steps 300 \--save_total_limit 2 \--logging_steps 10 \--only_save_model true \--gradient_checkpointing false

  1. Mixtral-8x7B-Instruct-v0.1模型
# Experimental environment: 2 * A100
# 2 * 65GB GPU memory
PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0,1 \
python llm_sft.py \--model_id_or_path AI-ModelScope/Mixtral-8x7B-Instruct-v0.1 \--model_revision master \--sft_type lora \--tuner_backend swift \--dtype AUTO \--output_dir output \--ddp_backend nccl \--dataset dureader-robust-zh \--train_dataset_sample -1 \--num_train_epochs 2 \--max_length 2048 \--check_dataset_strategy warning \--lora_rank 8 \--lora_alpha 32 \--lora_dropout_p 0.05 \--lora_target_modules ALL \--batch_size 1 \--weight_decay 0.01 \--learning_rate 1e-4 \--gradient_accumulation_steps 16 \--max_grad_norm 0.5 \--warmup_ratio 0.03 \--eval_steps 300 \--save_steps 300 \--save_total_limit 2 \--logging_steps 10 \--only_save_model true \--gradient_checkpointing false

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

--custom_train_dataset_path /path/to/local/train/file
--custom_val_dataset_path /path/to/local/val/file

数据集格式请参考:

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

# Experimental environment: A100
# 2 * 45GB GPU memory
PYTHONPATH=../../.. \
CUDA_VISIBLE_DEVICES=0,1 \
python llm_infer.py \--ckpt_dir "output/mistral-7b-moe/vx_xxx/checkpoint-xxx" \--load_args_from_ckpt_dir true \--eval_human false \--max_length 4096 \--max_new_tokens 2048 \--temperature 0.1 \--top_p 0.7 \--repetition_penalty 1.05 \--do_sample true \--merge_lora_and_save false \

微调的可视化结果

训练损失:

评估损失

训练后生成样例

[INFO:swift] Setting args.verbose: True
[PROMPT]<s> [INST] <<SYS>>
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
<</SYS>>Task: Question Generation
Context: 爬行垫根据中间材料的不同可以分为:XPE爬行垫、EPE爬行垫、EVA爬行垫、PVC爬行垫;其中XPE爬行垫、EPE爬行垫都属于PE材料加保鲜膜复合而成,都是无异味的环保材料,但是XPE爬行垫是品质较好的爬行垫,韩国进口爬行垫都是这种爬行垫,而EPE爬行垫是国内厂家为了减低成本,使用EPE(珍珠棉)作为原料生产的一款爬行垫,该材料弹性差,易碎,开孔发泡防水性弱。EVA爬行垫、PVC爬行垫是用EVA或PVC作为原材料与保鲜膜复合的而成的爬行垫,或者把图案转印在原材料上,这两款爬行垫通常有异味,如果是图案转印的爬行垫,油墨外露容易脱落。当时我儿子爬的时候,我们也买了垫子,但是始终有味。最后就没用了,铺的就的薄毯子让他爬。
Answer: XPE
Question:  [/INST][OUTPUT]什么材质的爬行垫好</s>[LABELS]爬行垫什么材质的好
--------------------------------------------------
[PROMPT]<s> [INST] <<SYS>>
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
<</SYS>>Task: Question Generation
Context: 真实情况是160-162。她平时谎报的168是因为不离脚穿高水台恨天高(15厘米) 图1她穿着高水台恨天高和刘亦菲一样高,(刘亦菲对外报身高172)范冰冰礼服下厚厚的高水台暴露了她的心机,对比一下两者的鞋子吧 图2 穿着高水台恨天高才和刘德华谢霆锋持平,如果她真的有168,那么加上鞋高,刘和谢都要有180?明显是不可能的。所以刘德华对外报的身高174减去10-15厘米才是范冰冰的真实身高 图3,范冰冰有一次脱鞋上场,这个最说明问题了,看看她的身体比例吧。还有目测一下她手上鞋子的鞋跟有多高多厚吧,至少超过10厘米。
Answer: 160-162
Question:  [/INST][OUTPUT]范冰冰身高</s>[LABELS]范冰冰多高真实身高
--------------------------------------------------
[PROMPT]<s> [INST] <<SYS>>
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
<</SYS>>Task: Question Generation
Context: 防水作为目前高端手机的标配,特别是苹果也支持防水之后,国产大多数高端旗舰手机都已经支持防水。虽然我们真的不会故意把手机放入水中,但是有了防水之后,用户心里会多一重安全感。那么近日最为火热的小米6防水吗?小米6的防水级别又是多少呢? 小编查询了很多资料发现,小米6确实是防水的,但是为了保持低调,同时为了不被别人说防水等级不够,很多资料都没有标注小米是否防水。根据评测资料显示,小米6是支持IP68级的防水,是绝对能够满足日常生活中的防水需求的。
Answer: IP68级
Question:  [/INST][OUTPUT]小米6防水等级</s>[LABELS]小米6防水等级

资源消耗:

2 * 46G

点击链接直达模型:Mixtral-8x7B-v0.1 · 模型库 (modelscope.cn)

这篇关于Mistral AI发布首个开源MoE模型,魔搭社区推理微调最佳实践来啦!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/580743

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

虚拟机Centos7安装MySQL数据库实践

《虚拟机Centos7安装MySQL数据库实践》用户分享在虚拟机安装MySQL的全过程及常见问题解决方案,包括处理GPG密钥、修改密码策略、配置远程访问权限及防火墙设置,最终通过关闭防火墙和停止Net... 目录安装mysql数据库下载wget命令下载MySQL安装包安装MySQL安装MySQL服务安装完成