Introduction
Diffusion Language Models (DLMs) are naturally suited for code infilling, but they have long been hamstrung by the "fixed-size canvas" problem—the requirement to pre-specify the exact length of the missing code. DreamOn solves this by introducing dynamic length control through two special tokens, [expand] and [delete], allowing models to autonomously resize their output. This simple yet effective framework enables DLMs to match or even exceed the performance of state-of-the-art autoregressive models on major code infilling benchmarks.
The "Fixed-Size Canvas" Problem
In the world of Large Language Models (LLMs), most models are "autoregressive," meaning they generate text one token at a time from left to right. This makes "infilling"—filling in a missing block of code between an existing prefix and suffix—cumbersome. To do it, autoregressive models usually have to rearrange the code (moving the suffix to the end) and use specialized prompts.
DLMs offer a more elegant alternative. They start with a "canvas" of mask tokens and refine them all at once in any order. However, DLMs suffer from a critical limitation: you must tell the model exactly how many mask tokens to start with. If the canvas is too small, the code is truncated; if it’s too large, the model generates redundant or incorrect "hallucinated" snippets to fill the space.
DreamOn addresses this bottleneck, allowing the model to decide for itself how much code is actually needed.
DreamOn's Dynamic Length Control
The authors propose a framework that adds native length control to the diffusion process without changing the underlying model architecture. They introduce two auxiliary states:
[expand]: When the model predicts this token at a masked position, that single mask is replaced by two new masks, effectively growing the canvas.[delete]: When the model predicts this token, the masked position is removed entirely, shrinking the canvas.
Training and Inference
To teach the model these behaviors, the researchers used a data augmentation strategy. They took ground-truth code and "compressed" it by merging random spans into [expand] tokens, or "inflated" it by inserting random [delete] tokens. They also implemented a weighted loss function to ensure the model didn't become "lazy" by over-predicting deletions.
During generation, the model adaptively expands or contracts the sequence based on its own confidence. A key optimization called Broadcasting Deletion was introduced: if the model decides to delete a mask and all tokens to its right are also masks, it deletes the entire trailing sequence at once. This significantly speeds up the process, turning a token-by-token chore into a single-step length prediction and reducing the total number of diffusion steps from over 120 to roughly 50 at larger mask lengths.
Experimental Setup and Datasets
The models (Dream-Coder-7B and DiffuCoder-7B) were fine-tuned on the OpenCoder SFT dataset, a subset of approximately 110,000 high-quality Python instruction-solution pairs. For evaluation, the authors used standard industry benchmarks:
- HumanEval-Infilling: Testing both single-line and more complex multi-line code completions.
- SantaCoder-FIM: A benchmark specifically designed for "Fill-In-the-Middle" tasks.
- ROCStories: Used to test the model's generalizability to natural language narrative infilling.
Key Results and Insights
- Massive Performance Gains: DreamOn provided an average 26.4% absolute improvement in pass rates over standard fixed-length diffusion baselines.
- Closing the "Oracle" Gap: Previously, DLMs only performed well if they were given the "Oracle" length (the exact correct number of tokens). DreamOn is the first framework to close this gap, allowing the model to reach Oracle-level performance using only its internal logic.
- Beating Autoregressive Giants: On the challenging HumanEval Multi-line benchmark, DreamOn (built on DreamCoder-7B) achieved a score of 63.8, outperforming the highly optimized Qwen2.5-Coder-7B (58.7).
- Structural Integrity: Unlike autoregressive models that disrupt the natural flow of code to perform infilling, DreamOn maintains the contextual structure, which the authors argue is why it excels at complex multi-line tasks.
- Stability: The model showed remarkable consistency. Whether started with a canvas of 4 masks or 64 masks, the final output quality remained high, proving the length-adaptation mechanism is robust.
Limitations and Future Work
While DreamOn is a significant step forward, the authors note a few areas for improvement. First, the current training relies on specific heuristic "merging" and "deletion" schedules; a more principled, theoretical formulation could yield even better results. Second, the [expand] token currently only doubles a mask (1 to 2), requiring multiple steps for very long completions. Future work could introduce a richer vocabulary of expansion factors (e.g., 1 to 4 or 1 to 8). Finally, while ROCStories results show promise for natural language, more extensive testing on diverse text types is needed to prove universal applicability beyond Python code.
Summary updated