🚀 DeepSeek-Coder-V2: Breaking the Barrier of Closed-Source Models in Code Intelligence
DeepSeek-Coder-V2 is an open - source Mixture - of - Experts (MoE) code language model. It achieves performance comparable to GPT4 - Turbo in code - specific tasks, enhancing coding and mathematical reasoning capabilities.
API Platform |
How to Use |
License |
Paper Link👁️
🚀 Quick Start
DeepSeek-Coder-V2 is an open - source Mixture - of - Experts (MoE) code language model. It's further pre - trained from an intermediate checkpoint of DeepSeek - V2 with additional 6 trillion tokens. This continued pre - training enhances its coding and mathematical reasoning capabilities while maintaining performance in general language tasks. It outperforms many closed - source models in coding and math benchmarks.
✨ Features
- Enhanced Capabilities: Substantially improves the coding and mathematical reasoning capabilities of DeepSeek - V2, and maintains performance in general language tasks.
- Expanded Support: Increases the supported programming languages from 86 to 338 and extends the context length from 16K to 128K.
- Superior Performance: Achieves better results than closed - source models like GPT4 - Turbo, Claude 3 Opus, and Gemini 1.5 Pro in coding and math benchmarks.
📦 Installation
This section mainly focuses on model downloads. We release the DeepSeek - Coder - V2 with 16B and 236B parameters based on the DeepSeekMoE framework, which has active parameters of only 2.4B and 21B, including base and instruct models.
Model |
#Total Params |
#Active Params |
Context Length |
Download |
DeepSeek-Coder-V2-Lite-Base |
16B |
2.4B |
128k |
🤗 HuggingFace |
DeepSeek-Coder-V2-Lite-Instruct |
16B |
2.4B |
128k |
🤗 HuggingFace |
DeepSeek-Coder-V2-Base |
236B |
21B |
128k |
🤗 HuggingFace |
DeepSeek-Coder-V2-Instruct |
236B |
21B |
128k |
🤗 HuggingFace |
💻 Usage Examples
Basic Usage
You can chat with the DeepSeek - Coder - V2 on DeepSeek's official website: coder.deepseek.com
Advanced Usage
We also provide OpenAI - Compatible API at DeepSeek Platform: platform.deepseek.com, and you can pay - as - you - go at an unbeatable price.
Inference Examples
Here, we provide some examples of how to use DeepSeek - Coder - V2 - Lite model. If you want to utilize DeepSeek - Coder - V2 in BF16 format for inference, 80GB*8 GPUs are required.
Code Completion
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))
Code Insertion
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):])
Chat Completion
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)
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))
The complete chat template can be found within tokenizer_config.json
located in the huggingface model repository.
An example of chat template is as follows:
<|begin▁of▁sentence|>User: {user_message_1}
Assistant: {assistant_message_1}<|end▁of▁sentence|>User: {user_message_2}
Assistant:
You can also add an optional system message:
<|begin▁of▁sentence|>{system_message}
User: {user_message_1}
Assistant: {assistant_message_1}<|end▁of▁sentence|>User: {user_message_2}
Assistant:
Inference with vLLM (recommended)
To utilize vLLM for model inference, please merge this Pull Request into your vLLM codebase: 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-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)
📄 License
This code repository is licensed under the MIT License. The use of DeepSeek - Coder - V2 Base/Instruct models is subject to the Model License. DeepSeek - Coder - V2 series (including Base and Instruct) supports commercial use.
📚 Documentation
If you have any questions, please raise an issue or contact us at service@deepseek.com.