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

CodeFusion: A Pre-trained Diffusion Model for Code Generation

Mukul Singh, José Cambronero, Sumit Gulwani, Vu Le, Carina Negreanu, Gust Verbruggen

Introduction

Modern code generation relies heavily on large-scale auto-regressive (AR) models such as GPT-4 or CodeLlama. These models predict code token-by-token from left to right, a constraint that limits their ability to revise earlier decisions. If an AR model makes a sub-optimal choice early in a function, it must continue building on that mistake or restart entirely. This "brittleness" often results in low diversity among generated candidates and logical inconsistencies.

CodeFusion addresses this by treating code generation as a global optimization problem. By utilizing a diffusion process, the model iteratively refines an entire program simultaneously, allowing it to "go back" and fix structural errors during the generation process. Despite having only 75 million parameters, CodeFusion demonstrates the ability to outperform massive models with up to 175 billion parameters in specific accuracy metrics.

Architecture and Methodology

CodeFusion employs a specialized Diffusion-based Encoder-Decoder architecture designed to handle the structured nature of programming languages.

The CodeFusion architecture, featuring an encoder for the NL query, a diffusion-based denoiser, and a transformer decoder for final code generation.

The Three-Stage Pipeline

  1. Encoder: A pre-trained CodeT5 encoder transforms the natural language (NL) query into a continuous representation, providing the necessary context for the generation.
  2. Denoiser: At the heart of the model is the denoiser ($N$). It begins with random Gaussian noise ($x^t$) and iteratively removes noise over $T$ timesteps (typically 1200) to recover clean program embeddings, conditioned on the NL representation ($E_s$).
  3. Transformer Decoder: Unlike standard text-diffusion models that map embeddings directly to vocabulary words, CodeFusion passes denoised embeddings through a transformer decoder. This stage uses self-attention to ensure the final output adheres to strict syntactic and semantic rules, significantly improving the validity of the generated code.

Continuous Paragraph Denoising (CPD)

To enhance structural learning, the authors adapted the CPD task for pre-training. Rather than noising random tokens, they targeted "critical" tokens—such as language keywords and identifiers—forcing the model to prioritize the structural backbone of the code during the learning phase.

Experimental Results

The model was evaluated across three distinct domains: Python (CoNaLa), Bash (NL2Bash), and Microsoft Excel Conditional Formatting (CF) rules.

Performance comparison of CodeFusion against state-of-the-art auto-regressive and diffusion models across Python, Bash, and Excel CF Rule datasets.

Performance and Efficiency

As shown in the results table, while GPT-3 (175B) maintains a slight lead in Top-1 accuracy for Python, CodeFusion (75M) consistently outperforms it and other larger models in Top-3 and Top-5 accuracy. This suggests that CodeFusion is more effective at generating a diverse set of high-quality solutions for a single prompt.

Syntactic Validity and Diversity

A key finding was the importance of the Transformer decoder. CodeFusion generates syntactically valid Python code 67.6% of the time, a massive improvement over previous diffusion baselines like Diffusion-LM (19.5%) and GENIE (24.2%). Furthermore, CodeFusion produces more diverse code candidates, with an average edit distance of 0.57 between top-5 candidates, compared to just 0.21 for GPT-3.

Discussion and Limitations

CodeFusion demonstrates that diffusion architectures are exceptionally parameter-efficient for structured tasks. However, the iterative nature of diffusion introduces a trade-off: inference latency. Requiring 1200 steps makes CodeFusion slower than single-pass AR models, with latency scaling quadratically relative to code length. Future work will focus on distillation techniques to reduce the number of required diffusion steps, potentially enabling real-time integration into developer environments.

Summary updated

BibTeX
@inproceedings{singh2023codefusion,
  author = {Mukul Singh and José Cambronero and Sumit Gulwani and Vu Le and Carina Negreanu and Gust Verbruggen},
  title = {{CodeFusion: A Pre-trained Diffusion Model for Code Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.emnlp-main.716},
}
Figure