COLD Decoding: Energy-based Constrained Text Generation with Langevin Dynamics

Lianhui Qin, Sean Welleck, Daniel Khashabi, Yejin Choi

TL;DR

COLD Decoding (Energy-based Constrained Decoding with Langevin Dynamics) is a new framework that allows off-the-shelf language models to follow complex constraints without any task-specific fine-tuning. By treating constraints as "energy functions" and using gradient-based sampling, it enables non-monotonic reasoning—allowing the model to "look ahead" or "revise" its path to satisfy both hard lexical requirements and soft topical goals.

The Challenge of Constrained Generation

Standard Large Language Models (LLMs) like GPT-2 or GPT-3 generate text greedily from left to right. This autoregressive nature makes it incredibly difficult to satisfy constraints that aren't strictly provided as prefixes. For example, if you need a sentence to include a specific keyword at the very end, or if you need to generate a "bridge" sentence between two existing ones (abductive reasoning), a standard LM has no easy way to "plan" its path.

Existing solutions usually fall into two camps:

  1. Supervised Fine-tuning: Training a specific model for every new task, which is expensive and often impossible for massive, proprietary models.
  2. Discrete Search: Using algorithms like beam search combined with heuristics. These often struggle with "soft" constraints or become computationally explosive as constraints grow.

COLD Decoding aims to provide a unified, "plug-and-play" decoding method that works with any pre-trained LM to satisfy diverse constraints on the fly.

Methods: Energy-Based Models and Langevin Dynamics

The authors formulate text generation as sampling from an Energy-Based Model (EBM). In this view, a "good" sequence has low energy, and a "bad" sequence (one that violates constraints) has high energy.

The core innovation is the application of Langevin Dynamics to text. Usually, Langevin dynamics is used in continuous spaces (like image generation) to iteratively move toward low-energy states using gradients. Since text is inherently discrete, the authors introduce several key techniques to bridge this gap:

The COLD decoding pipeline, showing initialization, Langevin dynamics on soft sequences, and discretization.
  • Soft Sequences: Instead of operating on discrete tokens, COLD performs optimization on a sequence of continuous "soft" tokens (logits).
  • Differentiable Constraints: The team defines energy functions for fluency (using the LM itself), future-token prediction (looking ahead to a target ending), and n-gram similarity (to ensure specific keywords appear).
  • Langevin Sampling: The algorithm starts with an initial guess and iteratively updates the soft tokens by following the gradient of the energy function, adding a bit of noise at each step to explore different possibilities.
  • Top-k Filtering "Guardian": To turn the final soft sequence back into readable text, they use the base LM as a "guardian" during discretization. This ensures the final choice remains fluent and probable, preventing the optimization process from generating constraint-satisfying gibberish.

Applications and Datasets

The researchers evaluated COLD on three distinct tasks using established benchmarks, demonstrating its flexibility across different types of constraints:

Examples of COLD applied to lexically-constrained generation, abductive reasoning, and counterfactual rewriting.
  • Abductive Reasoning (ART): Generating a logical bridge sentence between a given past and future context.
  • Counterfactual Story Rewriting (TIMETRAVEL): Revising a story ending to be consistent with a modified premise while keeping as much of the original ending as possible (a "minimal edit" constraint).
  • Lexically Constrained Generation (CommonGen): Generating a coherent sentence that must include a specific set of keywords.

Key Insights and Results

  • Unification of Constraints: Hard constraints (e.g., must include the word "apple") and soft constraints (e.g., must sound "happy") are treated identically as terms in an energy function. This allows for complex, multi-objective generation.
  • Gradients over Search: By using gradients to "nudge" the sequence toward satisfying constraints, COLD explores the space of possible sentences more efficiently than discrete search algorithms that have to commit to tokens one by one.
  • Non-monotonicity: Unlike standard decoding, COLD can update the "beginning" of a sentence based on a constraint at the "end," effectively allowing the model to reason globally across the sequence.

Performance Highlights: * Abductive Reasoning: COLD outperformed DELOREAN (a previous state-of-the-art gradient-based method) in both automatic metrics (BLEU, ROUGE) and human evaluation for overall coherence. * Counterfactual Rewriting: It achieved better "minimal edit" performance than baselines, successfully preserving original story details while adapting to new premises. * Lexical Constraints: On CommonGen, COLD achieved 94.5% keyword coverage, significantly higher than NEUROLOGIC (a specialized beam-search approach) and TSMH (a discrete MCMC approach), while maintaining comparable fluency. * Efficiency: COLD is roughly 30% faster than gradient-free MCMC methods like Mix-and-Match when using models of similar size, and it scales to much larger LMs (like GPT2-XL) with manageable overhead.

Limitations and Future Work

While powerful, COLD has a few current limitations:

  • Fixed Length: In its current form, COLD requires the sequence length to be specified beforehand. While the authors suggest using greedy decoding to extend sequences, a more dynamic length-control mechanism would be ideal.
  • Hyperparameter Tuning: The weights for different energy terms (e.g., how much to prioritize keywords vs. fluency) must be tuned for different tasks, which can be time-consuming.
  • Initialization Sensitivity: The quality of the final output can depend heavily on the initial "guess" sequence.

Future directions include exploring COLD for even more complex constraints, such as logical entailment or specific rhetorical styles, and applying it to massive models like GPT-3 via API-based gradient approximations.

Summary updated

BibTeX
@inproceedings{qin2022cold,
  author = {Lianhui Qin and Sean Welleck and Daniel Khashabi and Yejin Choi},
  title = {{COLD Decoding: Energy-based Constrained Text Generation with Langevin Dynamics}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=TzbAcdkUsv},
}
Figure