模型简介
模型特点
模型能力
使用案例
🚀 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 |
注意事项
⚠️ 重要提示
模型生成的代码不能保证按预期工作,可能效率低下、包含错误或漏洞。请参阅论文以深入了解模型局限性。
💡 使用建议
该模型不是指令模型,像“编写一个计算平方根的函数”这样的命令效果不佳。在使用时,请提供适当的上下文以获得更好的代码生成效果。



