Instructions to use dokoomae/MiniCPM5-1B-CMoE-e8s2a2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dokoomae/MiniCPM5-1B-CMoE-e8s2a2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="dokoomae/MiniCPM5-1B-CMoE-e8s2a2") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("dokoomae/MiniCPM5-1B-CMoE-e8s2a2") model = AutoModelForCausalLM.from_pretrained("dokoomae/MiniCPM5-1B-CMoE-e8s2a2", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use dokoomae/MiniCPM5-1B-CMoE-e8s2a2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "dokoomae/MiniCPM5-1B-CMoE-e8s2a2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "dokoomae/MiniCPM5-1B-CMoE-e8s2a2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/dokoomae/MiniCPM5-1B-CMoE-e8s2a2
- SGLang
How to use dokoomae/MiniCPM5-1B-CMoE-e8s2a2 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "dokoomae/MiniCPM5-1B-CMoE-e8s2a2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "dokoomae/MiniCPM5-1B-CMoE-e8s2a2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "dokoomae/MiniCPM5-1B-CMoE-e8s2a2" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "dokoomae/MiniCPM5-1B-CMoE-e8s2a2", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use dokoomae/MiniCPM5-1B-CMoE-e8s2a2 with Docker Model Runner:
docker model run hf.co/dokoomae/MiniCPM5-1B-CMoE-e8s2a2
MiniCPM5-1B-CMoE-e8s2a2 (Carved MoE + LoRA 蒸馏)
基于 arXiv:2502.04416 CMoE 算法对 openbmb/MiniCPM5-1B 进行 Carved MoE 转换,并使用原模型作为 teacher 做了 LoRA 蒸馏矫正。
配置 — e8s2a2
| 符号 | 值 | 含义 |
|---|---|---|
| E | 8 | 总 expert 槽位数(含 shared) |
| S | 2 | Shared expert 子块数(合并为 1 个 shared MLP) |
| A | 2 | 每 token 激活的路由专家数 |
| K_act | 10 | 激活分析时每个 token 标记的 top-K 神经元 |
结构
- 路由专家(routed):6 个,每个
intermediate = 4608/8 = 576 - 共享专家(shared):1 个 MLP,
intermediate = 2 × 576 = 1152(取激活率 top-1152 的神经元) - 总参数量:1,080,890,880 vs 原始 1,080,632,832 — 比率为 1.0002×(基本不变) ✓
- 激活参数/token:(A + S) × 576 = 4 × 576 = 2304 < 原 4608 → 0.5× 计算量 ✓
这是 CMoE 区别于普通 upcycling 的核心:参数不变,激活减半,推理更快。
架构:标准 Qwen2MoeForCausalLM
使用 🤗 Transformers 原生的 Qwen2MoeForCausalLM,**trust_remote_code=True 不需要**。Qwen2Moe 原生支持路由专家 + 共享专家,且注意力与 Llama 兼容(GQA + RoPE + RMSNorm),所以所有注意力/norm/embed/lm_head 权重直接从 MiniCPM5-1B(LlamaForCausalLM)复制。
转换算法(忠实于 JarvisPei/CMoE)
每层 L:
- 激活分析:用 wikitext-2 校准数据(32 samples × 512 tokens = 16k tokens)跑前向。在 FFN 输入处计算归一化的中间值
h = silu(gate_proj(norm(x))) * up_proj(norm(x)),对每个 (batch, seq) 位置取 top-10 abs(h) 标记为激活。 - 激活率:
rates[i] = #(neuron i 在 top-10 中) / total_tokens - 共享专家选择:取激活率 top-1152 的神经元作为共享专家。
- K-means 分组(1 次迭代):剩余 3456 个神经元,以激活标记为特征做 K-means。用 scipy
linear_sum_assignment做平衡分配,确保每个路由专家恰好分到 576 个神经元。 - 专家权重构造:每个专家的
gate_proj/up_proj/down_proj是原 FFN 在对应神经元索引处的切片。 - Router(analytical,无需训练):对每个路由专家,取距离簇心最近的"代表神经元",用其
up_proj权重做 L2 归一化作为 router 权重。 shared_expert_gate初始化为 0(sigmoid(0)=0.5,半强度共享)。
LoRA 蒸馏矫正
由于 CMoE 论文明确指出 training-free 输出不可直接使用,需要 2k 样本 SFT 微调。本模型做了 LoRA 蒸馏:
- Teacher:原 MiniCPM5-1B(LlamaForCausalLM)
- Student:上述 analytical 构造出的 CMoE v4
- 数据:50 个英文 prompt × 原模型 24 token 续写 → 61 个序列、共 1530 token
- 训练目标:
- LoRA on
mlp.gate.weight(router),rank=16 - LoRA on
mlp.shared_expert_gate.weight,rank=16 - 每个路由专家一个
extra_scale标量(per CMoE 论文)
- LoRA on
- Loss:
KL(teacher || student) + 0.5 * CE(student, teacher_argmax) - 3 epochs,约 8.5 分钟(CPU)
- Loss 收敛:3.13 → 1.16(min)/ 2.03(final)
验证结果
生成质量对比
| Prompt | 原模型 | CMoE training-free (v4) | CMoE + LoRA (此模型) |
|---|---|---|---|
| "Hello, my name is" | ", I am a new user of the Python..." | ", ( ( ( (" | " a person, as a person. It is a name..." |
| "The capital of France is" | "Paris" | ", ( ( \n (" | " a term for the term of the term..." |
| "In machine learning, gradient descent" | "is used to minimize the cost function..." | ",,,,,,," | ", and a deep network, the world is a set of mathematical properties..." |
数值相似度(vs 原 MiniCPM5-1B,30 个 prompt 平均)
| 指标 | v2 (旧 router) | v4 (改进 router) | v4_final (此模型) |
|---|---|---|---|
| cos_sim(last token) | 0.49 | 0.76 | 0.47 |
| KL(teacher‖student) | 9.71 | 4.97 | 4.65 |
| Top1 预测匹配率 | 1.6% | 20.0% | 23.3% |
注:cos_sim 在 LoRA 后略降,但 top1 + KL 都改善,说明模型学到了更"尖锐"的分布(更自信)。
推理速度(CPU, bf16)
| 指标 | 原 Llama | CMoE v4_final | 比率 |
|---|---|---|---|
| Forward 延迟(seq=64) | 338 ms | 337 ms | 1.00×(持平) |
| Generate 吞吐 | 5.85 tok/s | 6.87 tok/s | 1.17× 更快 ✓ |
CPU 上推理速度比 dense 模型快 17%,因为每 token 只激活 2304/4608 = 50% 的 FFN。
使用方法
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# 无需 trust_remote_code!
model = AutoModelForCausalLM.from_pretrained(
"dokoomae/MiniCPM5-1B-CMoE-e8s2a2",
dtype=torch.bfloat16,
)
tok = AutoTokenizer.from_pretrained("dokoomae/MiniCPM5-1B-CMoE-e8s2a2")
inputs = tok("Hello, my name is", return_tensors="pt")
out = model.generate(**inputs, max_new_tokens=30, do_sample=False, pad_token_id=tok.pad_token_id)
print(tok.decode(out[0], skip_special_tokens=True))
已知限制
- CPU 训练受限:仅用 1530 tokens 校准(论文用 256k),LoRA 只跑 3 epochs(论文用更长时间)。GPU 上效果会显著更好。
- Router 简化:原 CMoE 用双 Linear(
gate + classifier),输出为|classifier(x) * silu(gate(x))|。Qwen2Moe 只支持单 Linear router。本模型用up_proj单 Linear 初始化,损失部分双调制效果。要 100% 保真需自定义 modeling 代码(会要trust_remote_code=True)。 extra_scales单独存储:由于 Qwen2Moe 不原生支持extra_scale,本模型将其与 LoRA deltas 合并到 router 权重中(粗略近似)。完整extra_scales.json也已保存供参考。- 生成质量:相比原模型仍有差距,建议在实际部署前继续在 GPU 上做更长的 SFT。
文件清单
config.json— Qwen2MoeConfig(无自定义代码)model.safetensors— 完整 bf16 权重(2.1 GB)extra_scales.json— 每 layer 的 6 维 extra_scale 向量tokenizer.json,tokenizer_config.json,special_tokens_map.json,chat_template.jinja— 直接复制自openbmb/MiniCPM5-1Bgeneration_config.json— 原始生成配置
引用
@article{pei2025cmoe,
title={CMoE: Fast Carving of Mixture-of-Experts for Efficient LLM Inference},
author={Pei, Zehua and Zou, Lancheng and Zhen, Hui-Ling and Yu, Xianzhi and Liu, Wulong and Pan, Sinno Jialin and Yuan, Mingxuan and Yu, Bei},
journal={arXiv preprint arXiv:2502.04416},
year={2025}
}
许可证
继承自 openbmb/MiniCPM5-1B,详见原模型卡。
- Downloads last month
- 29
Model tree for dokoomae/MiniCPM5-1B-CMoE-e8s2a2
Base model
openbmb/MiniCPM5-1B