You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pre-trained models for Natural Languages (NL) like BERT and GPT have been recently shown to transfer well to Programming Languages (PL) and largely benefit a broad set of code-related tasks. Despite their success, most current methods either rely on an encoder-only (or decoder-only) pre-training that is suboptimal for generation (resp. understanding) tasks or process the code snippet in the same way as NL, neglecting the special characteristics of PL such as token types. We present CodeT5, a unified pre-trained encoder-decoder Transformer model that better leverages the code semantics conveyed from the developer-assigned identifiers. Our model employs a unified framework to seamlessly support both code understanding and generation tasks and allows for multi-task learning. Besides, we propose a novel identifier-aware pre-training task that enables the model to distinguish which code tokens are identifiers and to recover them when they are masked. Furthermore, we propose to exploit the user-written code comments with a bimodal dual generation task for better NL-PL alignment. Comprehensive experiments show that CodeT5 significantly outperforms prior methods on understanding tasks such as code defect detection and clone detection, and generation tasks across various directions including PL-NL, NL-PL, and PL-PL. Further analysis reveals that our model can better capture semantic information from code.
7
-
8
-

9
-
10
-
## Requirements
11
-
* Pytorch==1.7.1
12
-
* tensorboard==2.4.1
13
-
* transformers==4.6.1
14
-
* tree-sitter==0.2.2
2
+
This is the official PyTorch implementation for the following EMNLP 2021 paper from Salesforce Research:
3
+
4
+
**Title**: [CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation](https://arxiv.org/pdf/2109.00859.pdf)
CodeT5 is now in [hugginface](https://huggingface.co/)!
14
+
15
+
You can simply load the model ([CodeT5-small](https://huggingface.co/Salesforce/codet5-small) and [CodeT5-base](https://huggingface.co/Salesforce/codet5-base)) and do the inference:
16
+
17
+
```python
18
+
from transformers import RobertaTokenizer, T5ForConditionalGeneration
This repo provides the code for reproducing the experiments in [CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation](https://arxiv.org/pdf/2109.00859.pdf).
34
+
CodeT5 is a new pre-trained encoder-decoder model for programming languages, which is pre-trained on 8.35M functions in 8 programming languages (Python, Java, JavaScript, PHP, Ruby, Go, C, and C#).
35
+
In total, it achieves state-of-the-art results on 14 sub-tasks in a code intelligence benchmark - [CodeXGLUE](https://github.com/microsoft/CodeXGLUE).
36
+
37
+
Paper link: https://arxiv.org/abs/2109.00859
38
+
39
+
Blog link: https://blog.einstein.ai/codet5/
40
+
41
+
The code currently include two pre-trained checkpoints ([CodeT5-small](https://huggingface.co/Salesforce/codet5-small) and [CodeT5-base](https://huggingface.co/Salesforce/codet5-base)) and scripts to fine-tine them on 4 generation tasks (code summarization, code generation, translation, and refinement) plus 2 understanding tasks (code defect detection and clone detection) in CodeXGLUE.
42
+
43
+
In practice, CodeT5 can be deployed as an AI-powered coding assistant to boost the productivity of software developers.
44
+
At Salesforce, we build an [AI coding assistant demo](https://github.com/salesforce/CodeT5/raw/main/codet5.gif) using CodeT5 to provide three capabilities for Apex developers as a VS Code plugin:
45
+
46
+
-**Text-to-code generation**: generate code based on the natural language description.
47
+
-**Code autocompletion**: complete the whole function of code given the target function name.
48
+
-**Code summarization**: generate the summary of a function in natural language description.
49
+
50
+
## Table of Contents
51
+
52
+
1.[Citation](#citation)
53
+
2.[License](#license)
54
+
3.[Dependency](#dependency)
55
+
4.[Download](#download)
56
+
5.[Fine-tuning](#fine-tuning)
57
+
6.[Get Involved](#get-involved)
58
+
59
+
## Citation
60
+
If you find this code to be useful for your research, please consider citing.
61
+
```
62
+
@article{CodeT5,
63
+
title={CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation},
64
+
author={Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi},
65
+
year={2021},
66
+
journal={arXiv preprint arXiv:2109.00859},
67
+
}
68
+
```
69
+
70
+
## License
71
+
The code is released under the BSD-3 License (see `LICENSE.txt` for details), but we also ask that users respect the following:
72
+
73
+
This software should not be used to promote or profit from:
74
+
75
+
violence, hate, and division,
76
+
77
+
environmental destruction,
78
+
79
+
abuse of human rights, or
80
+
81
+
the destruction of people's physical and mental health.
82
+
83
+
We encourage users of this software to tell us about the applications in which they are putting it to use by emailing codeT5@salesforce.com, and to use [appropriate](https://arxiv.org/abs/1810.03993)[documentation](https://www.partnershiponai.org/about-ml/) when developing high-stakes applications of this model.
The repository structure is shown in the following after download:
20
105
```
21
106
├── CODE_OF_CONDUCT.md
22
107
├── README.md
23
108
├── SECURITY.md
24
-
├── CodeT5.png
25
-
├── _utils.py
109
+
├── codet5.gif
26
110
├── configs.py
27
111
├── models.py
28
112
├── run_clone.py
29
113
├── run_gen.py
30
114
├── utils.py
115
+
├── _utils.py
31
116
├── LICENSE.txt
32
117
├── data
33
-
│ ├── clone
34
-
│ ├── concode
35
-
│ ├── defect
36
-
│ ├── refine
37
-
│ │ ├── medium
38
-
│ │ └── small
39
-
│ ├── summarize
40
-
│ │ ├── go
41
-
│ │ ├── java
42
-
│ │ ├── javascript
43
-
│ │ ├── php
44
-
│ │ ├── python
45
-
│ │ └── ruby
46
-
│ └── translate
118
+
│ ├── clone
119
+
│ ├── concode
120
+
│ ├── defect
121
+
│ ├── refine
122
+
│ │ ├── medium
123
+
│ │ └── small
124
+
│ ├── summarize
125
+
│ │ ├── go
126
+
│ │ ├── java
127
+
│ │ ├── javascript
128
+
│ │ ├── php
129
+
│ │ ├── python
130
+
│ │ └── ruby
131
+
│ └── translate
47
132
├── evaluator
48
-
│ ├── bleu.py
49
-
│ ├── smooth_bleu.py
50
-
│ └── CodeBLEU
133
+
│ ├── bleu.py
134
+
│ ├── smooth_bleu.py
135
+
│ └── CodeBLEU
51
136
├── pretrained_models
52
-
│ └── codet5_base
53
-
│ └── codet5_small
137
+
│ ├── codet5_base
138
+
│ └── codet5_small
54
139
├── sh
55
-
│ ├── exp_with_args.sh
56
-
│ ├── run_exp.py
57
-
│ ├── results
58
-
│ ├── saved_models
59
-
│ └── tensorboard
140
+
│ ├── exp_with_args.sh
141
+
│ ├── run_exp.py
142
+
│ ├── results
143
+
│ ├── saved_models
144
+
│ └── tensorboard
60
145
└── tokenizer
61
146
└── salesforce
62
-
├── codet5-merges.txt
63
-
└── codet5-vocab.json
147
+
├── codet5-merges.txt
148
+
└── codet5-vocab.json
64
149
```
65
150
66
151
## Fine-tuning
@@ -70,7 +155,7 @@ You can use `run_exp.py` to run a broad set of experiments by simply passing the
70
155
In total, we support four models (i.e., ['roberta', 'codebert', 'codet5_small', 'codet5_base']) and six tasks (i.e., ['summarize', 'concode', 'translate', 'refine', 'defect', 'clone']).
71
156
For each task, we use the `sub_task` to specify which specific datasets to fine-tine on.
72
157
73
-
For example, if you want to run CodeT5-base on the code summarization task for Ruby, you can type the following command:
158
+
For example, if you want to run CodeT5-base model on the code summarization task for Ruby, you can simply run:
@@ -83,15 +168,12 @@ summary_dir: where to save the training curves
83
168
data_num: how many data instances to use, the default -1 is for using the full data
84
169
gpu: the index of the GPU to use in the cluster
85
170
```
86
-
You can also directly revise the suggested arguments in the `get_args_by_task_model` function of `run_exp.py`. The saved training curves in `summary_dir` can be visualized using [tensorboard](https://pypi.org/project/tensorboard/).
171
+
You can also directly revise the suggested arguments in the [get_args_by_task_model](https://github.com/salesforce/CodeT5/blob/4f8818aea1bf170f019381671087e4c4f9608005/sh/run_exp.py#L14) function.
172
+
Please refer to the argument flags in `configs.py` for the full available options.
173
+
The saved training curves in `summary_dir` can be visualized using [tensorboard](https://pypi.org/project/tensorboard/).
174
+
175
+
## Get Involved
176
+
177
+
Please create a GitHub issue if you have any questions, suggestions, requests or bug-reports.
178
+
We welcome PRs!
87
179
88
-
## Citation
89
-
If you find this code to be useful for your research, please consider citing.
90
-
```
91
-
@article{CodeT5,
92
-
title={CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation},
93
-
author={Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi},
0 commit comments