HuggingFace镜像/DeepSeek-V2-Lite
模型介绍文件和版本分析
下载使用量0
DeepSeek-V2

主页 聊天 Hugging Face
Discord 微信 关注Twitter
代码许可证 模型许可证

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

论文链接👁️

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

1. 引言

上周,DeepSeek-V2的发布和热议激发了社区对多头潜在注意力(MLA)的广泛关注!社区中的许多人建议开源一个较小的MoE模型以进行深入研究。现在,DeepSeek-V2-Lite版本发布了:

  • 总参数量16B,激活参数量2.4B,从零开始训练使用5.7T个标记
  • 在许多英文和中文基准测试中超过了7B密集模型和16B MoE模型
  • 可在单个40G GPU上部署,可在8x80G GPUs上微调

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-Lite16B2.4B32k🤗 HuggingFace
DeepSeek-V2-Lite-Chat (SFT)16B2.4B32k🤗 HuggingFace
DeepSeek-V2236B21B128k🤗 HuggingFace
DeepSeek-V2-Chat (RL)236B21B128k🤗 HuggingFace

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

4. 评估结果

基座模型

标准基准

基准测试领域DeepSeek 7B (密集)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
更多评估细节,如少样本设置和提示,请查看我们的论文。

聊天模型

标准基准

基准测试领域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

5. 模型架构

DeepSeek-V2 采用了创新架构以确保经济高效的训练和推断:

  • 对于注意力机制,我们设计了 MLA(多头潜在注意力),它利用低秩键-值联合压缩技术消除了推断时键-值缓存的瓶颈,从而支持高效的推断。
  • 对于前馈网络(FFNs),我们采用了 DeepSeekMoE 架构,这是一种高性能的 MoE 架构,可以在较低成本下训练更强大的模型。

DeepSeek-V2-Lite 共有 27 层,隐藏维度为 2048。它同样采用了 MLA,并具有 16 个注意力头,每个头的维度为 128。其 KV 压缩维度为 512,但与 DeepSeek-V2 略有不同,它不对查询进行压缩。对于解耦的查询和键,它具有每个头 64 维的维度。DeepSeek-V2-Lite 还采用了 DeepSeekMoE,除了第一层外的所有 FFNs 都被 MoE 层取代。每个 MoE 层由 2 个共享专家和 64 个路由专家组成,每个专家的中间隐藏维度为 1408。在路由专家中,每个标记将激活 6 个专家。在这种配置下,DeepSeek-V2-Lite 共包含 157 亿个参数,其中每个标记激活 24 亿个。

6. 训练细节

DeepSeek-V2-Lite 也是从零开始在 DeepSeek-V2 相同的预训练语料库上训练的,该语料库未受到任何 SFT 数据的污染。它使用 AdamW 优化器,并设置超参数 β1=0.9\beta_1=0.9β1​=0.9,β2=0.95\beta_2=0.95β2​=0.95,以及 weightdecay=0.1\mathrm{weight_decay}=0.1weightd​ecay=0.1。学习率采用预热和阶梯衰减策略进行调度。最初,学习率在最初的 2K 步骤内从 0 线性增加到最大值。之后,在学习了大约 80% 的标记后,学习率乘以 0.316,在学习了大约 90% 的标记后再次乘以 0.316。最大学习率设置为 4.2×10−44.2 \times 10^{-4}4.2×10−4,梯度裁剪范数设置为 1.0。我们没有为其采用批大小调度策略,而是使用恒定的批大小 4608 序列进行训练。在预训练期间,我们将最大序列长度设置为 4K,并在 5.7T 个标记上训练 DeepSeek-V2-Lite。我们利用管道并行技术将不同的层部署在不同的设备上,但对于每个层,所有专家都将部署在同一个设备上。因此,我们仅使用了小的专家级平衡损失,α1=0.001\alpha_{1}=0.001α1​=0.001,而没有使用设备级平衡损失和通信平衡损失。预训练完成后,我们还进行了长上下文扩展,对 DeepSeek-V2-Lite 进行了 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 = "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-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": "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:

使用 vLLM 进行推理(推荐)

为了使用 vLLM 进行模型推理,请将以下拉取请求合并到您的 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": "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 中使用它。 以下是一个示例:

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
    model='deepseek-chat',
    openai_api_key=<your-deepseek-api-key>,
    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: A Strong, Economical, and Efficient Mixture-of-Experts Language Model}, 
      author={DeepSeek-AI},
      year={2024},
      eprint={2405.04434},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}

10. 联系我们

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