Gradient-Based Constrained Sampling from Language Models
(2205.12558v2)
Published 25 May 2022 in cs.CL
Abstract: Large pretrained LLMs generate fluent text but are notoriously hard to controllably sample from. In this work, we study constrained sampling from such LLMs: 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 LLM 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.
The paper presents MUCOLA, which employs Langevin Dynamics to sample in embedding space for constrained text generation.
It formulates the problem using a Lagrangian energy function that balances LM likelihood with multiple differentiable constraints.
Experiments show improved toxicity avoidance, sentiment control, and keyword-guided generation while preserving fluency and diversity.
This paper introduces MUCOLA (Multiple Constraints Sampling from LLMs using Langevin Dynamics), a non-autoregressive algorithm for generating text from LLMs (LMs) that satisfies user-defined constraints while maintaining fluency and fidelity to the original LM distribution.
The core problem addressed is the difficulty in controlling large pretrained LMs like GPT-2 to generate text with specific properties (e.g., non-toxic, positive sentiment, containing certain keywords) without compromising the quality of the generated text. Autoregressive methods often struggle with global constraints and can alter the underlying LM distribution.
MUCOLA tackles this by framing constrained generation as sampling from an energy function that combines the LM's log-likelihood with differentiable constraint functions. Instead of optimizing for a single best output, it uses Langevin Dynamics, a gradient-based Markov Chain Monte Carlo (MCMC) method, to draw diverse samples.
Key Technical Contributions and Implementation Details:
Sampling in Embedding Space: Instead of representing tokens as distributions over the vocabulary (R∣V∣), MUCOLA operates directly on token embeddings (Rd, where d≪∣V∣).
An output sequence of length L is represented as e~={e1,…,eL}, where each en∈E (the LM's embedding table).
This significantly reduces the number of parameters from L×∣V∣ to L×d, allowing for much longer sequences to be processed within GPU memory constraints.
It provides a natural way to define certain constraints, particularly hard keyword constraints.
Langevin Dynamics: The sampling process iteratively updates the sequence embeddings e~ using gradient descent steps modified with Gaussian noise:
e~t=ProjE(e~t−1−η∇e~E(e~t−1)+2ηβzt)
* E(e~) is the energy function.
* η is the step size.
* β controls the noise variance (annealed over time).
* zt∼N(0,Id) is Gaussian noise.
* ProjE(⋅) projects the updated embedding back to the nearest embedding in the LM's table E using Euclidean distance, preventing adversarial solutions.
* The noise term allows the process to explore the solution space and escape local minima, generating diverse samples near the energy minimum.
Lagrangian Energy Function: The energy function E is defined using a Lagrangian formulation to handle multiple constraints {f1,…,fC} with thresholds {ϵ1,…,ϵC}:
E(e~)=−logP(e~∣x)−i=1∑Cλi(ϵi−fi([x],e~))
* The goal is to sample y∼P(y∣x) subject to fi([x],y)≤ϵi for all i.
* λi≥0 are Lagrangian multipliers, dynamically updated via gradient ascent: λit=max(0,λit−1+α∇λiE).
* This avoids manual weight tuning for constraints. If a constraint fi≤ϵi is satisfied, ϵi−fi≥0, and λi tends towards 0. If violated, ϵi−fi<0, and λi increases, penalizing the violation more heavily.
* Crucially, when all constraints are satisfied, E(e~)≈−logP(e~∣x), meaning the sampling process targets the original LM distribution.
Constraint Implementation: Constraints fi must be differentiable functions of the embeddings e~ and share the LM's embedding table E.
Soft Constraints (Classifiers/LMs): Auxiliary models (e.g., RoBERTa classifiers, GPT-2 based generative classifiers) are fine-tuned using the target LM's embedding table (potentially frozen, with a projection layer if dimensions differ). Constraints are defined on probabilities, e.g., pTOXIC(e~)<ϵ or logp(e~∣LABEL1)>logp(e~∣LABEL0). Prompt-based classification without fine-tuning is also explored.
Hard Constraints (Keywords): A differentiable distance function d(w,e~) measures the minimum Euclidean distance between the target keyword embedding ew and any embedding en in the sequence e~, using a Gumbel-Softmax trick for differentiable selection. The constraint is d(w,e~)<ϵw, where the threshold ϵw≈−logπw,w (related to the probability of the keyword under its own embedding's distribution) aims to guarantee the keyword's presence. This extends to phrases via convolutions.
Experiments and Applications:
MUCOLA was evaluated on several tasks using GPT2-Large/XL and BART-Large:
Toxicity Avoidance: Using a RoBERTa classifier constraint (pTOXIC<0.01), MUCOLA matched or outperformed baselines (DAPT, FUDGE, GeDi, DExperts) in reducing toxicity while achieving perplexity closer to unconstrained generation and maintaining diversity. Human evaluation showed similar toxicity/fluency to DExperts but better topicality.
Sentiment Control: Using various classifiers (discriminative, generative, prompt-based) trained on SST-2 and Yelp, MUCOLA generally achieved good sentiment control with perplexity closer to the base LM than most baselines. Combining two classifiers (MUCOLA-TWO-DISC) performed strongly. Prompt-based constraints showed promise. Some degradation (repetition) was noted for very long sequences (length 50).
Keyword-Guided Generation (COMMONGEN, ROC): Using the hard keyword distance constraint, MUCOLA achieved state-of-the-art coverage (percentage of required keywords included) and significantly better perplexity than baselines like COLD and Neurologic, though human fluency scores were slightly lower than the best baseline.
Terminology-Constrained Machine Translation: Using a MarianMT model, MUCOLA achieved perfect terminology coverage while maintaining the BLEU score of unconstrained translation.
Entity-Constrained Summarization: Preliminary results using BART showed promise in forcing specific entities into summaries.
Analysis:
Speed/Memory: MUCOLA's embedding-space operations allow processing significantly longer sequences (e.g., 500-1000 tokens) compared to vocabulary-space methods (limited to ~20-50 tokens) on standard GPUs. However, it's still slower (15-20x) than autoregressive decoding due to iterative updates.
Diversity: Langevin Dynamics noise, not random initialization, is the main driver of sample diversity.
Controllability: The threshold ϵi provides interpretable control over constraint strength. Lowering ϵ tightens control without hurting fluency (perplexity) but might decrease diversity.
Compatibility: The framework struggles when constraints are fundamentally incompatible (e.g., generating positive text containing strongly negative keywords).
Limitations:
Slower than autoregressive methods.
Requires pre-specifying output length (though standard mitigation techniques exist).
Can inherit LM degeneracy issues (e.g., repetition) for long sequences.
In conclusion, MUCOLA provides a flexible and effective framework for constrained sampling from large LMs. By operating in the embedding space and using Langevin Dynamics with a Lagrangian objective, it generates diverse, fluent text that adheres to multiple soft or hard constraints while staying close to the original LM's distribution.