StarCoder 2 Documentation

repository·main·Indexed 24 days ago

https://github.com/bigcode-project/starcoder2

A family of code generation models (3B, 7B, and 15B) trained on over 600 programming languages, designed for code completion tasks. Documentation covers installation, running models in full precision, bfloat16, float16, or quantized 8-bit/4-bit precision using bitsandbytes, deployment via Text-Generation-Inference (TGI), and fine-tuning using PEFT (LoRA) and the TRL library.

Tokens
2K
Snippets
8
Records
8
Agent score
35%

What's inside StarCoder 2

  1. Install StarCoder 2

    main

    To install StarCoder 2, install the dependencies listed in requirements.txt and export your Hugging Face token to access the models.

    pip install -r requirements.txt
    export HF_TOKEN=xxx
    pip install -r requirements.txt
    # export your HF token, found here: https://huggingface.co/settings/account
    export HF_TOKEN=xxx
  2. Setup for Fine-tuning StarCoder 2

    main

    To prepare for fine-tuning, install PyTorch (e.g., for CUDA 12.1) and the project requirements. You must also be logged into wandb and the Hugging Face Hub to push checkpoints.

    # Example for CUDA 12.1
    conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
    
    # Install requirements (installs transformers from source)
    pip install -r requirements.txt
    
    # Login to services
    wandb login
    huggingface-cli login
    conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
    
    pip install -r requirements.txt
    
    wandb login
    huggingface-cli login
  3. Deploy StarCoder 2 with Text-Generation-Inference (TGI)

    main

    You can run StarCoder 2 using Docker with the Text-Generation-Inference (TGI) container. Ensure you provide your Hugging Face token via the HUGGING_FACE_HUB_TOKEN environment variable.

    docker run -p 8080:80 -v $PWD/data:/data -e HUGGING_FACE_HUB_TOKEN=<YOUR BIGCODE ENABLED TOKEN> -d  ghcr.io/huggingface/text-generation-inference:latest --model-id bigcode/starcoder2-15b --max-total-tokens 8192
  4. Run StarCoder 2 using bfloat16 or float16

    main

    To reduce memory usage, you can load the model using torch.bfloat16 or torch.float16. Using torch.bfloat16 requires the accelerate library.

    pip install accelerate

    For float16, use torch_dtype=torch.float16 instead of bfloat16.

    # pip install accelerate
    import torch
    from transformers import AutoTokenizer, AutoModelForCausalLM
    
    checkpoint = "bigcode/starcoder2-15b"
    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    
    # for fp16 use `torch_dtype=torch.float16` instead
    model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", torch_dtype=torch.bfloat16)
    
    inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt").to("cuda")
    outputs = model.generate(inputs)
    print(tokenizer.decode(outputs[0]))
  5. Quantize StarCoder 2 with bitsandbytes

    main

    You can use bitsandbytes to load the model in 8-bit or 4-bit precision to significantly reduce the memory footprint.

    To use 4-bit, set load_in_4bit=True in the BitsAndBytesConfig instead of load_in_8bit=True.

    # pip install bitsandbytes accelerate
    from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
    
    # to use 4bit use `load_in_4bit=True` instead
    quantization_config = BitsAndBytesConfig(load_in_8bit=True)
    
    checkpoint = "bigcode/starcoder2-15b_16k"
    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    model = AutoModelForCausalLM.from_pretrained("bigcode/starcoder2-15b_16k", quantization_config=quantization_config)
    
    inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt").to("cuda")
    outputs = model.generate(inputs)
    print(tokenizer.decode(outputs[0]))
  6. Fine-tune StarCoder 2 using accelerate

    main

    Fine-tuning is performed efficiently using PEFT (LoRA), bitsandbytes (4-bit quantization), and SFTTrainer from the TRL library. Use accelerate launch to start the training script.

    If fine-tuning on custom datasets, ensure you update the --dataset_text_field argument to match the column name containing your code/text.

    accelerate launch finetune.py \
            --model_id "bigcode/starcoder2-3b" \
            --dataset_name "bigcode/the-stack-smol" \
            --subset "data/rust" \
            --dataset_text_field "content" \
            --split "train" \
            --max_seq_length 1024 \
            --max_steps 10000 \
            --micro_batch_size 1 \
            --gradient_accumulation_steps 8 \
            --learning_rate 2e-5 \
            --warmup_steps 20 \
            --num_proc "$(nproc)"
  7. Run StarCoder 2 in full precision

    main

    You can run StarCoder 2 using full precision on either CPU or GPU. Note that for GPU usage, set device = "cuda". To use multiple GPUs, use device_map="auto" in the from_pretrained call.

    Note: This requires installing transformers from source.

    pip install git+https://github.com/huggingface/transformers.git
    # pip install git+https://github.com/huggingface/transformers.git # TODO: merge PR to main
    from transformers import AutoModelForCausalLM, AutoTokenizer
    
    checkpoint = "bigcode/starcoder2-15b"
    device = "cuda" # for GPU usage or "cpu" for CPU usage
    
    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    # to use Multiple GPUs do `model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto")`
    model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
    
    inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt").to(device)
    outputs = model.generate(inputs)
    print(tokenizer.decode(outputs[0]))
  8. Use StarCoder 2 with Hugging Face pipeline

    main

    The pipeline API provides a high-level way to perform text generation with StarCoder 2.

    from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
    checkpoint = "bigcode/starcoder2-15b"
    
    model = AutoModelForCausalLM.from_pretrained(checkpoint)
    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    
    pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
    print( pipe("def hello():") )