HyperAIHyperAI

Command Palette

Search for a command to run...

Probabilistische Inferenz in Sprachmodellen durch Twisted Sequential Monte Carlo

Stephen Zhao Rob Brekelmans Alireza Makhzani Roger Grosse

Zusammenfassung

Zahlreiche Fähigkeitsund Sicherheitstechniken für Große Sprachmodelle (LLMs), darunter RLHF, automatisiertes Red-Teaming, Prompt-Engineering und Infilling, lassen sich als Stichprobenziehung aus einer unnormalisierten Zielverteilung formulieren, die durch eine gegebene Belohnungsoder Potenzialfunktion über die vollständige Sequenz definiert ist. In dieser Arbeit nutzen wir den reichhaltigen Werkzeugkasten von Sequential Monte Carlo (SMC) für diese probabilistischen Inferenzprobleme. Insbesondere verwenden wir gelernte Twist-Funktionen, um den erwarteten zukünftigen Wert des Potenzials zu jedem Zeitschritt abzuschätzen, wodurch sich die Inferenzzeitberechnung auf vielversprechende Teilsequenzen konzentrieren lässt. Wir schlagen eine neuartige kontrastive Methode zum Erlernen der Twist-Funktionen vor und stellen Verbindungen zur umfangreichen Literatur des weichen bestärkenden Lernens her. Als ergänzende Anwendung unseres Twisted-SMC-Frameworks präsentieren wir Methoden zur Bewertung der Genauigkeit von Inferenztechniken für Sprachmodelle mithilfe neuartiger bidirektionaler SMC-Schranken für die logarithmische Partitionsfunktion. Diese Schranken können verwendet werden, um die KL-Divergenz zwischen der Inferenzund der Zielverteilung in beide Richtungen abzuschätzen. Wir wenden unsere Inferenzbewertungstechniken an, um zu zeigen, dass Twisted SMC effektiv für das Ziehen von Stichproben unerwünschter Ausgaben aus einem vortrainierten Modell (ein nützlicher Bestandteil des Harmlessness-Trainings und des automatisierten Red-Teamings), das Generieren von Bewertungen mit unterschiedlicher Sentiment und das Durchführen von Infilling-Aufgaben ist.

One-sentence Summary

Vector Institute introduces Twisted Sequential Monte Carlo for probabilistic inference in LLMs, employing a novel contrastive method to learn twist functions that direct inference-time computation toward promising partial sequences, and proposes bidirectional SMC bounds for evaluating inference accuracy, demonstrating effectiveness on automated red-teaming, sentiment-controlled generation, and infilling tasks.

Key Contributions

  • The paper formulates capability and safety techniques such as RLHF, automated red-teaming, prompt engineering, and infilling as sampling from unnormalized target distributions, and introduces twisted Sequential Monte Carlo with learned twist functions to focus inference-time computation on promising partial sequences.
  • The paper proposes a contrastive method for learning twist functions, establishing connections to soft reinforcement learning and positioning the approach as a principled probabilistic extension of best-of-K decoding and related value-based sampling schemes.
  • The paper introduces bidirectional Sequential Monte Carlo bounds on the log partition function for estimating KL divergence between inference and target distributions, demonstrating twisted SMC for sampling undesirable pretrained outputs, generating reviews with varied sentiment, and infilling tasks.

Introduction

Many language model tasks, including RLHF, red-teaming, and reasoning, can be seen as steering generated text toward a specified property by scoring full sequences. The authors frame this as probabilistic inference, sampling from a base language model modulated by a potential function and estimating an intractable partition function over all possible responses. Prior sampling approaches are limited because the desired full-sequence distribution is non-causal, so generating tokens sequentially requires unknown future-aware marginals, and earlier SMC work mostly uses fixed or short-horizon guidance rather than learned twist functions. The authors leverage twisted sequential Monte Carlo and learn intermediate twist functions, including a contrastive twist learning method, to guide generation toward promising partial sequences and provide partition-function bounds for evaluating inference quality.

Method

The authors leverage a twisted sequential Monte Carlo framework to sample from language distributions defined by a pretrained base language model and a terminal scoring function. Let the base autoregressive model assign sequence probability p0(s1:T)p_0(\mathbf{s}_{1:T})p0​(s1:T​), and let the desired target distribution be defined by a terminal potential ϕ(s1:T)\phi(\mathbf{s}_{1:T})ϕ(s1:T​):

σ(s1:T)=1Zσp0(s1:T)ϕ(s1:T).\sigma(\mathbf{s}_{1:T}) = \frac{1}{\mathcal{Z}_\sigma} p_0(\mathbf{s}_{1:T}) \phi(\mathbf{s}_{1:T}).σ(s1:T​)=Zσ​1​p0​(s1:T​)ϕ(s1:T​).

Here σ\sigmaσ is the inference target, ϕ\phiϕ encodes the final objective, such as reward, likelihood, classification probability, or another terminal scoring signal, and Zσ\mathcal{Z}_\sigmaZσ​ is the partition function. The central difficulty is that direct sampling from σ\sigmaσ is intractable because ϕ\phiϕ is terminal, while generation proceeds token by token. The authors therefore introduce intermediate target distributions that guide early token decisions with information from the eventual terminal objective.

The method represents intermediate targets using twist functions ψt(s1:t)\psi_t(\mathbf{s}_{1:t})ψt​(s1:t​), which modulate the base model to approximate the true marginal of the target distribution at time ttt. For t<Tt < Tt<T, the twisted intermediate target is defined as

πt(s1:t)=1Ztψp0(s1:t)ψt(s1:t),\pi_t(\mathbf{s}_{1:t}) = \frac{1}{\mathcal{Z}_t^\psi} p_0(\mathbf{s}_{1:t}) \psi_t(\mathbf{s}_{1:t}),πt​(s1:t​)=Ztψ​1​p0​(s1:t​)ψt​(s1:t​),

while the final target remains

πT(s1:T)=1Zσp0(s1:T)ϕ(s1:T).\pi_T(\mathbf{s}_{1:T}) = \frac{1}{\mathcal{Z}_\sigma} p_0(\mathbf{s}_{1:T}) \phi(\mathbf{s}_{1:T}).πT​(s1:T​)=Zσ​1​p0​(s1:T​)ϕ(s1:T​).

The role of the twist is to summarize how future potential can be incorporated into present sampling decisions. If the twists were ideal, the intermediate targets would match the true target marginals:

πt∗(s1:t)=σ(s1:t).\pi_t^*(\mathbf{s}_{1:t}) = \sigma(\mathbf{s}_{1:t}).πt∗​(s1:t​)=σ(s1:t​).

The optimal twist satisfies a backward marginalization condition:

ψt∗(s1:t)∝∑st+1:Tp0(st+1:T∣s1:t)ϕ(s1:T),\psi_t^*(\mathbf{s}_{1:t}) \propto \sum_{\mathbf{s}_{t+1:T}} p_0(\mathbf{s}_{t+1:T} \mid \mathbf{s}_{1:t}) \phi(\mathbf{s}_{1:T}),ψt∗​(s1:t​)∝st+1:T​∑​p0​(st+1:T​∣s1:t​)ϕ(s1:T​),

with the corresponding recursion

ψt∗(s1:t)∝∑st+1p0(st+1∣s1:t)ψt∗(s1:t+1).\psi_t^*(\mathbf{s}_{1:t}) \propto \sum_{s_{t+1}} p_0(s_{t+1} \mid \mathbf{s}_{1:t}) \psi_t^*(\mathbf{s}_{1:t+1}).ψt∗​(s1:t​)∝st+1​∑​p0​(st+1​∣s1:t​)ψt∗​(s1:t+1​).

This condition shows that the optimal twist evaluates the expected terminal potential under future continuations generated by the base model. In other words, ψt(s1:t)\psi_t(\mathbf{s}_{1:t})ψt​(s1:t​) estimates how promising a partial sequence is for ultimately achieving high terminal potential.

At each generation step, the authors run a sequential Monte Carlo procedure with KKK partially generated sequences. Given a proposal q(st∣s1:t−1)q(s_t \mid \mathbf{s}_{1:t-1})q(st​∣s1:t−1​) and the twisted intermediate targets, the incremental importance weight at step ttt is

wt(s1:t)=p0(st∣s1:t−1)q(st∣s1:t−1)ψt(s1:t)ψt−1(s1:t−1).w_t(\mathbf{s}_{1:t}) = \frac{p_0(s_t \mid \mathbf{s}_{1:t-1})} {q(s_t \mid \mathbf{s}_{1:t-1})} \frac{\psi_t(\mathbf{s}_{1:t})} {\psi_{t-1}(\mathbf{s}_{1:t-1})}.wt​(s1:t​)=q(st​∣s1:t−1​)p0​(st​∣s1:t−1​)​ψt−1​(s1:t−1​)ψt​(s1:t​)​.

These weights correct the mismatch between the base model, the proposal, and the twisted intermediate targets. The authors highlight two natural proposal choices. The first is to use the base model directly as the proposal:

q=p0.q = p_0.q=p0​.

This is simple and tractable, but it may require a large number of particles KKK to approximate the terminal target well, especially when the terminal potential is sharp or highly selective.

The second choice is a twist-induced proposal. For a given set of twisted intermediate targets, the one-step variance-minimizing proposal has the form

qtπ(st∣s1:t−1)∝p0(st∣s1:t−1)ψt(s1:t).q_t^\pi(s_t \mid \mathbf{s}_{1:t-1}) \propto p_0(s_t \mid \mathbf{s}_{1:t-1}) \psi_t(\mathbf{s}_{1:t}).qtπ​(st​∣s1:t−1​)∝p0​(st​∣s1:t−1​)ψt​(s1:t​).

Equivalently,

qtπ(st∣s1:t−1)=1Ztπ(s1:t−1)p0(st∣s1:t−1)ψt(s1:t),q_t^\pi(s_t \mid \mathbf{s}_{1:t-1}) = \frac{1}{Z_t^\pi(\mathbf{s}_{1:t-1})} p_0(s_t \mid \mathbf{s}_{1:t-1}) \psi_t(\mathbf{s}_{1:t}),qtπ​(st​∣s1:t−1​)=Ztπ​(s1:t−1​)1​p0​(st​∣s1:t−1​)ψt​(s1:t​),

where

Ztπ(s1:t−1)=∑st∈Vp0(st∣s1:t−1)ψt(s1:t).Z_t^\pi(\mathbf{s}_{1:t-1}) = \sum_{s_t \in \mathcal{V}} p_0(s_t \mid \mathbf{s}_{1:t-1}) \psi_t(\mathbf{s}_{1:t}).Ztπ​(s1:t−1​)=st​∈V∑​p0​(st​∣s1:t−1​)ψt​(s1:t​).

This proposal is tractable in transformer architectures for t<Tt < Tt<T because the normalization sums over the discrete vocabulary V\mathcal{V}V of next tokens. The full twisted proposal over a sequence is

qπ(s1:T)=(∏t=1T−1qtπ(st∣s1:t−1))qT(sT∣s1:T−1).q^\pi(\mathbf{s}_{1:T}) = \left( \prod_{t=1}^{T-1} q_t^\pi(s_t \mid \mathbf{s}_{1:t-1}) \right) q_T(s_T \mid \mathbf{s}_{1:T-1}).qπ(s1:T​)=(t=1∏T−1​qtπ​(st​∣s1:t−1​))qT​(sT​∣s1:T−1​).

At the final step, the true terminal potential ϕ(s1:T)\phi(\mathbf{s}_{1:T})ϕ(s1:T​) may require a separate network, such as a reward model, classifier, or likelihood evaluator. The authors therefore use an approximate final twist ψT(s1:T)≈ϕ(s1:T)\psi_T(\mathbf{s}_{1:T}) \approx \phi(\mathbf{s}_{1:T})ψT​(s1:T​)≈ϕ(s1:T​) inside the proposal:

qT(sT∣s1:T−1)∝p0(sT∣s1:T−1)ψT(s1:T).q_T(s_T \mid \mathbf{s}_{1:T-1}) \propto p_0(s_T \mid \mathbf{s}_{1:T-1}) \psi_T(\mathbf{s}_{1:T}).qT​(sT​∣s1:T−1​)∝p0​(sT​∣s1:T−1​)ψT​(s1:T​).

With this proposal, the incremental weights for t<Tt < Tt<T become

wt(s1:t)=∑stp0(st∣s1:t−1)ψt(s1:t)ψt−1(s1:t−1),w_t(\mathbf{s}_{1:t}) = \frac{ \sum_{s_t} p_0(s_t \mid \mathbf{s}_{1:t-1}) \psi_t(\mathbf{s}_{1:t}) } {\psi_{t-1}(\mathbf{s}_{1:t-1})},wt​(s1:t​)=ψt−1​(s1:t−1​)∑st​​p0​(st​∣s1:t−1​)ψt​(s1:t​)​,

and at the final step,

wT(s1:T)=∑sTp0(sT∣s1:T−1)ψT(s1:T)ψT−1(s1:T−1)ϕ(s1:T)ψT(s1:T).w_T(\mathbf{s}_{1:T}) = \frac{ \sum_{s_T} p_0(s_T \mid \mathbf{s}_{1:T-1}) \psi_T(\mathbf{s}_{1:T}) } {\psi_{T-1}(\mathbf{s}_{1:T-1})} \frac{\phi(\mathbf{s}_{1:T})} {\psi_T(\mathbf{s}_{1:T})}.wT​(s1:T​)=ψT−1​(s1:T−1​)∑sT​​p0​(sT​∣s1:T−1​)ψT​(s1:T​)​ψT​(s1:T​)ϕ(s1:T​)​.

The key practical benefit is that the proposed next tokens are already biased toward continuations that the twist believes will achieve high terminal potential. The final weight then corrects any remaining gap between the approximate twist and the true potential.

The authors also connect this formulation to conditional inference. If the target is a posterior obtained by conditioning on an observation variable oTo_ToT​, the target is

σ(s1:T∣oT)=1Zσ(oT)p0(s1:T)σ(oT∣s1:T).\sigma(\mathbf{s}_{1:T} \mid o_T) = \frac{1}{\mathcal{Z}_\sigma(o_T)} p_0(\mathbf{s}_{1:T}) \sigma(o_T \mid \mathbf{s}_{1:T}).σ(s1:T​∣oT​)=Zσ​(oT​)1​p0​(s1:T​)σ(oT​∣s1:T​).

In this case, the optimal twist has a direct probabilistic meaning:

ψt∗(s1:t,oT)=σ(oT∣s1:t).\psi_t^*(\mathbf{s}_{1:t}, o_T) = \sigma(o_T \mid \mathbf{s}_{1:t}).ψt∗​(s1:t​,oT​)=σ(oT​∣s1:t​).

Thus, for conditional generation tasks, the twist learns the probability that a partial sequence can still lead to the observed target outcome. The unconditional terminal potential setting can be recovered by treating a binary observation as the potential. This perspective also supports exact posterior sampling in simulated settings through ancestral sampling from the joint distribution and conditioning on the simulated observation.

The authors further interpret the twist functions through reinforcement learning. When the terminal potential is written as an exponentiated reward,

ϕ(s1:T)=eβr(s1:T),\phi(\mathbf{s}_{1:T}) = e^{\beta r(\mathbf{s}_{1:T})},ϕ(s1:T​)=eβr(s1:T​),

the twist corresponds to an exponentiated soft value function:

ψt(s1:t)≈eβQ(s1:t).\psi_t(\mathbf{s}_{1:t}) \approx e^{\beta Q(\mathbf{s}_{1:t})}.ψt​(s1:t​)≈eβQ(s1:t​).

From this viewpoint, the twist behaves like a critic that estimates the value of partial sequences, while the proposal behaves like an actor that generates token continuations. The optimal twist recursion mirrors a soft Bellman-like equation, even when rewards are present only at the terminal step.

The main training method for the twists is contrastive twist learning. The authors train a parameterized family of twists ψtθ\psi_t^\thetaψtθ​ by minimizing the average KL divergence between the desired target marginal and the twisted intermediate marginal:

LCTL(θ)=∑t=1TDKL(σ(s1:t)∥πtθ(s1:t)).\mathcal{L}_{\mathrm{CTL}}(\theta) = \sum_{t=1}^{T} D_{\mathrm{KL}} \left( \sigma(\mathbf{s}_{1:t}) \left\| \pi_t^\theta(\mathbf{s}_{1:t}) \right. \right).LCTL​(θ)=t=1∑T​DKL​(σ(s1:t​)​πtθ​(s1:t​)).

The gradient of this objective has a contrastive structure:

∇θLCTL(θ)∝Eπtθ(s1:t)[∇θlog⁡ψtθ(s1:t)]−Eσ(s1:t)[∇θlog⁡ψtθ(s1:t)].\nabla_\theta \mathcal{L}_{\mathrm{CTL}}(\theta) \propto \mathbb{E}_{\pi_t^\theta(\mathbf{s}_{1:t})} \left[ \nabla_\theta \log \psi_t^\theta(\mathbf{s}_{1:t}) \right] - \mathbb{E}_{\sigma(\mathbf{s}_{1:t})} \left[ \nabla_\theta \log \psi_t^\theta(\mathbf{s}_{1:t}) \right].∇θ​LCTL​(θ)∝Eπtθ​(s1:t​)​[∇θ​logψtθ​(s1:t​)]−Eσ(s1:t​)​[∇θ​logψtθ​(s1:t​)].

The negative part of the gradient encourages the twist to increase scores under the current twisted distribution πtθ\pi_t^\thetaπtθ​. The positive part encourages the twist to increase scores on partial sequences that better represent the true target marginal σ(s1:t)\sigma(\mathbf{s}_{1:t})σ(s1:t​). This resembles contrastive learning or energy-based model training, where positive samples push the energy down, while negative samples push the model’s distribution toward realistic but less target-aligned examples.

For negative sampling, the authors approximate the expectation under πtθ\pi_t^\thetaπtθ​ using sequential importance sampling or sequential Monte Carlo. One can sample from a proposal such as the base model p0p_0p0​ or the twist-induced proposal qπq^\piqπ, then reweight the generated partial sequences using the unnormalized intermediate target weights. In a full SMC workflow, resampling across KKK particles provides a lower-variance approximation to the negative samples.

For positive sampling, the method relies on partial sequences that are drawn from, or weighted to approximate, the true target marginals. When exact target samples are available, for example through ancestral sampling from a tractable joint observation model, the exact posterior sample can be used directly. The authors describe truncating a complete exact sequence to a prefix of length ttt, since that prefix corresponds to a sample from the marginal σ(s1:t)\sigma(\mathbf{s}_{1:t})σ(s1:t​). This gives a clean positive sample for the twist gradient at step ttt.

When exact samples are not available, the authors use approximate positive sampling. They run SMC or SIS with KKK particles, but crucially they weight these particles using the true terminal potential ϕ(s1:T)\phi(\mathbf{s}_{1:T})ϕ(s1:T​), not merely the learned twist. The resulting weights approximate the target marginal distribution over sequences. The same set of particles and weights can then be used to estimate positive gradient contributions across different timesteps. Rejection sampling can also provide exact positive samples when an upper bound on the proposal-to-target likelihood ratio is known.

The training workflow can be summarized as a loop over generation steps. The authors draw or approximate positive partial sequences that reflect the true target marginal. They also draw negative partial sequences from the current proposal and reweight them under the current twisted intermediate target. The twist network is updated so that it assigns higher scores to positive partial sequences and lower scores to negative partial sequences. Over time, the intermediate twisted targets move closer to the true marginals of the final target distribution.

The authors also provide tools for evaluating the quality of this inference procedure. A central quantity is the log partition function log⁡Zσ\log \mathcal{Z}_\sigmalogZσ​, because it appears in KL divergence estimates and in likelihood-based diagnostics. They derive bidirectional stochastic bounds for log⁡Zσ\log \mathcal{Z}_\sigmalogZσ​ using the extended-state view of SMC. Let SSS denote the full extended state of particles, tokens, weights, and resampling indices. The lower bound is

EqSMC(S)[log⁡∏t=1T(1K∑i=1Kwt(s1:ti))],\mathbb{E}_{q_{\mathrm{SMC}}(S)} \left[ \log \prod_{t=1}^{T} \left( \frac{1}{K} \sum_{i=1}^{K} w_t(\mathbf{s}_{1:t}^{i}) \right) \right],EqSMC​(S)​[logt=1∏T​(K1​i=1∑K​wt​(s1:ti​))],

and the upper bound is

EσSMC(S)[log⁡∏t=1T(1K∑i=1Kwt(s1:ti))].\mathbb{E}_{\sigma_{\mathrm{SMC}}(S)} \left[ \log \prod_{t=1}^{T} \left( \frac{1}{K} \sum_{i=1}^{K} w_t(\mathbf{s}_{1:t}^{i}) \right) \right].EσSMC​(S)​[logt=1∏T​(K1​i=1∑K​wt​(s1:ti​))].

These satisfy

EqSMC(S)[log⁡∏t=1T1K∑i=1Kwt(s1:ti)]≤log⁡Zσ≤EσSMC(S)[log⁡∏t=1T1K∑i=1Kwt(s1:ti)].\mathbb{E}_{q_{\mathrm{SMC}}(S)} \left[ \log \prod_{t=1}^{T} \frac{1}{K} \sum_{i=1}^{K} w_t(\mathbf{s}_{1:t}^{i}) \right] \leq \log \mathcal{Z}_\sigma \leq \mathbb{E}_{\sigma_{\mathrm{SMC}}(S)} \left[ \log \prod_{t=1}^{T} \frac{1}{K} \sum_{i=1}^{K} w_t(\mathbf{s}_{1:t}^{i}) \right].EqSMC​(S)​[logt=1∏T​K1​i=1∑K​wt​(s1:ti​)]≤logZσ​≤EσSMC​(S)​[logt=1∏T​K1​i=1∑K​wt​(s1:ti​)].

The lower bound corresponds to sampling the extended SMC system from the proposal. The upper bound requires sampling from the corresponding target in the extended state space. This can be done when at least one exact target sample is available. During the extended SMC process, the exact target sample is protected so that its partial sequence remains in the particle set. The remaining particles follow the usual proposal and resampling dynamics. As KKK increases, the bounds become exact, allowing the authors to sandwich log⁡Zσ\log \mathcal{Z}_\sigmalogZσ​ and assess how closely the SMC procedure approximates the true target distribution.

Experiment

The experiments evaluate the framework by comparing upper and lower log partition bounds and KL divergences between proposal samples and target distributions across toxic story generation, sentiment review generation, and infilling. They show that twisted SMC can substantially improve sampling efficiency and estimation quality for rare events compared with standard importance sampling, while also serving as a tool to assess other inference methods. The results indicate that proposal quality depends on the training objective and sample availability, with CTL performing best under approximate positive sampling and DPG performing best when exact target samples are available.

In the conditional infilling setting, DPG achieves the best expected divergence scores in both KL directions, benefiting from access to exact target samples. Contrastive learning performs competitively but less consistently than DPG and is near SIXO, while FUDGE and RL are less favorable across the evaluated divergences. PPO is weaker for the target-to-proposal direction because it trains only from proposal samples. DPG is strongest for both proposal-to-target and target-to-proposal KL divergences in infilling. PPO performs relatively poorly when measuring how well the proposal covers the posterior.

In the conditional infilling setting, divergence scores are evaluated in both KL directions to assess how well each method aligns proposals with target and posterior samples. DPG achieves the strongest performance in both directions, aided by access to exact target samples, while contrastive learning remains competitive but less consistent than DPG. FUDGE and RL are less favorable overall, and PPO is weaker, particularly when the evaluation emphasizes coverage of the posterior.


KI mit KI entwickeln

Von der Idee bis zum Launch – beschleunigen Sie Ihre KI-Entwicklung mit kostenlosem KI-Co-Coding, sofort einsatzbereiter Umgebung und bestem GPU-Preis.

KI-gestütztes kollaboratives Programmieren
Sofort einsatzbereite GPUs
Die besten Preise

HyperAI Newsletters

Abonnieren Sie unsere neuesten Updates
Wir werden die neuesten Updates der Woche in Ihren Posteingang liefern um neun Uhr jeden Montagmorgen
Unterstützt von MailChimp