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

MistralAI发布了首个开源混合专家模型Mixtral8x7B,由8个70亿参数专家网络组成,支持多种语言并具备优秀性能。魔搭社区提供下载、推理和微调一站式体验,以及微调教程,展示了模型的高效能和灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文: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 torch

model_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 torch

model_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)

<think>好的,我现在需要回答用户关于开源LLM模型的问题。首先,用户之前问了如何部署Langchain-Chatchat,现在转向询问有哪些开源的LLM模型。这可能意味着用户对构建自己的聊天应用感兴趣,或者想了解可用的模型选项。 首先,我需要确定用户的需求。他们可能想了解目前主流的开源大语言模型,以便选择适合自己项目的模型。考虑到用户之前的问题涉及部署,他们可能对模型的易用性、硬件要求和中文支持特别关注。 接下来,我需要回忆当前知名的开源LLM模型。例如,Meta的Llama系列,特别是Llama 2和最新的Llama 3,这些都是非常重要的模型,拥有多种尺寸,适合不同计算资源。然后是中国的模型,比如ChatGLM系列(如ChatGLM3-6B),由智谱AI开发,支持中英双语,这对中文用户来说很关键。还有百川智能的Baichuan系列,以及零一万物的Yi模型,这些也都是中文社区中比较流行的。 另外,像Falcon、Mistral和MosaicML的MPT系列也是国际上的热门选择。需要提到它们的参数规模、适用场景以及许可证情况,因为有些模型虽然开源,但商用可能需要授权,比如Llama 2需要Meta的许可。 用户可能还关心模型的下载渠道,比如Hugging Face、ModelScope等平台,以及如何快速部署这些模型。同时,需要注意模型的硬件需求,比如显存大小,是否需要GPU加速等,这对用户部署时的硬件准备很重要。 此外,可能还需要提到一些微调框架,比如LLaMA-Factory,方便用户根据自己的需求对模型进行调整。最后,强调选择模型时要考虑语言支持、硬件限制和许可协议,确保合规和适用性。 需要组织这些信息,分门别类,结构清晰。可能分为国际模型、中文模型、其他模型,每个类别下列举几个主要模型,介绍其特点、参数、许可证和适用场景。同时,给出参考链接,方便用户进一步查阅。 检查是否有遗漏的重要模型,比如最近发布模型,如Qwen(通义千问)的开源版本,或者DeepSeek等。确保信息更新及时,特别是版本号,如Llama 3是较新的发布,需要包含进去。 最后,用简洁明了的语言表达,避免技术术语过多,但保持专业性。确保每个要点的信息准确无误,特别是许可证和下载来源,避免误导用户。</think>以下是当前主流的开源大语言模型分类介绍(截至2024年5月最新信息): --- ### **一、国际主流开源模型** 1. **Llama 系列(Meta)** - **Llama 3**(最新) - 参数规模:8B/70B - 特点:支持8K上下文,多语言优化 - 许可证:需申请商用授权 - 下载:https://llama.meta.com - **Llama 2**(经典版) - 参数:7B/13B/70B - 适用场景:通用对话、推理任务 - 衍生版本:Chinese-Llama-2(中文优化) 2. **Mistral 系列** - **Mistral-7B** - 特点:7B参数超越13B模型性能 - 优势:内存效率高(适合消费级显卡) - 许可证:Apache 2.0 3. **Falcon 系列(TII UAE)** - **Falcon-40B/180B** - 特色:基于Apache 2.0完全开源 - 适用场景:商业应用、多轮对话 --- ### **二、中文优化模型** | 模型名称 | 参数规模 | 亮点特性 | 推荐场景 | |------------------|----------|------------------------------|-----------------------| | **ChatGLM3-6B** | 6B | 中英双语、微调工具链完善 | 企业知识库问答 | | **Baichuan2** | 7B/13B | 支持法律/医疗垂直领域 | 专业领域对话 | | **Qwen-7B** | 7B | 阿里研发,支持插件扩展 | 复杂任务处理 | | **Yi-34B** | 34B | 全球首个200K上下文窗口 | 长文档理解 | | **DeepSeek-MoE** | 16B | 稀疏架构(显存节省40%) | 资源受限环境 | --- ### **三、其他重要模型** 1. **MPT 系列(MosaicML)** - 特色:支持8K+长上下文 - 推荐版本:MPT-30B(商用友好) 2. **Bloom(BigScience)** - 参数:176B - 特点:多语言支持(46种语言) - 局限:需要高性能硬件 3. **StarCoder(代码专用)** - 参数:15.5B - 专长:代码生成/补全 - 许可证:商用需授权 --- ### **四、模型获取渠道** 1. **Hugging Face Hub** ```python from huggingface_hub import snapshot_download snapshot_download(repo_id="meta-llama/Llama-2-7b-chat") ``` 2. **ModelScope(国内镜像)** ```bash git clone https://www.modelscope.cn/ZhipuAI/chatglm3-6b.git ``` 3. **官方Git仓库** - Baichuan:https://github.com/baichuan-inc - ChatGLM:https://github.com/THUDM --- ### **五、选型建议** 1. **资源优先型**: - 低显存(8GB):选择ChatGLM3-6B量化版 - 中等显存(24GB):Mistral-7B 2. **中文任务优先**: - 通用场景:Qwen-7B - 长文本处理:Yi-34B 3. **商业应用**: - 合规首选:Falcon-40B(Apache 2.0) - 高性能需求:Llama3-70B(需授权) --- ### **重要提醒** 1. 使用Llama系列需注意商用授权限制 2. 中文模型推荐搭配RAG技术增强效果 3. 最新趋势:MoE架构模型(如DeepSeek-MoE)显存效率提升显著 建议通过LLaMA-Factory等微调框架进行二次开发:https://github.com/hiyouga/LLaMA-Factory
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值