arXiv:2605.16829cs.CLcs.PL2026-05

用约束引导代码生成,让模型更准更安全。

Constrained Code Generation with Discrete Diffusion

论文配图:Constrained Code Generation with Discrete Diffusion
图 1 · 摘自论文原文
  • 在去噪过程中嵌入约束检查,动态调整生成路径。
  • 在多个基准上提升功能正确性与安全性,修正计算量更少。
  • 无需训练即可集成约束,适合对代码质量要求高的场景。

离散扩散模型通过逐步修复部分破坏的令牌序列来构建程序,并支持并行令牌优化。该范式在每一步去噪中暴露全局程序状态,为施加程序级功能与安全约束提供了自然干预点。本文提出无需训练的神经符号推理框架 CDC(Constrained Diffusion for Code),将约束满足直接融入反向去噪过程。CDC 在基础离散扩散采样器中引入感知约束的去噪算子,结合数学优化与程序分析,识别中间程序状态中与约束相关的区域,并局部调整去噪轨迹,引导生成走向可行程序,同时保持接近原始模型。在多个代码生成基准上,CDC 持续提升功能正确性、安全性及语法正确性,优于离散扩散和自回归基线方法,且所需修正计算更少、修改更局部。

原文摘要 · Abstract (English)

Discrete diffusion models are a powerful, emerging paradigm for code generation. They construct programs through iterative refinement of partially corrupted token sequences and enable parallel token refinement. Importantly, this paradigm exposes a global program state at each denoising step, which provides a natural intervention point for enforcing program-level functionality and security constraints, guiding the generation before the final code is committed. Building on this observation, the paper introduces Constrained Diffusion for Code (CDC), a training-free neurosymbolic inference framework that integrates constraint satisfaction directly into the reverse denoising process. CDC augments the base discrete diffusion sampler with constraint-aware denoising operators that combine mathematical optimization with program analysis to identify constraint-relevant regions of the intermediate program state and locally adjust the denoising trajectory, steering generation toward feasible programs while remaining close to the base model. Across code generation benchmarks, CDC consistently improves constraint satisfaction in functional correctness, security, and even syntax, outperforming discrete diffusion and autoregressive baselines with less corrective computation and more localized edits.

代码生成扩散模型约束求解

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。