Papers › Probabilistic Inference in Language Models via Twisted Sequential Monte Carlo

Probabilistic Inference in Language Models via Twisted Sequential Monte Carlo

26 Apr 2024arXiv:2404.17546archive 2025-07-28

Stephen Zhao, Rob Brekelmans, Alireza Makhzani, Roger Grosse

Numerous capability and safety techniques of Large Language Models (LLMs), including RLHF, automated red-teaming, prompt engineering, and infilling, can be cast as sampling from an unnormalized target distribution defined by a given reward or potential function over the full sequence. In this work, we leverage the rich toolkit of Sequential Monte Carlo (SMC) for these probabilistic inference problems. In particular, we use learned twist functions to estimate the expected future value of the potential at each timestep, which enables us to focus inference-time computation on promising partial sequences. We propose a novel contrastive method for learning the twist functions, and establish connections with the rich literature of soft reinforcement learning. As a complementary application of our twisted SMC framework, we present methods for evaluating the accuracy of language model inference techniques using novel bidirectional SMC bounds on the log partition function. These bounds can be used to estimate the KL divergence between the inference and target distributions in both directions. We apply our inference evaluation techniques to show that twisted SMC is effective for sampling undesirable outputs from a pretrained model (a useful component of harmlessness training and automated red-teaming), generating reviews with varied sentiment, and performing infilling tasks.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2404.17546")

Code

Syntology Ran 11 of 17 code samples harvested from 1 repository linked to this paper; 6 have no recorded run. Of those that ran: 11 ran with no contract checked.

By repository: official repository: 17 samples from 1 repository, 11 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

silent-zebra/twisted-smc-lm officialmentioned in papermentioned on GitHubjax report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

17 samples harvested; 11 ran; 0 honoured the contract we drafted; 6 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

11ran
6unverified

Licence: 17 of the 17 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from silent-zebra/twisted-smc-lm. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

batch_reward_model silent-zebra/twisted-smc-lm/reward_models.py official repository ran no licence file found · pointer only · e5a65f3e5daae4a8 · report
collect_info_across_trueposts silent-zebra/twisted-smc-lm/do_training_and_log_Z_bounds.py official repository ran no licence file found · pointer only · 1ad3697a8f2357c0 · report
get_all_seqs_up_to_output_len silent-zebra/twisted-smc-lm/custom_transformer_prob_utils.py official repository ran no licence file found · pointer only · 8df8935e525d9f54 · report
get_full_list_of_all_seqs_up_to_output_len silent-zebra/twisted-smc-lm/custom_transformer_prob_utils.py official repository ran no licence file found · pointer only · 7e325722c329023a · report
get_new_params_twist_and_optim_twist_state silent-zebra/twisted-smc-lm/do_training_and_log_Z_bounds.py official repository ran no licence file found · pointer only · 951de47945a1db46 · report
kl_div_jax silent-zebra/twisted-smc-lm/custom_transformer_prob_utils.py official repository ran fingerprinted no licence file found · pointer only · e2d64bdf2aa590e4 · report
kl_div_jax_sum_last_axis silent-zebra/twisted-smc-lm/backup/transformer_smc_huggingface.py official repository ran fingerprinted no licence file found · pointer only · f6fc75d8cc49afe3 · report
neg_beta_times_batch_reward_model_curry silent-zebra/twisted-smc-lm/reward_models.py official repository ran no licence file found · pointer only · ad44f86f80299d7f · report
neg_beta_times_reward_model silent-zebra/twisted-smc-lm/reward_models.py official repository ran no licence file found · pointer only · 3260314e40b4993d · report
populate_f_q_g_q_lists silent-zebra/twisted-smc-lm/get_kl_table.py official repository ran no licence file found · pointer only · 1121fae94eb827ac · report
stochastic_transformer_sample_iter silent-zebra/twisted-smc-lm/backup/transformer_smc_huggingface.py official repository ran no licence file found · pointer only · f9edaa06c26334a2 · report
get_logZ_midpoint_estimates silent-zebra/twisted-smc-lm/get_kl_table.py official repository unverified no licence file found · pointer only · 9c7f287d70eb2ae3 · report
get_logZ_midpoint_to_use silent-zebra/twisted-smc-lm/get_kl_table.py official repository unverified no licence file found · pointer only · d745ccf2ee440bf9 · report
get_tokenizer silent-zebra/twisted-smc-lm/huggingface_models_custom.py official repository unverified no licence file found · pointer only · 96271cafe3d6e6b1 · report
inspect_and_record_evidence_setting_for_index silent-zebra/twisted-smc-lm/do_training_and_log_Z_bounds.py official repository unverified no licence file found · pointer only · 5a8ba055520815ed · report
load_ckpt silent-zebra/twisted-smc-lm/plot_bounds.py official repository unverified no licence file found · pointer only · f7056ad862e2b7d9 · report
stochastic_transformer_sample silent-zebra/twisted-smc-lm/backup/transformer_smc_huggingface.py official repository unverified no licence file found · pointer only · 8c43715766ba7852 · report

Tasks

Language ModellingPrompt EngineeringRed Teaming

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

Focus

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections