DeepSeek Coder

repository·main·Indexed 12 days ago

https://github.com/deepseek-ai/deepseek-coder

A series of large-scale code language models trained on 2T tokens for high-performance code completion, infilling, and instruction following. The repository provides tools for fine-tuning via DeepSpeed and evaluation across benchmarks including DS-1000, HumanEval, MBPP, LeetCode Contest, and PAL-Math.

Tokens
8.8K
Snippets
27
Records
32
Agent score
98%

What's inside DeepSeek Coder

  1. Prompting for Math Problem Solving with Python (PAL-Math)

    main

    To solve mathematical problems using the Program-Aided Language (PAL) approach, use a prompt that instructs the model to use Python for computation and to display the final result in LaTeX.

    Prompt Structure:

    1. Instruction: "Let's use python to solve math problems. Display the final result in LaTeX."
    2. Question: The mathematical problem to be solved.
    3. Solution: A Python function named solution() that performs the calculation and returns the result (often formatted as a string or LaTeX).
    Let's use python to solve math problems. Display the final result in LaTeX.
    
    Question: [Mathematical Question]
    
    ```python
    def solution():
        # ... computation logic ...
        return result
  2. Evaluate Base models using accelerate

    main

    To evaluate a base model (e.g., DeepSeek-Coder-1.3b-Base) on the HumanEval dataset using multiple GPUs, use the eval_pal.py script via accelerate.

    Note: If you are testing different programming languages, you must update the execution paths in humaneval/execution.py to ensure the environment can run the generated code correctly.

    Required arguments:

    • --logdir: The model name or path.
    • --language: The programming language to evaluate (e.g., python).
    • --dataroot: The root directory of the dataset.
    MODEL_NAME_OR_PATH="deepseek-ai/deepseek-coder-1.3b-base"
    DATASET_ROOT="data/"
    LANGUAGE="python"
    python -m accelerate.commands.launch --config_file test_config.yaml eval_pal.py --logdir ${MODEL_NAME_OR_PATH} --language ${LANGUAGE} --dataroot ${DATASET_ROOT}
  3. Fine-tune DeepSeek-Coder using deepspeed

    main

    Use the finetune_deepseekcoder.py script via the deepspeed launcher to fine-tune models like deepseek-ai/deepseek-coder-6.7b-instruct.

    Key requirements:

    • Set DATA_PATH to your prepared JSONL file.
    • Set OUTPUT_PATH for model checkpoints.
    • Adjust hyperparameters such as learning_rate and per_device_train_batch_size based on your hardware and task.
    • Use a DeepSpeed configuration file (e.g., configs/ds_config_zero3.json) for optimized training.
    DATA_PATH="<your_data_path>"
    OUTPUT_PATH="<your_output_path>"
    MODEL_PATH="deepseek-ai/deepseek-coder-6.7b-instruct"
    
    deepspeed finetune_deepseekcoder.py \
        --model_name_or_path $MODEL_PATH \
        --data_path $DATA_PATH \
        --output_dir $OUTPUT_PATH \
        --num_train_epochs 3 \
        --model_max_length 1024 \
        --per_device_train_batch_size 16 \
        --per_device_eval_batch_size 1 \
        --gradient_accumulation_steps 4 \
        --evaluation_strategy "no" \
        --save_strategy "steps" \
        --save_steps 100 \
        --save_total_limit 100 \
        --learning_rate 2e-5 \
        --warmup_steps 10 \
        --logging_steps 1 \
        --lr_scheduler_type "cosine" \
        --gradient_checkpointing True \
        --report_to "tensorboard" \
        --deepspeed configs/ds_config_zero3.json \
        --bf16 True
  4. Generate GGUF models for llama.cpp

    main

    Since DeepSeek Coder uses a HuggingFace Tokenizer, you can generate GGUF models by using a specific branch of llama.cpp that supports these pre-tokenizers.

    Steps:

    1. Clone llama.cpp and checkout the regex_gpt2_preprocess branch.
    2. Build llama.cpp.
    3. Use convert-hf-to-gguf.py to convert the model.
    4. Use ./quantize to apply quantization (e.g., q4_0).
    git clone https://github.com/DOGEwbx/llama.cpp.git
    cd llama.cpp
    git checkout regex_gpt2_preprocess
    make
    python3 -m pip install -r requirements.txt
    # generate GGUF model
    python convert-hf-to-gguf.py <MODEL_PATH> --outfile <GGUF_PATH> --model-name deepseekcoder
    # use q4_0 quantization as an example
    ./quantize <GGUF_PATH> <OUTPUT_PATH> q4_0
    ./main -m <OUTPUT_PATH> -n 128 -p <PROMPT>
  5. Fine-tune DeepSeek-Coder with DeepSpeed

    main

    To fine-tune models on downstream tasks, use the finetune/finetune_deepseekcoder.py script.

    Requirements:

    1. Install fine-tuning dependencies: pip install -r finetune/requirements.txt.
    2. Prepare data in JSON-serialized format where each line has instruction and output fields.
    3. Use DeepSpeed for training.

    Execution Example: Set DATA_PATH and OUTPUT_PATH environment variables and run the script via deepspeed.

    DATA_PATH="<your_data_path>"
    OUTPUT_PATH="<your_output_path>"
    MODEL="deepseek-ai/deepseek-coder-6.7b-instruct"
    
    cd finetune && deepspeed finetune_deepseekcoder.py \
        --model_name_or_path $MODEL \
        --data_path $DATA_PATH \
        --output_dir $OUTPUT_PATH \
        --num_train_epochs 3 \
        --model_max_length 1024 \
        --per_device_train_batch_size 16 \
        --per_device_eval_batch_size 1 \
        --gradient_accumulation_steps 4 \
        --evaluation_strategy "no" \
        --save_strategy "steps" \
        --save_steps 100 \
        --save_total_limit 100 \
        --learning_rate 2e-5 \
        --warmup_steps 10 \
        --logging_steps 1 \
        --lr_scheduler_type "cosine" \
        --gradient_checkpointing True \
        --report_to "tensorboard" \
        --deepspeed configs/ds_config_zero3.json \
        --bf16 True
  6. High-throughput Inference with vLLM

    main

    For high-throughput inference, use the vLLM library.

    Text Completion: Pass a list of strings to llm.generate(). Chat Completion: Use tokenizer.apply_chat_template with tokenize=False to convert messages into formatted prompts before passing them to llm.generate().

    from vllm import LLM, SamplingParams
    
    tp_size = 4 # Tensor Parallelism
    sampling_params = SamplingParams(temperature=0.7, top_p=0.9, max_tokens=100)
    model_name = "deepseek-ai/deepseek-coder-6.7b-base"
    llm = LLM(model=model_name, trust_remote_code=True, gpu_memory_utilization=0.9, tensor_parallel_size=tp_size)
    
    prompts = ["If everyone in a country loves one another,"]
    outputs = llm.generate(prompts, sampling_params)
    print([output.outputs[0].text for output in outputs])
  7. Run MBPP evaluation using accelerate

    main

    You can evaluate a DeepSeek Coder model on the MBPP dataset using the eval_pal.py script. The provided example uses accelerate.commands.launch to distribute the workload across 8 GPUs using a configuration file named test_config.yaml.

    Required Arguments:

    • --logdir: The path/name of the model being evaluated.
    • --dataroot: The directory containing the MBPP dataset.
    MODEL_NAME_OR_PATH="deepseek-ai/deepseek-coder-1.3b-base"
    DATASET_ROOT="data/"
    LANGUAGE="python"
    python -m accelerate.commands.launch --config_file test_config.yaml eval_pal.py --logdir ${MODEL_NAME_OR_PATH} --dataroot ${DATASET_ROOT} 
  8. Install dependencies for evaluation

    main

    To set up the environment for evaluating DeepSeek-Coder models on code generation benchmarks, install the following Python packages:

    pip install accelerate
    pip install attrdict
    pip install transformers
    pip install pytorch
    pip install accelerate
    pip install attrdict
    pip install transformers
    pip install pytorch
  9. Evaluate Instruction-tuned models

    main

    To evaluate instruction-based models (e.g., deepseek-coder-33b-instruct), use the eval_instruct.py script. This script allows you to specify the model, output path, and language.

    Required arguments:

    • --model: The full model identifier (e.g., deepseek-ai/deepseek-coder-33b-instruct).
    • --output_path: The file path where the results will be saved in .jsonl format.
    • --language: The programming language to evaluate.
    • --temp_dir: A directory for temporary files used during evaluation.
    LANG="python"
    OUPUT_DIR="output"
    MODEL="deepseek-coder-33b-instruct"
    
    CUDA_VISIBLE_DEVICES=0,1 python eval_instruct.py \
        --model "deepseek-ai/$MODEL" \
        --output_path "$OUPUT_DIR/${LANG}.$MODEL.jsonl" \
        --language $LANG \
        --temp_dir $OUPUT_DIR