DeepSeek-V2:一个强大、经济且高效的混合专家语言模型

模型下载 | 评测结果 | 模型架构 | API 平台 | 许可证 | 引用

论文链接 👁️

引言

今天,我们推出 DeepSeek-V2,一个强大的混合专家(Mixture-of-Experts, MoE)语言模型,具有训练经济和推理高效的特点。它包含 236B 总参数,其中每个 token 激活 21B 参数。与 DeepSeek 67B 相比,DeepSeek-V2 实现了更强的性能,同时节省了 42.5% 的训练成本,减少了 93.3% 的 KV 缓存,并将最大生成吞吐量提升至 5.76 倍。

我们在包含 8.1 万亿 token 的多样化高质量语料库上预训练了 DeepSeek-V2。在全面的预训练之后,我们通过监督微调(SFT)和强化学习(RL)的过程来充分释放模型的潜力。评估结果验证了我们方法的有效性,因为 DeepSeek-V2 在标准基准测试和开放式生成评估上都取得了显著的性能。

新闻

  • 2024.05.16:我们发布了 DeepSeek-V2-Lite。
  • 2024.05.06:我们发布了 DeepSeek-V2。

模型下载

模型总参数激活参数上下文长度下载
DeepSeek-V2-Lite16B2.4B32k🤗 HuggingFace
DeepSeek-V2-Lite-Chat (SFT)16B2.4B32k🤗 HuggingFace
DeepSeek-V2236B21B128k🤗 HuggingFace
DeepSeek-V2-Chat (RL)236B21B128k🤗 HuggingFace

由于 HuggingFace 的限制,当前开源代码在使用 HuggingFace 在 GPU 上运行时,性能比我们内部代码库慢。为了促进我们模型的有效执行,我们提供了专门的 vllm 解决方案,优化了模型的运行性能。

评估结果

基础模型

#### 标准基准测试(大于 67B 的模型)

基准测试领域LLaMA3 70BMixtral 8x22BDeepSeek-V1 (Dense-67B)DeepSeek-V2 (MoE-236B)
MMLU英文78.977.671.378.5
BBH英文81.078.968.778.9
C-Eval中文67.558.666.181.7
CMMLU中文69.360.070.884.0
HumanEval代码48.253.145.148.8
MBPP代码68.664.257.466.6
GSM8K数学83.080.363.479.2
Math数学42.242.518.743.6

#### 标准基准测试(小于 16B 的模型)

基准测试领域DeepSeek 7B (Dense)DeepSeekMoE 16BDeepSeek-V2-Lite (MoE-16B)
架构-MHA+DenseMHA+MoEMLA+MoE
MMLU英文48.245.058.3
BBH英文39.538.944.1
C-Eval中文45.040.660.3
CMMLU中文47.242.564.3
HumanEval代码26.226.829.9
MBPP代码39.039.243.2
GSM8K数学17.418.841.1
Math数学3.34.317.1

更多评估细节,如少样本设置和提示词,请查看我们的论文。

#### 上下文窗口

在"大海捞针"(NIAH)测试中的评估结果。DeepSeek-V2 在高达 128K 的所有上下文窗口长度上表现良好。

Chat 模型

#### 标准基准测试(大于 67B 的模型)

基准测试领域QWen1.5 72B ChatMixtral 8x22BLLaMA3 70B InstructDeepSeek-V1 Chat (SFT)DeepSeek-V2 Chat (SFT)DeepSeek-V2 Chat (RL)
MMLU英文76.277.880.371.178.477.8
BBH英文65.978.480.171.781.379.7
C-Eval中文82.260.067.965.280.978.0
CMMLU中文82.961.070.767.882.481.6
HumanEval代码68.975.076.273.876.881.1
MBPP代码52.264.469.861.470.472.0
LiveCodeBench (0901-0401)代码18.825.030.518.328.732.5
GSM8K数学81.987.993.284.190.892.2
Math数学40.649.848.532.652.753.9

#### 标准基准测试(小于 16B 的模型)

基准测试领域DeepSeek 7B Chat (SFT)DeepSeekMoE 16B Chat (SFT)DeepSeek-V2-Lite 16B Chat (SFT)
MMLU英文49.747.255.7
BBH英文43.142.248.1
C-Eval中文44.740.060.1
CMMLU中文51.249.362.5
HumanEval代码45.145.757.3
MBPP代码39.046.245.8
GSM8K数学62.662.272.0
Math数学14.715.227.9

#### 英文开放式生成评估

我们在 AlpacaEval 2.0 和 MTBench 上评估了我们的模型,展示了 DeepSeek-V2-Chat-RL 在英文对话生成方面的竞争力。

#### 中文开放式生成评估

Alignbench (https://arxiv.org/abs/2311.18743)

模型开源/闭源总分中文推理中文语言
gpt-4-1106-preview闭源8.017.738.29
DeepSeek-V2 Chat (RL)开源7.917.458.36
erniebot-4.0-202404 (文心一言)闭源7.897.618.17
DeepSeek-V2 Chat (SFT)开源7.747.308.17
gpt-4-0613闭源7.537.477.59
erniebot-4.0-202312 (文心一言)闭源7.366.847.88
moonshot-v1-32k-202404 (月之暗面)闭源7.226.428.02
Qwen1.5-72B-Chat (通义千问)开源7.196.457.93
DeepSeek-67B-Chat开源6.435.757.11
Yi-34B-Chat (零一万物)开源6.124.867.38
gpt-3.5-turbo-0613闭源6.085.356.71
DeepSeek-V2-Lite 16B Chat开源6.014.717.32

#### 编码基准测试

我们在 LiveCodeBench (0901-0401) 上评估了我们的模型,这是一个为实时代码挑战设计的基准测试。如图所示,DeepSeek-V2 在 LiveCodeBench 上展示了相当高的熟练度,其 Pass@1 得分超过了其他几个复杂模型。这一性能突出了模型在处理实时代码任务方面的有效性。

模型架构

DeepSeek-V2 采用创新架构来保证训练经济和推理高效:

  • 在注意力方面,我们设计了 MLA(多头潜在注意力),利用低秩键值联合压缩来消除推理时键值缓存的瓶颈,从而支持高效推理。
  • 在前馈网络(FFNs)方面,我们采用 DeepSeekMoE 架构,这是一种高性能的 MoE 架构,能够以更低的成本训练更强的模型。

聊天网站

您可以在 DeepSeek 官方网站上与 DeepSeek-V2 聊天:chat.deepseek.com

API 平台

我们还在 DeepSeek 平台上提供与 OpenAI 兼容的 API:platform.deepseek.com。注册即可获得数百万免费 token。您也可以按使用量付费,享受无与伦比的价格。

如何在本地运行

要以 BF16 格式使用 DeepSeek-V2 进行推理,需要 80GB\*8 GPU。

使用 Huggingface Transformers 推理

您可以直接使用 Huggingface Transformers 进行模型推理。

#### 文本补全

python
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)}

device_map 不能设置为 auto

model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, device_map="sequential", torch_dtype=torch.bfloat16, max_memory=max_memory, attn_implementation="eager") 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)

#### 聊天补全

python
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)}

device_map 不能设置为 auto

model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, device_map="sequential", torch_dtype=torch.bfloat16, max_memory=max_memory, attn_implementation="eager") 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:

使用 SGLang 推理(推荐)

SGLang 目前支持 MLA 优化、FP8 (W8A8)、FP8 KV Cache 和 Torch Compile,在开源框架中提供了最佳的延迟和吞吐量。以下是启动与 OpenAI API 兼容服务器的一些示例命令:

bash

BF16,张量并行度 = 8

python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V2-Chat --tp 8 --trust-remote-code

BF16,使用 torch.compile(编译可能需要几分钟)

python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V2-Lite-Chat --trust-remote-code --enable-torch-compile

FP8,张量并行度 = 8,FP8 KV 缓存

python3 -m sglang.launch_server --model deepseek-ai/DeepSeek-V2-Chat --tp 8 --trust-remote-code --quant fp8 --kv-cache-dtype fp8_e5m2

启动服务器后,您可以使用 OpenAI API 查询它:

python
import openai
client = openai.Client(
    base_url="http://127.0.0.1:30000/v1", api_key="EMPTY")

聊天补全

response = client.chat.completions.create( model="default", messages=[ {"role": "system", "content": "You are a helpful AI assistant"}, {"role": "user", "content": "List 3 countries and their capitals."}, ], temperature=0, max_tokens=64, ) print(response)

使用 vLLM 推理(推荐)

要使用 vLLM 进行模型推理,请将此 Pull Request 合并到您的 vLLM 代码库中:vllm-project/vllm#4650

python
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

max_model_len, tp_size = 8192, 8 model_name = "deepseek-ai/DeepSeek-V2-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": "Who are you?"}], [{"role": "user", "content": "Translate the following content into Chinese directly: DeepSeek-V2 adopts innovative architectures to guarantee economical training and efficient inference."}], [{"role": "user", "content": "Write a piece of quicksort code in 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 中使用它。以下是一个示例:

python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
    model='deepseek-chat',
    openai_api_key=,
    openai_api_base='https://api.deepseek.com/v1',
    temperature=0.85,
    max_tokens=8000)

许可证

此代码仓库根据 MIT 许可证 获得许可。DeepSeek-V2 Base/Chat 模型的使用受 模型许可证 约束。DeepSeek-V2 系列(包括 Base 和 Chat)支持商业使用。

引用

bibtex
@misc{deepseekv2,
    title={DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model}, 
    author={DeepSeek-AI},
    year={2024},
    eprint={2405.04434},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}

联系

如果您有任何问题,请提出 issue 或通过 service@deepseek.com 与我们联系。

ESC

输入关键词开始搜索

支持搜索标题、内容、标签