Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer
Paper
•
1701.06538
•
Published
•
7
MiniMind2-MoE 是一个基于混合专家(Mixture of Experts, MoE)架构的轻量级中文对话语言模型。该模型采用了4个路由专家 + 1个共享专家的MoE设计,在保持较小参数规模的同时,通过动态专家选择机制实现了更强的表达能力。
本模型由 MiniMind 项目训练产出,经过了完整的预训练和监督微调(SFT)流程。
架构类型: MiniMindForCausalLM (MoE)
总参数量: 149.13M
├─ 基础参数: 16.4M
└─ MoE专家: 132.7M (89%)
MoE配置:
├─ 路由专家数: 4
├─ 共享专家数: 1
└─ 每token激活专家数: 2
基础架构:
├─ 隐藏层维度: 640
├─ 中间层维度: 1728
├─ 注意力头数: 8
├─ KV注意力头数: 2 (GQA)
├─ 隐藏层数: 8
├─ 词表大小: 6400
├─ 最大序列长度: 32768
├─ 位置编码: RoPE (theta=1000000.0)
├─ 激活函数: SiLU
└─ 精度: float16
Python >= 3.8
torch >= 2.0.0
transformers >= 4.35.0
pip install torch transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# 加载模型和分词器
model_path = "./MiniMind2"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True # 重要:MoE架构需要自定义代码
)
# 准备对话
messages = [
{"role": "user", "content": "请介绍一下混合专家(MoE)模型的优势"}
]
# 应用对话模板
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# 编码输入
inputs = tokenizer(text, return_tensors="pt").to(model.device)
# 生成回复
outputs = model.generate(
inputs["input_ids"],
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
do_sample=True,
repetition_penalty=1.0
)
# 解码输出
response = tokenizer.decode(outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=True)
print(response)
不同的生成参数会影响模型输出的质量和多样性:
| 参数 | 推荐值 | 说明 |
|---|---|---|
temperature |
0.7-1.0 | 控制生成的随机性,越高越随机 |
top_p |
0.9-0.95 | 核采样参数,控制输出多样性 |
top_k |
40-50 | 限制每步采样的词汇数量 |
repetition_penalty |
1.0-1.2 | 惩罚重复内容 |
max_new_tokens |
256-1024 | 最大生成长度 |
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float32,
device_map="cpu",
trust_remote_code=True
)
from transformers import BitsAndBytesConfig
# 8-bit量化
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
quantization_config=quantization_config,
device_map="auto",
trust_remote_code=True
)
作为一个轻量级模型(149M参数),MiniMind2-MoE在以下方面存在局限:
建议在实际应用中根据具体需求进行评估和测试。
本模型使用 ChatML 格式进行对话:
<|im_start|>user
你好,请介绍一下自己<|im_end|>
<|im_start|>assistant
你好!我是一个AI语言助手...<|im_end|>
使用 tokenizer.apply_chat_template() 会自动处理格式转换。
每个Transformer层包含:
├─ Self-Attention (标准)
└─ MoE FFN
├─ 路由专家 1 (gate_proj, up_proj, down_proj)
├─ 路由专家 2
├─ 路由专家 3
├─ 路由专家 4
├─ 共享专家 1 (始终激活)
└─ 路由网络 (选择top-2专家)
总参数: 149.13M
├─ Embeddings: 4.1M × 2 = 8.2M
├─ 每层参数: 17.66M
│ ├─ Attention: 1.12M
│ └─ MoE FFN: 16.54M
│ ├─ 4个路由专家: 3.32M × 4 = 13.27M
│ ├─ 1个共享专家: 3.32M
│ └─ 路由网络: 2.6K
└─ 8层总计: 141.28M
如果您使用了本模型,请引用 MiniMind 项目:
@misc{minimind2025,
title={MiniMind: 从零训练的轻量级语言模型},
author={Jingyao Gong},
year={2025},
howpublished={\url{https://github.com/jingyaogong/minimind}},
}
本模型遵循 Apache 2.0 许可证。
感谢 MiniMind 项目及其贡献者,感谢开源社区的支持!
完整MoE架构 · 149M参数 · 中文优化