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 View this paper on Syntology, its page in Syntology's graph. That page adds which of its functions are also present in other papers' code Syntology holds, with dates, and its dated run record as JSON and a README badge. It also lists the repositories linked to the paper, the abstract and the calls for agents.

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

For agents, on Syntology's MCP service (how to connect):

Code

Syntology 17 code samples harvested from 1 repository linked to this paper: 11 ran (of which 0 constructed an object rather than computing a result; 9 with no instrument failure: 0 honoured, 2 violated, 7 with no contract checked; 2 where Syntology's instrument failed) · 6 unverified. Where Syntology's instrument failed: 2 where our contract draft was wrong; failures of Syntology's instrument, not of the code.

By repository: official repository: 17 samples from 1 repository, 11 ran (9 with no instrument failure; 2 where Syntology's instrument failed). The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

Levels written on 2026-09-24 (UTC): the dates Syntology's graph wrote each sample's verification level, not when the code executed.

silent-zebra/twisted-smc-lm officialmentioned in papermentioned on GitHubjaxSyntology: 17 samples, 11 ran (2 instrument-failed) · 6 unverified 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 (of which 0 constructed an object rather than computing a result; 9 with no instrument failure: 0 honoured, 2 violated, 7 with no contract checked; 2 where Syntology's instrument failed) · 6 unverified. “Where Syntology's instrument failed” counts failures of Syntology's instrument, not of the code. Syntology's record for this page last changed ; when this build read Syntology's graph is in the build record. That date is not when the samples ran.

2ran · violated contract
2ran · our draft was wrong
7ran
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. A line beginning “fixture:” under a sample that ran gives the arguments Syntology's fixture called the sample with, each as name:type/rank/element kind (? where none was recorded), and the type of what the fixture's run returned, exactly as the harness recorded them; the arguments and the returned type were recorded by two separate runs of the same fixture. No output value is shown, and a returned type says nothing about whether the output was right. “Level recorded” gives the UTC date when Syntology's graph wrote the sample's verification level, not when the code executed; where the level was written before Syntology's graph kept that date, the row says “level recorded, date not recorded”. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself. “Our draft was wrong” and “the contract check could not run” are failures of Syntology's instrument, not of the code: the sample ran on Syntology's synthesized input, and then the drafted contract did not describe it, or the check itself raised an error (RAISES), lacked a dependency (DEP_MISSING) or ran out of time (TIMEOUT); hover the label for which. A metamorphic tier, where shown, is evidence of the output's shape, not that the sample implements the method.

Each sample ends with its code_sha256. A sample's code_sha256 is Syntology's identity for that exact code. It is printed in full (16 hexadecimal characters for every sample in this build), and get_code(code_sha256="…") takes it exactly as printed. An agent calls it on Syntology's MCP service (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 are grouped by repository: a repository marked official first, then by how directly the repository is tied to the paper, then in the archive's order; never by how many ran. 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 six 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; the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar); this paper's copy was not located, so its licence is not recorded; or this paper's copy is cleared, but the stored copy of the same code that Syntology serves is not. 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.

A function or class name links to its page when the name occurs in the harvested code of at least 5 papers; the name is a string, not an identity.

silent-zebra/twisted-smc-lm official repository 17 samples, 11 ran (2 instrument-failed) · 6 unverified
batch_reward_model reward_models.py ran · our draft was wrong no licence file found · pointer only · e5a65f3e5daae4a8 · report fixture: signature reward_model_fn:callable/0/? · returned function · level recorded 2026-09-24
collect_info_across_trueposts do_training_and_log_Z_bounds.py ran no licence file found · pointer only · 1ad3697a8f2357c0 · report fixture: signature rng_key:ArrayImpl/1/int, start:float/0/float, n_trueposts_for_evals:int/0/int, n_samples_for_plots:list/1/int, inspect_and_record_evidence_setting_fn:callable/0/?, prompt:str/0/str, params_p:dict/0/?, params_twist:dict/0/?, output_len:int/0/int, log_true_final_twist:ndarray/0/float, true_posterior_samples:ndarray/2/float, smc_procedure_type:str/0/str, proposal_is_p:bool/0/bool, condition_twist_on_tokens:bool/0/bool, huggingface_model:callable/0/?, params_proposal:dict/0/?, tokenizer:MockTokenizer/0/?, logZ_ubs_iwae_across_samples_and_trueposts:list/1/list, logZ_lbs_iwae_across_samples_and_trueposts:list/1/list, logZ_ubs_smc_across_samples_and_trueposts:list/1/list, logZ_lbs_smc_across_samples_and_trueposts:list/1/list, list_of_stuff_across_trueposts_only_largest_n_samples:list/1/list · returned tuple · level recorded 2026-09-24
get_all_seqs_up_to_output_len custom_transformer_prob_utils.py ran no licence file found · pointer only · 8df8935e525d9f54 · report fixture: signature prompt:ArrayImpl/1/int, n_vocab:int/0/int, output_len:int/0/int · returned ArrayImpl · level recorded 2026-09-24
get_full_list_of_all_seqs_up_to_output_len custom_transformer_prob_utils.py ran no licence file found · pointer only · 7e325722c329023a · report fixture: signature prompt:ArrayImpl/1/int, n_vocab:int/0/int, output_len:int/0/int · returned list · level recorded 2026-09-24
get_new_params_twist_and_optim_twist_state do_training_and_log_Z_bounds.py ran no licence file found · pointer only · 951de47945a1db46 · report fixture: signature optimizer_twist:GradientTransformationExtraArgs/1/callable, grad_params_twist:dict/0/?, optim_twist_state:tuple/1/ScaleByAdamState, params_twist:dict/0/? · returned tuple · level recorded 2026-09-24
kl_div_jax custom_transformer_prob_utils.py ran · violated contract fingerprinted no licence file found · pointer only · e2d64bdf2aa590e4 · report fixture: signature log_p_target:ArrayImpl/2/float, log_p_curr:ArrayImpl/2/float · returned ArrayImpl · level recorded 2026-09-24
kl_div_jax_sum_last_axis backup/transformer_smc_huggingface.py ran · violated contract fingerprinted no licence file found · pointer only · f6fc75d8cc49afe3 · report fixture: signature log_p:ArrayImpl/2/float, log_q:ArrayImpl/2/float · returned ArrayImpl · level recorded 2026-09-24
neg_beta_times_batch_reward_model_curry reward_models.py ran no licence file found · pointer only · ad44f86f80299d7f · report fixture: signature prompt_len:int/0/int, beta:float/0/float, reward_model_fn:callable/0/? · returned function · level recorded 2026-09-24
neg_beta_times_reward_model reward_models.py ran no licence file found · pointer only · 3260314e40b4993d · report fixture: signature single_seq:Tensor/2/float, prompt_len:int/0/int, beta:float/0/float, reward_model_fn:callable/0/? · returned Tensor · level recorded 2026-09-24
populate_f_q_g_q_lists get_kl_table.py ran · our draft was wrong no licence file found · pointer only · 1121fae94eb827ac · report fixture: signature load_prefixes:list/1/list · returned tuple · level recorded 2026-09-24
stochastic_transformer_sample_iter backup/transformer_smc_huggingface.py ran no licence file found · pointer only · f9edaa06c26334a2 · report fixture: signature carry:tuple/1/ArrayImpl, t:int/0/int · returned tuple · level recorded 2026-09-24
get_logZ_midpoint_estimates get_kl_table.py unverified no licence file found · pointer only · 9c7f287d70eb2ae3 · report
get_logZ_midpoint_to_use get_kl_table.py unverified no licence file found · pointer only · d745ccf2ee440bf9 · report
get_tokenizer in 156 papers huggingface_models_custom.py unverified no licence file found · pointer only · 96271cafe3d6e6b1 · report
inspect_and_record_evidence_setting_for_index do_training_and_log_Z_bounds.py unverified no licence file found · pointer only · 5a8ba055520815ed · report
load_ckpt in 31 papers plot_bounds.py unverified no licence file found · pointer only · f7056ad862e2b7d9 · report
stochastic_transformer_sample backup/transformer_smc_huggingface.py 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