NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Principled Gradient-Based MCMC for Conditional Sampling of Text

Li Du, Afra Amini, Lucas Torroba Hennigen, Xinyan Velocity Yu, Holden Lee, Jason Eisner, Ryan Cotterell

TL;DR

Researchers have developed a mathematically "faithful" method to sample text from energy-based models by combining discrete Markov Chain Monte Carlo (MCMC) with gradient information. Unlike previous continuous-relaxation methods that suffered from systematic biases, the proposed samplers (p-NCG and GwL) use a tractable Metropolis-Hastings correction. This ensures they converge to the true target distribution, resulting in text that is both more fluent and significantly better at adhering to complex constraints.

The Challenge of Controlled Text Generation

Controlled text generation aims to make a language model (LM) produce text that satisfies specific constraints, such as a target sentiment, topic, or structural format. This is often framed as sampling from an Energy-Based Model (EBM). In this setup, the probability of a generated sentence is proportional to $\exp(-U(w))$, where the "energy" $U(w)$ combines the LM's natural fluency with a penalty for violating the desired constraints.

The core challenge is a mismatch of domains: text is inherently discrete (composed of distinct words or tokens), but the energy functions derived from neural networks are differentiable only in continuous space (token embeddings). Previous attempts to bridge this gap, such as COLD or MUCOLA, relied on "continuous relaxations" or "projections." They would take a gradient step in the continuous embedding space and then "round" back to the nearest discrete word.

The authors of this paper prove that these previous methods are unfaithful—they do not actually converge to the intended target distribution. They suffer from a structural bias: if one word has many similar-looking synonyms (neighbors in embedding space) and another does not, the sampler will incorrectly favor the word with more neighbors, even if their actual energies are identical.

Faithful Samplers via Discrete Proposals

To solve this, the authors introduce two primary "faithful" samplers that utilize the Metropolis-Hastings (MH) framework. The breakthrough is defining a proposal distribution directly over the discrete vocabulary, which makes the MH correction factor (the mathematical step that ensures convergence to the true distribution) tractable to calculate.

  1. p-NCG ($\ell_p$-Norm Constrained Gradient sampler): This method proposes changes to all positions in a sentence simultaneously. It uses a first-order Taylor expansion of the energy function to find "good" directions to move in embedding space, but adds an $\ell_p$-norm penalty to keep the proposed words reasonably close to the current ones.
  2. GwL (Gibbs with Langevin): A variant of Gibbs sampling that updates one word at a time. It uses a similar gradient-informed proposal but specifically excludes "self-transitions" (proposing the same word again) to increase sampling efficiency.
  3. Hybrid Sampling: The authors observed that p-NCG is excellent for broad exploration early in the sampling process, while GwL excels at refining a sentence once it becomes coherent. They propose a hybrid approach that begins with p-NCG and transitions to GwL.

The "Aha!" Moment: Previous methods couldn't use an MH correction because their transition probabilities required calculating an intractable integral over the entire continuous embedding space. By defining the proposal as a discrete softmax over the vocabulary, the authors bypassed this mathematical roadblock entirely.

Key Insights and Performance

The researchers evaluated their methods across several distinct setups, including a mathematical toy model, topic control (E2E dataset), sentiment control (SST2 dataset), and position-constrained generation (COLLIE dataset).

1. True Convergence

In a toy linear-chain Ising model, the authors demonstrated the theoretical superiority of their approach.

Total Variation distance comparing faithful samplers to biased baselines.

As shown above, the faithful samplers successfully converge to the true distribution (reaching a Total Variation distance of zero). In contrast, prior methods like COLD and MUCOLA plateau at a significant distance from the truth due to their inherent structural biases.

2. Success vs. Fluency

While biased samplers can sometimes produce acceptable results by "getting lucky," the faithful samplers consistently achieved higher success rates in following constraints without sacrificing the fluency (measured by perplexity, or PPL) of the text.

Performance on Topic and Sentiment Control tasks.

On the Sentiment Control task, the hybrid p-NCG + GwL achieved a remarkable 96% success rate with a perplexity of 23.61. For comparison, the previous state-of-the-art (SVS) achieved only an 84% success rate while suffering a worse perplexity of 32.73. On Topic Control, the faithful sampler reached a 99% success rate with an incredibly low perplexity of 5.17, effectively "solving" the constraint while maintaining the natural fluency of the base GPT-2 model.

3. The Geometry of Embeddings Matters

The authors discovered that using an $\ell_1$-norm (absolute value) penalty works significantly better than the standard $\ell_2$-norm (Euclidean) penalty. This is because word embedding spaces exhibit "strange geometry" where a small number of dimensions often have disproportionately large variances, making $\ell_1$ regularization much more effective for discrete text spaces.

Limitations and Future Directions

  • Fixed Length: The current implementation requires the sequence length $N$ to be fixed in advance. It cannot yet handle dynamic moves like "insert" or "delete" to change sentence length mid-sampling.
  • Hyperparameter Tuning: The step size $\alpha$ still requires manual tuning for different models and tasks. Adapting "auto-tuning" methods (like NUTS) to this discrete setting is a promising area for future work.
  • Speed: Like all MCMC methods, this approach is slower than simple autoregressive decoding. While it is parallelizable across token positions, it still requires many iterations to fully converge.

Summary updated

BibTeX
@inproceedings{du2024principled,
  author = {Li Du and Afra Amini and Lucas Torroba Hennigen and Xinyan Velocity Yu and Holden Lee and Jason Eisner and Ryan Cotterell},
  title = {{Principled Gradient-Based MCMC for Conditional Sampling of Text}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=AwLLSlJAeJ},
}
Figure