Skip to content

Commit 0391b55

Browse files
authored
Update README.md
1 parent 000d6dc commit 0391b55

File tree

1 file changed

+136
-54
lines changed

1 file changed

+136
-54
lines changed

README.md

Lines changed: 136 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,151 @@
11
# CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation
2-
This is the official PyTorch implementation for the following EMNLP 2021 paper from Salesforce Research: \
3-
**Title**: [CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation](https://arxiv.org/pdf/2109.00859.pdf) [[blog]](https://blog.einstein.ai/codet5/) \
4-
**Authors**: Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi \
5-
**Abstract**:
6-
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-
![CodeT5 framework](CodeT5.png)
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)
5+
6+
**Authors**: [Yue Wang](https://yuewang-cuhk.github.io/), [Weishi Wang](https://www.linkedin.com/in/weishi-wang/), [Shafiq Joty](https://raihanjoty.github.io/), and [Steven C.H. Hoi](https://sites.google.com/view/stevenhoi/home)
7+
8+
![CodeT5 demo](codet5.gif)
9+
10+
## Updates
11+
**Sep 24, 2021**
12+
13+
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
19+
20+
tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-base')
21+
model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-base')
22+
23+
text = "def greet(user): print(f'hello <extra_id_0>!')"
24+
input_ids = tokenizer(text, return_tensors="pt").input_ids
25+
26+
# simply generate one code span
27+
generated_ids = model.generate(input_ids, max_length=8)
28+
print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
29+
# this prints "{user.username}"
30+
```
31+
32+
## Introduction
33+
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.
84+
85+
## Dependency
86+
- Pytorch 1.7.1
87+
- tensorboard 2.4.1
88+
- transformers 4.6.1
89+
- tree-sitter 0.2.2
1590

1691
## Download
1792
* [Pre-trained checkpoints & Fine-tuning data](https://console.cloud.google.com/storage/browser/sfr-codet5-data-research)
1893

19-
## File Structure
94+
Instructions for download:
95+
```
96+
pip install gsutil
97+
98+
gsutil -m cp -r \
99+
"gs://sfr-codet5-data-research/data/" \
100+
"gs://sfr-codet5-data-research/pretrained_models/" \
101+
.
102+
```
103+
104+
The repository structure is shown in the following after download:
20105
```
21106
├── CODE_OF_CONDUCT.md
22107
├── README.md
23108
├── SECURITY.md
24-
├── CodeT5.png
25-
├── _utils.py
109+
├── codet5.gif
26110
├── configs.py
27111
├── models.py
28112
├── run_clone.py
29113
├── run_gen.py
30114
├── utils.py
115+
├── _utils.py
31116
├── LICENSE.txt
32117
├── 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
47132
├── evaluator
48-
   ├── bleu.py
49-
   ├── smooth_bleu.py
50-
   └── CodeBLEU
133+
├── bleu.py
134+
├── smooth_bleu.py
135+
└── CodeBLEU
51136
├── pretrained_models
52-
   └── codet5_base
53-
   └── codet5_small
137+
── codet5_base
138+
└── codet5_small
54139
├── 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
60145
└── tokenizer
61146
└── salesforce
62-
      ├── codet5-merges.txt
63-
      └── codet5-vocab.json
147+
├── codet5-merges.txt
148+
└── codet5-vocab.json
64149
```
65150

66151
## Fine-tuning
@@ -70,7 +155,7 @@ You can use `run_exp.py` to run a broad set of experiments by simply passing the
70155
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']).
71156
For each task, we use the `sub_task` to specify which specific datasets to fine-tine on.
72157

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:
74159
```
75160
python run_exp.py --model_tag codet5_base --task summarize --sub_task ruby
76161
```
@@ -83,15 +168,12 @@ summary_dir: where to save the training curves
83168
data_num: how many data instances to use, the default -1 is for using the full data
84169
gpu: the index of the GPU to use in the cluster
85170
```
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!
87179

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},
94-
year={2021},
95-
journal={arXiv preprint arXiv:2109.00859},
96-
}
97-
```

0 commit comments

Comments
 (0)