# starcoder2 **Repository Path**: devai/starcoder2 ## Basic Information - **Project Name**: starcoder2 - **Description**: 由BigCode项目团队打造的一款强大的代码生成模型系列,包含了3B、7B以及15B三个版本。该模型在超过600种编程语言的海量数据上训练而成,数据来源广泛,包括The Stack v2、Wikipedia、Arxiv和GitHub上的问题等。星代码2采用了先进的组查询注意力机制,并利用了滑动窗口策略,以支持大规模上下文理解。模型设计旨在进行高效的代码补全,而非直接响应指令式请求。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: finetune - **Homepage**: https://blog.csdn.net/gitblog_01233/article/details/143040215 - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-04 - **Last Updated**: 2025-08-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # StarCoder 2
[🤗 Models] | [Paper] | [VSCode]
StarCoder2 is a family of code generation models (3B, 7B, and 15B), trained on 600+ programming languages from [The Stack v2]() and some natural language text such as Wikipedia, Arxiv, and GitHub issues. The models use Grouped Query Attention, a context window of 16,384 tokens, with sliding window attention of 4,096 tokens. The 3B & 7B models were trained on 3+ trillion tokens, while the 15B was trained on 4+ trillion tokens. # Disclaimer Before you can use the models, go to `hf.co/bigcode/starcoder2-15b` and accept the agreement, and make sure you are logged into the Hugging Face hub: ```bash huggingface-cli login ``` # Table of Contents 1. [Quickstart](#quickstart) - [Installation](#installation) - [Model usage and memory footprint](#model-usage-and-memory-footprint) - [Text-generation-inference code](#text-generation-inference) 2. [Fine-tuning](#fine-tuning) - [Setup](#setup) - [Training](#training) 3. [Evaluation](#evaluation) # Quickstart StarCoder2 models are intended for code completion, they are not instruction models and commands like "Write a function that computes the square root." do not work well. ## Installation First, we have to install all the libraries listed in `requirements.txt` ```bash pip install -r requirements.txt # export your HF token, found here: https://huggingface.co/settings/account export HF_TOKEN=xxx ``` ## Model usage and memory footprint Here are some examples to load the model and generate code, with the memory footprint of the largest model, `StarCoder2-15B`. Ensure you've installed `transformers` from source (it should be the case if you used `requirements.txt`) ```bash pip install git+https://github.com/huggingface/transformers.git ``` ### Running the model on CPU/GPU/multi GPU * _Using full precision_ ```python # 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])) ``` * _Using `torch.bfloat16`_ ```python # 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])) ``` ```bash >>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB") Memory footprint: 32251.33 MB ``` ### Quantized Versions through `bitsandbytes` * _Using 8-bit precision (int8)_ ```python # 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])) ``` ```bash >>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB") # load_in_8bit Memory footprint: 16900.18 MB # load_in_4bit >>> print(f"Memory footprint: {model.get_memory_footprint() / 1e6:.2f} MB") Memory footprint: 9224.60 MB ``` You can also use `pipeline` for the generation: ```python 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():") ) ``` ## Text-generation-inference: TODO ```bash docker run -p 8080:80 -v $PWD/data:/data -e HUGGING_FACE_HUB_TOKEN=