模型概述
模型特點
模型能力
使用案例
🚀 StarCoder2
StarCoder2-3B是一個具有30億參數的模型,它基於17種編程語言進行訓練,能夠生成代碼片段。該模型在代碼生成領域具有一定的應用價值,但生成的代碼可能存在效率低、包含錯誤或漏洞等問題。
🚀 快速開始
安裝依賴
首先,確保從源代碼安裝transformers
:
pip install git+https://github.com/huggingface/transformers.git
運行模型
在CPU/GPU/多GPU上運行模型
- 使用全精度
# 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
- 使用
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
通過bitsandbytes
進行量化版本
- 使用8位精度(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
✨ 主要特性
- 多語言支持:StarCoder2-3B模型在17種編程語言上進行訓練,能夠處理多種編程語言的代碼生成任務。
- 先進技術應用:採用了分組查詢注意力、16,384個標記的上下文窗口以及4,096個標記的滑動窗口注意力等技術。
- 大規模訓練:在3萬億以上的標記上使用中間填充目標進行訓練。
📦 安裝指南
確保從源代碼安裝transformers
:
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-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]))
高級用法
# 使用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]))
📚 詳細文檔
模型概述
StarCoder2-3B模型是一個具有30億參數的模型,它在來自The Stack v2的17種編程語言上進行訓練,排除了選擇退出請求的數據。該模型採用了分組查詢注意力、16,384個標記的上下文窗口以及4,096個標記的滑動窗口注意力,並在3萬億以上的標記上使用中間填充目標進行訓練。
- 項目網站:bigcode-project.org
- 論文:鏈接
- 聯繫方式:contact@bigcode-project.org
- 支持語言:17種編程語言
預期用途
該模型在GitHub代碼以及其他選定的數據源(如Arxiv和Wikipedia)上進行訓練。因此,它不是一個指令模型,像“編寫一個計算平方根的函數”這樣的命令效果不佳。
生成代碼
可以在StarCoder2的GitHub倉庫中找到微調腳本。
歸屬與其他要求
該模型的預訓練數據集僅過濾了允許使用的許可證和無許可證的代碼。然而,模型可以逐字生成數據集中的源代碼。代碼的許可證可能要求歸屬和/或其他特定要求,必須遵守這些要求。我們提供了一個搜索索引,可以通過預訓練數據進行搜索,以確定生成的代碼來自何處,並對代碼進行適當的歸屬。
侷限性
該模型在600多種編程語言的源代碼上進行訓練。源代碼中主要使用的語言是英語,儘管也存在其他語言。因此,該模型能夠在提供一些上下文的情況下生成代碼片段,但不能保證生成的代碼按預期工作。它可能效率低下、包含錯誤或漏洞。有關模型侷限性的深入討論,請參閱論文。
訓練
模型
- 架構:具有分組查詢和滑動窗口注意力以及中間填充目標的Transformer解碼器
- 預訓練步驟:120萬
- 預訓練標記:3萬億以上
- 精度:bfloat16
硬件
- GPU:160個A100
軟件
- 框架:TODO
- 神經網絡:PyTorch
🔧 技術細節
模型架構
採用Transformer解碼器,結合分組查詢和滑動窗口注意力以及中間填充目標。
訓練參數
- 預訓練步驟:120萬
- 預訓練標記:3萬億以上
- 精度:bfloat16
硬件資源
使用160個A100 GPU進行訓練。
軟件框架
使用PyTorch作為神經網絡框架。
📄 許可證
該模型遵循BigCode OpenRAIL-M v1許可協議。可以在此處找到完整協議。
📚 引用
@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}
}
模型指標
數據集 | 任務類型 | 指標類型 | 指標值 |
---|---|---|---|
CruxEval-I | 文本生成 | pass@1 | 32.7 |
DS-1000 | 文本生成 | pass@1 | 25.0 |
GSM8K (PAL) | 文本生成 | 準確率 | 27.7 |
HumanEval+ | 文本生成 | pass@1 | 27.4 |
HumanEval | 文本生成 | pass@1 | 31.7 |
RepoBench-v1.1 | 文本生成 | 編輯相似度 | 71.19 |
注意事項
⚠️ 重要提示
模型生成的代碼不能保證按預期工作,可能效率低下、包含錯誤或漏洞。請參閱論文以深入瞭解模型侷限性。
💡 使用建議
該模型不是指令模型,像“編寫一個計算平方根的函數”這樣的命令效果不佳。在使用時,請提供適當的上下文以獲得更好的代碼生成效果。



