DeepSeek-Coder-V2:打破代码智能闭源模型壁垒
DeepSeek-Coder-V2:打破代码智能闭源模型壁垒
1. 简介
我们提出了 DeepSeek-Coder-V2,这是一个开源的混合专家 (MoE) 代码语言模型,在代码特定任务中实现了与 GPT4-Turbo 相当的性能。具体来说,DeepSeek-Coder-V2 从 DeepSeek-V2 的中间检查点进一步预训练,增加了 6 万亿个 token。通过这种持续的预训练,DeepSeek-Coder-V2 大幅增强了 DeepSeek-V2 的编码和数学推理能力,同时在一般语言任务中保持了相当的性能。与 DeepSeek-Coder-33B 相比,DeepSeek-Coder-V2 在代码相关任务的各个方面以及推理和通用能力方面都有了显著的进步。此外,DeepSeek-Coder-V2 将其对编程语言的支持从 86 种扩展到 338 种,同时将上下文长度从 16K 扩展到 128K。
在标准基准评估中,DeepSeek-Coder-V2 在编码和数学基准测试中的表现优于 GPT4-Turbo、Claude 3 Opus 和 Gemini 1.5 Pro 等闭源模型。支持的编程语言列表可找到。
2. 模型下载
我们基于框架发布了参数量分别为16B和236B的DeepSeek-Coder-V2,而DeepSeek-Coder-V2的有效参数量仅为2.4B和21B,包括基础模型和指导模型。
模型 | #总参数 | #活动参数 | 上下文长度 | 下载 |
---|---|---|---|---|
DeepSeek-Coder-V2-Lite-Base | 16B | 2.4B | 128千 | |
DeepSeek-Coder-V2-Lite-指导 | 16B | 2.4B | 128千 | |
DeepSeek-Coder-V2-基础版 | 236B | 21B | 128千 | |
DeepSeek-Coder-V2-指导 | 236B | 21B | 128千 |
3.评估结果
3.1 代码生成
#TP | #美联社 | 人力评估 | 马来西亚公共服务计划+ | 活码测试 | 美国陆军航空兵司令部 | |
---|---|---|---|---|---|---|
闭源模型 | ||||||
Gemini-1.5-Pro | - | - | 83.5 | 74.6 | 34.1 | 4.9 |
克劳德-3-作品 | - | - | 84.2 | 72.0 | 34.6 | 7.8 |
GPT-4-Turbo-1106 | - | - | 87.8 | 69.3 | 37.1 | 11.1 |
GPT-4-Turbo-0409 | - | - | 88.2 | 72.2 | 45.7 | 12.3 |
GPT-4o-0513 | - | - | 91.0 | 73.5 | 43.4 | 18.8 |
开源模型 | ||||||
代码 | 22B | 22B | 78.1 | 68.2 | 31.0 | 4.6 |
DeepSeek-Coder-指导 | 33B | 33B | 79.3 | 70.1 | 22.5 | 4.2 |
Llama3-指导 | 70B | 70B | 81.1 | 68.8 | 28.7 | 3.3 |
DeepSeek-Coder-V2-Lite-指导 | 16B | 2.4B | 81.1 | 68.8 | 24.3 | 6.5 |
DeepSeek-Coder-V2-指导 | 236B | 21B | 90.2 | 76.2 | 43.4 | 12.1 |
3.2 代码完成
模型 | #TP | #美联社 | RepoBench(Python) | RepoBench (Java) | HumanEval FIM |
---|---|---|---|---|---|
代码 | 22B | 22B | 46.1 | 45.7 | 83.0 |
DeepSeek-Coder 库 | 7B | 7B | 36.2 | 43.3 | 86.1 |
DeepSeek-Coder 库 | 33B | 33B | 39.1 | 44.8 | 86.4 |
DeepSeek-Coder-V2-Lite-Base | 16B | 2.4B | 38.9 | 43.3 | 86.4 |
3.3 代码修复
#TP | #美联社 | 缺陷4J | SWE 工作台 | 艾德 | |
---|---|---|---|---|---|
闭源模型 | |||||
Gemini-1.5-Pro | - | - | 18.6 | 19.3 | 57.1 |
克劳德-3-作品 | - | - | 25.5 | 11.7 | 68.4 |
GPT-4-Turbo-1106 | - | - | 22.8 | 22.7 | 65.4 |
GPT-4-Turbo-0409 | - | - | 24.3 | 18.3 | 63.9 |
GPT-4o-0513 | - | - | 26.1 | 26.7 | 72.9 |
开源模型 | |||||
代码 | 22B | 22B | 17.8 | 2.7 | 51.1 |
DeepSeek-Coder-指导 | 33B | 33B | 11.3 | 0.0 | 54.5 |
Llama3-指导 | 70B | 70B | 16.2 | - | 49.2 |
DeepSeek-Coder-V2-Lite-指导 | 16B | 2.4B | 9.2 | 0.0 | 44.4 |
DeepSeek-Coder-V2-指导 | 236B | 21B | 21.0 | 12.7 | 73.7 |
3.4 数学推理
#TP | #美联社 | GSM8K | 数学 | 2024年国际医学博览会 | 数学奥德赛 | |
---|---|---|---|---|---|---|
闭源模型 | ||||||
Gemini-1.5-Pro | - | - | 90.8 | 67.7 | 2/30 | 45.0 |
克劳德-3-作品 | - | - | 95.0 | 60.1 | 2/30 | 40.6 |
GPT-4-Turbo-1106 | - | - | 91.4 | 64.3 | 1/30 | 49.1 |
GPT-4-Turbo-0409 | - | - | 93.7 | 73.4 | 3/30 | 46.8 |
GPT-4o-0513 | - | - | 95.8 | 76.6 | 2/30 | 53.2 |
开源模型 | ||||||
Llama3-指导 | 70B | 70B | 93.0 | 50.4 | 1/30 | 27.9 |
DeepSeek-Coder-V2-Lite-指导 | 16B | 2.4B | 86.4 | 61.8 | 0/30 | 44.4 |
DeepSeek-Coder-V2-指导 | 236B | 21B | 94.9 | 75.7 | 4/30 | 53.7 |
3.5 通用自然语言
基准 | 领域 | DeepSeek-V2-Lite 聊天 | DeepSeek-Coder-V2-Lite 指导 | DeepSeek-V2 聊天 | DeepSeek-Coder-V2 指导 |
---|---|---|---|---|---|
百比黑 | 英语 | 48.1 | 61.2 | 79.7 | 83.9 |
莫尔登大学 | 英语 | 55.7 | 60.1 | 78.1 | 79.2 |
ARC-Easy | 英语 | 86.1 | 88.9 | 98.1 | 97.4 |
ARC 挑战赛 | 英语 | 73.4 | 77.4 | 92.3 | 92.8 |
琐事问答 | 英语 | 65.2 | 59.5 | 86.7 | 82.3 |
自然问题 | 英语 | 35.5 | 30.8 | 53.4 | 47.5 |
评估 | 英语 | 42.8 | 28.7 | 61.4 | 60 |
线索 | 中国人 | 80.0 | 76.5 | 89.9 | 85.9 |
C-评估 | 中国人 | 60.1 | 61.6 | 78.0 | 79.4 |
加拿大蒙特利尔大学 | 中国人 | 62.5 | 62.7 | 81.6 | 80.9 |
竞技场-困难 | - | 11.4 | 38.1 | 41.6 | 65.0 |
AlpaceEval 2.0 | - | 16.9 | 17.7 | 38.9 | 36.9 |
MT-Bench | - | 7.37 | 7.81 | 8.97 | 8.77 |
Alignbench | - | 6.02 | 6.83 | 7.91 | 7.84 |
3.6 上下文窗口
(NIAH) 测试的评估结果Needle In A Haystack
。DeepSeek-Coder-V2 在高达128K的所有上下文窗口长度上均表现良好。
4. 聊天网站
您可以在DeepSeek的官方网站上与DeepSeek-Coder-V2进行聊天:
5. API 平台
我们还在 DeepSeek 平台上提供与 OpenAI 兼容的 API:,您还可以以无与伦比的价格按使用量付费。
6. 如何在本地运行
这里我们提供了一些如何使用 DeepSeek-Coder-V2-Lite 模型的示例。如果您想使用 BF16 格式的 DeepSeek-Coder-V2 进行推理,则需要 80GB*8 GPU。
使用 Huggingface 的 Transformers 进行推理
您可以直接使用进行模型推理。
代码完成 from transformers import AutoTokenizer, AutoModelForCausalLM import torch tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Base", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Base", trust_remote_code=True, torch_dtype=torch.bfloat16).cuda() input_text = "#write a quick sort algorithm" inputs = tokenizer(input_text, return_tensors="pt").to(model.device) outputs = model.generate(**inputs, max_length=128) print(tokenizer.decode(outputs[0], skip_special_tokens=True))
代码插入 from transformers import AutoTokenizer, AutoModelForCausalLM import torch tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Base", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Base", trust_remote_code=True, torch_dtype=torch.bfloat16).cuda() input_text = """<|fim▁begin|>def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [] right = [] <|fim▁hole|> if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right)<|fim▁end|>""" inputs = tokenizer(input_text, return_tensors="pt").to(model.device) outputs = model.generate(**inputs, max_length=128) print(tokenizer.decode(outputs[0], skip_special_tokens=True)[len(input_text):])
聊天完成 from transformers import AutoTokenizer, AutoModelForCausalLM import torch tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct", trust_remote_code=True, torch_dtype=torch.bfloat16).cuda() messages=[ { 'role': 'user', 'content': "write a quick sort algorithm in python."} ] inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device) # tokenizer.eos_token_id is the id of <|end▁of▁sentence|> token outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, top_k=50, top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id) print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
完整的聊天模板可以在tokenizer_config.json
huggingface 模型库中找到。
聊天模板示例如下: <|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:
在最后一轮对话中,请注意“Assistant:”冒号后没有空格。添加空格可能会导致 16B-Lite 型号出现以下问题:
- 英文问题得到中文解答。
- 响应包含乱码文本。
- 回答重复过多。
旧版本的 Ollama 存在此错误(参见),但最新版本已修复。
使用 vLLM 进行推理(推荐)
要利用进行模型推理,请将此 Pull Request 合并到您的 vLLM 代码库中:。 from transformers import AutoTokenizer from vllm import LLM, SamplingParams max_model_len, tp_size = 8192, 1 model_name = "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct" 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": "write a quick sort algorithm in python."}], [{"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)
7. 许可
本代码库遵循。DeepSeek-Coder-V2 Base/Instruct 模型的使用受约束。DeepSeek-Coder-V2 系列(包括 Base 和 Instruct)支持商业使用。