NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Code Needs Comments: Enhancing Code LLMs with Comment Augmentation

Demin Song, Honglin Guo, Yunhua Zhou, Shuhao Xing, Yudong Wang, Zifan Song, Wenwei Zhang, Qipeng Guo, Hang Yan, Xipeng Qiu, Dahua Lin

Introduction

Large Language Models (LLMs) have demonstrated impressive capabilities in code generation and understanding. However, a significant bottleneck remains: the scarcity of high-quality alignment between Natural Language (NL) and Programming Language (PL). In massive datasets like StarCoder, comments—the primary source of NL-PL alignment—account for only 16-20% of non-white characters. This "silent code" limits the model's ability to grasp the semantic intent behind the syntax. This paper introduces a self-augmentation pipeline that enables code LLMs to document their own training data, creating a richer learning signal.

The Comment Augmentation Pipeline

The authors propose a three-stage workflow to densify the NL-PL relationship within existing codebases. This process, termed Comment Augmentation, transforms "silent" code into well-documented training samples.

The three-stage self-augmentation pipeline: Instruction Tuning, NL-Aligned Code Data Generation, and Self Augmentation.
  1. Instruction Tuning: A base model (e.g., CodeLlama-7b) is fine-tuned into a specialized "Code Comment Generator" using a small, high-quality dataset of code-comment pairs curated by GPT-4.
  2. Constrained Generation: To ensure the integrity of the original code, the model uses a line-by-line generation algorithm. It copies the original code exactly and only generates text when it encounters a comment marker (e.g., #). This method prevents hallucinations and ensures the code remains functional.
  3. Data Filtering: Two layers of filtering are applied. An Implicit Filter trains the model to output an <|EOT|> token for code that doesn't require comments. An Explicit Filter uses rule-based checks to discard malformed or irrelevant generations.
  4. Self-Augmentation: The original code is enriched with these synthetic comments and used for further pre-training, creating a "Better Code LLM."

The Impact of Comment Density

A core contribution of this work is the definition and analysis of Comment Density—the ratio of non-white characters in comments to the total non-white characters in a file. The researchers found a strong positive correlation between training set comment density and downstream performance.

Performance (Pass@1) improves significantly as the comment density (CD) of the training data increases.

As shown in the experimental results, increasing the comment density from 0.00 to 0.33 leads to a consistent and significant rise in Pass@1 results across various token counts. This suggests that the presence of natural language "bridges" is more critical for model performance than previously recognized.

Experimental Results

The self-augmentation approach was tested across several model families, including Llama 2, Code Llama, and InternLM2. The results were consistently positive:

  • CodeLlama-7b: Performance on the HumanEval benchmark improved from 31.10% to 39.02%.
  • Llama2-7b: Saw a dramatic jump from 12.25% to 23.17% Pass@1.
  • InternLM2-7b: Improved from 43.29% to 49.39%.

Notably, the constrained generation approach not only maintained code correctness but also provided a speedup of up to 2.4x compared to standard free-form generation. Furthermore, the synthetic comments proved to be "portable"—comments generated by one model (CodeLlama) effectively improved the training of others (InternLM2), indicating they capture fundamental programming logic.

Conclusion

This research highlights that code LLMs are not just limited by the amount of code they see, but by the quality of the semantic context provided. By using models to document their own training data, we can overcome the natural scarcity of comments in open-source repositories. The resulting "densified" datasets allow models to learn the deep relationships between logic and language, ultimately leading to more capable and reliable coding assistants.

Summary updated

BibTeX
@inproceedings{song2024code,
  author = {Demin Song and Honglin Guo and Yunhua Zhou and Shuhao Xing and Yudong Wang and Zifan Song and Wenwei Zhang and Qipeng Guo and Hang Yan and Xipeng Qiu and Dahua Lin},
  title = {{Code Needs Comments: Enhancing Code LLMs with Comment Augmentation}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.809},
}
Figure