原文:Mistral AI發(fā)布首個(gè)開源MoE模型,魔搭社區(qū)推理微調(diào)最佳實(shí)踐來啦! - 知乎
導(dǎo)讀
繼Mistral 7B 后,Mistral AI 近日又放出一記大招——發(fā)布了引爆開源社區(qū)的首個(gè) MoE 開源模型 Mixtral 8x7B,在 Apache 2.0 許可證下可商用。Mixtral-8x7B 是一款混合專家模型(Mixtrue of Experts),由8個(gè)擁有70億參數(shù)的專家網(wǎng)絡(luò)組成,這種結(jié)構(gòu)不僅提高了模型處理信息的效率,還降低了運(yùn)行成本。
在能力上,Mixtral-8x7B 支持 32k token 上下文長度,支持英語、法語、意大利語、德語和西班牙語,擁有優(yōu)秀的代碼生成能力,可微調(diào)為指令跟隨模型(Mixtral 8x7B Instruct,已同步開源),在 MT-Bench 上達(dá)到 8.3 分,達(dá)到了可媲美GPT3.5的水平。
Mixtral-8x7B?在大多數(shù)Benchmarks中表現(xiàn)
與 Llama2 70B 和 GPT3.5相當(dāng),甚至部分項(xiàng)上更優(yōu)于二者
Mixtral 擁有46.7B的總參數(shù)量,但每個(gè)token只使用 12.9B參數(shù),也就是說,Mixtral的實(shí)際執(zhí)行速度和所需的成本和一個(gè)12.9B的模型相當(dāng)。下圖展示了官方公布的模型生成質(zhì)量與推理消耗成本的關(guān)系,與Llama 2相比,Mistral 7B和Mixtral 8x7B表現(xiàn)出自己高能效的優(yōu)勢。
目前魔搭社區(qū)已經(jīng)支持?Mixtral-8x7B、Mixtral-8x7B-Instruct 的下載、推理、微調(diào)一站式體驗(yàn),并提供對應(yīng)最佳實(shí)踐教程,歡迎感興趣的開發(fā)者小伙伴們來玩!
環(huán)境配置與安裝
- python 3.8及以上版本
- pytorch 1.12及以上版本,推薦2.0及以上版本
- 建議使用CUDA 11.4及以上
- transformers>=4.36.0
本文主要演示的模型為?Mixtral-8x7B-v0.1?和?Mixtral-8x7B-Instruct-v0.1?兩個(gè)MoE模型。這兩個(gè)模型參數(shù)量大概是47B左右。半徑度訓(xùn)練和推理均需要兩張A100,或同等顯存(約90G~120G顯存)。
模型鏈接和下載
Mixtral-MoE系列模型現(xiàn)已在ModelScope社區(qū)開源,包括:
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
社區(qū)支持直接下載模型的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")
值得一提的是,魔搭社區(qū)同步上線了Mistral-7B-Instruct-v0.2的新模型:
https://www.modelscope.cn/models/AI-ModelScope/Mistral-7B-Instruct-v0.2/summary
社區(qū)支持直接下載模型的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模型微調(diào)和微調(diào)后推流
微調(diào)代碼開源地址:
https://github.com/modelscope/swift/tree/main/examples/pytorch/llm
clone swift倉庫并安裝SWIFT(魔搭官方提供的訓(xùn)練推理框架)
# 設(shè)置pip全局鏡像和安裝相關(guān)的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]
# 下面的腳本需要在此目錄下執(zhí)行
cd examples/pytorch/llm
模型微調(diào)腳本
由于模型尺寸較大,因此我們支持了基于LoRA的訓(xùn)練,精度使用了半精度。
- 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
- 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
訓(xùn)練過程也支持本地?cái)?shù)據(jù)集,需要指定如下參數(shù):
--custom_train_dataset_path /path/to/local/train/file
--custom_val_dataset_path /path/to/local/val/file
數(shù)據(jù)集格式請參考:
模型微調(diào)后的推理腳本,這里的ckpt_dir需要修改為訓(xùn)練生成的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 \
微調(diào)的可視化結(jié)果
訓(xùn)練損失:
評估損失
訓(xùn)練后生成樣例
[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: 爬行墊根據(jù)中間材料的不同可以分為:XPE爬行墊、EPE爬行墊、EVA爬行墊、PVC爬行墊;其中XPE爬行墊、EPE爬行墊都屬于PE材料加保鮮膜復(fù)合而成,都是無異味的環(huán)保材料,但是XPE爬行墊是品質(zhì)較好的爬行墊,韓國進(jìn)口爬行墊都是這種爬行墊,而EPE爬行墊是國內(nèi)廠家為了減低成本,使用EPE(珍珠棉)作為原料生產(chǎn)的一款爬行墊,該材料彈性差,易碎,開孔發(fā)泡防水性弱。EVA爬行墊、PVC爬行墊是用EVA或PVC作為原材料與保鮮膜復(fù)合的而成的爬行墊,或者把圖案轉(zhuǎn)印在原材料上,這兩款爬行墊通常有異味,如果是圖案轉(zhuǎn)印的爬行墊,油墨外露容易脫落。當(dāng)時(shí)我兒子爬的時(shí)候,我們也買了墊子,但是始終有味。最后就沒用了,鋪的就的薄毯子讓他爬。
Answer: XPE
Question: [/INST][OUTPUT]什么材質(zhì)的爬行墊好</s>
[LABELS]爬行墊什么材質(zhì)的好
--------------------------------------------------
[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: 真實(shí)情況是160-162。她平時(shí)謊報(bào)的168是因?yàn)椴浑x腳穿高水臺恨天高(15厘米) 圖1她穿著高水臺恨天高和劉亦菲一樣高,(劉亦菲對外報(bào)身高172)范冰冰禮服下厚厚的高水臺暴露了她的心機(jī),對比一下兩者的鞋子吧 圖2 穿著高水臺恨天高才和劉德華謝霆鋒持平,如果她真的有168,那么加上鞋高,劉和謝都要有180?明顯是不可能的。所以劉德華對外報(bào)的身高174減去10-15厘米才是范冰冰的真實(shí)身高 圖3,范冰冰有一次脫鞋上場,這個(gè)最說明問題了,看看她的身體比例吧。還有目測一下她手上鞋子的鞋跟有多高多厚吧,至少超過10厘米。
Answer: 160-162
Question: [/INST][OUTPUT]范冰冰身高</s>
[LABELS]范冰冰多高真實(shí)身高
--------------------------------------------------
[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: 防水作為目前高端手機(jī)的標(biāo)配,特別是蘋果也支持防水之后,國產(chǎn)大多數(shù)高端旗艦手機(jī)都已經(jīng)支持防水。雖然我們真的不會故意把手機(jī)放入水中,但是有了防水之后,用戶心里會多一重安全感。那么近日最為火熱的小米6防水嗎?小米6的防水級別又是多少呢? 小編查詢了很多資料發(fā)現(xiàn),小米6確實(shí)是防水的,但是為了保持低調(diào),同時(shí)為了不被別人說防水等級不夠,很多資料都沒有標(biāo)注小米是否防水。根據(jù)評測資料顯示,小米6是支持IP68級的防水,是絕對能夠滿足日常生活中的防水需求的。
Answer: IP68級
Question: [/INST][OUTPUT]小米6防水等級</s>
[LABELS]小米6防水等級
資源消耗:
2 * 46G
文章來源:http://www.zghlxwxcb.cn/news/detail-794608.html
點(diǎn)擊鏈接直達(dá)模型:Mixtral-8x7B-v0.1 · 模型庫 (modelscope.cn)文章來源地址http://www.zghlxwxcb.cn/news/detail-794608.html
到了這里,關(guān)于Mistral AI發(fā)布首個(gè)開源MoE模型,魔搭社區(qū)推理微調(diào)最佳實(shí)踐來啦!的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!