🚀 phixtral-2x2_8
phixtral-2x2_8 是首個基於兩個 microsoft/phi-2 模型構建的混合專家模型(MoE),其靈感源自 mistralai/Mixtral-8x7B-v0.1 架構。該模型的性能優於單個專家模型。
你可以通過這個 Space 來試用它。

🚀 快速開始
你可以通過這個 Colab 筆記本 在免費的 T4 GPU 上以 4 位精度運行 Phixtral。
!pip install -q --upgrade transformers einops accelerate bitsandbytes
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "phixtral-2x2_8"
instruction = '''
def print_prime(n):
"""
Print all primes between 1 and n
"""
'''
torch.set_default_device("cuda")
model = AutoModelForCausalLM.from_pretrained(
f"mlabonne/{model_name}",
torch_dtype="auto",
load_in_4bit=True,
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
f"mlabonne/{model_name}",
trust_remote_code=True
)
inputs = tokenizer(
instruction,
return_tensors="pt",
return_attention_mask=False
)
outputs = model.generate(**inputs, max_length=200)
text = tokenizer.batch_decode(outputs)[0]
print(text)
✨ 主要特性
🏆 評估
評估使用 LLM AutoEval 在 Nous 套件上進行。
你可以查看 YALL - Yet Another LLM Leaderboard 來將其與其他模型進行比較。
🧩 配置
該模型使用 mergekit 庫的自定義版本(mixtral 分支)和以下配置構建:
base_model: cognitivecomputations/dolphin-2_6-phi-2
gate_mode: cheap_embed
experts:
- source_model: cognitivecomputations/dolphin-2_6-phi-2
positive_prompts: [""]
- source_model: lxuechen/phi-2-dpo
positive_prompts: [""]
💻 使用示例
基礎用法
!pip install -q --upgrade transformers einops accelerate bitsandbytes
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "phixtral-2x2_8"
instruction = '''
def print_prime(n):
"""
Print all primes between 1 and n
"""
'''
torch.set_default_device("cuda")
model = AutoModelForCausalLM.from_pretrained(
f"mlabonne/{model_name}",
torch_dtype="auto",
load_in_4bit=True,
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
f"mlabonne/{model_name}",
trust_remote_code=True
)
inputs = tokenizer(
instruction,
return_tensors="pt",
return_attention_mask=False
)
outputs = model.generate(**inputs, max_length=200)
text = tokenizer.batch_decode(outputs)[0]
print(text)
高級用法
受 mistralai/Mixtral-8x7B-v0.1 啟發,你可以在 config.json
文件中指定 num_experts_per_tok
和 num_local_experts
(默認均為 2)。此配置會在 configuration.py
中自動加載。
vince62s 在 modeling_phi.py
文件中實現了 MoE 推理代碼。具體可查看 MoE 類。
🤝 致謝
特別感謝 vince62s 提供的推理代碼和專家數量的動態配置。他非常有耐心,幫助我調試了所有問題。
感謝 Charles Goddard 提供的 mergekit 庫以及 MoE for clowns 的實現。
感謝 ehartford 和 lxuechen 提供的微調後的 phi-2 模型。
📄 許可證
本項目採用 MIT 許可證,詳情請見 許可證鏈接。