README.md 5.6 KB
Newer Older
dcuai's avatar
dcuai committed
1
# StarCoder2
dengjb's avatar
dengjb committed
2
3
4
5
6
7
8
9
10
StarCoder2模型是一系列3B、7B和15B模型,使用来自Stack-v2数据集的3.3 至4.3万亿个代码标记进行训练,包含600多种编程语言。
## 论文
`StarCoder 2 and The Stack v2: The Next Generation`<br>
[StarCoder2](https://arxiv.org/pdf/2402.19173)
## 模型结构
StarCoder2的模型结构主要基于StarCoderBase模型架构进行了微小的改动,首先使用RoPE旋转位置编码。其次使用了GQA模块替换了MQA模块。
<div align=center>
    <img src="./asserts/model_architecture.png"/>
</div>
dengjb's avatar
dengjb committed
11

dengjb's avatar
dengjb committed
12
13
14
15
16
17
## 算法原理
使用GQA模块能够带来更好的速度,使用GQA的head数量不同则会带来速度和性能平衡转换<br>
使用了RoPE位置旋转编码来替代Embedding编码,使得模型获得更好的外推性。<br>
<div align=center>
    <img src="./asserts/model_blocks.png"/>
</div>
dengjb's avatar
dengjb committed
18

dengjb's avatar
dengjb committed
19
20
## 环境配置
-v 路径、docker_name和imageID根据实际情况修改
dengjb's avatar
dengjb committed
21

dengjb's avatar
dengjb committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
### Docker(方法一)

```bash
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-centos7.6-dtk24.04-py310
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/starcoder2_pytorch
pip install -r requirements.txt  -i http://mirrors.huaweicloud.com/repository/pypi/simple
export HF_ENDPOINT=https://hf-mirror.com
```

### Dockerfile(方法二)

```bash
cd docker
dengjb's avatar
dengjb committed
37
docker build --no-cache -t starcoder2:latest .
dengjb's avatar
dengjb committed
38
39
40
41
42
43
44
45
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash

cd /your_code_path/starcoder2_pytorch
pip install -r requirements.txt  -i http://mirrors.huaweicloud.com/repository/pypi/simple
export HF_ENDPOINT=https://hf-mirror.com
```

### Anaconda(方法三)
chenzk's avatar
chenzk committed
46
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.sourcefind.cn/tool/)开发者社区下载安装。
dengjb's avatar
dengjb committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
```
DTK驱动: dtk24.04
python: python3.10
torch: 2.1.0
```
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`

其它非深度学习库安装方式如下:
```bash
pip install -r requirements.txt  -i http://mirrors.huaweicloud.com/repository/pypi/simple
export HF_ENDPOINT=https://hf-mirror.com
```

## 数据集
dengjb's avatar
dengjb committed
61
62
63
finetune训练样例数据采用bigcode/the-stack-smol 下的子集/data/rust <br>
- 官方下载地址
  - [下载地址](https://hf-mirror.com/datasets/bigcode/the-stack-smol)<br>
dengjb's avatar
dengjb committed
64
```angular2html
dengjb's avatar
dengjb committed
65
66
├── data                                                                                                                            
│   ├── assembly                                                                                                                     
dengjb's avatar
dengjb committed
67
68
69
70
71
72
73
74
75
76
│   │   └── data.json
│   ├── rust                                                                                          
│   │   └── data.json  
......
```
<div align=center>
    <img src="./asserts/dataset.png"/>
</div>

## 训练
dengjb's avatar
dengjb committed
77
### 单机两卡
dengjb's avatar
dengjb committed
78
79
80
81
82
83
84
85
具体参数更改请在train.sh文件中进行,以下为必要参数 <br>
dataset_name="{数据集地址}" <br>
model_name="{预训练模型加载地址}" <br>
```bash
bash ./train.sh
```
## 推理
基于Huggingface's Transformers进行推理.<br>
dengjb's avatar
dengjb committed
86
87
88
89
- 官方下载地址:
  - [官方权重下载地址](https://huggingface.co/bigcode/starcoder2-7b) 
  
默认需存放至weights文件夹中<br>
dengjb's avatar
dengjb committed
90
91
92
93
94
95
96
97
98
99
100
101
也可自行更改 inference.py文件中的 model_name 参数<br>
```bash
HIP_VISIBLE_DEVICES=0 python inference.py
```
## Result
prompt:def print_hello_world():",<br>
result:
<div align=center>
    <img src="./asserts/result.png"/>
</div>

### 精度
dengjb's avatar
dengjb committed
102
103
104
105
106
107
训练集bigcode/the-stack-smol/data/rust

|  device  | train_loss | steps |
|:--------:|:----------:|:-----:|
|  A800*2  |   1.2758   |  100  |
|  K100*2  |   1.2772   |  100  |
dengjb's avatar
dengjb committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

## 应用场景
### 算法类别
代码生成

### 热点应用行业
制造,能源,教育

## 预训练权重
模型目录结构如下:
```
# starcoder2-7b/
├── config.json                                                                                           
├── generation_config.json                                                                                
├── merges.txt                                                                                            
├── model-00001-of-00003.safetensors                                                                      
├── model-00002-of-00003.safetensors                                                                      
├── model-00003-of-00003.safetensors                                                                      
├── model.safetensors.index.json                                                                          
├── README.md                                                                                             
├── special_tokens_map.json                                                                               
├── tokenizer_config.json                                                                                 
├── tokenizer.json                                                                                        
└── vocab.json 
```

## 源码仓库及问题反馈
chenzk's avatar
chenzk committed
135
- https://developer.sourcefind.cn/codes/modelzoo/starcoder2_pytorch
dengjb's avatar
dengjb committed
136
137

## 参考资料
dengjb's avatar
dengjb committed
138
139
140
- https://github.com/bigcode-project/starcoder2
- https://huggingface.co/bigcode/starcoder2-7b
- https://huggingface.co/datasets/bigcode/the-stack-smol