Skip to main content
QUICK REVIEW

[Paper Review] Gradient-Based Constrained Sampling from Language Models

Sachin Kumar, Biswajit Paria|arXiv (Cornell University)|May 25, 2022
Topic ModelingComputer Science6 citations
TL;DR

This paper proposes MuCoLa, a non-autoregressive sampling method for large language models that uses Langevin dynamics to generate text satisfying multiple differentiable constraints while preserving fluency and model performance. By combining language model likelihood and constraints into a single energy function, MuCoLa achieves state-of-the-art results in toxicity avoidance, sentiment control, and keyword-guided generation with improved sample quality and diversity.

ABSTRACT

Large pretrained language models generate fluent text but are notoriously hard to controllably sample from. In this work, we study constrained sampling from such language models: generating text that satisfies user-defined constraints, while maintaining fluency and the model's performance in a downstream task. We propose MuCoLa -- a sampling procedure that combines the log-likelihood of the language model with arbitrary (differentiable) constraints in a single energy function, and then generates samples in a non-autoregressive manner. Specifically, it initializes the entire output sequence with noise and follows a Markov chain defined by Langevin Dynamics using the gradients of the energy function. We evaluate MuCoLa on text generation with soft and hard constraints as well as their combinations obtaining significant improvements over competitive baselines for toxicity avoidance, sentiment control, and keyword-guided generation.

Motivation & Objective

  • To address the challenge of controlling text generation from large language models while maintaining fluency and downstream task performance.
  • To overcome limitations of autoregressive decoding methods that struggle with global context and multiple competing constraints.
  • To develop a sampling method that preserves the underlying language model distribution while incorporating arbitrary differentiable constraints.
  • To enable both soft constraints (e.g., via classifiers) and hard constraints (e.g., keyword presence/absence) in a unified framework.
  • To achieve high-quality, diverse, and controllable text generation without modifying the language model's probability distribution.

Proposed method

  • Formulates a joint energy function combining the language model's log-likelihood and differentiable constraint functions.
  • Uses Langevin dynamics to iteratively update a full sequence of token embeddings, initialized with random noise, to minimize the energy function.
  • Applies gradient-based updates to the embedding space rather than the vocabulary space, reducing memory and computational costs.
  • Introduces stochasticity via additive noise in gradients to enable diverse sampling while maintaining convergence to low-energy solutions.
  • Defines constraints as Lagrangian terms with thresholds, enabling multi-constraint optimization without manual weighting.
  • Operationalizes the method using a shared embedding table for both the language model and constraint functions, enabling end-to-end differentiability.

Experimental results

Research questions

  • RQ1Can a non-autoregressive sampling method effectively satisfy multiple, potentially conflicting, differentiable constraints in text generation?
  • RQ2Does combining language model likelihood with constraint functions in a single energy function preserve model fluency and task performance?
  • RQ3How does the proposed method compare to autoregressive baselines in terms of constraint satisfaction, fluency, and diversity?
  • RQ4Can the method handle both soft constraints (e.g., toxicity scores) and hard constraints (e.g., keyword presence) simultaneously?
  • RQ5What is the scalability and memory efficiency of the method compared to alternative approaches in the vocabulary space?

Key findings

  • MuCoLa significantly outperforms competitive baselines in toxicity avoidance, achieving lower toxicity scores while maintaining high fluency and relevance.
  • The method achieves strong performance in sentiment control tasks, successfully generating text with desired sentiment even when requiring the presence of opposing sentiment words.
  • For keyword-guided generation, MuCoLa generates diverse, relevant outputs that include required keywords with high accuracy and minimal disfluency.
  • In machine translation, MuCoLa improves entity fidelity by enforcing hard constraints on named entity preservation, outperforming standard decoding methods.
  • The method scales to long sequences—up to 1000 tokens—demonstrating superior memory efficiency compared to vocabulary-space baselines, which fail beyond 50 tokens.
  • Human evaluation confirms that MuCoLa generates higher-quality and more diverse outputs than autoregressive baselines, with comparable or better constraint satisfaction.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.