1. 介绍
今天,我们介绍了DeepSeek-V2,这是一个强大的专家混合(MoE)语言模型,其特点是训练经济且推理高效。它总共包含236B个参数,每个token激活21B个。与DeepSeek 67B相比,DeepSeek-V2实现了更强的性能,同时节省了42.5%的训练成本,减少了93.3%的KV缓存,并将最大生成吞吐量提高了5.76倍。
图片
图片
我们在包含8.1万亿token的多样化和高质量语料库上预训练了DeepSeek-V2。随后,我们通过监督式微调(SFT)和强化学习(RL)的过程来充分释放模型的潜力。评估结果验证了我们方法的有效性,因为DeepSeek-V2在标准基准测试和开放式生成评估上都取得了显著的性能。
2. 模型下载
由于HuggingFace的限制,当前开源代码在GPU上运行时的性能比我们内部代码库慢。为了促进我们模型的有效执行,我们提供了一个专门的vllm解决方案,该解决方案优化了我们模型的运行性能。
Model | Context Length | Download |
DeepSeek-V2 | 128k | 🤗 HuggingFace |
DeepSeek-V2-Chat(RL) | 128k | 🤗 HuggingFace |
3. 评估结果
基础模型
Benchmark | Domain | LLaMA3 70B | Mixtral 8x22B | DeepSeek V1 (Dense-67B) | DeepSeek V2 (MoE-236B) |
MMLU | English | 78.9 | 77.6 | 71.3 | 78.5 |
BBH | English | 81.0 | 78.9 | 68.7 | 78.9 |
C-Eval | Chinese | 67.5 | 58.6 | 66.1 | 81.7 |
CMMLU | Chinese | 69.3 | 60.0 | 70.8 | 84.0 |
HumanEval | Code | 52.4 | 39.0 | 42.7 | 40.9 |
MBPP | Code | 68.6 | 64.2 | 57.4 | 66.6 |
GSM8K | Math | 83.0 | 80.3 | 63.4 | 79.2 |
Math | Math | 42.2 | 42.5 | 18.7 | 43.6 |
在Needle In A Haystack (NIAH)测试中,DeepSeek-V2在所有上下文窗口长度上表现良好,直到128K。
图片
chat模型
我们在AlpacaEval 2.0和MTBench上评估了我们的模型,展示了DeepSeek-V2-Chat-RL在英语对话生成上的竞争力。
Benchmark | Domain | QWen1.5 72B Chat | Mixtral 8x22B | LLaMA3 70B Instruct | DeepSeek V1 Chat (SFT) | DeepSeek V2 Chat(SFT) | DeepSeek V2 Chat(RL) |
MMLU | English | 76.2 | 77.8 | 80.3 | 71.1 | 78.4 | 77.8 |
BBH | English | 65.9 | 78.4 | 80.1 | 71.7 | 81.3 | 79.7 |
C-Eval | Chinese | 82.2 | 60.0 | 67.9 | 65.2 | 80.9 | 78.0 |
CMMLU | Chinese | 82.9 | 61.0 | 70.7 | 67.8 | 82.4 | 81.6 |
HumanEval | Code | 68.9 | 75.0 | 76.2 | 73.8 | 76.8 | 81.1 |
MBPP | Code | 52.2 | 64.4 | 69.8 | 61.4 | 70.4 | 72.0 |
LiveCodeBench (0901-0401) | Code | 18.8 | 25.0 | 30.5 | 18.3 | 28.7 | 32.5 |
GSM8K | Math | 81.9 | 87.9 | 93.2 | 84.1 | 90.8 | 92.2 |
Math | Math | 40.6 | 49.8 | 48.5 | 32.6 | 52.7 | 53.9 |
图片
Chinese Open Ended Generation Evaluation
模型 | 开源/闭源 | 总分 | 中文推理 | 中文语言 |
gpt-4-1106-preview | 闭源 | 8.01 | 7.73 | 8.29 |
DeepSeek-V2 Chat(RL) | 开源 | 7.91 | 7.45 | 8.36 |
erniebot-4.0-202404(文心一言) | 闭源 | 7.89 | 7.61 | 8.17 |
DeepSeek-V2 Chat(SFT) | 开源 | 7.74 | 7.30 | 8.17 |
gpt-4-0613 | 闭源 | 7.53 | 7.47 | 7.59 |
erniebot-4.0-202312(文心一言) | 闭源 | 7.36 | 6.84 | 7.88 |
moonshot-v1-32k-202404(月之暗面) | 闭源 | 7.22 | 6.42 | 8.02 |
Qwen1.5-72B-Chat(通义千问) | 开源 | 7.19 | 6.45 | 7.93 |
DeepSeek-67B-Chat | 开源 | 6.43 | 5.75 | 7.11 |
Yi-34B-Chat(零一万物) | 开源 | 6.12 | 4.86 | 7.38 |
gpt-3.5-turbo-0613 | 闭源 | 6.08 | 5.35 | 6.71 |
编码基准测试
我们在LiveCodeBench (0901-0401)上评估了我们的模型,这是一个为实时编码挑战设计的基准测试。如图所示,DeepSeek-V2在LiveCodeBench上展示了相当的熟练程度,其Pass@1得分超过了其他几个复杂的模型。这一性能突出了模型在处理实时编码任务方面的有效性。
图片
4. 模型架构
DeepSeek-V2采用创新的架构以保证训练经济和推理高效:
- 对于注意力机制,我们设计了IEAttn,它使用低秩键值联合压缩来消除推理时键值缓存的瓶颈,从而支持高效推理。
- 对于前馈网络(FFNs),我们采用了DeepSeekMoE架构,这是一种高性能的MoE架构,使我们能够以更低的成本训练更强的模型。
图片
5. 聊天网站
你可以在DeepSeek的官方网站上与DeepSeek-V2进行聊天:chat.deepseek.com
6. API平台
我们还提供与OpenAI兼容的API,可以在DeepSeek平台上使用:platform.deepseek.com。注册即可获得数百万免费token。你也可以按使用量付费,享受无与伦比的价格。
图片
7. 如何本地运行
要使用BF16格式的DeepSeek-V2进行推理,需要80GB*8的GPU。
使用Huggingface的Transformers进行推理
你可以直接使用Huggingface的Transformers进行模型推理。
文本补全
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
model_name = "deepseek-ai/DeepSeek-V2"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
# `max_memory` 应根据你的设备设置
max_memory = {i: "75GB" for i in range(8)}
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16, max_memory=max_memory)
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
text = "An attention function can be described as mapping a query and a set of key-value pairs to an output, where the query, keys, values, and output are all vectors. The output is"
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-Chat"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
# `max_memory` 应根据你的设备设置
max_memory = {i: "75GB" for i in range(8)}
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16, max_memory=max_memory)
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
messages = [
{"role": "user", "content": "Write a piece of quicksort code in 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|>User: {user_message_1}
Assistant: {assistant_message_1}<|end of sentence|>User: {user_message_2}
Assistant:
你也可以添加一个可选的系统消息:
<|begin of sentence|>{system_message}
User: {user_message_1}
Assistant: {assistant_message_1}<|end of sentence|>User: {user_message_2}
Assistant:
8. 许可证
此代码库在MIT许可证下授权。DeepSeek-V2基础/聊天模型的使用受模型许可证的约束。DeepSeek-V2系列(包括基础和聊天)支持商业用途。
9. 引用
@misc{deepseek-v2,
author = {DeepSeek-AI},
title = {DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model},
year = {2024},
note = {GitHub repository},
url = {https://github.com/deepseek-ai/deepseek-v2}
}