Abstract
While Chain-of-Thought (CoT) prompting has proven effective for large-scale language models like GPT-4, smaller models (3B–7B parameters) often experience performance degradation when forced to generate separate reasoning plans. We introduce MANGO (comMents As Natural loGic pivOts), a framework that leverages inline comments as structural anchors to bridge the gap between natural language requirements and programming syntax. By utilizing Comment Contrastive Learning (CCL) and Logical Comment Prompting (LCP), MANGO significantly improves the coding performance and robustness of mid-sized open-source LLMs on standard benchmarks.
1. Introduction
Generating complex code requires decomposing high-level problems into logical steps. State-of-the-art models typically use Chain-of-Thought (CoT) to "think out loud" before coding. However, a significant "capability gap" exists: smaller models often lack the instruction-following maturity to benefit from separate planning steps, which can lead to lower pass rates.
This paper proposes that for mid-sized models, the most natural reasoning medium is already present in their training data: inline comments. Comments represent the unique intersection where natural language (NL) and programming language (PL) coexist within the same structural space. By treating comments as "natural logic pivots," models can maintain logical consistency without switching between disparate cognitive modes.
2. Methodology: The MANGO Framework
MANGO consists of two primary components designed to align the model's internal logic with the structure of source code:
2.1 Comment Contrastive Learning (CCL)
During the fine-tuning phase, models are presented with pairs of identical code snippets—one containing comments and one without. A contrastive loss function with a specific "margin" is applied to encourage the model to prefer the commented version. This process teaches the model that comments are valuable anchors for the underlying logic of the code.
2.2 Logical Comment Prompting (LCP)
To replace the standard "think step-by-step" CoT prompt, we introduce LCP. This method instructs the model to generate code "with comments to explain the logic." By keeping the reasoning inline and physically adjacent to the code it describes, LCP helps the model maintain structural alignment and reduces the confusion often caused by separate planning blocks.
3. Experimental Setup
The researchers evaluated MANGO using StarCoder and WizardCoder (3B and 7B variants) as backbone models. Training was conducted on CodeM-Python, a dataset of 9,600 examples distilled from GPT-4. Performance was measured on two industry-standard benchmarks: HumanEval (164 problems) and MBPP (500 problems).
4. Results and Analysis
MANGO consistently outperformed standard Supervised Fine-Tuning (SFT) and CoT baselines across all tested scales:
- Performance Gains: On HumanEval, MANGO improved the pass@10 rate of StarCoder-7B by 7.52 percentage points. On MBPP, WizardCoder-7B achieved a significant boost, demonstrating the framework's effectiveness across different model architectures.
- The CoT Capability Gap: The results confirm that CoT can be detrimental to smaller models. For instance, in the WizardCoder-3B HumanEval tests, the
\wCoTsetting performed significantly worse than standard SFT, whereas MANGO provided a consistent improvement. - Error Reduction: Analysis of failure cases showed that MANGO reduced all error categories, including syntax, runtime, and logic errors. Specifically, the "All Wrong Answer" rate on HumanEval dropped from 10.24% to 8.49% compared to the baseline.
- Ease of Generation: Token-level cross-entropy loss measurements revealed that models find it significantly easier to generate code with inline comments than to generate a separate CoT plan, validating the hypothesis that comments are a more accessible reasoning tool for mid-sized LLMs.
5. Discussion and Conclusion
MANGO demonstrates that for mid-sized LLMs, structural alignment is more critical than separate reasoning. By focusing on comments as the "missing link" between NL and PL, MANGO provides a robust and prompt-insensitive method for improving code generation.
Future work will explore the sensitivity of the contrastive loss "margin"—which currently shows performance degradation if set too low—and the application of comment pivots to more complex, multi-turn autonomous agent tasks where logical transparency is essential.
Summary updated