CoolFace
Modelpublic

Salesforce/codet5-base-multi-sum

sourceHugging Facebsd-3-clauseupdated 4y agoView on Hugging Face
32likes443downloads
Model Card

CodeT5-base for Code Summarization

CodeT5-base model fine-tuned on CodeSearchNet data in a multi-lingual training setting ( Ruby/JavaScript/Go/Python/Java/PHP) for code summarization. It was introduced in this EMNLP 2021 paper CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation by Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi. Please check out more at this repository.

How to use

Here is how to use this model:

python
from transformers import RobertaTokenizer, T5ForConditionalGeneration

if __name__ == '__main__':
    tokenizer = RobertaTokenizer.from_pretrained('Salesforce/codet5-base-multi-sum')
    model = T5ForConditionalGeneration.from_pretrained('Salesforce/codet5-base-multi-sum')

    text = """def svg_to_image(string, size=None):
    if isinstance(string, unicode):
        string = string.encode('utf-8')
        renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
    if not renderer.isValid():
        raise ValueError('Invalid SVG data.')
    if size is None:
        size = renderer.defaultSize()
        image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
        painter = QtGui.QPainter(image)
        renderer.render(painter)
    return image"""

    input_ids = tokenizer(text, return_tensors="pt").input_ids

    generated_ids = model.generate(input_ids, max_length=20)
    print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
    # this prints: "Convert a SVG string to a QImage."

Fine-tuning data

We employ the filtered version of CodeSearchNet data [Husain et al., 2019] from CodeXGLUE benchmark for fine-tuning on code summarization. The data is tokenized with our pre-trained code-specific BPE (Byte-Pair Encoding) tokenizer. One can prepare text (or code) for the model using RobertaTokenizer with the vocab files from codet5-base.

Data statistic

Programming LanguageTrainingDevTest
Python251,82013,91414,918
PHP241,24112,98214,014
Go167,2887,3258,122
Java164,9235,18310,955
JavaScript58,0253,8853,291
Ruby24,9271,4001,261

Training procedure

We fine-tune codet5-base on these six programming languages (Ruby/JavaScript/Go/Python/Java/PHP) in the multi-task learning setting. We employ the balanced sampling to avoid biasing towards high-resource tasks. Please refer to the paper for more details.

Evaluation results

Unlike the paper allowing to select different best checkpoints for different programming languages (PLs), here we employ one checkpoint for all PLs. Besides, we remove the task control prefix to specify the PL in training and inference. The results on the test set are shown as below:

ModelRubyJavascriptGoPythonJavaPHPOverall
Seq2Seq9.6410.2113.9815.9315.0921.0814.32
Transformer11.1811.5916.3815.8116.2622.1215.56
RoBERTa11.1711.9017.7218.1416.4724.0216.57
CodeBERT12.1614.9018.0719.0617.6525.1617.83
PLBART14.1115.5618.9119.3018.4523.5818.32
CodeT5-small14.8715.3219.2520.0419.9225.4619.14
CodeT5-base15.2416.1619.5620.0120.3126.0319.55
CodeT5-base-multi-sum15.2416.1819.9520.4220.2626.1019.69

Citation

bibtex
@inproceedings{
    wang2021codet5,
    title={CodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation}, 
    author={Yue Wang, Weishi Wang, Shafiq Joty, Steven C.H. Hoi},
    booktitle={Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing, EMNLP 2021},
    year={2021},
}