ernie-code-560m / README.md
cyk1337's picture
Update README.md
004e350 verified
|
raw
history blame
5.07 kB
metadata
license: mit

ERNIE-Code

ERNIE-Code: Beyond English-Centric Cross-lingual Pretraining for Programming Languages

ernie-code-comp

ERNIE-Code is a unified large language model (LLM) that connects 116 natural languages with 6 programming languages. We employ two pre-training methods for universal cross-lingual pre-training: span-corruption language modeling that learns patterns from monolingual NL or PL; and pivot-based translation language modeling that relies on parallel data of many NLs and PLs. Extensive results show that ERNIE-Code outperforms previous multilingual LLMs for PL or NL across a wide range of end tasks of code intelligence, including multilingual code-to-text, text-to-code, code-to-code, and text-to-text generation. We further show its advantage of zero-shot prompting on multilingual code summarization and text-to-text translation.

ACL 2023 (Findings) | arXiv

Multilingual Text-to-Code / Code-to-Text

First preprocess the input prompt:

  def clean_up_code_spaces(s: str):
      # post process
      # ===========================
      new_tokens = ["<pad>", "</s>", "<unk>", "\n", "\t", "<|space|>"*4, "<|space|>"*2, "<|space|>"]
      for tok in new_tokens:
          s = s.replace(f"{tok} ", tok)

      cleaned_tokens = ["<pad>", "</s>", "<unk>"]
      for tok in cleaned_tokens:
          s = s.replace(tok, "")
      s = s.replace("<|space|>", " ")
      # ===========================
      return s

Then use transformers to load the model:

import torch
from transformers import (
    AutoModelForSeq2SeqLM,
    AutoModelForCausalLM,
    AutoTokenizer
)

model_name = "baidu/ernie-code-560m"

model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# note that you can use aforementioned `clean_up_code_spaces` to proprocess the code

input_code="快速排序"
prompt="translate Chinese to English: \n%s" % (input_code)  # your prompt here

model_inputs = tokenizer(prompt, max_length=512, padding=False, truncation=True, return_tensors="pt")
input_ids = model_inputs.input_ids.cuda() # by default
attention_mask = model_inputs.attention_mask.cuda() # by default

output = model.generate(input_ids=input_ids, attention_mask=attention_mask, 
        num_beams=5, max_length=512) # change to your own decoding methods

# Ensure to customize the post-processing of clean_up_code_spaces output according to specific requirements.
output = tokenizer.decode(output.flatten(), skip_special_tokens=True)

Zero-shot Examples

  • Multilingual code-to-text generation (zero-shot)

code-to-text-examples

zh_code-to-text_examples-1

  • Multilingual text-to-text translation (zero-shot)

zero-shot-mt-examples

BibTeX

@inproceedings{chai-etal-2023-ernie,
    title = "{ERNIE}-Code: Beyond {E}nglish-Centric Cross-lingual Pretraining for Programming Languages",
    author = "Chai, Yekun  and
      Wang, Shuohuan  and
      Pang, Chao  and
      Sun, Yu  and
      Tian, Hao  and
      Wu, Hua",
    booktitle = "Findings of the Association for Computational Linguistics: ACL 2023",
    month = jul,
    year = "2023",
    address = "Toronto, Canada",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2023.findings-acl.676",
    pages = "10628--10650",
    abstract = "Software engineers working with the same programming language (PL) may speak different natural languages (NLs) and vice versa, erecting huge barriers to communication and working efficiency. Recent studies have demonstrated the effectiveness of generative pre-training in computer programs, yet they are always English-centric. In this work, we step towards bridging the gap between multilingual NLs and multilingual PLs for large language models (LLMs). We release ERNIE-Code, a unified pre-trained language model for 116 NLs and 6 PLs. We employ two methods for universal cross-lingual pre-training: span-corruption language modeling that learns patterns from monolingual NL or PL; and pivot-based translation language modeling that relies on parallel data of many NLs and PLs. Extensive results show that ERNIE-Code outperforms previous multilingual LLMs for PL or NL across a wide range of end tasks of code intelligence, including multilingual code-to-text, text-to-code, code-to-code, and text-to-text generation. We further show its advantage of zero-shot prompting on multilingual code summarization and text-to-text translation. We release our code and pre-trained checkpoints.",
}