Controllable Generation via Locally Constrained Resampling

Kareem Ahmed, Kai-Wei Chang, Guy Van den Broeck

Large Language Models (LLMs) are incredibly fluent, yet they frequently stumble when tasked with "structured" generation. Whether generating a valid JSON schema, executable code, or solving a Sudoku puzzle, LLMs are prone to making short-sighted errors. GEN-C (Generative Constrained Resampling) is a novel framework designed to force LLMs to follow complex logical constraints without the myopic pitfalls of current methods. By combining local probabilistic approximations with constraint circuits, GEN-C achieves perfect accuracy on complex tasks like Sudoku, where even state-of-the-art models like GPT-4o and Gemini 1.5 frequently fail.

The Flaw in Greedy Masking

Current state-of-the-art tools for constrained generation (such as Outlines or Guidance) rely on "greedy masking." At every single token generation step, these tools block any token that would immediately violate a given constraint.

However, this approach lacks foresight. A token choice that appears valid in the moment might lead to a "dead end" ten tokens later, where no valid continuation exists. For example, if a model must choose between "Adam" and "Poorva" to start a sentence, a greedy approach might pick "Adam" 50% of the time, even if the name "Adam" guarantees a rule violation later in the sequence. To fix this, we need Bayesian conditioning—evaluating the probability of the entire sequence given the constraint, rather than filtering strictly token-by-token.

The GEN-C Methodology

To achieve tractable Bayesian conditioning, GEN-C operates in three main stages:

The GEN-C pipeline: from an initial unconstrained LLM distribution, to a local approximation, and finally importance-weighted resampling to satisfy constraints.
  1. Local Approximation: Calculating the exact probabilities for the entire, intractable LLM distribution is computationally infeasible. Instead, GEN-C starts with a model sample and induces a "pseudolikelihood" distribution. This creates a local, factorized version of the distribution centered around that sample, making the math much easier to handle.
  2. Constraint Compilation: GEN-C represents logical constraints using Constraint Circuits. The standard approach in the field relies on Regular Expressions compiled into Deterministic Finite Automata (DFAs). However, constraint circuits can be exponentially more compact. They can represent complex logic (like the intersecting rules of Sudoku) that would cause a DFA to explode in size.
An example of a constraint circuit representing logical rules over text generation, allowing for efficient computation of valid sequences.
  1. Importance Sampling & Resampling: The local distribution is then conditioned on the constraint circuit to draw valid samples. Because this local distribution is only an approximation of the true LLM distribution, GEN-C uses Importance Sampling to calculate weights that correct the bias. A final resampling step ensures the output is both valid (satisfying the constraint) and high-quality (matching the LLM's original intent).

Key Results and Evaluation

The researchers evaluated GEN-C across three distinct and challenging tasks:

  • Sudoku Mastery: On a dataset of 10,000 9x9 Sudoku puzzles with 10 missing entries, GEN-C achieved a flawless 100% accuracy. In stark contrast, standard prompting with GPT-4o mini solved only ~41%, and Gemini 1.5 Flash solved ~31%.
  • Warcraft Shortest Path: Given 10,000 terrain maps where the model must generate a valid, minimum-cost path between two points, GEN-C reached 78.13% exact match accuracy and 100% consistency (meaning every generated path was at least a valid route). This significantly outperformed standard CNN-LSTM baselines.
  • Detoxification (REALTOXICITYPROMPTS): When tested on a benchmark of ~100,000 prompts to steer the model away from offensive language, GEN-C reduced toxicity more effectively than standard "word banning" methods while maintaining the same level of text quality and perplexity.

Limitations and Future Directions

While highly effective, GEN-C does introduce computational overhead. The method requires expanding a batch to evaluate "Hamming distance 1" perturbations (checking all possible single-token changes), which is more expensive than simple greedy decoding. Additionally, the quality of the final result depends heavily on the initial sample; if the LLM's first guess is too far from any valid solution, the local approximation may struggle to find a path to a valid output. Finally, while constraint circuits are more efficient than DFAs, compiling complex constraints still requires domain expertise.

Future work aims to integrate GEN-C more tightly with circuit libraries (like pyjuice) to improve inference speed and to explore its potential for enforcing factual accuracy in LLM outputs.

Summary updated

BibTeX
@inproceedings{ahmed2025controllable,
  author = {Kareem Ahmed and Kai-Wei Chang and Guy Van den Broeck},
  title = {{Controllable Generation via Locally Constrained Resampling}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=8g4XgC8HPF},
}
Figure