Controllable Generation via Locally Constrained Resampling

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

Abstract

Large Language Models (LLMs) often struggle with strict logical constraints, frequently failing in tasks like generating valid JSON or solving puzzles. Current "greedy masking" techniques are myopic, often leading models into "dead ends" where constraints cannot be satisfied. We present GEN-C (Locally Constrained Resampling), a global Bayesian approach that leverages tractable constraint circuits and importance resampling. GEN-C ensures generated sequences satisfy complex logical rules while maintaining high fluency and quality.

Introduction: The Myopia of Greedy Masking

While LLMs excel at fluent text generation, they lack the inherent ability to follow rigid structural or logical rules. The standard industry approach, greedy masking, filters tokens at each generation step to prevent immediate constraint violations. However, this local decision-making often fails to account for long-term dependencies. For instance, a model might start a sentence that must end with a specific word, only to find halfway through that no grammatically correct path remains. This "dead end" problem necessitates a more holistic, global reasoning framework.

The GEN-C Framework

GEN-C shifts the paradigm from step-by-step masking to Bayesian conditioning. The process involves four key stages:

  1. Local Approximation: Directly conditioning on the full LLM distribution is computationally infeasible. GEN-C instead creates a local, factorized approximation (a pseudolikelihood) around an initial sample.
  2. Constraint Circuits: To represent complex logic without the state-space explosion of Deterministic Finite Automata (DFAs), the authors utilize Constraint Circuits. These circuits allow for efficient probabilistic reasoning.
  3. Tractable Conditioning: The local approximation is conditioned on the logical constraint $\alpha$, producing a "biased" distribution that is guaranteed to satisfy the required rules.
  4. Importance Resampling: To correct the bias from the initial approximation, importance sampling is applied. Multiple candidates are drawn from the constrained distribution and weighted by their original LLM likelihood to produce the final output.
The GEN-C resampling process: transitioning from the original LLM distribution to a local approximation, and finally to a constrained distribution that satisfies logical rules.

Constraint Circuits and Probabilistic Reasoning

A core innovation of GEN-C is the use of Constraint Circuits. Unlike DFAs, which can grow exponentially with the complexity of the rules, circuits provide a succinct representation. This allows the system to calculate the exact probability that a sequence will satisfy a constraint, enabling precise "steering" of the model's output.

Tractable conditioning using Constraint Circuits. Probabilities are propagated through the circuit to efficiently calculate the likelihood of satisfying a given constraint.

Experimental Evaluation

The researchers tested GEN-C across three challenging domains:

  • Sudoku: Solving 9x9 grids with 10 missing entries, requiring highly interdependent logical consistency.
  • Warcraft Shortest Path: Generating valid, minimum-cost paths on terrain maps.
  • Detoxification: Steering models away from toxic expressions using the REALTOXICITYPROMPTS benchmark.

Key Results

GEN-C demonstrated significant improvements over both standard prompting and greedy masking:

  • Logical Perfection: GEN-C achieved 100% accuracy on Sudoku puzzles, vastly outperforming GPT-4o (<50%) and Gemini 1.5 Flash (26%).
  • Pathfinding Consistency: In the Warcraft task, GEN-C maintained 100% consistency in path validity, with a 78.13% exact match rate.
  • Fluent Detoxification: In detoxification tasks, GEN-C effectively reduced toxicity while maintaining text quality (perplexity) comparable to the unconstrained base model, avoiding the "robotic" feel of simple word banning.

Conclusion and Future Directions

GEN-C proves that global constraints require global reasoning. By combining local approximations with tractable circuits, it overcomes the limitations of myopic masking. While the current implementation faces some computational overhead due to the need for "Hamming distance 1" perturbations, future work aims to optimize performance through tighter integration with circuit libraries like pyjuice. This will pave the way for real-time, complex controllable generation in even larger-scale applications.

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