Skip to main content
QUICK REVIEW

[Paper Review] CodeBERTScore: Evaluating Code Generation with Pretrained Models of Code

Shuyan Zhou, Uri Alon|arXiv (Cornell University)|Feb 10, 2023
Software Engineering ResearchComputer Science6 citations
TL;DR

CodeBERTScore is a novel code generation evaluation metric that extends BERTScore by incorporating both the generated code and its natural language context into contextual embeddings from pretrained code models. It achieves higher correlation with human preference and functional correctness than existing metrics across four programming languages, outperforming BLEU, CodeBLEU, and execution-based baselines.

ABSTRACT

Since the rise of neural natural-language-to-code models (NL->Code) that can generate long expressions and statements rather than a single next-token, one of the major problems has been reliably evaluating their generated output. In this paper, we propose CodeBERTScore: an evaluation metric for code generation, which builds on BERTScore (Zhang et al., 2020). Instead of encoding only the generated tokens as in BERTScore, CodeBERTScore also encodes the natural language input preceding the generated code, thus modeling the consistency between the generated code and its given natural language context as well. We perform an extensive evaluation of CodeBERTScore across four programming languages. We find that CodeBERTScore achieves a higher correlation with human preference and with functional correctness than all existing metrics. That is, generated code that receives a higher score by CodeBERTScore is more likely to be preferred by humans, as well as to function correctly when executed. We release five language-specific pretrained models to use with our publicly available code. Our language-specific models have been downloaded more than 1,000,000 times from the Huggingface Hub. Our code and data are available at https://github.com/neulab/code-bert-score

Motivation & Objective

  • To address the limitations of existing code generation evaluation metrics, which rely on lexical matching and fail to capture semantic equivalence.
  • To improve evaluation by modeling the consistency between generated code and its natural language input context.
  • To develop a metric that correlates more strongly with human preference and functional correctness than prior approaches.
  • To create a scalable, unsupervised evaluation method that does not require test cases or execution, reducing dependency on costly human-annotated datasets.

Proposed method

  • CodeBERTScore encodes both the generated code and the reference code using language models fine-tuned on code, including the natural language instruction or comment preceding the code.
  • It computes token-level cosine similarity between contextual embeddings of generated and reference code tokens, capturing semantic alignment beyond lexical matching.
  • Precision and recall are computed using the best-matching token pairs, with a focus on context-aware alignment rather than exact token overlap.
  • The metric integrates the natural language context into the encoding process, enabling better modeling of intent and programmatic semantics.
  • It uses multilingual, language-specific CodeBERT-style models fine-tuned on code, allowing cross-lingual evaluation.
  • The method is fully unsupervised and does not require test execution or hand-written test cases, enabling broad applicability.

Experimental results

Research questions

  • RQ1Can a code generation evaluation metric that incorporates natural language context achieve higher correlation with human preference than existing metrics?
  • RQ2Does modeling semantic similarity through contextual embeddings improve alignment with functional correctness compared to n-gram or AST-based metrics?
  • RQ3How does CodeBERTScore perform across multiple programming languages compared to state-of-the-art baselines like BLEU, CodeBLEU, and execution-based evaluation?
  • RQ4Can a metric based on pretrained code models outperform metrics relying on exact lexical or structural matching in diverse code generation scenarios?
  • RQ5To what extent does including the natural language input in the encoding process enhance the evaluation of code quality?

Key findings

  • CodeBERTScore achieves significantly higher correlation with human preference than all prior metrics, including BLEU, CrystalBLEU, CodeBLEU, and execution-based baselines.
  • Generated code that scores higher on CodeBERTScore is more likely to be functionally correct when executed, demonstrating strong alignment with functional correctness.
  • The metric outperforms existing methods on four programming languages: Java, Python, JavaScript, and C++.
  • The five language-specific pretrained models released by the authors have been downloaded over 1,000,000 times from the HuggingFace Hub, indicating strong community adoption.
  • CodeBERTScore maintains high performance even when evaluating non-identical but semantically equivalent code, correctly preferring functionally correct implementations over syntactically similar but incorrect ones.
  • The method is computationally feasible in practice, as it requires only a single forward pass per model, and GPU usage is justified given the widespread availability of GPUs in modern ML pipelines.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.