TL;DR
AnCoder introduces AnchorTree, a novel framework that guides discrete diffusion language models to generate code by leveraging the hierarchical structure of Abstract Syntax Trees (ASTs). By prioritizing the generation of structurally critical tokens (like keywords and identifiers) from the top of the syntax tree down, AnCoder significantly improves both the syntactic validity and functional correctness of diffusion-based code generation.
What Problem Does This Paper Address?
Most modern code generation models rely on autoregressive (AR) decoding, which generates code sequentially from left to right. However, source code is inherently non-sequential; its logic relies on nested structures, loops, and scoped variables. Forcing an AR model to generate code sequentially creates a structural mismatch that hinders global planning.
Discrete Diffusion Language Models (DLMs) offer a promising alternative by iteratively unmasking and refining tokens across the entire sequence at once. Unfortunately, standard DLMs often fail to respect the rigid syntax of programming languages, frequently producing broken, un-executable code. While previous work introduced "hard anchoring"—forcing the model to resolve important tokens (like nouns or verbs in natural language) early in the denoising process—this binary approach treats all anchors equally. It fails to distinguish between a top-level function definition and a deeply nested if-else statement, making it inadequate for the strict hierarchical nature of code.
Methods
To bridge this gap, the authors present AnchorTree, a soft anchoring framework that utilizes a program's Abstract Syntax Tree (AST) to guide the diffusion process.
The core architecture splits code generation into a two-stage process:
- Anchor Network ($y_{\theta_A}$): Predicts and unmasks structurally and semantically salient "anchor" tokens early in the denoising process. The authors define anchors as keywords (e.g.,
if,while,def) which dictate control flow, and identifiers (e.g., variable and function names) which carry semantic intent. - Denoiser Network ($x_{\theta_D}$): Uses these established anchors as a scaffold to fill in the remaining low-level implementation details.
Unlike hard anchoring, AnchorTree introduces a soft anchoring scheme where anchor tokens are weighted based on their depth in the AST. Tokens closer to the root node (like function signatures) receive higher weights, encouraging the model to resolve the global structure of the program before diving into nested loops or expressions.
The anchor weight $\mu(l)$ at a given position $l$ is defined using an exponential depth-decay schedule: $$\eta(l) = \gamma \cdot \exp(-\beta \cdot \max(\text{depth}(l) - d_0, 0))$$
This ensures a coarse-to-fine generation sequence, mirroring how human programmers plan code.
Datasets
The authors trained a family of 809M-parameter models, called AnCoder, using a three-stage pipeline based on datasets from OpenCoder:
- Stage 1 (Pretraining Adaptation):
opc-fineweb-code(49.49B tokens) to transition the model from an autoregressive paradigm to a diffusion paradigm. - Stage 2 (Mid-training): OpenCoder annealing datasets (10.33B tokens), featuring a mix of algorithmic code, synthetic code snippets, math problems, and web code.
- Stage 3 (Supervised Fine-Tuning): A Python-focused subset of the annealing corpus (1.1B tokens) formatted as instruction-style prompts.
For evaluation, the models were tested on two standard benchmarks:
- HumanEval: 164 Python programming problems evaluating algorithmic reasoning.
- MBPP (Mostly Basic Python Programs): 974 prompts covering basic procedural concepts.
Key Insights and Ideas
- AST Ancestry as a Denoising Hint: The authors proved experimentally that unmasking a token's AST ancestors provides a significantly better context clue for predicting a masked token than unmasking random tokens.
- Coarse-to-Fine is Optimal: When revealing hints to the model, performance improves the fastest when revealing tokens from the outside-in (root-to-leaves). This validates the core philosophy of AnchorTree.
- Soft Anchoring Beats Hard Anchoring: Treating code structure as a gradient of importance rather than a binary "anchor vs. non-anchor" choice yields cleaner syntax and better logic.
Key Results
AnCoder with AnchorTree consistently outperformed standard Masked Diffusion Language Models (MDLM) and hard-anchoring baselines across both benchmarks:
- HumanEval: AnchorTree boosted syntactic validity from 65.58% (MDLM baseline) to 74.72% (a +13.9% relative improvement). More importantly, functional correctness (Pass@1) jumped from 3.29% to 5.45% (a +65.7% relative improvement).
- MBPP: Syntactic validity rose from 74.46% to 80.12%, while Pass@1 increased from 6.34% to 9.10%.
While these diffusion models still trail behind the absolute performance of pure autoregressive baselines (such as Qwen2.5-Coder-0.5B, which achieves a 26.83% Pass@1 on HumanEval), AnCoder significantly closes the well-documented performance gap between AR models and DLMs.
Limitations and Future Work
- Nascent Architecture: The design space for two-stage anchor-denoiser networks is still largely unexplored, meaning optimal parameter-tying or information-passing mechanisms between the two networks have yet to be found.
- Static vs. Dynamic Limitations: AnchorTree relies entirely on static code hierarchies extracted from ASTs. It cannot inherently reason about dynamic execution behavior or runtime states.
- Scaling: The current work isolates anchoring performance at the ~800M parameter scale; future work is required to see how these structural priors scale to 1B+ parameter regimes.
Summary updated