模型下载 | 评估结果 | 模型架构 | API平台 | 许可证 | 引用
上周,DeepSeek-V2的发布和热议激发了社区对多头潜在注意力(MLA)的广泛关注!社区中的许多人建议开源一个较小的MoE模型以进行深入研究。现在,DeepSeek-V2-Lite版本发布了:
DeepSeek-V2是一个强大的混合专家(MoE)语言模型,以经济训练和高效推理为特点。DeepSeek-V2采用创新的架构,包括多头潜在注意力(MLA)和DeepSeekMoE。MLA通过将键-值(KV)缓存压缩成潜在向量来保证高效推理,而DeepSeekMoE通过稀疏计算使训练强大的模型在经济成本上成为可能。
随着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解决方案,以优化运行我们模型的有效性能。
| 基准测试 | 领域 | DeepSeek 7B (密集) | DeepSeekMoE 16B | DeepSeek-V2-Lite (MoE-16B) |
|---|---|---|---|---|
| 架构 | - | MHA+Dense | 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 |
| Math | 数学 | 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 |
| Math | 数学 | 14.7 | 15.2 | 27.9 |
DeepSeek-V2 采用了创新架构以确保经济高效的训练和推断:
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 亿个。
DeepSeek-V2-Lite 也是从零开始在 DeepSeek-V2 相同的预训练语料库上训练的,该语料库未受到任何 SFT 数据的污染。它使用 AdamW 优化器,并设置超参数 ,,以及 。学习率采用预热和阶梯衰减策略进行调度。最初,学习率在最初的 2K 步骤内从 0 线性增加到最大值。之后,在学习了大约 80% 的标记后,学习率乘以 0.316,在学习了大约 90% 的标记后再次乘以 0.316。最大学习率设置为 ,梯度裁剪范数设置为 1.0。我们没有为其采用批大小调度策略,而是使用恒定的批大小 4608 序列进行训练。在预训练期间,我们将最大序列长度设置为 4K,并在 5.7T 个标记上训练 DeepSeek-V2-Lite。我们利用管道并行技术将不同的层部署在不同的设备上,但对于每个层,所有专家都将部署在同一个设备上。因此,我们仅使用了小的专家级平衡损失,,而没有使用设备级平衡损失和通信平衡损失。预训练完成后,我们还进行了长上下文扩展,对 DeepSeek-V2-Lite 进行了 SFT,并获得了一个名为 DeepSeek-V2-Lite Chat 的聊天模型。
要在推断时使用 BF16 格式的 DeepSeek-V2-Lite,需要 40GB*1 GPU。
您可以直接使用 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 代码库中: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)由于我们的 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)本代码仓库遵循 MIT许可协议。DeepSeek-V2基础/聊天模型的使用需遵守 模型许可协议。DeepSeek-V2系列(包括基础和聊天模型)均支持商业用途。
@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}
}如果您有任何疑问,请提出一个议题或通过 service@deepseek.com 与我们联系。