🚀 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 许可证,详情请见 许可证链接。