Abstract
Large language models (LMs) excel at generating fluent text but often struggle to adhere to complex lexical constraints. We present NeuroLogic Decoding, a novel decoding algorithm that enables LMs to satisfy constraints expressed in Conjunctive Normal Form (CNF) predicate logic. By integrating logical constraints directly into the beam search process, NeuroLogic Decoding achieves high efficiency—matching the asymptotic complexity of standard beam search—while significantly improving constraint satisfaction. Our experiments across four diverse benchmarks demonstrate that this approach allows even unsupervised models to outperform heavily fine-tuned counterparts in tasks such as recipe generation, commonsense reasoning, and gender-bias mitigation.
Introduction
The current paradigm of "pre-train and fine-tune" often fails to instill strict logical adherence in language models. While a model may learn the general structure of a task, it frequently "hallucinates" extra information or ignores specific negative constraints. Previous attempts to enforce constraints during decoding have typically suffered from either prohibitive computational costs or a tendency to produce incoherent text due to overly greedy optimization.
NeuroLogic Decoding addresses these challenges by treating lexical constraints as logical predicates. This allows for the expression of complex requirements, such as mandatory inclusions, alternatives, and strict exclusions (e.g., "Use 'beef' OR 'steak', AND 'soy sauce', but NOT 'pork'").
Methodology: Logic-Aware Beam Search
The core of NeuroLogic Decoding is a modified beam search that efficiently navigates the space of possible sequences while tracking the state of logical constraints.
The algorithm operates through three primary mechanisms:
- Pruning: Candidates that irreversibly violate negative constraints (e.g., generating a forbidden word) are immediately discarded. The search space is further narrowed to top-tier candidates based on a combination of model probability and constraint progress.
- Grouping: To maintain diversity and avoid local optima, hypotheses are grouped based on the specific set of constraints they have satisfied. This prevents the beam from being dominated by "easy" paths that ignore long-term requirements.
- Selecting: A scoring function balances the original LM probability with a reward for satisfying new constraints.
Crucially, the algorithm maintains a runtime complexity of $O(Nk)$, where $N$ is sequence length and $k$ is beam size, making it as efficient as standard unconstrained beam search.
Experimental Setup and Datasets
We evaluated NeuroLogic Decoding across several challenging benchmarks that require precise lexical control:
- CommonGen: Generating sentences from a set of concepts (e.g., "dog," "frisbee," "catch").
- Recipe1M+: Generating instructions from ingredients while strictly avoiding unlisted items.
- Data-Grounded Dialogue: Producing responses based on specific factual attributes.
- WinoMT: Reducing gender bias in machine translation by enforcing correct gender inflections through logical constraints.
Results and Discussion
NeuroLogic Decoding demonstrates significant improvements over standard decoding methods and even task-specific fine-tuning:
- Constraint Adherence: In recipe generation, the hallucination rate for extra ingredients dropped from 14.3% to 0.6%.
- Zero-Shot Performance: Unsupervised GPT-2 models using NeuroLogic Decoding frequently outperformed fine-tuned models using standard beam search, highlighting the power of inference-time control.
- Bias Mitigation: The algorithm improved gender inflection accuracy in machine translation by 30.5% for German and 28.0% for French.
Conclusion
NeuroLogic Decoding provides a robust framework for guiding neural text generation with formal logic. By shifting the focus from massive fine-tuning to intelligent decoding, we can achieve better control over model outputs without sacrificing fluency or efficiency. Future work will explore the automatic extraction of logical constraints from natural language prompts to further enhance the accessibility of this approach.
Summary updated