Model Overview
Model Features
Model Capabilities
Use Cases
๐ StarCoder2
StarCoder2-3B is a 3B parameter model trained on 17 programming languages, offering code generation capabilities with certain limitations.
๐ Quick Start
Installation
First, make sure to install transformers
from source:
pip install git+https://github.com/huggingface/transformers.git
Running the model on CPU/GPU/multi GPU
- Using full precision
# pip install git+https://github.com/huggingface/transformers.git # TODO: merge PR to main
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "bigcode/starcoder2-3b"
device = "cuda" # for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# for multiple GPUs install accelerate and 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]))
>>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB")
Memory footprint: 12624.81 MB
- Using
torch.bfloat16
# pip install accelerate
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
checkpoint = "bigcode/starcoder2-3b"
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]))
>>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB")
Memory footprint: 6312.41 MB
Quantized Versions through bitsandbytes
- Using 8-bit precision (int8)
# 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-3b"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint, 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]))
>>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB")
# load_in_8bit
Memory footprint: 3434.07 MB
# load_in_4bit
>>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB")
Memory footprint: 1994.90 MB
โจ Features
- Multi - language Support: Trained on 17 programming languages, capable of generating code snippets in various programming contexts.
- Advanced Attention Mechanisms: Utilizes grouped - query and sliding window attention, along with the Fill - in - the - Middle objective.
- Large - scale Training: Pretrained on over 3 trillion tokens with 1.2 million steps.
๐ฆ Installation
To use the model, you need to install the transformers
library from source:
pip install git+https://github.com/huggingface/transformers.git
๐ป Usage Examples
Basic Usage
The following code demonstrates how to load the model and generate code using full precision:
# pip install git+https://github.com/huggingface/transformers.git # TODO: merge PR to main
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "bigcode/starcoder2-3b"
device = "cuda" # for GPU usage or "cpu" for CPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
# for multiple GPUs install accelerate and 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]))
Advanced Usage
Using torch.bfloat16
for more memory - efficient inference:
# pip install accelerate
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
checkpoint = "bigcode/starcoder2-3b"
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]))
๐ Documentation
Model Summary
The StarCoder2 - 3B model is a 3B parameter model trained on 17 programming languages from The Stack v2, excluding opt - out requests. It uses Grouped Query Attention, a context window of 16,384 tokens with a sliding window attention of 4,096 tokens, and was trained using the Fill - in - the - Middle objective on 3+ trillion tokens.
- Project Website: [bigcode - project.org](https://www.bigcode - project.org)
- Paper: Link
- Point of Contact: [contact@bigcode - project.org](mailto:contact@bigcode - project.org)
- Languages: 17 Programming languages
Intended Use
The model was trained on GitHub code and additional selected data sources like Arxiv and Wikipedia. It is not an instruction model, and commands like "Write a function that computes the square root." do not work well.
Attribution & Other Requirements
The pretraining dataset of the model was filtered for permissive licenses and code with no license only. However, the model can generate source code verbatim from the dataset. The code's license might require attribution and/or other specific requirements that must be respected. We provide a [search index](https://huggingface.co/spaces/bigcode/search - v2) that lets you search through the pretraining data to identify where the generated code came from and apply the proper attribution to your code.
Limitations
The model has been trained on source code from 600+ programming languages. The predominant language in source is English, although other languages are also present. The model can generate code snippets given some context, but the generated code is not guaranteed to work as intended. It can be inefficient, contain bugs or exploits. See the paper for an in - depth discussion of the model limitations.
Training
Model
- Architecture: Transformer decoder with grouped - query and sliding window attention and Fill - in - the - Middle objective
- Pretraining steps: 1.2 million
- Pretraining tokens: 3+ trillion
- Precision: bfloat16
Hardware
- GPUs: 160 A100
Software
- Framework: TODO
- Neural networks: PyTorch
๐ง Technical Details
- Model Architecture: The model uses a Transformer decoder architecture with grouped - query attention, sliding window attention, and the Fill - in - the - Middle objective.
- Training Process: It was pretrained for 1.2 million steps on over 3 trillion tokens using bfloat16 precision.
- Hardware Setup: 160 A100 GPUs were used for training.
๐ License
The model is licensed under the BigCode OpenRAIL - M v1 license agreement. You can find the full agreement [here](https://huggingface.co/spaces/bigcode/bigcode - model - license - agreement).
๐ Citation
@misc{lozhkov2024starcoder,
title={StarCoder 2 and The Stack v2: The Next Generation},
author={Anton Lozhkov and Raymond Li and Loubna Ben Allal and Federico Cassano and Joel Lamy - Poirier and Nouamane Tazi and Ao Tang and Dmytro Pykhtar and Jiawei Liu and Yuxiang Wei and Tianyang Liu and Max Tian and Denis Kocetkov and Arthur Zucker and Younes Belkada and Zijian Wang and Qian Liu and Dmitry Abulkhanov and Indraneil Paul and Zhuang Li and Wen - Ding Li and Megan Risdal and Jia Li and Jian Zhu and Terry Yue Zhuo and Evgenii Zheltonozhskii and Nii Osae Osae Dade and Wenhao Yu and Lucas Krauร and Naman Jain and Yixuan Su and Xuanli He and Manan Dey and Edoardo Abati and Yekun Chai and Niklas Muennighoff and Xiangru Tang and Muhtasham Oblokulov and Christopher Akiki and Marc Marone and Chenghao Mou and Mayank Mishra and Alex Gu and Binyuan Hui and Tri Dao and Armel Zebaze and Olivier Dehaene and Nicolas Patry and Canwen Xu and Julian McAuley and Han Hu and Torsten Scholak and Sebastien Paquet and Jennifer Robinson and Carolyn Jane Anderson and Nicolas Chapados and Mostofa Patwary and Nima Tajbakhsh and Yacine Jernite and Carlos Muรฑoz Ferrandis and Lingming Zhang and Sean Hughes and Thomas Wolf and Arjun Guha and Leandro von Werra and Harm de Vries},
year={2024},
eprint={2402.19173},
archivePrefix={arXiv},
primaryClass={cs.SE}
}
โ ๏ธ Important Note
The generated code by the model is not guaranteed to work as intended. It can be inefficient, contain bugs or exploits.
๐ก Usage Tip
Use the [search index](https://huggingface.co/spaces/bigcode/search - v2) to check the source of the generated code and ensure proper attribution.

