深度求索Deepseek近日发布了v2版本的模型,沿袭了1月发布的 Deepseek-MoE(混合专家模型)的技术路线,采用大量的小参数专家进行建模,同时在训练和推理上加入了更多的优化。它由 236B 个参数组成,其中 21B 个参数用于激活每个标记。与 DeepSeek 67B 相比,DeepSeek-V2 性能更强,同时节省了 42.5% 的训练成本,减少了 93.3% 的 KV 缓存,最大生成吞吐量提高到 5.76 倍。
在技术报告的开始,Deepseek团队用多个数字和两张图直观地概括了目前模型取得的效果。模型参数量方面达到236B ,同时由于模型小专家混合的特性,模型在推理时的激活参数很少,可以实现高推理速度。在通用能力的表现上,模型在MMLU多选题benchmark上拿到78.5 分,取得了第二名,Deepseek-V2在众多开源模型中表现仅次于70B 的 LLaMA3,超过了他们此前发布的V1代67B的非MoE模型。在成本效率方面,相比V1的稠密模型,V2模型节约了42.5%的训练成本,减少了推理时93.3%的 KV-cache 显存占用,将生成的吞吐量也提升到了原来的5.76倍。借助YaRN优化的长度外推训练方法,模型的上下文能力得以扩展到了128k大小。下面我们结合代码和技术报告,对Deepseek-V2模型进行详细的解读。
Base Model
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 | 48.2 | 53.1 | 45.1 | 48.8 |
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 |
Chat Model
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 |
中文推理
模型 | 开源/闭源 | 总分 | 中文推理 | 中文语言 |
---|---|---|---|---|
gpt-4-1106-preview | 闭源 | 8.01 | 7.73 | 8.29 |
DeepSeek-V2 Chat (RL) | 开源 | 7.91 | 7.45 | 8.35 |
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 |
代码
文本生成
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` should be set based on your devices
max_memory = {i: "75GB" for i in range(8)}
# `device_map` cannot be set to `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)
聊天机器人
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` should be set based on your devices
max_memory = {i: "75GB" for i in range(8)}
# `device_map` cannot be set to `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)
我原本以为阿里 Qwen 已经天下无敌,没想到杭州居然还有高手!哈哈哈!