模型简介
模型特点
模型能力
使用案例
模型下载 | 评估结果 | 模型架构 | API平台 | 许可证 | 引用
DeepSeek-V2:强大、经济且高效的专家混合语言模型
1. 简介
上周,DeepSeek-V2的发布和热议引发了广泛对MLA(多头潜在注意力)的关注!社区中许多人建议开源一个较小的MoE模型以进行深入研究。现在,DeepSeek-V2-Lite正式亮相:
- 总参数量16B,激活参数量2.4B,使用5.7T tokens从头训练
- 在多项中英文基准测试中超越7B密集模型和16B MoE模型
- 可在单块40G GPU上部署,支持8x80G GPU进行微调
DeepSeek-V2是一款强大的专家混合(MoE)语言模型,具有经济高效的训练和推理特性。DeepSeek-V2采用了创新的架构设计,包括多头潜在注意力(MLA)和DeepSeekMoE。MLA通过将键值(KV)缓存显著压缩为潜在向量,确保高效的推理性能;而DeepSeekMoE则通过稀疏计算实现经济高效的强大模型训练。
2. 动态
- 2024.05.16:我们发布了DeepSeek-V2-Lite。
- 2024.05.06:我们发布了DeepSeek-V2。
3. 模型下载
DeepSeek-V2开源了两种规模的基座模型和对话模型:
模型 | 总参数量 | 激活参数量 | 上下文长度 | 下载链接 |
---|---|---|---|---|
DeepSeek-V2-Lite | 16B | 2.4B | 32k | ü§ó HuggingFace |
DeepSeek-V2-Lite-Chat (SFT) | 16B | 2.4B | 32k | ü§ó HuggingFace |
DeepSeek-V2 | 236B | 21B | 128k | ü§ó HuggingFace |
DeepSeek-V2-Chat (RL) | 236B | 21B | 128k | ü§ó HuggingFace |
由于HuggingFace平台限制,当前开源代码在GPU上运行时性能低于我们内部代码库。为优化模型运行效率,我们提供了专用的vllm解决方案。
4. 评估结果
基座模型
标准基准测试
测试集 | 领域 | DeepSeek 7B (密集) | DeepSeekMoE 16B | DeepSeek-V2-Lite (MoE-16B) |
---|---|---|---|---|
架构 | - | MHA+密集 | MHA+MoE | MLA+MoE |
MMLU | 英语 | 48.2 | 45.0 | 58.3 |
BBH | 英语 | 39.5 | 38.9 | 44.1 |
C-Eval | 中文 | 45.0 | 40.6 | 60.3 |
CMMLU | 中文 | 47.2 | 42.5 | 64.3 |
HumanEval | 代码 | 26.2 | 26.8 | 29.9 |
MBPP | 代码 | 39.0 | 39.2 | 43.2 |
GSM8K | 数学 | 17.4 | 18.8 | 41.1 |
数学 | 数学 | 3.3 | 4.3 | 17.1 |
对话模型
标准基准测试
测试集 | 领域 | DeepSeek 7B Chat (SFT) | DeepSeekMoE 16B Chat (SFT) | DeepSeek-V2-Lite 16B Chat (SFT) |
---|---|---|---|---|
MMLU | 英语 | 49.7 | 47.2 | 55.7 |
BBH | 英语 | 43.1 | 42.2 | 48.1 |
C-Eval | 中文 | 44.7 | 40.0 | 60.1 |
CMMLU | 中文 | 51.2 | 49.3 | 62.5 |
HumanEval | 代码 | 45.1 | 45.7 | 57.3 |
MBPP | 代码 | 39.0 | 46.2 | 45.8 |
GSM8K | 数学 | 62.6 | 62.2 | 72.0 |
数学 | 数学 | 14.7 | 15.2 | 27.9 |
5. 模型架构
DeepSeek-V2采用创新架构确保经济高效的训练和推理:
- 注意力机制:设计MLA(多头潜在注意力),通过低秩键值联合压缩消除推理时键值缓存的瓶颈,支持高效推理
- 前馈网络:采用DeepSeekMoE架构,这种高性能MoE架构能以更低成本训练更强模型
DeepSeek-V2-Lite包含27层,隐藏维度为2048。同样采用MLA架构,配备16个注意力头,每个头维度为128。其KV压缩维度为512,但与DeepSeek-V2略有不同,不压缩查询向量。对于解耦的查询和键,每个头维度为64。DeepSeek-V2-Lite也采用DeepSeekMoE,除第一层外所有FFN层均替换为MoE层。每个MoE层包含2个共享专家和64个路由专家,每个专家的中间隐藏维度为1408。在路由专家中,每个token激活6个专家。在此配置下,DeepSeek-V2-Lite总参数量为15.7B,每个token激活参数量为2.4B。
6. 训练细节
DeepSeek-V2-Lite同样基于DeepSeek-V2的预训练语料从头训练,未受任何SFT数据污染。使用AdamW优化器,超参数设置为$\beta_1=0.9$、$\beta_2=0.95$、$\mathrm{weight_decay}=0.1$。学习率采用warmup-and-step-decay策略调度:在前2K步从0线性增长至最大值;随后在训练约80% tokens后乘以0.316,90% tokens后再次乘以0.316。最大学习率设为$4.2 \times 10^{-4}$,梯度裁剪范数为1.0。未采用批量大小调度策略,保持恒定批量大小4608序列。预训练阶段最大序列长度设为4K,训练总tokens达5.7T。通过流水线并行将不同层部署在不同设备上,但每层的所有专家均部署在同一设备。因此仅采用小型专家级平衡损失($\alpha_{1}=0.001$),未使用设备级平衡损失和通信平衡损失。预训练完成后,同样进行了长上下文扩展、SFT处理,得到DeepSeek-V2-Lite Chat对话模型。
7. 本地运行指南
使用BF16格式运行DeepSeek-V2-Lite推理需要40GB*1 GPU。
使用Huggingface Transformers推理
可直接使用Huggingface Transformers进行模型推理。
文本补全
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
model_name = "deepseek-ai/DeepSeek-V2-Lite"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
text = "注意力函数可描述为将查询和一组键值对映射到输出的过程,其中查询、键、值和输出均为向量。输出是"
inputs = tokenizer(text, return_tensors="pt")
outputs = model.generate(**inputs.to(model.device), max_new_tokens=100)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(result)
对话补全
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
model_name = "deepseek-ai/DeepSeek-V2-Lite-Chat"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
messages = [
{"role": "user", "content": "用C++写一段快速排序代码"}
]
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
print(result)
完整对话模板可在Huggingface模型仓库的tokenizer_config.json
中查看。
示例对话模板如下:
<ÔΩúbegin‚ñÅof‚ñÅsentenceÔΩú>用户:{用户消息_1}
助手:{助手回复_1}<ÔΩúend‚ñÅof‚ñÅsentenceÔΩú>用户:{用户消息_2}
助手:
也可添加可选系统消息:
<ÔΩúbegin‚ñÅof‚ñÅsentenceÔΩú>{系统消息}
用户:{用户消息_1}
助手:{助手回复_1}<ÔΩúend‚ñÅof‚ñÅsentenceÔΩú>用户:{用户消息_2}
助手:
使用vLLM推理(推荐)
使用vLLM进行模型推理时,请将PR #4650合并至您的vLLM代码库:https://github.com/vllm-project/vllm/pull/4650。
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams
max_model_len, tp_size = 8192, 1
model_name = "deepseek-ai/DeepSeek-V2-Lite-Chat"
tokenizer = AutoTokenizer.from_pretrained(model_name)
llm = LLM(model=model_name, tensor_parallel_size=tp_size, max_model_len=max_model_len, trust_remote_code=True, enforce_eager=True)
sampling_params = SamplingParams(temperature=0.3, max_tokens=256, stop_token_ids=[tokenizer.eos_token_id])
messages_list = [
[{"role": "user", "content": "你是谁?"}],
[{"role": "user", "content": "将以下内容直接翻译成中文:DeepSeek-V2 adopts innovative architectures to guarantee economical training and efficient inference."}],
[{"role": "user", "content": "用C++写一段快速排序代码。"}],
]
prompt_token_ids = [tokenizer.apply_chat_template(messages, add_generation_prompt=True) for messages in messages_list]
outputs = llm.generate(prompt_token_ids=prompt_token_ids, sampling_params=sampling_params)
generated_text = [output.outputs[0].text for output in outputs]
print(generated_text)
LangChain支持
由于我们的API与OpenAI兼容,可轻松集成至langchain。 示例如下:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model='deepseek-chat',
openai_api_key=<您的deepseek-api密钥>,
openai_api_base='https://api.deepseek.com/v1',
temperature=0.85,
max_tokens=8000)
8. 许可证
本代码仓库遵循MIT许可证。DeepSeek-V2基座/对话模型的使用受模型许可证约束。DeepSeek-V2系列(包括基座和对话模型)支持商业用途。
9. 引用
@misc{deepseekv2,
title={DeepSeek-V2:强大、经济且高效的专家混合语言模型},
author={DeepSeek-AI},
year={2024},
eprint={2405.04434},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
10. 联系我们
如有任何疑问,请提交issue或发送邮件至service@deepseek.com。


